Class: CmAdmin::ResourceController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- CmAdmin::ResourceController
- Includes:
- Pagy::Backend, Pundit::Authorization
- Defined in:
- app/controllers/cm_admin/resource_controller.rb
Instance Method Summary collapse
- #cm_create(params) ⇒ Object
- #cm_custom_method(params) ⇒ Object
- #cm_destroy(params) ⇒ Object
- #cm_edit(params) ⇒ Object
- #cm_index(params) ⇒ Object
- #cm_new(params) ⇒ Object
- #cm_show(params) ⇒ Object
- #cm_update(params) ⇒ Object
- #custom_controller_action(action_name, params) ⇒ Object
- #filter_by(params, records, filter_params = {}, sort_params = {}) ⇒ Object
- #import ⇒ Object
- #import_form ⇒ Object
- #resource_identifier ⇒ Object
- #resource_params(params) ⇒ Object
- #resource_responder ⇒ Object
Methods inherited from ApplicationController
Instance Method Details
#cm_create(params) ⇒ Object
57 58 59 60 61 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 57 def cm_create(params) @ar_object = @model.ar_model.name.classify.constantize.new(resource_params(params)) resource_identifier resource_responder end |
#cm_custom_method(params) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 94 def cm_custom_method(params) scoped_model = "CmAdmin::#{@model.name}Policy::Scope".constantize.new(Current.user, @model.name.constantize).resolve resource_identifier 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 format.html { render @action.partial } else ar_object = @action.code_block.call(@ar_object) if ar_object.errors.empty? redirect_url = @model.current_action.redirection_url || @action.redirection_url || request.referrer || "/cm_admin/#{@model.ar_model.table_name}/#{@ar_object.id}" format.html { redirect_to redirect_url, notice: "#{@action.name.titleize} is successful" } else = ar_object.errors..map{|| "<li>#{}</li>"}.join format.html { redirect_to request.referrer, alert: "<b>#{@action.name.titleize} is unsuccessful</b><br /><ul>#{}</ul>" } end end end end end |
#cm_destroy(params) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 63 def cm_destroy(params) @ar_object = @model.ar_model.name.classify.constantize.find(params[:id]) redirect_url = request.referrer || cm_admin.send("#{@model.name.underscore}_index_path") respond_to do |format| if @ar_object.destroy format.html { redirect_back fallback_location: redirect_url, notice: "#{action_name.titleize} #{@ar_object.class.name.downcase} is successful" } else format.html { redirect_back fallback_location: redirect_url, notice: "#{action_name.titleize} #{@ar_object.class.name.downcase} is unsuccessful" } end end end |
#cm_edit(params) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 41 def cm_edit(params) @current_action = CmAdmin::Models::Action.find_by(@model, name: 'edit') @ar_object = @model.ar_model.name.classify.constantize.find(params[:id]) resource_identifier respond_to do |format| format.html { render '/cm_admin/main/' + action_name } end end |
#cm_index(params) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 8 def cm_index(params) @current_action = CmAdmin::Models::Action.find_by(@model, name: 'index') # Based on the params the filter and pagination object to be set @ar_object = filter_by(params, nil, @model.filter_params(params)) # resource_identifier respond_to do |format| if request.xhr? format.html { render partial: '/cm_admin/main/table' } else format.html { render '/cm_admin/main/' + action_name } end end end |
#cm_new(params) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 32 def cm_new(params) @current_action = CmAdmin::Models::Action.find_by(@model, name: 'new') @ar_object = @model.ar_model.new resource_identifier respond_to do |format| format.html { render '/cm_admin/main/' + action_name } end end |
#cm_show(params) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 22 def cm_show(params) @current_action = CmAdmin::Models::Action.find_by(@model, name: 'show') scoped_model = "CmAdmin::#{@model.name}Policy::Scope".constantize.new(Current.user, @model.name.constantize).resolve @ar_object = scoped_model.find(params[:id]) resource_identifier respond_to do |format| format.html { render '/cm_admin/main/' + action_name } end end |
#cm_update(params) ⇒ Object
50 51 52 53 54 55 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 50 def cm_update(params) @ar_object = @model.ar_model.name.classify.constantize.find(params[:id]) @ar_object.assign_attributes(resource_params(params)) resource_identifier resource_responder end |
#custom_controller_action(action_name, params) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 155 def custom_controller_action(action_name, params) @current_action = CmAdmin::Models::Action.find_by(@model, name: action_name.to_s) return unless @current_action @ar_object = @model.ar_model.name.classify.constantize.find(params[:id]) return @ar_object unless @current_action.child_records child_records = @ar_object.send(@current_action.child_records) @reflection = @model.ar_model.reflect_on_association(@current_action.child_records) @associated_model = if @reflection.klass.column_names.include?('type') CmAdmin::Model.find_by(name: @reflection.plural_name.classify) else CmAdmin::Model.find_by(name: @reflection.klass.name) end @associated_ar_object = if child_records.is_a? ActiveRecord::Relation filter_by(params, child_records, @associated_model.filter_params(params)) else child_records end return @ar_object, @associated_model, @associated_ar_object end |
#filter_by(params, records, filter_params = {}, sort_params = {}) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 177 def filter_by(params, records, filter_params={}, sort_params={}) filtered_result = OpenStruct.new sort_column = "created_at" sort_direction = %w[asc desc].include?(sort_params[:sort_direction]) ? sort_params[:sort_direction] : "asc" sort_params = {sort_column: sort_column, sort_direction: sort_direction} records = "CmAdmin::#{@model.name}Policy::Scope".constantize.new(Current.user, @model.name.constantize).resolve if records.nil? records = records.order("#{@current_action.sort_column} #{@current_action.sort_direction}") final_data = CmAdmin::Models::Filter.filtered_data(filter_params, records, @associated_model ? @associated_model.filters : @model.filters) pagy, records = pagy(final_data) filtered_result.data = records filtered_result.pagy = pagy # filtered_result.facets = paginate(page, raw_data.size) # filtered_result.sort = sort_params # filtered_result.facets.sort = sort_params return filtered_result end |
#import ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 75 def import @model = Model.find_by({name: controller_name.titleize}) allowed_params = params.permit(file_import: [:associated_model_name, :import_file]).to_h file_import = ::FileImport.new(allowed_params[:file_import]) file_import.added_by = Current.user respond_to do |format| if file_import.save! format.html { redirect_back fallback_location: cm_admin.send("#{@model.name.underscore}_index_path"), notice: "Your import is successfully queued." } end end end |
#import_form ⇒ Object
87 88 89 90 91 92 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 87 def import_form @model = Model.find_by({name: controller_name.titleize}) respond_to do |format| format.html { render '/cm_admin/main/import_form' } end end |
#resource_identifier ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 122 def resource_identifier @ar_object, @associated_model, @associated_ar_object = custom_controller_action(action_name, params.permit!) if !@ar_object.present? && params[:id].present? controller_name.classify.constantize, policy_class: "CmAdmin::#{controller_name.classify}Policy".constantize if defined? "CmAdmin::#{controller_name.classify}Policy".constantize aar_model = request.url.split('/')[-2].classify.constantize if params[:aar_id] @associated_ar_object = aar_model.find(params[:aar_id]) if params[:aar_id] nested_tables = @model.available_fields[:new].except(:fields).keys nested_tables += @model.available_fields[:edit].except(:fields).keys @reflections = @model.ar_model.reflect_on_all_associations nested_tables.each do |table_name| reflection = @reflections.select {|x| x if x.name == table_name}.first if reflection.macro == :has_many @ar_object.send(table_name).build if action_name == "new" || action_name == "edit" elsif action_name == "new" @ar_object.send(('build_' + table_name.to_s).to_sym) end end end |
#resource_params(params) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 195 def resource_params(params) permittable_fields = @permitted_fields || @model.ar_model.columns.map(&:name).reject { |i| CmAdmin::REJECTABLE_FIELDS.include?(i) }.map(&:to_sym) permittable_fields += @model.ar_model.name.constantize.reflect_on_all_associations.map {|x| next if x.[:polymorphic] if x.class.name.include?('HasOne') x.name.to_s.gsub('_attachment', '').gsub('rich_text_', '').to_sym elsif x.class.name.include?('HasMany') Hash[x.name.to_s.gsub('_attachment', ''), []] end }.compact nested_tables = @model.available_fields[:new].except(:fields).keys nested_tables += @model.available_fields[:edit].except(:fields).keys nested_fields = nested_tables.uniq.map {|table| Hash[ table.to_s + '_attributes', table.to_s.classify.constantize.columns.map(&:name).reject { |i| CmAdmin::REJECTABLE_FIELDS.include?(i) }.map(&:to_sym) + [:id, :_destroy] ] } permittable_fields += nested_fields @model.ar_model.columns.map { |col| permittable_fields << col.name.split('_cents') if col.name.include?('_cents') } params.require(@model.name.underscore.to_sym).permit(*permittable_fields) end |
#resource_responder ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/controllers/cm_admin/resource_controller.rb', line 140 def resource_responder respond_to do |format| if params["referrer"] redirect_url = params["referrer"] else redirect_url = CmAdmin::Engine.mount_path + "/#{@model.name.underscore.pluralize}/#{@ar_object.id}" end if @ar_object.save format.html { redirect_to redirect_url, notice: "#{action_name.titleize} #{@ar_object.class.name.downcase} is successful" } else format.html { render '/cm_admin/main/new', notice: "#{action_name.titleize} #{@ar_object.class.name.downcase} is unsuccessful" } end end end |