Class: WcoEmail::EmailActionTemplatesController

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

Instance Method Summary collapse

Instance Method Details

#destroyObject

Alphabetized : )



8
9
10
11
12
13
14
# File 'app/controllers/wco_email/email_action_templates_controller.rb', line 8

def destroy
  @tmpl = WcoEmail::EmailActionTemplate.find( params[:id] )
  authorize! :delete, @tmpl
  @tmpl.delete
  flash_notice 'Probably success'
  redirect_to action: :index
end

#editObject



16
17
18
19
20
# File 'app/controllers/wco_email/email_action_templates_controller.rb', line 16

def edit
  @tmpl = WcoEmail::EmailActionTemplate.find( params[:id] )
  @tmpl.ties.push WcoEmail::EmailActionTemplateTie.new( next_tmpl_id: nil )
  authorize! :edit, @tmpl
end

#indexObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/wco_email/email_action_templates_controller.rb', line 22

def index
  authorize! :index, @new_tmpl
  if params[:deleted]
    @tmpls = WcoEmail::EmailActionTemplate.deleted
  else
    @tmpls = WcoEmail::EmailActionTemplate.all
  end

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

  @tmpls = @tmpls.order_by( slug: :asc )
end

#newObject



38
39
40
# File 'app/controllers/wco_email/email_action_templates_controller.rb', line 38

def new
  authorize! :new, @new_tmpl
end

#showObject



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

def show
  @tmpl = WcoEmail::EmailActionTemplate.find( params[:id] )
  authorize! :show, @tmpl
end

#updateObject

or create



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/wco_email/email_action_templates_controller.rb', line 47

def update # or create
  puts! params, 'params'

  if params[:id]
    @tmpl = WcoEmail::EmailActionTemplate.find( params[:id] )
  else
    @tmpl = WcoEmail::EmailActionTemplate.new
  end
  authorize! :upsert, @tmpl

  if params[:tmpl][:ties_attributes]
    params[:tmpl][:ties_attributes].each do |k, v|
      if !v[:next_tmpl_id].present?
        params[:tmpl][:ties_attributes].delete( k )
      end
      if v[:to_delete] == "1"
        WcoEmail::EmailActionTemplateTie.find( v[:id] ).delete
        params[:tmpl][:ties_attributes].delete( k )
      end
    end
  end

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

end