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

#basic_auth, #home, #tinymce

Instance Method Details

#createObject



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

def create
  @ctx    = ::Ish::EmailContext.new params[:ctx].permit!
  @tmpl   = ::Ish::EmailTemplate.find @ctx.email_template_id

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

  authorize! :create, @ctx
  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



29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 29

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

#editObject



41
42
43
44
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 41

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

#iframe_srcObject



46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 46

def iframe_src
  @ctx = @email_context = Ish::EmailContext.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

  render layout: false
end

#indexObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 58

def index
  authorize! :index, ::Ish::EmailContext
  @ctxs = ::Ish::EmailContext.all.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



76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 76

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

#send_immediateObject



88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 88

def send_immediate
  @ctx = ::Ish::EmailContext.find params[:id]
  authorize! :do_send, @ctx
  flash_notice 'Sent immediately.'

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

  redirect_to action: 'index'
end

#send_scheduleObject



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

def send_schedule
  @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

#showObject



110
111
112
113
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 110

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

#summaryObject



115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 115

def summary
  authorize! :summary, Ish::EmailContext
  @results = Ish::EmailContext.summary

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

#updateObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/ish_manager/email_contexts_controller.rb', line 127

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

  if @ctx.update_attributes params[:ctx].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