Class: Api::BaseController

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

Instance Method Summary collapse

Instance Method Details

#access_deniedObject



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

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

#admin_token_passed_in_headersObject



37
38
39
# File 'app/controllers/api/base_controller.rb', line 37

def admin_token_passed_in_headers
  request.headers['HTTP_AUTHORIZATION'].present?
end

#createObject



21
22
23
24
25
26
27
# File 'app/controllers/api/base_controller.rb', line 21

def create
  if @object.save
    render :text => "Resource created\n", :status => 201, :location => object_url
  else
    respond_with(@object.errors, :status => 422)
  end
end

#eventObject

Generic action to handle firing of state events on an object



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

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

#indexObject



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

#showObject



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

#updateObject



29
30
31
32
33
34
35
# File 'app/controllers/api/base_controller.rb', line 29

def update
  if @object.update_attributes(params[object_name])
    render :nothing => true
  else
    respond_with(@object.errors, :status => 422)
  end
end