Module: Effective::CrudController
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/controllers/concerns/effective/crud_controller.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#collection_post_action(action) ⇒ Object
No attributes are assigned or saved.
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
-
#member_post_action(action) ⇒ Object
No attributes are assigned or saved.
- #new ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#collection_post_action(action) ⇒ Object
No attributes are assigned or saved. We purely call action! on the resource
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 293 def collection_post_action(action) action = action.to_s.gsub('bulk_', '').to_sym raise 'expected post, patch or put http action' unless (request.post? || request.patch? || request.put?) raise "expected #{resource_name} to respond to #{action}!" if resources.to_a.present? && !resources.first.respond_to?("#{action}!") successes = 0 resource_klass.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
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 191 def create self.resource ||= resource_scope.new @page_title ||= "New #{resource_name.titleize}" EffectiveResources.(self, :create, resource) action = commit_action[:action] EffectiveResources.(self, action, resource) unless action == :save resource.assign_attributes(send(resource_params_method_name)) resource.created_by ||= current_user if resource.respond_to?(:created_by=) if save_resource(resource, action) flash[:success] ||= flash_success(resource, action) redirect_to(resource_redirect_path) else flash.now[:danger] ||= flash_danger(resource, action) render :new end end |
#destroy ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 250 def destroy self.resource = resource_scope.find(params[:id]) @page_title ||= "Destroy #{resource}" EffectiveResources.(self, :destroy, resource) if resource.destroy flash[:success] ||= flash_success(resource, :delete) else flash[:danger] ||= flash_danger(resource, :delete) end redirect_to(resource_redirect_path) end |
#edit ⇒ Object
221 222 223 224 225 226 227 228 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 221 def edit self.resource ||= resource_scope.find(params[:id]) @page_title ||= "Edit #{resource}" EffectiveResources.(self, :edit, resource) run_callbacks(:resource_render) end |
#index ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 152 def index @page_title ||= resource_plural_name.titleize EffectiveDatatables.(self, :index, resource_klass) self.resources ||= resource_scope.all if resource_datatable_class @datatable ||= resource_datatable_class.new(self, resource_datatable_attributes) end run_callbacks(:resource_render) end |
#member_post_action(action) ⇒ Object
No attributes are assigned or saved. We purely call action! on the resource.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 266 def member_post_action(action) raise 'expected post, patch or put http action' unless (request.post? || request.patch? || request.put?) if save_resource(resource, action) flash[:success] ||= flash_success(resource, action) redirect_to(referer_redirect_path || resource_redirect_path) else flash.now[:danger] ||= flash_danger(resource, action) if resource_edit_path && (referer_redirect_path || '').end_with?(resource_edit_path) @page_title ||= "Edit #{resource}" render :edit elsif resource_new_path && (referer_redirect_path || '').end_with?(resource_new_path) @page_title ||= "New #{resource_name.titleize}" render :new elsif resource_show_path && (referer_redirect_path || '').end_with?(resource_show_path) @page_title ||= resource_name.titleize render :show else @page_title ||= resource.to_s flash[:danger] = flash.now[:danger] redirect_to(referer_redirect_path || resource_redirect_path) end end end |
#new ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 165 def new self.resource ||= resource_scope.new self.resource.assign_attributes( params.to_unsafe_h.except(:controller, :action, :id).select { |k, v| resource.respond_to?("#{k}=") } ) if params[:duplicate_id] duplicate = resource_scope.find(params[:duplicate_id]) EffectiveResources.(self, :show, duplicate) self.resource = duplicate_resource(duplicate) raise "expected duplicate_resource to return an unsaved new #{resource_klass} resource" unless resource.kind_of?(resource_klass) && resource.new_record? if ( = flash[:success].to_s).present? flash.delete(:success) flash.now[:success] = "#{message.chomp('.')}. Adding another #{resource_name.titleize} based on previous." end end @page_title ||= "New #{resource_name.titleize}" EffectiveResources.(self, :new, resource) run_callbacks(:resource_render) end |
#show ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 212 def show self.resource ||= resource_scope.find(params[:id]) @page_title ||= resource.to_s EffectiveResources.(self, :show, resource) run_callbacks(:resource_render) end |
#update ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 230 def update self.resource ||= resource_scope.find(params[:id]) @page_title = "Edit #{resource}" EffectiveResources.(self, :update, resource) action = commit_action[:action] EffectiveResources.(self, action, resource) unless action == :save resource.assign_attributes(send(resource_params_method_name)) if save_resource(resource, action) flash[:success] ||= flash_success(resource, action) redirect_to(resource_redirect_path) else flash.now[:danger] ||= flash_danger(resource, action) render :edit end end |