require'trusty_cms/resource_responses'classAdmin::ResourceController<ApplicationControllerextendTrustyCms::ResourceResponseshelper_method:model,:current_object,:models,:current_objects,:model_symbol,:plural_model_symbol,:model_class,:model_name,:plural_model_namebefore_action:populate_formatbefore_action:never_cachebefore_action:load_models,only: :indexbefore_action:load_model,only: %i[new create edit update remove destroy]before_action:set_owner_or_editor,only: %i[new create update]after_action:clear_model_cache,only: %i[create update destroy]cattr_reader:paginatedcattr_accessor:default_per_page,:will_paginate_optionscreate_responsesdo|r|# Equivalent respond_to block for :plural responses:# respond_to do |wants|# wants.xml { render :xml => models }# wants.json { render :json => models }# wants.any# endr.plural.publish(:xml,:json){renderformat_symbol=>models}r.singular.publish(:xml,:json){renderformat_symbol=>model}r.singular.default{redirect_toedit_model_pathifaction_name=='show'}r.not_found.publish(:xml,:json){head:not_found}r.not_found.default{announce_not_found;redirect_toaction: 'index'}r.invalid.publish(:xml,:json){renderformat_symbol=>model.errors,:status=>:unprocessable_entity}r.invalid.default{announce_validation_errors;renderaction: template_name}r.stale.publish(:xml,:json){head:conflict}r.stale.default{announce_update_conflict;renderaction: template_name}r.create.publish(:xml,:json){renderformat_symbol=>model,:status=>:created,:location=>url_for(format: format_symbol,id: model)}r.create.default{redirect_tocontinue_url(params)}r.update.publish(:xml,:json){head:ok}r.update.default{redirect_tocontinue_url(params)}r.destroy.publish(:xml,:json){head:deleted}r.destroy.default{redirect_tocontinue_url(params)}enddefindexresponse_for:pluralend%i[show new edit remove].eachdo|action|class_eval%{
def #{action} # def show
response_for :singular # response_for :singular
end # end
},__FILE__,__LINE__end%i[create update].eachdo|action|class_eval%{
def #{action} # def create
model.update!(permitted_params[model_symbol]) # model.update!(params[model_symbol])
response_for :#{action} # response_for :create
end # end
},__FILE__,__LINE__enddefdestroymodel.destroyresponse_for:destroyenddefself.model_class(model_class=nil)@model_class||=(model_class||controller_name).to_s.singularize.camelize.constantizeend# call paginate_models to declare that will_paginate should be used in the index view# options specified here are accessible in the view by calling will_paginate_options# eg.## Class MyController < Admin::ResourceController# paginate_models :per_page => 100defself.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# returns a hash of options that can be passed to will_paginate# the @pagination_for@ helper method calls @will_paginate_options@ unless other options are supplied.## pagination_for(@events)defwill_paginate_optionsself.class.will_paginate_options||{}endhelper_method:will_paginate_options# a convenience method that returns true if paginate_models has been called on this controller class# and can be used to make display decisions in controller and viewdefpaginated?self.class.paginated==true&¶ms[:pp]!='all'endhelper_method:paginated?# return a hash of page and per_page that can be used to build a will_paginate collection# the per_page figure can be set in several ways:# request parameter > declared by paginate_models > default set in config entry @admin.pagination.per_page@ > overall default of 50defpagination_parameterspp=params[:pp]||TrustyCms.config['admin.pagination.per_page']pp=(self.class.default_per_page||50)ifpp.blank?{page: (params[:p]||1).to_i,per_page: pp.to_i,}endprotecteddefrescue_action(exception)caseexceptionwhenActiveRecord::RecordInvalidresponse_for:invalidwhenActiveRecord::StaleObjectErrorresponse_for:stalewhenActiveRecord::RecordNotFoundresponse_for:not_foundelsesuperendenddefmodel_classself.class.model_classenddefset_owner_or_editorifmodel.has_attribute?:created_by_idmodel.created_by_id=current_user.idifmodel.id==nilmodel.updated_by_id=current_user.idendenddefmodelinstance_variable_get("@#{model_symbol}")||load_modelendalias:current_object:modeldefmodel=(object)instance_variable_set("@#{model_symbol}",object)enddefload_modelself.model=ifparams[:id]model_class.find(params[:id])elsemodel_class.newendenddefmodelsinstance_variable_get("@#{plural_model_symbol}")||load_modelsendalias:current_objects:modelsdefmodels=(objects)instance_variable_set("@#{plural_model_symbol}",objects)enddefload_modelsself.models=paginated??model_class.paginate(pagination_parameters):model_class.allenddefmodel_namemodel_class.nameenddefplural_model_namemodel_name.pluralizeendalias:models_name:plural_model_namedefmodel_symbolmodel_name.underscore.internenddefplural_model_symbolmodel_name.pluralize.underscore.internendalias:models_symbol:plural_model_symboldefhumanized_model_namet(model_name.underscore.downcase)enddefcontinue_url(options)options[:redirect_to]||(params[:continue]?{action: 'edit',id: model.id}:index_page_for_model)enddefindex_page_for_modelparts={action: 'index'}ifpaginated?&&model&&i=model_class.all.index(model)p=(i/pagination_parameters[:per_page].to_i)+1parts[:p]=pifp&&p>1endpartsenddefedit_model_pathmethod="edit_admin_#{model_name.underscore}_path"sendmethod.to_sym,params[:id]enddefannounce_validation_errorsflash.now[:error]=t('resource_controller.validation_errors')enddefannounce_removedActiveSupport::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)enddefannounce_not_foundflash[:notice]=t('resource_controller.not_found',humanized_model_name: humanized_model_name)enddefannounce_update_conflictflash.now[:error]=t('resource_controller.update_conflict',humanized_model_name: humanized_model_name)enddefclear_model_cacheRails.cache.clearenddefformat_symbolformat.to_symenddefformatparams[:format]||'html'end# I would like to set this to expires_in(1.minute, :private => true) to allow for more fluid navigation# but the annoyance for concurrent authors would be too great.defnever_cacheexpires_nowend# Assist with user agents that cause improper content-negotiation# warn "Remove default HTML format, Accept header no longer used. (#{__FILE__}: #{__LINE__})" if Rails.version !~ /^2\.1/defpopulate_formatparams[:format]||='html'unlessrequest.xhr?enddefpermitted_paramsmodel_symbols=ActiveRecord::Base.descendants.map{|a|a.name.underscore.to_sym}keys=params.keys.map{|k|k.underscore.to_sym}valid_symbols=model_symbols&keysvalid_symbols.eachdo|symbol|params[symbol].permit!endparamsendend