Class: IshManager::EmailTemplatesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#basic_auth, #home, #tinymce

Instance Method Details

#createObject

before_action :set_lists, only: %i| new |



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

def create
  authorize! :create, ::Ish::EmailTemplate
  template = ::Ish::EmailTemplate.create params[:ish_email_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



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

def destroy
  authorize! :destroy, ::Ish::EmailTemplate
  @template = Ish::EmailTemplate.where({ id: params[:id] }).first || Ish::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



28
29
30
31
# File 'app/controllers/ish_manager/email_templates_controller.rb', line 28

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

#iframe_srcObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/ish_manager/email_templates_controller.rb', line 33

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

  @lead = Lead.find_by({ email: '[email protected]' })
  @ctx = Ctx.new({ email_template: @tmpl, lead_id: @lead.id })

  @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("&")

  to = Ishapi::Engine.routes.url_helpers.email_unsubscribes_url({
    template_id: @ctx.tmpl.id,
    lead_id:     @lead.id,
    token:       @lead.unsubscribe_token,
  })
  obf = Office::ObfuscatedRedirect.find_or_create_by({ to: to })
  @unsubscribe_url = Ishapi::Engine.routes.url_helpers.obf_url( obf.id )

  to = Ishapi::Engine.routes.url_helpers.users_dashboard_url({
    lead_id: @lead.id,
  })
  obf = Office::ObfuscatedRedirect.find_or_create_by({ to: to })
  @update_preferences_url = Ishapi::Engine.routes.url_helpers.obf_url( obf.id )

  eval( @ctx.tmpl.config_exe )

  render layout: false
end

#indexObject



67
68
69
70
# File 'app/controllers/ish_manager/email_templates_controller.rb', line 67

def index
  authorize! :index, Ish::EmailTemplate
  @templates = Ish::EmailTemplate.all.order_by( slug: :asc ).page( params[:templates_page] ).per( current_profile.per_page )
end

#newObject



72
73
74
75
# File 'app/controllers/ish_manager/email_templates_controller.rb', line 72

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

#showObject



77
78
79
80
81
82
83
84
# File 'app/controllers/ish_manager/email_templates_controller.rb', line 77

def show
  authorize! :show, Ish::EmailTemplate
  @templates = Ish::EmailTemplate.all.page( params[:templates_page] )

  @tmpl = @email_template = Ish::EmailTemplate.where({ id: params[:id] }).first ||
    Ish::EmailTemplate.find_by({ slug: params[:id] })
  @ctx = @email_context = ::Ish::EmailContext.new({ body: Ish::LoremIpsum.html })
end

#updateObject



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

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