Class: WcoEmail::ContextsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/wco_email/contexts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/wco_email/contexts_controller.rb', line 8

def create
  @ctx    = WcoEmail::Context.new params[:context].permit!
  @tmpl   = WcoEmail::EmailTemplate.find @ctx.email_template_id

  @ctx.from_email ||= @tmpl.from_email
  @ctx.subject    ||= @tmpl.subject

  if params[:respond_inline]
    @ctx.reply_to_message_id = params[:respond_inline]
  end

  authorize! :create, @ctx
  if @ctx.save
    flash_notice 'Saved.'
    redirect_to action: 'show', id: @ctx.id
    return
  else
    flash_alert @ctx
    redirect_to request.referrer
  end
end

#destroyObject



30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/wco_email/contexts_controller.rb', line 30

def destroy
  @ctx = WcoEmail::Context.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

#editObject



42
43
44
45
# File 'app/controllers/wco_email/contexts_controller.rb', line 42

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

#indexObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/wco_email/contexts_controller.rb', line 47

def index
  authorize! :index, WcoEmail::Context
  @ctxs = WcoEmail::Context.where(
    :send_at.ne => nil,
    sent_at:       nil,
  ).order_by( sent_at: :desc, send_at: :desc
  ).page( params[:ctxs_page]
  ).per( current_profile.per_page )

  if params[:lead_id]
    @lead = Lead.find params[:lead_id]
    @ctxs = @ctxs.where( lead_id: @lead.id )
  else
    if my_truthy? params[:sent]
      @ctxs = @ctxs.or({ :sent_at.ne => nil }, { :unsubscribed_at.ne => nil })
    else
      @ctxs = @ctxs.where( sent_at: nil, unsubscribed_at: nil )
    end
  end
end

#newObject



68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/wco_email/contexts_controller.rb', line 68

def new
  authorize! :new, WcoEmail::Context
  @tmpl = @email_template = WcoEmail::EmailTemplate.where( slug: params[:template_slug] ).first ||
    WcoEmail::EmailTemplate.where( id: params[:template_slug] ).first ||
    WcoEmail::EmailTemplate.where({ slug: 'blank' }).last
  attrs = {}
  if @tmpl
    attrs = @tmpl.attributes.slice( :subject, :body, :from_email )
  end
  @ctx = WcoEmail::Context.new({ email_template: @tmpl }.merge(attrs))
end

#send_immediateObject



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

def send_immediate
  @ctx = WcoEmail::Context.find params[:id]
  authorize! :do_send, @ctx

  out = WcoEmail::ApplicationMailer.send_context_email( @ctx[:id].to_s )
  Rails.env.production? ? out.deliver_later : out.deliver_now

  flash_notice 'Sent immediately.'
  redirect_to request.referrer
end

#send_scheduleObject



91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/wco_email/contexts_controller.rb', line 91

def send_schedule
  @ctx = WcoEmail::Context.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

#showObject



102
103
104
105
# File 'app/controllers/wco_email/contexts_controller.rb', line 102

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

#show_iframeObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/wco_email/contexts_controller.rb', line 107

def show_iframe
  @ctx = @email_context = WcoEmail::Context.find params[:id]
  authorize! :iframe_src, @ctx

  @tmpl        = @email_template = @ctx.email_template
  @tmpl_config = OpenStruct.new JSON.parse( @ctx.tmpl[:config_json] )
  @lead        = @ctx.lead
  @body        = @ctx.body

  @renderer = WcoEmail::ApplicationMailer.renderer ctx: @ctx

  render layout: false
end

#summaryObject



121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/wco_email/contexts_controller.rb', line 121

def summary
  authorize! :summary, WcoEmail::Context
  @results = WcoEmail::Context.summary

  respond_to do |format|
    format.html
    format.csv do
      render layout: false
    end
  end
end

#updateObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/wco_email/contexts_controller.rb', line 133

def update
  @ctx = WcoEmail::Context.find params[:id]
  authorize! :update, @ctx

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