Class: Api::BaseController
- Inherits:
-
Spree::BaseController
- Object
- Spree::BaseController
- Api::BaseController
show all
- Defined in:
- app/controllers/api/base_controller.rb
Direct Known Subclasses
AdjustmentsController, CountriesController, InventoryUnitsController, LineItemsController, MailMethodsController, OptionTypesController, OrdersController, PaymentMethodsController, ProductGroupsController, ProductsController, PromotionsController, PropertiesController, PrototypesController, ReportsController, ShippingCategoriesController, ShippingMethodsController, StatesController, TaxCategoriesController, TaxRatesController, TaxonomiesController, TaxonsController, UsersController, VariantsController, ZonesController
Instance Method Summary
collapse
Instance Method Details
#access_denied ⇒ Object
44
45
46
|
# File 'app/controllers/api/base_controller.rb', line 44
def access_denied
render :text => 'access_denied', :status => 401
end
|
40
41
42
|
# File 'app/controllers/api/base_controller.rb', line 40
def
request.['HTTP_AUTHORIZATION'].present?
end
|
#create ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'app/controllers/api/base_controller.rb', line 21
def create
if @object.save
render :json => @object.to_json, :status => 201
else
respond_with(@object.errors, :status => 422)
end
end
|
#event ⇒ Object
Generic action to handle firing of state events on an object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'app/controllers/api/base_controller.rb', line 49
def event
valid_events = model_class.state_machine.events.map(&:name)
valid_events_for_object = @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
|
#index ⇒ Object
9
10
11
12
13
|
# File 'app/controllers/api/base_controller.rb', line 9
def index
respond_with(@collection) do |format|
format.json { render :json => @collection.to_json(collection_serialization_options) }
end
end
|
#show ⇒ Object
15
16
17
18
19
|
# File 'app/controllers/api/base_controller.rb', line 15
def show
respond_with(@object) do |format|
format.json { render :json => @object.to_json(object_serialization_options) }
end
end
|
#update ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'app/controllers/api/base_controller.rb', line 30
def update
if @object.update_attributes(params[object_name])
render :json => @object.to_json, :status => 201
else
respond_with(@object.errors, :status => 422)
end
end
|