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

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, options = {})
  if options[:decorator]
    resource = Cyrax::Presenter.present(resource, options)
  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_errorsObject



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

Parameters:

  • result

    The data you want to respond with - can be an Active Record Relation, the class of the Model itself (e.g. Product)

  • options (Hash) (defaults to: {})

    Options



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, options = {})
  options[:as] ||= accessor
  options[:assignments] =  @_assignments
  name = options[: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?
    options = {decorator: decorator_class}.merge(options)
  end
  if respond_to?(:serializable?) && serializable?
    options = {serializer: serializer_class}.merge(options)
  end
  result = Cyrax::Presenter.present(result, options)
  response = Cyrax::Response.new(name, result, options)
  response.message = @_message
  response.errors = @_errors
  response.assignments = @_assignments
  response.status = @_status
  response
end

#response_nameObject



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 set_message(message)
  if message.is_a?(Symbol)
    service_name = self.class.name.demodulize.underscore
    @_message = I18n.t("cyrax.#{service_name}.#{message}", default: "#{response_name.titleize} successfully #{message}")
  else
    @_message = 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.messages.present?
    model.errors.messages.each do |key, value|
      add_error key, value
    end
  end
end

#sync_errors_with?(model) ⇒ Boolean

Returns:

  • (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