Class: Softwear::Library::ApiController

Inherits:
ActionController::Base
  • Object
show all
Extended by:
InheritedResources::ClassMethods, InheritedResources::UrlHelpers
Includes:
InheritedResources::Actions, InheritedResources::BaseHelpers
Defined in:
lib/softwear/library/api_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_classObject



22
23
24
# File 'lib/softwear/library/api_controller.rb', line 22

def self.base_class
  Softwear::Library::ApiController
end

.token_authenticate(user_class, options = {}) ⇒ Object



26
27
28
29
30
31
# 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

Instance Method Details

#createObject



75
76
77
78
79
80
81
82
83
# 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(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

#optionsObject



85
86
87
# File 'lib/softwear/library/api_controller.rb', line 85

def options
  head(:ok) if request.request_method == 'OPTIONS'
end

#showObject



49
50
51
52
53
# File 'lib/softwear/library/api_controller.rb', line 49

def show
  super do |format|
    format.json(&render_json)
  end
end

#updateObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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