class Softwear::Library::ApiController

Public Class Methods

base_class() click to toggle source
# File lib/softwear/library/api_controller.rb, line 22
def self.base_class
  Softwear::Library::ApiController
end
token_authenticate(user_class, options = {}) click to toggle source
# File lib/softwear/library/api_controller.rb, line 26
def self.token_authenticate(user_class, options = {})
  include Softwear::Auth::TokenAuthentication
  self.user_class = user_class
  self.token_auth_options = options
  prepend_before_filter :token_authenticate_user!
end

Protected Class Methods

model_name() click to toggle source
# File lib/softwear/library/api_controller.rb, line 128
def self.model_name
  name.gsub('Api::', '').gsub('Controller', '').singularize
end

Public Instance Methods

create() click to toggle source
# File lib/softwear/library/api_controller.rb, line 75
def create
  create! do |success, failure|
    success.json do
      headers['Location'] = resource_url(record.id)
      render_json(status: 201).call
    end
    failure.json(&render_errors)
  end
end
index() { || ... } click to toggle source
# File lib/softwear/library/api_controller.rb, line 33
def index(&block)
  yield if block_given?

  key_values = (permitted_attributes + [:id]).uniq.map do |a|
    [a, params[a]] if params[a]
  end
    .compact

  self.records ||= resource_class
  self.records = records.where(Hash[key_values])

  respond_to do |format|
    format.json(&render_json(records))
  end
end
options() click to toggle source
# File lib/softwear/library/api_controller.rb, line 85
def options
  head(:ok) if request.request_method == 'OPTIONS'
end
show() click to toggle source
Calls superclass method
# File lib/softwear/library/api_controller.rb, line 49
def show
  super do |format|
    format.json(&render_json)
  end
end
update() click to toggle source
# File lib/softwear/library/api_controller.rb, line 55
def update
  self.record = resource_class.find(params[:id])

  permitted_attributes.each do |a|
    begin
      record.send("#{a}=", params[a]) if params[a]
    rescue ActiveRecord::AssociationTypeMismatch
      # If you try to send "job" as an attribute to order, we're assuming it's
      # not intentional. Send "job_attributes" or update the job model separately
      # to actually update the job.
    end
  end

  if record.save
    respond_to { |format| format.json(&render_json) }
  else
    respond_to { |format| format.json(&render_errors) }
  end
end

Protected Instance Methods

allow_origin() click to toggle source
# File lib/softwear/library/api_controller.rb, line 153
def allow_origin
  headers['Access-Control-Allow-Origin'] = '*'
  headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, PATCH, DELETE'
  headers['Access-Control-Allow-Headers'] = 'X-Requested-With'
  headers['Access-Control-Allow-Credentials'] = 'true'
end
base_class() click to toggle source
# File lib/softwear/library/api_controller.rb, line 91
def base_class
  self.class.base_class
end
includes() click to toggle source

Override this to specify the :include option of rendering json.

# File lib/softwear/library/api_controller.rb, line 133
def includes
  {}
end
permitted_attributes() click to toggle source
# File lib/softwear/library/api_controller.rb, line 160
def permitted_attributes
  resource_class.column_names + ['state_event']
end
permitted_params() click to toggle source
# File lib/softwear/library/api_controller.rb, line 164
def permitted_params
  { resource_class.model_name.element => permitted_attributes }
end
record() click to toggle source
# File lib/softwear/library/api_controller.rb, line 145
def record
  instance_variable_get("@#{resource_class.model_name.element}")
end
record=(r) click to toggle source
# File lib/softwear/library/api_controller.rb, line 149
def record=(r)
  instance_variable_set("@#{resource_class.model_name.element}", r)
end
records() click to toggle source
# File lib/softwear/library/api_controller.rb, line 137
def records
  instance_variable_get("@#{resource_class.model_name.collection}")
end
records=(r) click to toggle source
# File lib/softwear/library/api_controller.rb, line 141
def records=(r)
  instance_variable_set("@#{resource_class.model_name.collection}", r)
end
render_errors(options = {}) click to toggle source
# File lib/softwear/library/api_controller.rb, line 114
def render_errors(options = {})
  proc do
    Rails.logger.error "API #{record.class.name} ERROR: #{record.errors.full_messages}"

    rendering = {
      json: { errors: record.errors },
      status: :unprocessable_entity
    }
      .merge(options)

    render rendering
  end
end
render_json(options = {}) click to toggle source
# File lib/softwear/library/api_controller.rb, line 95
def render_json(options = {})
  proc do
    if options.is_a?(Hash)
      records = nil
    else
      records = options
      options = {}
    end
    rendering = {
      json: (records || record),
      methods: (['id'] + permitted_attributes).uniq,
      include: includes
    }
      .merge(options)

    render rendering
  end
end