Class: InlineFormsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- InlineFormsController
- Includes:
- CancanConcern
- 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_action extracts the class and puts it in @Klass
Instance Method Summary collapse
-
#create ⇒ Object
:create creates the object made with :new.
-
#destroy ⇒ Object
:destroy destroys the record, but also shows an undo link (with paper_trail).
-
#edit ⇒ Object
:edit presents a form to edit one specific attribute from an object.
- #extract_translations ⇒ Object
-
#index ⇒ Object
: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.
-
#new ⇒ Object
:new prepares a new object, updates the list of objects and replaces it with an empty form.
-
#revert ⇒ Object
:revert works like undo.
-
#show ⇒ Object
:show shows one attribute (attribute) from a record (object).
-
#update ⇒ Object
:update updates a specific attribute from an object.
Methods included from CancanConcern
#cancan_disabled?, #cancan_enabled?
Instance Method Details
#create ⇒ Object
:create creates the object made with :new. It then presents the list of objects.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 92 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).[: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.where(conditions).paginate(:page => params[:page]) @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 |
#destroy ⇒ Object
:destroy destroys the record, but also shows an undo link (with paper_trail)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 183 def destroy @update_span = params[:update] @object = referenced_object if current_user.role? :superadmin @object.destroy respond_to do |format| format.html { } unless @Klass.not_accessible_through_html? format.js { render :show_undo } end elsif (@Klass.safe_deletable? rescue false) @object.safe_delete(current_user) respond_to do |format| format.html { } unless @Klass.not_accessible_through_html? format.js { render :close } end end end |
#edit ⇒ Object
:edit presents a form to edit one specific attribute from an object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 78 def edit @object = referenced_object @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_translations ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 224 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 |
#index ⇒ Object
: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.
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 'lib/app/controllers/inline_forms_controller.rb', line 32 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).[: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.where(conditions).paginate(:page => params[:page]) 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 |
#new ⇒ Object
: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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 60 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).[: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.html { render 'inline_forms/_new', :layout => 'inline_forms' } unless @Klass.not_accessible_through_html? format.js { } end end |
#revert ⇒ Object
:revert works like undo. Thanks Ryan Bates: railscasts.com/episodes/255-undo-with-paper-trail
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 203 def revert @update_span = params[:update] if current_user.role? :superadmin @version = PaperTrail::Version.find(params[:id]) @version.reify.save! @object = @Klass.find(@version.item_id) (:revert, @object) if cancan_enabled? respond_to do |format| format.html { } unless @Klass.not_accessible_through_html? format.js { render :close } end elsif (@Klass.safe_deletable? rescue false) @object = referenced_object @object.safe_restore respond_to do |format| format.html { } unless @Klass.not_accessible_through_html? format.js { render :close } end end end |
#show ⇒ Object
:show shows one attribute (attribute) from a record (object). It includes the link to ‘edit’
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 148 def show @object = referenced_object @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.html { } unless @Klass.not_accessible_through_html? format.js { render :show_element } end end end |
#update ⇒ Object
:update updates a specific attribute from an object.
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 132 def update @object = referenced_object @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.html { } unless @Klass.not_accessible_through_html? format.js { } end end |