Class: Basepack::BaseController
- Inherits:
-
InheritedResources::Base
- Object
- InheritedResources::Base
- Basepack::BaseController
- Defined in:
- app/controllers/basepack/base_controller.rb
Direct Known Subclasses
Class Method Summary collapse
-
.actions(*actions_to_keep) ⇒ Object
Copied from inherited_resources for support of custom actions.
- .create_custom_action(resource_or_collection, action) ⇒ Object
Instance Method Summary collapse
-
#bulk_delete(options = {}, &block) ⇒ Object
[DELETE] /resources/bulk_delete.
-
#bulk_edit(option = {}, &block) ⇒ Object
Bulk_edit shows form for bulk editing items (edit multiple items at one) Forms look like normal edit form, but for the fields where you can set several values (has_and_belongs_to_many, has_many with through, tags, etc.) the form shows possibilites for add/delete items as well.
- #bulk_edit_form_for(resource_or_chain, options = {}) ⇒ Object
- #bulk_update(options = {}, &block) ⇒ Object (also: #bulk_update!)
- #create(options = {}, &block) ⇒ Object (also: #create!)
-
#default_query ⇒ Object
Used to set default query (default fiter) It is called by before_filter.
-
#default_query_params ⇒ Object
returns the hash of default query params redefine in sub-classes to customize the default filter.
- #destroy(options = {}, &block) ⇒ Object (also: #destroy!)
-
#diff ⇒ Object
(also: #diff!)
[GET] /resources/:id/diff/:id2.
-
#diff_form_for(resource, resource2, options = {}) ⇒ Object
resource or chain.
- #edit_form_for(resource_or_chain, options = {}) ⇒ Object
-
#export ⇒ Object
(also: #export!)
[GET,POST] /resources/export.
- #export_form_for(query_form) ⇒ Object
- #filters ⇒ Object
- #index(options = {}, &block) ⇒ Object (also: #index!)
- #list_form_for(query_form) ⇒ Object
-
#merge(options = {}, &block) ⇒ Object
(also: #merge!)
[POST] /resources/:id/diff/:id2.
- #options(options = {}) ⇒ Object (also: #options!)
-
#query ⇒ Object
(also: #query!)
[GET,POST] /resources/query.
- #query_form_for(class_or_chain, scope, options = {}) ⇒ Object
- #show_form_for(resource_or_chain) ⇒ Object
- #taggings(options = {}) ⇒ Object (also: #taggings!)
- #update(options = {}, &block) ⇒ Object (also: #update!)
Class Method Details
.actions(*actions_to_keep) ⇒ Object
Copied from inherited_resources for support of custom actions. Defines wich actions will be inherited from the inherited controller. Syntax is borrowed from resource_controller.
actions :index, :show, :edit
actions :all, :except => :index
21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/controllers/basepack/base_controller.rb', line 21 def actions(*actions_to_keep) raise ArgumentError, 'Wrong number of arguments. You have to provide which actions you want to keep.' if actions_to_keep.empty? = actions_to_keep. actions_to_remove = Array([:except]) actions_to_remove += __actions - actions_to_keep.map { |a| a.to_sym } unless actions_to_keep.first == :all actions_to_remove.map! { |a| a.to_sym }.uniq! (instance_methods.map { |m| m.to_sym } & actions_to_remove).each do |action| undef_method action, "#{action}!" end end |
.create_custom_action(resource_or_collection, action) ⇒ Object
10 11 12 13 |
# File 'app/controllers/basepack/base_controller.rb', line 10 def create_custom_action(resource_or_collection, action) super self.__actions += [action.to_sym] end |
Instance Method Details
#bulk_delete(options = {}, &block) ⇒ Object
[DELETE] /resources/bulk_delete
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'app/controllers/basepack/base_controller.rb', line 218 def bulk_delete( = {}, &block) unless params[:ids] redirect_to collection_url return end ids = params[:ids].split(',') col = collection_without_pagination.accessible_by(current_ability, :destroy).where(id: ids) ret = col.destroy_all destroyed_count = ret.count not_destroyed_count = ids.count - destroyed_count = "#{destroyed_count} #{resource_class.model_name.human(count: destroyed_count)}" if not_destroyed_count == 0 flash[:success] = t("admin.flash.successful", name: , action: t("admin.actions.delete.done")) else = ", #{not_destroyed_count} #{resource_class.model_name.human(count: not_destroyed_count)}" flash[:error] = t("admin.flash.successful", name: , action: t("admin.actions.delete.done")) flash[:error] << t("admin.flash.error", name: , action: t("admin.actions.delete.done")) end redirect_to collection_url end |
#bulk_edit(option = {}, &block) ⇒ Object
Bulk_edit shows form for bulk editing items (edit multiple items at one) Forms look like normal edit form, but for the fields where you can set several values (has_and_belongs_to_many, has_many with through, tags, etc.) the form shows possibilites for add/delete items as well. The particular fields partials are defined in app/views/forms/bulk_edit basepack directiry.
You can configure a field input by defining bulk_edit_partion in the field's configuration.
If the form's field is blank, then the attribute in the model is not changed because of that the validation on presence is excluded.
I dont know how to check if the form is valid, so I allways proseed updated and redirect to the index action (and show proper notice/error on the screen).
The defaut action for extend fields should be configured by bulk_edit accessors in the model. For example:
def bulk_edit @bulk_edit ||= OpenStruct.new @ddbulk_edit.my_habtml_relation_ids = 'delete' @bulk_edit end
following method should be useful only for rendering the form again
currently there is no such workflow.
def bulk_edit=(fields) @bulk_edit = OpenStruct.new(fields) end
For customizing particular behaviour for field action on the model you can define methods with naming "bulk_edit_<field_name>=", as demonstrates following example:
def bulk_edit_tag_list=(action, new_value) value_arr = new_value.to_s.split(/,/) case action when 'add' self.tag_list += value_arr when 'assign' self.tag_list = new_value when 'delete' self.tag_list -= value_arr else raise ArgumentError, "Unknow bulk action: #Basepack::BaseController.actionaction.inspect" end end
TODO:
- the URL contains filter params, which could overflow the GET length limit
- filter like f=... don't work
306 307 308 |
# File 'app/controllers/basepack/base_controller.rb', line 306 def bulk_edit(option = {}, &block) # empty, all is done by 'custom_actions' end |
#bulk_edit_form_for(resource_or_chain, options = {}) ⇒ Object
389 390 391 |
# File 'app/controllers/basepack/base_controller.rb', line 389 def bulk_edit_form_for(resource_or_chain, = {}) form_factory_rails_admin(:bulk_edit, Basepack::Forms::BulkEdit, resource_or_chain, ) end |
#bulk_update(options = {}, &block) ⇒ Object Also known as: bulk_update!
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'app/controllers/basepack/base_controller.rb', line 310 def bulk_update( = {}, &block) bulk_params = resource_build_bulk_params bulk_values = build_resource_params.first.slice(*bulk_params.keys) update_params = build_resource_params.first.except(*bulk_params.keys).reject { |k,v| v.blank? } res = collection_without_pagination.accessible_by(current_ability, :update).reject do |object| update_bulk_params(object, update_params, bulk_params, bulk_values) end if res.empty? flash[:notice] ||= redirect_to polymorphic_path(chain_with_class, query_params) else flash[:error] ||= filter_params = { "f[#{resource_class.primary_key}_in]" => res.map(&:id) }.reverse_merge(query_params) redirect_to polymorphic_path(chain_with_class, filter_params) end end |
#create(options = {}, &block) ⇒ Object Also known as: create!
123 124 125 |
# File 'app/controllers/basepack/base_controller.rb', line 123 def create(={}, &block) super(.reverse_merge(notice: ), &block) end |
#default_query ⇒ Object
Used to set default query (default fiter) It is called by before_filter. To define default custom filter redefine method default_query_params
65 66 67 68 |
# File 'app/controllers/basepack/base_controller.rb', line 65 def default_query return if default_query_params.nil? or default_query_params.empty? redirect_to query_resources_path(default_query_params) unless params[:f] end |
#default_query_params ⇒ Object
returns the hash of default query params redefine in sub-classes to customize the default filter
72 73 74 |
# File 'app/controllers/basepack/base_controller.rb', line 72 def default_query_params nil end |
#destroy(options = {}, &block) ⇒ Object Also known as: destroy!
129 130 131 |
# File 'app/controllers/basepack/base_controller.rb', line 129 def destroy(={}, &block) super(.reverse_merge(notice: ), &block) end |
#diff ⇒ Object Also known as: diff!
[GET] /resources/:id/diff/:id2
188 189 190 191 |
# File 'app/controllers/basepack/base_controller.rb', line 188 def diff @resource2 = resource2 respond_with(chain) end |
#diff_form_for(resource, resource2, options = {}) ⇒ Object
resource or chain
380 381 382 383 |
# File 'app/controllers/basepack/base_controller.rb', line 380 def diff_form_for(resource, resource2, = {}) # resource or chain [:method] = :post Basepack::Forms::Factories::RailsAdmin.new(:edit, view_context, Basepack::Forms::Diff, Basepack::Forms::Groups::Diff).new_form(resource, resource2, ) end |
#edit_form_for(resource_or_chain, options = {}) ⇒ Object
374 375 376 377 378 |
# File 'app/controllers/basepack/base_controller.rb', line 374 def edit_form_for(resource_or_chain, = {}) res = Array.wrap(resource_or_chain).last section = (res.is_a?(Class) or res.new_record?) ? :create : :update form_factory_rails_admin(section, Basepack::Forms::Edit, resource_or_chain, ) end |
#export ⇒ Object Also known as: export!
[GET,POST] /resources/export
176 177 178 179 180 181 182 183 |
# File 'app/controllers/basepack/base_controller.rb', line 176 def export if format = params[:json] && :json || params[:csv] && :csv || params[:xml] && :xml request.format = format index else render end end |
#export_form_for(query_form) ⇒ Object
385 386 387 |
# File 'app/controllers/basepack/base_controller.rb', line 385 def export_form_for(query_form) form_factory_rails_admin(:export, Basepack::Forms::Export, query_form.chain_with_class, query_form: query_form) end |
#filters ⇒ Object
393 394 395 396 |
# File 'app/controllers/basepack/base_controller.rb', line 393 def filters collection #just for authorize resource redirect_to polymorphic_path(Basepack::Settings.filters.model_name.constantize, 'f[filter_type_eq]' => resource_class) end |
#index(options = {}, &block) ⇒ Object Also known as: index!
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/controllers/basepack/base_controller.rb', line 77 def index(={}, &block) block ||= proc do |format| format.html do render :index # index is also called from query action end format.json do schema = export_form.schema_from_params(params[:schema]) send_export_data(header: '[', footer: ']') do |object, i| "#{',' if i > 0}#{object.to_json(schema)}" end end format.xml do schema = {skip_instruct: true}.reverse_merge(export_form.schema_from_params(params[:schema])) send_export_data( header: %Q{<?xml version="1.0" encoding="UTF-8"?>\n<#{resource_class.model_name.plural} type="array">\n}, footer: %Q{</#{resource_class.model_name.plural}>} ) do |object, i| object.to_xml(schema) end end format.csv do form = export_form fields = form.fields_from_params(params[:schema]) = params[:csv_options] || {} = {encoding: 'UTF-8', col_sep: [:col_sep].presence || Basepack::Settings.export.default_col_sep} header = [:skip_header] == 'true' ? '' : CSV.generate_line(form.csv_header(fields), ) send_export_data(header: header) do |object, i| CSV.generate_line(form.csv_row_for_resource(object, fields), ) end end end super(, &block) end |
#list_form_for(query_form) ⇒ Object
330 331 332 |
# File 'app/controllers/basepack/base_controller.rb', line 330 def list_form_for(query_form) form_factory_rails_admin(:list, Basepack::Forms::List, query_form.chain_with_class, query_form: query_form) end |
#merge(options = {}, &block) ⇒ Object Also known as: merge!
[POST] /resources/:id/diff/:id2
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'app/controllers/basepack/base_controller.rb', line 196 def merge( = {}, &block) (:update, resource) # CanCan @resource = resource @resource2 = resource2 merge = params[:merge] merge.each do |key, val| if val == "right" resource[key] = @resource2[key] end end resource.save [:notice] ||= respond_with(*with_chain(resource), , &block) end |
#options(options = {}) ⇒ Object Also known as: options!
135 136 137 138 139 140 141 142 143 144 |
# File 'app/controllers/basepack/base_controller.rb', line 135 def (={}) primary_key = resource_class.primary_key response = ([:collection] || collection).map do |object| { :id => object.send(primary_key), :text => ERB::Util.html_escape(object.to_details_label), } end render :json => response end |
#query ⇒ Object Also known as: query!
[GET,POST] /resources/query
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/controllers/basepack/base_controller.rb', line 149 def query filter_class_name = Basepack::Settings.filters.model_name if filter_class_name.present? and params[:filter_name].present? filter_class = filter_class_name.constantize filter = filter_class.new( name: params[:filter_name], filter: params[:ql] || query_form.conditions_to_ql, filter_type: resource_class.to_s ) filter.assign_attributes(current_ability.attributes_for(:create, filter_class)) if filter.save flash.now[:notice] = (Basepack::Utils.model_config(filter_class).label) else flash.now[:error] = I18n.t :error_filter, scope: [:misc] end end if request.xhr? render partial: "query" else index end end |
#query_form_for(class_or_chain, scope, options = {}) ⇒ Object
338 339 340 341 342 343 |
# File 'app/controllers/basepack/base_controller.rb', line 338 def query_form_for(class_or_chain, scope, = {}) Basepack::Forms::Factories::QueryRailsAdmin.new(view_context).new_form( class_or_chain, { scope: scope }.reverse_merge!(.reverse_merge(params: params, auth_object: current_ability)) ) end |
#show_form_for(resource_or_chain) ⇒ Object
334 335 336 |
# File 'app/controllers/basepack/base_controller.rb', line 334 def show_form_for(resource_or_chain) form_factory_rails_admin(:show, Basepack::Forms::Show, resource_or_chain) end |
#taggings(options = {}) ⇒ Object Also known as: taggings!
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'app/controllers/basepack/base_controller.rb', line 345 def taggings(={}) (action_name.to_sym, resource_class) query_params = params.clone #for inital data on selectbox change searching from id_tq to name_id if (t = query_params['f']) and (t = t.delete 'id_eq') query_params['f']['name_in'] = t.map! { |value| value.strip } end #add filter to search only on proper type query_params['f'] ||= {} query_params['f']['taggings_taggable_type_eq'] = resource_class.to_s query_form = query_form_for( ActsAsTaggableOn::Tag, ActsAsTaggableOn::Tag.all.accessible_by(current_ability), params: query_params ) response = query_form.collection.map do |object| { :id => ERB::Util.html_escape(object.name), :text => ERB::Util.html_escape(object.name), } end render :json => response end |
#update(options = {}, &block) ⇒ Object Also known as: update!
117 118 119 |
# File 'app/controllers/basepack/base_controller.rb', line 117 def update(={}, &block) super(.reverse_merge(notice: ), &block) end |