Class: IshManager::EmailContextsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ish_manager/email_contexts_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#home

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 8

def create
  authorize! :create, ::Ish::EmailContext
  pparams = params[:ish_email_context].permit!
  pparams[:tmpl] = JSON.parse(pparams[:tmpl])
  @ctx = ::Ish::EmailContext.new pparams
  if @ctx.save
    flash[:notice] = 'Saved.'
    redirect_to action: 'show', id: @ctx.id
    return
  else
    # flash[:alert] = "Could not save: #{@ctx.errors.full_messages.join(', ')}"
    flash[:alert] = ['Could not save:'] + @ctx.errors.full_messages
    render action: :new
    return
  end
end

#destroyObject



25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 25

def destroy
  @ctx = Ish::EmailContext.find params[:id]
  authorize! :destroy, @ctx
  flag = @ctx.destroy
  if flag
    flash[:notice] = 'Destroyed the email context'
  else
    flash[:alert] = 'Could not destroy email context'
  end
  redirect_to action: :index
end

#do_sendObject



37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 37

def do_send
  @ctx = ::Ish::EmailContext.find params[:id]
  authorize! :do_send, @ctx

  flash[:notice] = 'Scheduled a single send - v2'
  @ctx.send_at = Time.now
  @ctx.save

  redirect_to action: 'index'
end

#editObject



48
49
50
51
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 48

def edit
  @ctx = ::Ish::EmailContext.find params[:id]
  authorize! :edit, @ctx
end

#iframe_srcObject



53
54
55
56
57
58
59
60
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 53

def iframe_src
  @ctx = @email_context = Ish::EmailContext.find params[:id]
  authorize! :iframe_src, @ctx
  @tmpl = @email_template = @ctx.email_template
  @lead = @ctx.lead
  # @body = @ctx.body_templated
  render "ish_manager/email_templates/_#{@tmpl.layout}", layout: false
end

#indexObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 62

def index
  authorize! :index, ::Ish::EmailContext
  @ctxs = ::Ish::EmailContext.all

  if params[:notsent]
    @ctxs = @ctxs.where( sent_at: nil )
  end

  if params[:lead_id]
    @lead = Lead.find params[:lead_id]
    @ctxs = @ctxs.where( to_email: @lead.email )
  end

  @ctxs = @ctxs.page( params[Ish::EmailContext::PAGE_PARAM_NAME] )

  render layout: 'ish_manager/application_fullwidth'
end

#newObject



80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 80

def new
  authorize! :new, ::Ish::EmailContext
  @template = Ish::EmailTemplate.where( slug: params[:template_slug] ).first
  @template ||= Ish::EmailTemplate.where( id: params[:template_slug] ).first
  attrs = {}
  if @template
    attrs = @template.attributes.slice( :subject, :body, :from_email )
  end
  @ctx = ::Ish::EmailContext.new( { email_template: @template }.merge(attrs) )
end

#showObject



91
92
93
94
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 91

def show
  @ctx = @email_context = ::Ish::EmailContext.find( params[:id] )
  authorize! :show, @ctx
end

#updateObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 96

def update
  @ctx = ::Ish::EmailContext.find params[:id]
  authorize! :update, @ctx
  pparams = params[:ish_email_context].permit!

  if @ctx.update_attributes pparams
    flash[:notice] = 'Saved.'
    redirect_to action: 'show', id: @ctx.id
    return
  else
    flash[:alert] = "Could not save: #{@ctx.errors.full_messages.join(', ')}"
    render action: :edit
    return
  end
end