Module: ActsResponsible::Core

Defined in:
lib/acts_responsible/core.rb

Instance Method Summary collapse

Instance Method Details

#render_empty_response(options = {}) ⇒ Object



19
20
21
# File 'lib/acts_responsible/core.rb', line 19

def render_empty_response(options={})
  render_response(nil, nil, nil, options)
end

#render_error(status, errors = [], options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/acts_responsible/core.rb', line 23

def render_error(status, errors=[], options={})
  @responsibility ||= {}
  response_errors = []

  if errors.is_a? ActiveModel::Errors
    errors.keys.each do |field|
      errors.full_messages_for(field).each do |message|
        response_errors << {field: field, detail: message}
      end
    end
  else
    response_errors = Array.wrap(errors)
  end

  @responsibility[:errors] = response_errors

  options[:status] = status
  render_empty_response options
end

#render_response(data, data_key, template, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/acts_responsible/core.rb', line 4

def render_response(data, data_key, template, options={})
  prefix = self.responsible_config[:prefix]

  @responsibility ||= {}
  @responsibility.merge!({
    data_key: data_key,
    data: data,
    metadata: options.delete(:meta),
    partial: "#{prefix}/#{template}"
  })

  options.merge!(template: 'acts_responsible/response')
  render options
end