Module: ContactFieldActions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
# File 'app/controllers/contact_field_actions.rb', line 2

def self.included(base)
  base.class_eval do
    before_filter :load_contactable
    helper ContactableHelper
  end
end

Instance Method Details

#association_nameObject



15
16
17
# File 'app/controllers/contact_field_actions.rb', line 15

def association_name
  self.field_name.pluralize
end

#createObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/contact_field_actions.rb', line 23

def create
  @field = @contactable.send("#{self.association_name}").new(params[self.field_name.to_sym])

  if @field.save
    flash[:notice] = t(self.association_name + '.create.notice')
    respond_to do |format|
      format.js {}
    end
  else
    # TODO: handle errors via js
  end
end

#destroyObject



49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/contact_field_actions.rb', line 49

def destroy
  @field = @contactable.send("#{self.association_name}").find(params[:id])
  @field_dom_id = dom_id @field
  if @field.destroy
    flash[:notice] = t(self.association_name + '.destroy.notice')

    respond_to do |format|
      format.js {}
    end
  end
end

#field_nameObject



19
20
21
# File 'app/controllers/contact_field_actions.rb', line 19

def field_name
  self.controller_class_name.chomp("Controller").singularize.underscore
end

#load_contactableObject



9
10
11
12
13
# File 'app/controllers/contact_field_actions.rb', line 9

def load_contactable
  path_component = request.path.split('/').reject(&:blank?).first.singularize
  class_name = path_component.capitalize
  @contactable = Kernel.const_get(class_name).find(params["#{path_component}_id"])
end

#updateObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/contact_field_actions.rb', line 36

def update
  @field = @contactable.send("#{self.association_name}").find(params[:id])

  @field.attributes = params[self.field_name.to_sym]
  if @field.save
    flash[:notice] = t(self.association_name + '.update.notice')

    respond_to do |format|
      format.js {}
    end
  end
end