Module: Effective::CrudController::Save
- Included in:
- Effective::CrudController
- Defined in:
- app/controllers/concerns/effective/crud_controller/save.rb
Instance Method Summary collapse
-
#commit_action(action = nil) ⇒ Object
Based on the incoming params or passed action.
-
#duplicate_resource(resource) ⇒ Object
Should return a new resource based on the passed one.
- #reload_resource ⇒ Object
- #resource_flash(status, resource, action, e: nil) ⇒ Object
-
#save_resource(resource, action = :save, &block) ⇒ Object
This calls the appropriate member action, probably save!, on the resource.
Instance Method Details
#commit_action(action = nil) ⇒ Object
Based on the incoming params or passed action. Merges all options.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'app/controllers/concerns/effective/crud_controller/save.rb', line 6 def commit_action(action = nil) config = (['create', 'update'].include?(params[:action]) ? self.class.submits : self.class.) ons = self.class.ons commit = config[params[:commit].to_s] commit ||= config.find { |_, v| v[:action] == action }.try(:last) commit ||= config.find { |_, v| v[:action] == :save }.try(:last) if [nil, :create, :update].include?(action) commit ||= { action: (action || :save) } on = ons[params[:commit].to_s] || ons[action] || ons[commit[:action]] on.present? ? commit.reverse_merge(on) : commit end |
#duplicate_resource(resource) ⇒ Object
Should return a new resource based on the passed one
75 76 77 |
# File 'app/controllers/concerns/effective/crud_controller/save.rb', line 75 def duplicate_resource(resource) resource.dup end |
#reload_resource ⇒ Object
70 71 72 |
# File 'app/controllers/concerns/effective/crud_controller/save.rb', line 70 def reload_resource self.resource.reload if resource.respond_to?(:reload) end |
#resource_flash(status, resource, action, e: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/controllers/concerns/effective/crud_controller/save.rb', line 57 def resource_flash(status, resource, action, e: nil) submit = commit_action(action) = submit[status].respond_to?(:call) ? instance_exec(&submit[status]) : submit[status] return .gsub('@resource', resource.to_s) if .present? case status when :success then flash_success(resource, action) when :danger then flash_danger(resource, action, e: e) else raise "unknown resource flash status: #{status}" end end |
#save_resource(resource, action = :save, &block) ⇒ Object
This calls the appropriate member action, probably save!, on the resource.
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 |
# File 'app/controllers/concerns/effective/crud_controller/save.rb', line 21 def save_resource(resource, action = :save, &block) save_action = ([:create, :update].include?(action) ? :save : action) raise "expected @#{resource_name} to respond to #{save_action}!" unless resource.respond_to?("#{save_action}!") resource.current_user ||= current_user if resource.respond_to?(:current_user=) ActiveRecord::Base.transaction do begin run_callbacks(:resource_before_save) if resource.public_send("#{save_action}!") == false raise("failed to #{action} #{resource}") end yield if block_given? run_callbacks(:resource_after_save) return true rescue => e Rails.logger.info "Failed to #{action}: #{e.}" if Rails.env.development? if resource.respond_to?(:restore_attributes) && resource.persisted? resource.restore_attributes(['status', 'state']) end flash.now[:danger] = resource_flash(:danger, resource, action, e: e) raise ActiveRecord::Rollback end end run_callbacks(:resource_error) false end |