Class: InlineFormsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- InlineFormsController
- 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
- #cancan_disabled? ⇒ Boolean
- #cancan_enabled? ⇒ Boolean
-
#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.
-
#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.
Class Method Details
.cancan_enabled? ⇒ Boolean
26 27 28 29 30 31 32 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 26 def self.cancan_enabled? begin ::Ability && true rescue NameError false end end |
Instance Method Details
#cancan_disabled? ⇒ Boolean
38 39 40 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 38 def cancan_disabled? ! self.class.cancan_enabled? end |
#cancan_enabled? ⇒ Boolean
34 35 36 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 34 def cancan_enabled? self.class.cancan_enabled? end |
#create ⇒ Object
:create creates the object made with :new. It then presents the list of objects.
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 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 110 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 == :associated end @parent_class = params[:parent_class] @parent_id = params[:parent_id] @PER_PAGE = 5 unless @parent_class.nil? # for the logic behind the :conditions see the #index method. @parent_class.nil? ? conditions = [ @Klass.order_by_clause.to_s + " like ?", "%#{params[:search]}%" ] : conditions = [ "#{@parent_class.foreign_key} = ?", @parent_id ] object[@parent_class.foreign_key] = @parent_id unless @parent_class.nil? if object.save flash.now[:success] = "Successfully created #{object.class.to_s.underscore}." if cancan_enabled? @objects = @Klass.accessible_by(current_ability).order(@Klass.order_by_clause).paginate :page => params[:page], :per_page => @PER_PAGE || 12, :conditions => conditions else @objects = @Klass.order(@Klass.order_by_clause).paginate :page => params[:page], :per_page => @PER_PAGE || 12, :conditions => conditions end respond_to do |format| format.js { render :list } end else flash.now[:error] = "Kan #{object.class.to_s.underscore} niet aanmaken.".html_safe object.errors.each do |e| flash.now[:error] << '<br />'.html_safe + e[0].to_s + ": " + e[1] end respond_to do |format| @object = object @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)
193 194 195 196 197 198 199 200 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 193 def destroy @update_span = params[:update] @object = @Klass.find(params[:id]) @object.destroy respond_to do |format| format.js { render :show_undo } end end |
#edit ⇒ Object
:edit presents a form to edit one specific attribute from an object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 97 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.js { } end 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.
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 |
# 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] @PER_PAGE = 5 unless @parent_class.nil? # 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 if @parent_class.nil? conditions = [ @Klass.order_by_clause.to_s + " like ?", "%#{params[:search]}%" ] else conditions = [ "#{@parent_class.foreign_key} = ?", @parent_id ] end # if we are using cancan, then make sure to select only accessible records if cancan_enabled? @objects = @Klass.accessible_by(current_ability).order(@Klass.order_by_clause).paginate( :page => params[:page], :per_page => @PER_PAGE || 12, :conditions => conditions ) else @objects = @Klass.order(@Klass.order_by_clause).paginate( :page => params[:page], :per_page => @PER_PAGE || 12, :conditions => conditions ) end 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
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 82 def new @object = @Klass.new @update_span = params[:update] @parent_class = params[:parent_class] unless @parent_class.nil? @parent_id = params[:parent_id] @object[@parent_class.foreign_key] = @parent_id end @object.inline_forms_attribute_list = @inline_forms_attribute_list if @inline_forms_attribute_list respond_to do |format| format.js { } end end |
#revert ⇒ Object
:revert works like undo. Thanks Ryan Bates: railscasts.com/episodes/255-undo-with-paper-trail
204 205 206 207 208 209 210 211 212 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 204 def revert @update_span = params[:update] @version = Version.find(params[:id]) @version.reify.save! @object = @Klass.find(@version.item_id) respond_to do |format| format.js { render :close } end end |
#show ⇒ Object
: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 |
# 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 @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 |
#update ⇒ Object
:update updates a specific attribute from an object.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/app/controllers/inline_forms_controller.rb', line 147 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 #puts "Requested #{request.format}" respond_to do |format| format.js { } end end |