Module: ApiControllerHelpers::Helpers

Defined in:
lib/api_controller_helpers/helpers.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



29
30
31
# File 'lib/api_controller_helpers/helpers.rb', line 29

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#render_action_outcome(record) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/api_controller_helpers/helpers.rb', line 5

def render_action_outcome(record)
  if record.valid?
    render_record(record)
  else
    render_errors(record.errors)
  end
end

#render_collection(collection) ⇒ Object



21
22
23
# File 'lib/api_controller_helpers/helpers.rb', line 21

def render_collection(collection)
  render json: wrap_result(collection)
end

#render_errors(errors) ⇒ Object



17
18
19
# File 'lib/api_controller_helpers/helpers.rb', line 17

def render_errors(errors)
  render json: wrap_result(errors: errors), status: :conflict
end

#render_record(record) ⇒ Object



13
14
15
# File 'lib/api_controller_helpers/helpers.rb', line 13

def render_record(record)
  render json: wrap_result(record)
end

#wrap_result(result) ⇒ Object



25
26
27
# File 'lib/api_controller_helpers/helpers.rb', line 25

def wrap_result(result)
  {data: result}
end