module Relaxation::Relaxable

Constants

DEFAULT_SCOPE
PERMITTED

Attributes

create_whitelist[RW]
filter_whitelist[RW]
scope_method[RW]
update_whitelist[RW]

Private Instance Methods

_create() click to toggle source
# File lib/relaxation/relaxable.rb, line 68
def _create
  render status: :created, json: relation.create!(create_params)
end
_destroy() click to toggle source
# File lib/relaxation/relaxable.rb, line 76
def _destroy
  relation.find(params[:id]).destroy
  head :no_content
end
_index() click to toggle source
# File lib/relaxation/relaxable.rb, line 60
def _index
  render_list(relation.where(filters))
end
_show() click to toggle source
# File lib/relaxation/relaxable.rb, line 64
def _show
  render status: :ok, json: relation.find(params[:id])
end
_update() click to toggle source
# File lib/relaxation/relaxable.rb, line 72
def _update
  render status: :ok, json: relation.find(params[:id]).tap { |r| r.update!(update_params) }
end
create_params() click to toggle source
# File lib/relaxation/relaxable.rb, line 109
def create_params
  required_params.permit(*self.class.create_whitelist)
end
filters() click to toggle source
# File lib/relaxation/relaxable.rb, line 105
def filters
  params.permit(*self.class.filter_whitelist)
end
model_name() click to toggle source
# File lib/relaxation/relaxable.rb, line 97
def model_name
  controller_name.classify
end
relation() click to toggle source
# File lib/relaxation/relaxable.rb, line 85
def relation
  scope ? scope.send(relation_name) : model_name.constantize
end
relation_name() click to toggle source
# File lib/relaxation/relaxable.rb, line 89
def relation_name
  scope && scope.class.reflect_on_all_associations(:has_many).find do |association|
    class_name = association.options && association.options[:class_name]
    class_name ||= association.name.to_s.classify
    class_name == model_name
  end.name
end
render_list(list) click to toggle source
# File lib/relaxation/relaxable.rb, line 101
def render_list(list)
  render status: :ok, json: list
end
required_params() click to toggle source
# File lib/relaxation/relaxable.rb, line 117
def required_params
  params.require("#{controller_name.singularize}")
end
scope() click to toggle source
# File lib/relaxation/relaxable.rb, line 81
def scope
  send(self.class.scope_method)
end
update_params() click to toggle source
# File lib/relaxation/relaxable.rb, line 113
def update_params
  required_params.permit(*self.class.update_whitelist)
end