Class: WcoEmail::EmailTemplatesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/wco_email/email_templates_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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

def create
  authorize! :create, WcoEmail::EmailTemplate

  if params[:photo].present?
    photo = Wco::Photo.create! params[:photo].permit!
    params[:template][:photo_id] = photo.id
  end

  @tmpl = WcoEmail::EmailTemplate.create params[:template].permit!
  if @tmpl.persisted?
    flash[:notice] = 'Success.'
    if params[:template][:photo].present?
      if @tmpl.photo.present?
        @tmpl.photo.update(params[:template][:photo])
      else
        @tmpl.photo = Wco::Photo.create(params[:template][:photo])
      end
    end

  else
    flash[:alert] = "Could not create an email template: #{@tmpl.errors.full_messages.join(', ')}."
  end
  redirect_to action: :index
end

#destroyObject



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

def destroy
  authorize! :destroy, WcoEmail::EmailTemplate
  @tmpl = WcoEmail::EmailTemplate.where({ id: params[:id] }).first || WcoEmail::EmailTemplate.find_by({ slug: params[:id] })
  if @tmpl.destroy
    flash[:notice] = 'Success.'
  else
    flash[:alert] = 'Cannot destroy this template.'
  end
  redirect_to action: :index
end

#editObject



40
41
42
43
44
45
# File 'app/controllers/wco_email/email_templates_controller.rb', line 40

def edit
  @tmpl = @email_template = WcoEmail::EmailTemplate.where({ id: params[:id] }).first
  authorize! :edit, @tmpl

  @photo = @tmpl.photo || @tmpl.build_photo
end

#iframe_srcObject



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

def iframe_src
  @tmpl = @email_template = WcoEmail::EmailTemplate.where({ id: params[:id] }).first ||
    WcoEmail::EmailTemplate.find_by({ slug: params[:id] })
  authorize! :iframe_src, @email_template

  @lead        = Lead.find_by({ email: 'poxlovi@gmail.com' })
  @ctx         = Ctx.new({ email_template: @tmpl, lead_id: @lead.id })
  @tmpl_config = OpenStruct.new JSON.parse( @ctx.tmpl[:config_json] )

  @utm_tracking_str = {
    'cid'          => @ctx.lead_id,
    'utm_campaign' => @ctx.tmpl.slug,
    'utm_medium'   => 'email',
    'utm_source'   => @ctx.tmpl.slug,
  }.map { |k, v| "#{k}=#{v}" }.join("&")

  @unsubscribe_url = WcoEmail::Engine.routes.url_helpers.unsubscribes_url({
    template_id: @ctx.tmpl.id,
    lead_id:     @lead.id,
    token:       @lead.unsubscribe_token,
  })

  render layout: false
end

#indexObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/wco_email/email_templates_controller.rb', line 72

def index
  authorize! :index, WcoEmail::EmailTemplate
  @templates = WcoEmail::EmailTemplate.all

  if params[:q]
    q = URI.decode(params[:q])
    @templates = @templates.where({ :slug => /#{q}/i })
  end

  @templates = @templates.order_by( slug: :asc )

  if '_index_expanded' == params[:view]
    @templates = @templates.page( params[:templates_page]
      ).per( current_profile.per_page
      )
  end
  render params[:view] || '_index'
end

#newObject



91
92
93
94
95
96
# File 'app/controllers/wco_email/email_templates_controller.rb', line 91

def new
  @tmpl = WcoEmail::EmailTemplate.new
  authorize! :new, WcoEmail::EmailTemplate

  @photo = @tmpl.photo || @tmpl.build_photo
end

#showObject



98
99
100
101
102
103
104
# File 'app/controllers/wco_email/email_templates_controller.rb', line 98

def show
  @tmpl   = WcoEmail::EmailTemplate.where({ id: params[:id] }).first
  @tmpl ||= WcoEmail::EmailTemplate.find_by({ slug: params[:id] })
  authorize! :show, @tmpl

  @ctx    = @email_context = WcoEmail::Context.new
end

#show_iframeObject



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

def show_iframe
  @tmpl   = WcoEmail::EmailTemplate.where({     id: params[:id] }).first
  @tmpl ||= WcoEmail::EmailTemplate.find_by({ slug: params[:id] })
  authorize! :show, @tmpl

  @ctx = WcoEmail::Context.new({
    email_template: @tmpl,
    lead:           Wco::Lead.find_by({ email: 'poxlovi@gmail.com' }),
  })

  eval( @tmpl.config_exe )
  @renderer = WcoEmail::ApplicationMailer.renderer ctx: @ctx


  render layout: false
end

#updateObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/wco_email/email_templates_controller.rb', line 123

def update
  @tmpl = WcoEmail::EmailTemplate.where({ id: params[:id] }).first
  authorize! :update, @tmpl

  if params[:photo].present?
    photo = Wco::Photo.create! params[:photo].permit!
    params[:template][:photo_id] = photo.id
  end

  flag = @tmpl.update_attributes( params[:template].permit! )
  if flag
    flash[:notice] = 'Success.'
  else
    flash[:alert] = "No luck. #{@tmpl.errors.full_messages.join(', ')}"
  end
  redirect_to action: :edit
end