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
# 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])
  @email_ctx = ::Ish::EmailContext.new pparams
  if @email_ctx.save
    flash[:notice] = 'Saved.'
    redirect_to action: 'show', id: @email_ctx.id
    return
  else
    flash[:alert] = "Could not save: #{@email_ctx.errors.full_messages.join(', ')}"
    render action: :new
    return
  end
end

#destroyObject



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

def destroy
  @email_ctx = Ish::EmailContext.find params[:id]
  authorize! :destroy, @email_ctx
  flag = @email_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



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

def do_send
  @ctx = ::Ish::EmailContext.find params[:id]
  authorize! :do_send, @ctx
  case @ctx.type
  when ::Ish::EmailContext::TYPE_SINGLE
    flash[:notice] = 'Scheduled a single send'
    IshManager::OfficeMailer.send_context_email(params[:id]).deliver_later
  when ::Ish::EmailContext::TYPE_CAMPAIGN
    flash[:notice] = 'Scheduled campaign send'
    IshManager::EmailCampaignJob.new.perform(params[:id])
  end

  redirect_to action: 'index'
end

#editObject



51
52
53
54
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 51

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

#iframe_srcObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 56

def iframe_src
  @email_ctx = Ish::EmailContext.find params[:id]
  authorize! :iframe_src, @email_ctx
  @email_template = @email_ctx.email_template
  case @email_template.type
  when 'partial'
    render 'ish_manager/email_templates/iframe_src', layout: false
    return
  when 'plain'
    @body = @email_ctx.body_templated
    render 'ish_manager/email_templates/plain', layout: false
    return
  end
end

#indexObject



71
72
73
74
75
76
77
78
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 71

def index
  authorize! :index, ::Ish::EmailContext
  @email_ctxs = ::Ish::EmailContext.all
  if params[:notsent]
    @email_ctxs = @email_ctxs.where( sent_at: nil )
  end
  @email_ctxs = @email_ctxs.page( params[Ish::EmailContext::PAGE_PARAM_NAME] )
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
  @email_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
  @email_ctx = ::Ish::EmailContext.find( params[:id] )
  authorize! :show, @email_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
  @email_ctx = ::Ish::EmailContext.find params[:id]
  authorize! :update, @email_ctx
  pparams = params[:ish_email_context].permit!
  pparams[:tmpl] = JSON.parse(pparams[:tmpl])
  if @email_ctx.update_attributes pparams
    flash[:notice] = 'Saved.'
    redirect_to action: 'show', id: @email_ctx.id
    return
  else
    flash[:alert] = "Could not save: #{@email_ctx.errors.full_messages.join(', ')}"
    render action: :edit
    return
  end
end