Class: Api::BaseController

Inherits:
Spree::BaseController
  • Object
show all
Defined in:
app/controllers/api/base_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resource_controller_for_apiObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/api/base_controller.rb', line 3

def self.resource_controller_for_api
  resource_controller
  before_filter :check_http_authorization
  skip_before_filter :verify_authenticity_token, :if => lambda { admin_token_passed_in_headers }

  index do
    wants.json { render :json => collection.to_json(collection_serialization_options) }
  end

  show do
    wants.json { render :json => object.to_json(object_serialization_options) }
    failure.wants.json { render :text => "Failure\n", :status => 500 }
  end

  create do
    wants.json { render :text => "Resource created\n", :status => 201, :location => object_url }
    failure.wants.json { render :text => "Failure\n", :status => 500 }
  end

  update do
    wants.json { render :nothing => true }
    failure.wants.json { render :text => "Failure\n", :status => 500 }
  end

  define_method :admin_token_passed_in_headers do
    request.headers['HTTP_AUTHORIZATION'].present?
  end

  define_method :end_of_association_chain do
    parent? ? parent_association.scoped : model.scoped(:include  => eager_load_associations)
  end

  define_method :collection do
    @collection ||= search.do_search.limit(100)
  end
end

Instance Method Details

#access_deniedObject



40
41
42
# File 'app/controllers/api/base_controller.rb', line 40

def access_denied
  render :text => 'access_denied', :status => 401
end

#eventObject

Generic action to handle firing of state events on an object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/api/base_controller.rb', line 45

def event
  valid_events = model.state_machine.events.map(&:name)
  valid_events_for_object = object.state_transitions.map(&:event)

  if params[:e].blank?
    errors = t('api.errors.missing_event')
  elsif valid_events_for_object.include?(params[:e].to_sym)
    object.send("#{params[:e]}!")
    errors = nil
  elsif valid_events.include?(params[:e].to_sym)
    errors = t('api.errors.invalid_event_for_object', :events => valid_events_for_object.join(','))
  else
    errors = t('api.errors.invalid_event', :events => valid_events.join(','))
  end

  respond_to do |wants|
    wants.json do
      if errors.blank?
        render :nothing => true
      else
        render :json => errors.to_json, :status => 422
      end
    end
  end
end