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
91 92 93 |
# File 'app/controllers/concerns/effective/crud_controller/save.rb', line 91 def duplicate_resource(resource) resource.dup end |
#reload_resource ⇒ Object
86 87 88 |
# File 'app/controllers/concerns/effective/crud_controller/save.rb', line 86 def reload_resource self.resource.reload if resource.respond_to?(:reload) end |
#resource_flash(status, resource, action, e: nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/controllers/concerns/effective/crud_controller/save.rb', line 73 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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=) success = false ActiveRecord::Base.transaction do begin run_callbacks(:resource_before_save) if resource.public_send("#{save_action}!") == false raise Effective::ActionFailed.new("failed to #{action}") end yield if block_given? run_callbacks(:resource_after_save) success = true rescue => e if Rails.env.development? Rails.logger.info " \e[31m\e[1mFAILED\e[0m\e[22m" # bold red Rails.logger.info " Unable to #{action} #{resource} - #{e.class} #{e}" e.backtrace.first(5).each { |line| Rails.logger.info(' ' + line) } end if resource.respond_to?(:restore_attributes) && resource.persisted? resource.restore_attributes(['status', 'state']) end flash.now[:danger] = resource_flash(:danger, resource, action, e: e) case e when ActiveRecord::StaleObjectError flash.now[:danger] = "#{flash.now[:danger]} <a href='#', class='alert-link' onclick='window.location.reload(true); return false;'>reload page and try again</a>" raise(ActiveRecord::Rollback) # This is a soft error, we want to display the flash message to user when Effective::ActionFailed, ActiveRecord::RecordInvalid, RuntimeError raise(ActiveRecord::Rollback) # This is a soft error, we want to display the flash message to user else raise(e) # This is a real error that should be sent to 500. Client should not see the message. end end end run_callbacks(:resource_error) unless success run_callbacks(:resource_after_commit) if success success end |