class Admin::ResourceController
def self.model_class(model_class = nil)
def self.model_class(model_class = nil) @model_class ||= (model_class || controller_name).to_s.singularize.camelize.constantize end
def self.paginate_models(options = {})
def self.paginate_models(options = {}) @@paginated = true @@will_paginate_options = options.slice(:class, :previous_label, :next_label, :inner_window, :outer_window, :separator, :container).merge(param_name: :p) @@default_per_page = options[:per_page] end
def announce_not_found
def announce_not_found flash[:notice] = t('resource_controller.not_found', humanized_model_name: humanized_model_name) end
def announce_removed
def announce_removed ActiveSupport::Deprecation.warn('announce_removed is no longer encouraged in TrustyCms 0.9.x.', caller) flash[:notice] = t('resource_controller.removed', humanized_model_name: humanized_model_name) end
def announce_update_conflict
def announce_update_conflict flash.now[:error] = t('resource_controller.update_conflict', humanized_model_name: humanized_model_name) end
def announce_validation_errors
def announce_validation_errors flash.now[:error] = t('resource_controller.validation_errors') end
def clear_model_cache
def clear_model_cache Rails.cache.clear end
def continue_url(options)
def continue_url(options) options[:redirect_to] || (params[:continue] ? { action: 'edit', id: model.id } : index_page_for_model) end
def destroy
def destroy model.destroy response_for :destroy end
def edit_model_path
def edit_model_path method = "edit_admin_#{model_name.underscore}_path" send method.to_sym, params[:id] end
def format
def format params[:format] || 'html' end
def format_symbol
def format_symbol format.to_sym end
def humanized_model_name
def humanized_model_name t(model_name.underscore.downcase) end
def index
def index response_for :plural end
def index_page_for_model
def index_page_for_model parts = { action: 'index' } if paginated? && model && i = model_class.all.index(model) p = (i / pagination_parameters[:per_page].to_i) + 1 parts[:p] = p if p && p > 1 end parts end
def load_model
def load_model self.model = if params[:id] model_class.find(params[:id]) else model_class.new end end
def load_models
def load_models self.models = paginated? ? model_class.paginate(pagination_parameters) : model_class.all end
def model
def model instance_variable_get("@#{model_symbol}") || load_model end
def model=(object)
def model=(object) instance_variable_set("@#{model_symbol}", object) end
def model_class
def model_class self.class.model_class end
def model_name
def model_name model_class.name end
def model_symbol
def model_symbol model_name.underscore.intern end
def models
def models instance_variable_get("@#{plural_model_symbol}") || load_models end
def models=(objects)
def models=(objects) instance_variable_set("@#{plural_model_symbol}", objects) end
def never_cache
I would like to set this to expires_in(1.minute, :private => true) to allow for more fluid navigation
def never_cache expires_now end
def paginated?
a convenience method that returns true if paginate_models has been called on this controller class
def paginated? self.class.paginated == true && params[:pp] != 'all' end
def pagination_parameters
the per_page figure can be set in several ways:
return a hash of page and per_page that can be used to build a will_paginate collection
def pagination_parameters pp = params[:pp] || TrustyCms.config['admin.pagination.per_page'] pp = (self.class.default_per_page || 50) if pp.blank? { page: (params[:p] || 1).to_i, per_page: pp.to_i, } end
def permitted_params
def permitted_params model_symbols = ActiveRecord::Base.descendants.map { |a| a.name.underscore.to_sym } keys = params.keys.map { |k| k.underscore.to_sym } valid_symbols = model_symbols & keys valid_symbols.each do |symbol| params[symbol].permit! end params end
def plural_model_name
def plural_model_name model_name.pluralize end
def plural_model_symbol
def plural_model_symbol model_name.pluralize.underscore.intern end
def populate_format
Assist with user agents that cause improper content-negotiation
def populate_format params[:format] ||= 'html' unless request.xhr? end
def rescue_action(exception)
def rescue_action(exception) case exception when ActiveRecord::RecordInvalid response_for :invalid when ActiveRecord::StaleObjectError response_for :stale when ActiveRecord::RecordNotFound response_for :not_found else super end end
def set_owner_or_editor
def set_owner_or_editor if model.has_attribute? :created_by_id model.created_by_id = current_user.id if model.id == nil model.updated_by_id = current_user.id end end
def will_paginate_options
def will_paginate_options self.class.will_paginate_options || {} end