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
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 355 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 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
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 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 193 def create self.resource ||= resource_scope.new @page_title ||= "New #{resource_name.titleize}" action = commit_action[:action] EffectiveResources.(self, action, resource) unless action == :save EffectiveResources.(self, :create, resource) if action == :save resource.assign_attributes(send(resource_params_method_name)) resource.created_by ||= current_user if resource.respond_to?(:created_by=) respond_to do |format| if save_resource(resource, action) format.html do flash[:success] ||= resource_flash(:success, resource, action) redirect_to(resource_redirect_path) end format.js do if specific_redirect_path? flash[:success] ||= resource_flash(:success, resource, action) redirect_to(resource_redirect_path) else flash.now[:success] ||= resource_flash(:success, resource, action) reload_resource # create.js.erb end end else flash.delete(:success) flash.now[:danger] ||= resource_flash(:danger, resource, action) format.html { render :new } format.js {} # create.js.erb end end end |
#destroy ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 288 def destroy self.resource = resource_scope.find(params[:id]) action = :destroy @page_title ||= "Destroy #{resource}" EffectiveResources.(self, action, resource) respond_to do |format| if save_resource(resource, action) format.html do flash[:success] ||= resource_flash(:success, resource, action) redirect_to(resource_redirect_path) end format.js do if specific_redirect_path? flash[:success] ||= resource_flash(:success, resource, action) redirect_to(resource_redirect_path) else flash.now[:success] ||= resource_flash(:success, resource, action) # delete.js.erb end end else flash.delete(:success) format.html do flash[:danger] ||= resource_flash(:danger, resource, action) redirect_to(resource_redirect_path) end format.js do flash.now[:danger] ||= resource_flash(:danger, resource, action) # destroy.js.erb end end end end |
#edit ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 241 def edit self.resource ||= resource_scope.find(params[:id]) @page_title ||= "Edit #{resource}" EffectiveResources.(self, :edit, resource) run_callbacks(:resource_render) end |
#index ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 154 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.
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 328 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] ||= resource_flash(:success, resource, action) redirect_to(referer_redirect_path || resource_redirect_path) else flash.now[:danger] ||= resource_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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 167 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] = "#{.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
232 233 234 235 236 237 238 239 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 232 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
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 284 285 286 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 250 def update self.resource ||= resource_scope.find(params[:id]) @page_title = "Edit #{resource}" action = commit_action[:action] EffectiveResources.(self, action, resource) unless action == :save EffectiveResources.(self, :update, resource) if action == :save resource.assign_attributes(send(resource_params_method_name)) respond_to do |format| if save_resource(resource, action) format.html do flash[:success] ||= resource_flash(:success, resource, action) redirect_to(resource_redirect_path) end format.js do if specific_redirect_path? flash[:success] ||= resource_flash(:success, resource, action) redirect_to(resource_redirect_path) else flash.now[:success] ||= resource_flash(:success, resource, action) reload_resource # update.js.erb end end else flash.delete(:success) flash.now[:danger] ||= resource_flash(:danger, resource, action) format.html { render :edit } format.js { } # update.js.erb end end end |