Class: Spree::Api::V2::Storefront::GuestsController

Inherits:
ResourceController
  • Object
show all
Includes:
OrderConcern
Defined in:
app/controllers/spree/api/v2/storefront/guests_controller.rb

Direct Known Subclasses

UserGuestsController

Instance Method Summary collapse

Instance Method Details

#collection_serializerObject

override



17
18
19
# File 'app/controllers/spree/api/v2/storefront/guests_controller.rb', line 17

def collection_serializer
  SpreeCmCommissioner::V2::Storefront::GuestSerializer
end

#createObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/spree/api/v2/storefront/guests_controller.rb', line 26

def create
  spree_authorize! :update, spree_current_order, order_token

  resource = scope.new(guest_params)

  if resource.save
    render_serialized_payload(201) { serialize_resource(resource) }
  else
    render_error_payload(resource.errors, 400)
  end
end

#model_classObject

override



12
13
14
# File 'app/controllers/spree/api/v2/storefront/guests_controller.rb', line 12

def model_class
  SpreeCmCommissioner::Guest
end

#resource_serializerObject

override



22
23
24
# File 'app/controllers/spree/api/v2/storefront/guests_controller.rb', line 22

def resource_serializer
  SpreeCmCommissioner::V2::Storefront::GuestSerializer
end

#updateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/spree/api/v2/storefront/guests_controller.rb', line 38

def update
  spree_authorize! :update, spree_current_order, order_token

  begin
    if guest_params[:guest_dynamic_fields_attributes]
      resource.guest_dynamic_fields.destroy_all

      guest_params[:guest_dynamic_fields_attributes].each do |attr|
        field = build_dynamic_field(attr)
        field.save!
      end
    end

    resource.assign_attributes(guest_params.except(:guest_dynamic_fields_attributes))

    if resource.save
      render_serialized_payload { serialize_resource(resource) }
    else
      render_error_payload(resource.errors, 400)
    end
  rescue ActiveRecord::RecordInvalid => e
    render_error_payload(e.record.errors, 400)
  end
end