module Terrain::Resource::Actions

Public Instance Methods

create() click to toggle source
# File lib/terrain/resource.rb, line 33
def create
  record = resource.new(permitted_params)
  authorize_record(record)

  render json: create_record, include: [], status: 201
end
destroy() click to toggle source
# File lib/terrain/resource.rb, line 54
def destroy
  record = load_record
  authorize_record(record)
  destroy_record(record)

  render nothing: true, status: 204
end
index() click to toggle source
# File lib/terrain/resource.rb, line 23
def index
  scope = order(resource_scope)

  range = request.headers['Range']
  page = Terrain::Page.new(scope, range)

  headers['Content-Range'] = page.content_range
  render json: page.records, include: (params[:include] || [])
end
show() click to toggle source
# File lib/terrain/resource.rb, line 40
def show
  record = load_record
  authorize_record(record)

  render json: record, include: params[:include] || []
end
update() click to toggle source
# File lib/terrain/resource.rb, line 47
def update
  record = load_record
  authorize_record(record)

  render json: update_record(record), include: []
end

Private Instance Methods

authorize_record(record) click to toggle source
# File lib/terrain/resource.rb, line 97
def authorize_record(record)
  authorize(record) if pundit_policy?(record)
end
create_record() click to toggle source
# File lib/terrain/resource.rb, line 130
def create_record
  resource.create!(permitted_params)
end
destroy_record(record) click to toggle source
# File lib/terrain/resource.rb, line 139
def destroy_record(record)
  record.delete
end
includes_hash() click to toggle source
# File lib/terrain/resource.rb, line 68
def includes_hash
  if params[:include].present?
    ActiveModel::Serializer::IncludeTree::Parsing.include_string_to_hash(params[:include])
  else
    {}
  end
end
load_record() click to toggle source
# File lib/terrain/resource.rb, line 126
def load_record
  preloaded_resource.find(params[:id])
end
order(scope) click to toggle source
# File lib/terrain/resource.rb, line 101
def order(scope)
  if params[:order].present?
    order = params[:order].gsub(/ /, '').split(',').map do |field|
      direction = 'asc'

      if field[0] == '-'
        direction = 'desc'
        field = field[1..-1]
      end

      "#{field} #{direction}"
    end

    scope = scope.order(order)
  end

  scope
end
permitted_params() click to toggle source
# File lib/terrain/resource.rb, line 80
def permitted_params
  params.permit(self.class.permit)
end
preloaded_resource() click to toggle source
# File lib/terrain/resource.rb, line 76
def preloaded_resource
  resource.includes(includes_hash)
end
pundit_policy?(object) click to toggle source
# File lib/terrain/resource.rb, line 92
def pundit_policy?(object)
  finder = Pundit::PolicyFinder.new(object)
  finder.policy.present?
end
pundit_user() click to toggle source
# File lib/terrain/resource.rb, line 84
def pundit_user
  if defined?(current_user)
    current_user
  else
    nil
  end
end
resource() click to toggle source
# File lib/terrain/resource.rb, line 64
def resource
  self.class.instance_variable_get('@resource')
end
resource_scope() click to toggle source
# File lib/terrain/resource.rb, line 120
def resource_scope
  scope = preloaded_resource.all
  scope = policy_scope(scope) if pundit_policy?(scope)
  scope
end
update_record(record) click to toggle source
# File lib/terrain/resource.rb, line 134
def update_record(record)
  record.update_attributes!(permitted_params)
  record
end