Class: WcoEmail::EmailTemplatesController

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

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
# File 'app/controllers/wco_email/email_templates_controller.rb', line 5

def create
  authorize! :create, WcoEmail::EmailTemplate
  @template = WcoEmail::EmailTemplate.create params[:template].permit!
  if @template.persisted?
    flash[:notice] = 'Success.'
  else
    flash[:alert] = "Could not create an email template: #{@template.errors.full_messages.join(', ')}."
  end
  redirect_to action: :index
end

#destroyObject



16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/wco_email/email_templates_controller.rb', line 16

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

#editObject



27
28
29
30
# File 'app/controllers/wco_email/email_templates_controller.rb', line 27

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

#iframe_srcObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/wco_email/email_templates_controller.rb', line 32

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



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

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



76
77
78
79
# File 'app/controllers/wco_email/email_templates_controller.rb', line 76

def new
  @new_email_template = WcoEmail::EmailTemplate.new
  authorize! :new, WcoEmail::EmailTemplate
end

#showObject



81
82
83
84
85
86
87
# File 'app/controllers/wco_email/email_templates_controller.rb', line 81

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/wco_email/email_templates_controller.rb', line 89

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' }),
  })

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

  render layout: false
end

#updateObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/wco_email/email_templates_controller.rb', line 104

def update
  @template = WcoEmail::EmailTemplate.where({ id: params[:id] }).first
  authorize! :update, @template
  flag = @template.update_attributes( params[:template].permit! )
  if flag
    flash[:notice] = 'Success.'
  else
    flash[:alert] = "No luck. #{@template.errors.full_messages.join(', ')}"
  end
  redirect_to action: :edit
end