Class: InlineFormsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/app/controllers/inline_forms_controller.rb

Overview

Generic controller for the inline_forms plugin.

Usage

If you have an Example class, make an ExampleController that is a subclass of InlineFormsController

class ExampleController < InlineFormsController
end

That’s it! It’ll work. But please read about the InlineForms::InlineFormsGenerator first!

You can override the methods in your ExampleController

def index
  @objects=@Klass.all
end

and @Klass will be set to Example by the getKlass before filter.

How it works

The getKlass before_filter extracts the class and puts it in @Klass

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cancan_enabled?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/app/controllers/inline_forms_controller.rb', line 27

def self.cancan_enabled?
  begin
    ::Ability && true
  rescue NameError
    false
  end
end

Instance Method Details

#cancan_disabled?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/app/controllers/inline_forms_controller.rb', line 39

def cancan_disabled?
  ! self.class.cancan_enabled?
end

#cancan_enabled?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/app/controllers/inline_forms_controller.rb', line 35

def cancan_enabled?
  self.class.cancan_enabled?
end

#createObject

:create creates the object made with :new. It then presents the list of objects.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/app/controllers/inline_forms_controller.rb', line 109

def create
  @object ||= @Klass.new
  @update_span = params[:update]
  attributes = @inline_forms_attribute_list || @object.inline_forms_attribute_list
  attributes.each do | attribute, name, form_element |
    send("#{form_element.to_s}_update", @object, attribute) unless form_element == :tree || form_element == :associated || (cancan_enabled? && cannot?(:read, @Klass.to_s.underscore.pluralize.to_sym, attribute))
  end
  @parent_class = params[:parent_class]
  @parent_id = params[:parent_id]
  # for the logic behind the :conditions see the #index method.
  conditions = nil
  if @parent_class.nil? || @Klass.reflect_on_association(@parent_class.underscore.to_sym).nil?
    conditions = [ @Klass.table_name + "." + @Klass.order_by_clause + " like ?", "%#{params[:search]}%" ] if @Klass.respond_to?(:order_by_clause) && ! @Klass.order_by_clause.nil?
  else
    foreign_key = @Klass.reflect_on_association(@parent_class.underscore.to_sym).options[:foreign_key] || @parent_class.foreign_key
    conditions =  [ "#{foreign_key} = ?", @parent_id ]
    @object[foreign_key] = @parent_id
  end
  if @object.save
    flash.now[:success] = t('success', :message => @object.class.model_name.human)
    @objects = @Klass
    @objects = @Klass.accessible_by(current_ability) if cancan_enabled?
    @objects = @objects.order(@Klass.table_name + "." + @Klass.order_by_clause) if @Klass.respond_to?(:order_by_clause) && ! @Klass.order_by_clause.nil?
    @objects = @objects.paginate :page => params[:page], :conditions => conditions
    @object = nil
    respond_to do |format|
      format.js { render :list}
    end
  else
    flash.now[:header] = ["Kan #{@object.class.to_s.underscore} niet aanmaken."]
    flash.now[:error] = @object.errors.to_a
    respond_to do |format|
      @object.inline_forms_attribute_list = attributes
      format.js { render :new}
    end
  end
end

#destroyObject

:destroy destroys the record, but also shows an undo link (with paper_trail)



197
198
199
200
201
202
203
204
# File 'lib/app/controllers/inline_forms_controller.rb', line 197

def destroy
  @update_span = params[:update]
  @object = @Klass.find(params[:id])
  @object.destroy
  respond_to do |format|
    format.js { render :show_undo }
  end
end

#editObject

:edit presents a form to edit one specific attribute from an object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/app/controllers/inline_forms_controller.rb', line 95

def edit
  @object = @Klass.find(params[:id])
  @attribute = params[:attribute]
  @form_element = params[:form_element]
  @sub_id = params[:sub_id]
  @update_span = params[:update]
  respond_to do |format|
    #format.html { } unless @Klass.not_accessible_through_html?
    format.js { }
  end
end

#extract_translationsObject



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/app/controllers/inline_forms_controller.rb', line 219

def extract_translations
  keys_array = []
  I18n::Backend::ActiveRecord::Translation.order(:locale, :thekey).each do |t|
    keys_array << deep_hashify([ t.locale, t.thekey.split('.'), t.value ].flatten)
  end
  keys_hash = {}
  keys_array.each do |h|
    keys_hash = deep_merge(keys_hash, h)
  end
  @display_array = unravel(keys_hash)
end

#indexObject

