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
185 186 187 188 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 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 185 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_plural_name.titleize}" if request.get? @datatable = resource_datatable(action) 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 => e false end end.length end render json: { status: 200, message: "Successfully #{action_verb(action)} #{successes} / #{resources.length} selected #{resource_plural_name}" } end |
#create ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 60 def create Rails.logger.info 'Processed by Effective::CrudController#create' self.resource ||= resource_scope.new action = (commit_action[:action] == :save ? :create : commit_action[:action]) resource.current_user ||= current_user if resource.respond_to?(:current_user=) resource.created_by ||= current_user if resource.respond_to?(:created_by=) resource.assign_attributes(send(resource_params_method_name)) EffectiveResources.(self, action, resource) @page_title ||= "New #{resource_name.titleize}" if save_resource(resource, action) respond_with_success(resource, action) else respond_with_error(resource, action) end end |
#destroy ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 136 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}" if save_resource(resource, action) respond_with_success(resource, action) else respond_with_error(resource, action) end end |
#edit ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 98 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}" run_callbacks(:resource_render) respond_to do |format| format.html { } format.js { render('edit.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_plural_name.titleize self.resources ||= resource_scope.all if resource_scope.respond_to?(:all) @datatable = resource_datatable(:index) run_callbacks(:resource_render) end |
#member_action(action) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 156 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}" if request.get? run_callbacks(:resource_render) respond_to do |format| format.html { } format.js { render(template_present?(action) ? action : 'member_action.js', locals: { action: action }) } end return end to_assign = (send(resource_params_method_name) rescue {}) resource.assign_attributes(to_assign) if to_assign.present? && to_assign.permitted? 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 |
# 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 to_assign = if view_context.respond_to?(:inline_datatable?) && view_context.inline_datatable? EffectiveDatatables.find(params[:_datatable_id], params[:_datatable_attributes]).attributes elsif 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] duplicate = resource_scope.find(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_name.titleize} based on previous." end end EffectiveResources.(self, :new, resource) @page_title ||= "New #{resource_name.titleize}" run_callbacks(:resource_render) respond_to do |format| format.html { } format.js { render('new.js') } end end |
#show ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 81 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 run_callbacks(:resource_render) respond_to do |format| format.html { } format.js { render('show.js') } end end |
#update ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 115 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}" resource.current_user ||= current_user if resource.respond_to?(:current_user=) resource.updated_by ||= current_user if resource.respond_to?(:updated_by=) 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 |