Module: Cyrax::Extensions::HasResponse
- Extended by:
- ActiveSupport::Concern
- Included in:
- Base
- Defined in:
- lib/cyrax/extensions/has_response.rb
Constant Summary collapse
- STATUS_VALUES =
{ created: 201, no_content: 204, bad_request: 400, not_allowed: 403 }
Instance Method Summary collapse
- #add_error(key, value = nil) ⇒ Object
- #add_error_unless(key, value, condition) ⇒ Object
- #assign_resource(resource_name, resource, options = {}) ⇒ Object
- #assignment(resource_name) ⇒ Object
- #reset_errors ⇒ Object
-
#respond_with(result, options = {}) ⇒ Object
Generates the response for to pass to the Rails controller.
- #response_name ⇒ Object
- #set_message(message) ⇒ Object
- #set_status(status) ⇒ Object
- #sync_errors_with(model) ⇒ Object
- #sync_errors_with?(model) ⇒ Boolean
Instance Method Details
#add_error(key, value = nil) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/cyrax/extensions/has_response.rb', line 24 def add_error(key, value = nil) if value.blank? raise "Use key-value syntax for adding errors" end @_errors ||= {} @_errors[key.to_sym] = value end |
#add_error_unless(key, value, condition) ⇒ Object
32 33 34 |
# File 'lib/cyrax/extensions/has_response.rb', line 32 def add_error_unless(key, value, condition) add_error(key, value) unless condition end |
#assign_resource(resource_name, resource, options = {}) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/cyrax/extensions/has_response.rb', line 11 def assign_resource(resource_name, resource, = {}) if [:decorator] resource = Cyrax::Presenter.present(resource, ) end @_assignments ||= {} @_assignments[resource_name.to_sym] = resource end |
#assignment(resource_name) ⇒ Object
19 20 21 22 |
# File 'lib/cyrax/extensions/has_response.rb', line 19 def assignment(resource_name) @_assignments ||= {} @_assignments[resource_name] end |
#reset_errors ⇒ Object
56 57 58 |
# File 'lib/cyrax/extensions/has_response.rb', line 56 def reset_errors @_errors = {} end |
#respond_with(result, options = {}) ⇒ Object
Generates the response for to pass to the Rails controller
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cyrax/extensions/has_response.rb', line 83 def respond_with(result, = {}) [:as] ||= accessor [:assignments] = @_assignments name = [:name] || response_name result = result.result.to_model if result.is_a?(Cyrax::Response) if sync_errors_with?(result) sync_errors_with(result) end if respond_to?(:decorable?) && decorable? = {decorator: decorator_class}.merge() end if respond_to?(:serializable?) && serializable? = {serializer: serializer_class}.merge() end result = Cyrax::Presenter.present(result, ) response = Cyrax::Response.new(name, result, ) response. = @_message response.errors = @_errors response.assignments = @_assignments response.status = @_status response end |
#response_name ⇒ Object
76 77 78 |
# File 'lib/cyrax/extensions/has_response.rb', line 76 def response_name self.class.name.demodulize.underscore end |
#set_message(message) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/cyrax/extensions/has_response.rb', line 60 def () if .is_a?(Symbol) service_name = self.class.name.demodulize.underscore @_message = I18n.t("cyrax.#{service_name}.#{}", default: "#{response_name.titleize} successfully #{}") else @_message = end end |
#set_status(status) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/cyrax/extensions/has_response.rb', line 69 def set_status(status) if status.is_a?(Symbol) status = STATUS_VALUES[status] end @_status = status end |
#sync_errors_with(model) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cyrax/extensions/has_response.rb', line 36 def sync_errors_with(model) model = model.to_model if model.respond_to?(:to_model) return unless model (@_errors || {}).each do |key, value| next unless model.respond_to?(key) model.errors.add key, value unless model.errors.include?(key) end if model.errors..present? model.errors..each do |key, value| add_error key, value end end end |
#sync_errors_with?(model) ⇒ Boolean
50 51 52 53 54 |
# File 'lib/cyrax/extensions/has_response.rb', line 50 def sync_errors_with?(model) model = model.to_model if model.respond_to?(:to_model) model && model.respond_to?(:errors) && model.errors.respond_to?(:messages) end |