class CmAdmin::ResourceController

def cm_custom_method(params)

def cm_custom_method(params)
  records = "CmAdmin::#{@model.name}Policy::Scope".constantize.new(Current.user, @model.name.constantize).resolve
  @current_action = @action
  if  @action.parent == 'index'
    records = apply_scopes(records)
    @ar_object = filter_by(params, records, filter_params: @model.filter_params(params))
  else
    resource_identifier
  end
  respond_to do |format|
    if @action.action_type == :custom
      if @action.child_records
        if request.xhr?
          format.html { render partial: '/cm_admin/main/associated_table' }
        else
          format.html { render @action.layout }
        end
      elsif @action.display_type == :page
        data = @action.parent == "index" ? @ar_object.data : @ar_object
        # TODO: To set a default value for @action.layout, Since it is used in render above,
        # Need to check and fix it.
        format.html { render @action.partial, layout: @action.layout || 'cm_admin' }
      else
        begin
          response_object = @action.code_block.call(@response_object)
          if response_object.class == Hash
            format.json { render json: response_object }
          elsif response_object.errors.empty?
            redirect_url = @model.current_action.redirection_url || @action.redirection_url || request.referrer || "/cm_admin/#{@model.ar_model.table_name}/#{@response_object.id}"
            format.html { redirect_to redirect_url, notice: "#{@action.name.titleize} is successful" }
          else
            error_messages = response_object.errors.full_messages.map{|error_message| "<li>#{error_message}</li>"}.join
            format.html { redirect_to request.referrer, alert: "<b>#{@action.name.titleize} is unsuccessful</b><br /><ul>#{error_messages}</ul>" }
          end
        rescue => exception
          format.html { redirect_to request.referrer, alert: "<div><b>#{@action.name.titleize} is unsuccessful</b><br /><ul><li>#{exception.message}</li></ul></div>" }
        end
      end
    end
  end
end