:index shows a list of all objects from class @Klass, using will_paginate, including a link to ‘new’, that allows you to create a new record.



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
# File 'lib/app/controllers/inline_forms_controller.rb', line 48

def index
  @update_span = params[:update]
  @parent_class = params[:parent_class]
  @parent_id = params[:parent_id]
  @ul_needed = params[:ul_needed]
  # if the parent_class is not nill, we are in associated list and we don't search there.
  # also, make sure the Model that you want to do a search on has a :name attribute. TODO
  conditions = nil
  if @parent_class.nil? || @Klass.reflect_on_association(@parent_class.underscore.to_sym).nil?
    conditions = [ @Klass.table_name + "." + @Klass.order_by_clause + " like ?", "%#{params[:search]}%" ] if @Klass.respond_to?(:order_by_clause) && ! @Klass.order_by_clause.nil?
  else
    foreign_key = @Klass.reflect_on_association(@parent_class.underscore.to_sym).options[:foreign_key] || @parent_class.foreign_key
    conditions =  [ "#{foreign_key} = ?", @parent_id ]
  end
  # if we are using cancan, then make sure to select only accessible records
  @objects ||= @Klass.accessible_by(current_ability) if cancan_enabled?
  @objects ||= @Klass
  @objects = @objects.order(@Klass.table_name + "." + @Klass.order_by_clause) if @Klass.respond_to?(:order_by_clause) && ! @Klass.order_by_clause.nil?
  @objects = @objects.paginate(
      :page => params[:page],
      :conditions => conditions )
  respond_to do |format|
    format.html { render 'inline_forms/_list', :layout => 'inline_forms' } unless @Klass.not_accessible_through_html?
    format.js { render :list }
  end
end

#newObject

:new prepares a new object, updates the list of objects and replaces it with an empty form. After pressing OK or Cancel, the list of objects is retrieved in the same way as :index



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/app/controllers/inline_forms_controller.rb', line 78

def new
  @object ||= @Klass.new
  @update_span = params[:update]
  @parent_class = params[:parent_class]
  begin
    @parent_id = params[:parent_id]
    foreign_key = @Klass.reflect_on_association(@parent_class.underscore.to_sym).options[:foreign_key] || @parent_class.foreign_key
    @object[foreign_key] = @parent_id
  end unless @parent_class.nil? || @Klass.reflect_on_association(@parent_class.underscore.to_sym).nil?

  @object.inline_forms_attribute_list = @inline_forms_attribute_list if @inline_forms_attribute_list
  respond_to do |format|
    format.js { }
  end
end

#revertObject

:revert works like undo. Thanks Ryan Bates: railscasts.com/episodes/255-undo-with-paper-trail



208
209
210
211
212
213
214
215
216
217
# File 'lib/app/controllers/inline_forms_controller.rb', line 208

def revert
  @update_span = params[:update]
  @version = PaperTrail::Version.find(params[:id])
  @version.reify.save!
  @object = @Klass.find(@version.item_id)
  authorize!(:revert, @object) if cancan_enabled?
  respond_to do |format|
    format.js { render :close }
  end
end

#showObject

:show shows one attribute (attribute) from a record (object). It includes the link to ‘edit’



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/app/controllers/inline_forms_controller.rb', line 163

def show
  @object = @Klass.find(params[:id])
  @attribute = params[:attribute]
  @form_element = params[:form_element]
  close = params[:close] || false
  if @form_element == "associated"
    @sub_id = params[:sub_id]
    if @sub_id.to_i > 0
      @associated_record_id = @object.send(@attribute.to_s.singularize + "_ids").index(@sub_id.to_i)
      @associated_record = @object.send(@attribute)[@associated_record_id]
    end
  end
  if @form_element == "has_one"
    @associated_record = @object.send(@attribute)
    @associated_record_id = @associated_record.id
  end
  @update_span = params[:update]
  if @attribute.nil?
    respond_to do |format|
      @attributes = @object.inline_forms_attribute_list
      if close
        format.js { render :close }
      else
        format.js { }
      end
    end
  else
    respond_to do |format|
      format.js { render :show_element }
    end
  end
end

#updateObject

:update updates a specific attribute from an object.



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/app/controllers/inline_forms_controller.rb', line 148

def update
  @object = @Klass.find(params[:id])
  @attribute = params[:attribute]
  @form_element = params[:form_element]
  @sub_id = params[:sub_id]
  @update_span = params[:update]
  send("#{@form_element.to_s}_update", @object, @attribute)
  @object.save
  respond_to do |format|
    format.js { }
  end
end