Module: Effective::CrudController::Actions
- Included in:
- Effective::CrudController
- Defined in:
- app/controllers/concerns/effective/crud_controller/actions.rb
Instance Method Summary collapse
- #collection_action(action) ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #member_action(action) ⇒ Object
- #new ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#collection_action(action) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 239 def collection_action(action) Rails.logger.info 'Processed by Effective::CrudController#collection_action' action = action.to_s.gsub('bulk_', '').to_sym if params[:ids].present? self.resources ||= resource_scope.where(id: params[:ids]) end if request.get? && effective_resource.scope?(action) self.resources ||= resource_scope.public_send(action) end self.resources ||= resource_scope.all EffectiveResources.(self, action, resource_klass) @page_title ||= "#{action.to_s.titleize} #{resource_human_name}".html_safe if request.get? @datatable = resource_datatable() run_callbacks(:resource_render) view = lookup_context.template_exists?(action, _prefixes) ? action : :index render(view, locals: { action: action }) return end raise "expected all #{resource_name} objects to respond to #{action}!" if resources.to_a.present? && !resources.all? { |resource| resource.respond_to?("#{action}!") } successes = 0 # No attributes are assigned or saved. We purely call action! on the resource ActiveRecord::Base.transaction do successes = resources.select do |resource| begin resource.public_send("#{action}!") if EffectiveResources.(self, action, resource) rescue Exception => e false end end.length end render json: { status: 200, message: "Successfully #{action_verb(action)} #{successes} / #{resources.length} selected #{resource_human_plural_name.downcase}" } end |
#create ⇒ Object
73 74 75 76 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 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 73 def create Rails.logger.info 'Processed by Effective::CrudController#create' self.resource ||= resource_scope.new action = (commit_action[:action] == :save ? :create : commit_action[:action]) if respond_to?(:current_user) && resource.respond_to?(:current_user=) resource.current_user ||= current_user end if respond_to?(:current_user) && resource.respond_to?(:created_by=) resource.created_by ||= (impersonation_user || current_user) end if respond_to?(:current_user) && resource.respond_to?(:updated_by=) resource.updated_by ||= (impersonation_user || current_user) end if resource.respond_to?(:current_path=) resource.current_path ||= resource_path end resource.assign_attributes(send(resource_params_method_name)) EffectiveResources.(self, action, resource) @page_title ||= "New #{resource_human_name}".html_safe if save_resource(resource, action) respond_with_success(resource, action) else respond_with_error(resource, action) end end |
#destroy ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 169 def destroy Rails.logger.info 'Processed by Effective::CrudController#destroy' if params[:ids].present? return collection_action(:destroy) end self.resource = resource_scope.find(params[:id]) action = :destroy EffectiveResources.(self, action, resource) @page_title ||= "Destroy #{resource}".html_safe if save_resource(resource, action) respond_with_success(resource, action) else respond_with_error(resource, action) end end |
#edit ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 123 def edit Rails.logger.info 'Processed by Effective::CrudController#edit' self.resource ||= resource_scope.find(params[:id]) EffectiveResources.(self, :edit, resource) @page_title ||= "Edit #{resource}".html_safe run_callbacks(:resource_render) respond_to do |format| format.html { } format.js { render('edit', formats: :js) } end end |
#index ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 5 def index Rails.logger.info 'Processed by Effective::CrudController#index' EffectiveResources.(self, :index, resource_klass) @page_title ||= resource_human_plural_name.to_s.html_safe self.resources ||= resource_scope.all if resource_scope.respond_to?(:all) @datatable = resource_datatable() run_callbacks(:resource_render) end |
#member_action(action) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 189 def member_action(action) Rails.logger.info "Processed by Effective::CrudController#member_action" self.resource ||= resource_scope.find(params[:id]) EffectiveResources.(self, action, resource) @page_title ||= "#{action.to_s.titleize} #{resource}".html_safe if request.get? run_callbacks(:resource_render) respond_to do |format| format.html { } format.js do html_template = action if template_present?(action, format: :html) template = template_present?(action) ? action : 'member_action' render(template, formats: :js, locals: { action: action, html_template: html_template }) end end return end to_assign = (send(resource_params_method_name) rescue {}) resource.assign_attributes(to_assign) if to_assign.present? && to_assign.permitted? if respond_to?(:current_user) && resource.respond_to?(:current_user=) resource.current_user ||= current_user end if respond_to?(:current_user) && resource.respond_to?(:updated_by=) resource.updated_by ||= (impersonation_user || current_user) end if respond_to?(:current_user) && resource.try(:new_record?) && resource.respond_to?(:created_by=) resource.created_by ||= (impersonation_user || current_user) end if resource.respond_to?(:current_path=) resource.current_path ||= resource_path end if save_resource(resource, action) respond_with_success(resource, action) else respond_with_error(resource, action) end end |
#new ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 17 def new Rails.logger.info 'Processed by Effective::CrudController#new' self.resource ||= resource_scope.new # Assign attributes from the query string to_assign = if view_context.respond_to?(:inline_datatable?) && view_context.inline_datatable? EffectiveDatatables.find(params[:_datatable_id], params[:_datatable_attributes]).attributes end to_assign ||= if params[resource_name].present? && params[resource_name].respond_to?(:to_unsafe_h) params[resource_name].to_unsafe_h end to_assign ||= if params[effective_resource_name].present? && params[effective_resource_name].respond_to?(:to_unsafe_h) params[effective_resource_name].to_unsafe_h end to_assign ||= if params.present? params.to_unsafe_h.except(:controller, :action, :id, :duplicate_id) end if to_assign.present? resource.assign_attributes(to_assign.select { |k, v| resource.respond_to?("#{k}=") }) end # Duplicate if possible if params[:duplicate_id].present? duplicate = (resource_scope.find(params[:duplicate_id]) rescue false) duplicate ||= resource_scope.find_by_id(params[:duplicate_id]) EffectiveResources.(self, :show, duplicate) self.resource = duplicate_resource(duplicate) unless resource.kind_of?(resource_scope.klass) && resource.new_record? raise "expected duplicate_resource to return an unsaved new #{resource_scope.klass} resource" end if ( = flash[:success].to_s).present? flash.delete(:success) flash.now[:success] = "#{.chomp('.')}. Adding another #{resource_human_name} based on previous." end end EffectiveResources.(self, :new, resource) @page_title ||= "New #{resource_human_name}".html_safe run_callbacks(:resource_render) respond_to do |format| format.html { } format.js { render('new', formats: :js) } end end |
#show ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 107 def show Rails.logger.info 'Processed by Effective::CrudController#show' self.resource ||= resource_scope.find(params[:id]) EffectiveResources.(self, :show, resource) @page_title ||= resource.to_s.html_safe run_callbacks(:resource_render) respond_to do |format| format.html { } format.js { render('show', formats: :js) } end end |
#update ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 139 def update Rails.logger.info 'Processed by Effective::CrudController#update' self.resource ||= resource_scope.find(params[:id]) action = (commit_action[:action] == :save ? :update : commit_action[:action]) EffectiveResources.(self, action, resource) @page_title ||= "Edit #{resource}".html_safe if respond_to?(:current_user) && resource.respond_to?(:current_user=) resource.current_user ||= current_user end if respond_to?(:current_user) && resource.respond_to?(:updated_by=) resource.updated_by ||= (impersonation_user || current_user) end if resource.respond_to?(:current_path=) resource.current_path ||= resource_path end resource.assign_attributes(send(resource_params_method_name)) if save_resource(resource, action) respond_with_success(resource, action) else respond_with_error(resource, action) end end |