Module: Auth::Concerns::Shopping::CartItemControllerConcern

Extended by:
ActiveSupport::Concern
Included in:
Shopping::CartItemsController
Defined in:
app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb

Instance Method Summary collapse

Instance Method Details

#createObject

expects the product id, resource_id is the logged in resource, and quantity



38
39
40
41
42
43
44
45
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 38

def create
  check_for_create(@auth_shopping_cart_item)
  @auth_shopping_cart_item = add_owner_and_signed_in_resource(@auth_shopping_cart_item)
  @auth_shopping_cart_item = @auth_shopping_cart_item.create_with_embedded(@auth_shopping_cart_item.product_id)
  puts "auth shopping cart item becomes:"
  puts @auth_shopping_cart_item.to_s
  respond_with @auth_shopping_cart_item
end

#create_many_itemsObject

BULK ITEM CREATE. This is utilized to create multiple cart items, first , then redirects to create a cart, with those cart items.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 91

def create_many_items
  cart_items = []
  errors = []
  params[:cart_item][:product_ids].each do |product_id|
    cart_item = Auth.configuration.cart_item_class.constantize.new(product_id: product_id)
    cart_item.resource_id = lookup_resource.id.to_s
    cart_item.resource_class = lookup_resource.class.name.to_s
    cart_item.signed_in_resource = current_signed_in_resource
    create_result = cart_item.create_with_embedded(cart_item.product_id)
    if create_result == false
      ## here there will be the validation errors on the original instance.
      cart_items << cart_item
      errors << cart_item.errors.full_messages
    else
      ## here the create_result becomes the new cart_item.
      cart_items << create_result
    end
  end
  respond_to do |format|
    format.html do 
      redirect_to cart_items_path
    end
    format.json do 
      if errors.empty?
        render :json => {cart_items: cart_items}, :status => 200
      else
        render :json => {cart_items: cart_items, errors: errors}, :status => 422
      end
    end
  end
end

#create_multipleObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 124

def create_multiple
  #puts "came to create multiple."
  #puts "params are:"
  #puts params.to_s
  @auth_shopping_cart_items = []
  @auth_shopping_cart = @auth_shopping_cart_class.new(:add_cart_item_ids => [], :remove_cart_item_ids => [])
  #puts "auth shopping discount is:"
  #puts @auth_shopping_discount.to_s
  @auth_shopping_cart.discount_id = @auth_shopping_discount.id.to_s
 
  unless @auth_shopping_discount.new_record?
    errors = []
    @auth_shopping_discount.product_ids.each do |product_id|

        cart_item = Auth.configuration.cart_item_class.constantize.new 
        cart_item = add_owner_and_signed_in_resource(cart_item)  
        if cart_item_created = cart_item.create_with_embedded(product_id)
          @auth_shopping_cart_items << cart_item_created
          @auth_shopping_cart.add_cart_item_ids << cart_item_created.id.to_s
        else
          @auth_shopping_cart_items << cart_item
          errors << cart_item.errors.full_messages
        end 
    end
  else

  end
    
  respond_to do |format|
    format.html do 
      render 'create_multiple.html.erb'
    end

    format.json do 
      unless errors.empty?
        render :json => {cart_items: @auth_shopping_cart_items, errors: errors}, :status => 422
      else
        render :json => {cart_items: @auth_shopping_cart_items}, :status => 200
      end
    end

  end
   

end

#destroyObject

can be removed. responds with 204, and empty response body, if all is ok.



73
74
75
76
77
78
79
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 73

def destroy
  not_found if @auth_shopping_cart_item.nil?
  puts "the accepted is:"
  puts @auth_shopping_cart_item.accepted.to_s
  @auth_shopping_cart_item.destroy
  respond_with @auth_shopping_cart_item
end

#indexObject

should show those cart items which do not have a parent_id. since these are the pending cart items. all remaining cart items have already been assigned to carts



65
66
67
68
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 65

def index
  @auth_shopping_cart_items = @auth_shopping_cart_item_class.find_cart_items({:resource => lookup_resource}).page 1
  respond_with @auth_shopping_cart_items
end

#initialize_varsObject

if an id is provided in the permitted params then tries to find that in the database, and makes a new cart item out of it. if no id is provided then creates a new cart_item from the permitted params, but excluding the id key. if a collection i.e plural resources is present in the permitted_params and its also there in our auth resources, then create a resource class and resource symbol out of it and assign resource as in the comments.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 19

def initialize_vars
  instantiate_shopping_classes

  @auth_shopping_discount_object_params = permitted_params.fetch(:discount,{})

  
  if !@auth_shopping_discount_object_params.blank?

  @auth_shopping_discount = params[:id] ? @auth_shopping_discount_class.find(params[:id]) : @auth_shopping_discount_class.new(@auth_shopping_discount_object_params)
  
  end

  @auth_shopping_cart_item_params = permitted_params.fetch(:cart_item,{})
  @auth_shopping_cart_item = params[:id] ? @auth_shopping_cart_item_class.find_self(params[:id],current_signed_in_resource) : @auth_shopping_cart_item_class.new(@auth_shopping_cart_item_params)
  
end

#permitted_paramsObject

this permitted params is overridden in the dummy app, and as a result throws unpermitted parameters for the daughter app parameters, even though they are subsequently permitted, since super is called first.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 171

def permitted_params

  
  if action_name.to_s == "update"
    
    params.permit({cart_item: [:discount_code,:quantity]},:id)

  elsif action_name.to_s == "create_multiple"
    params.permit({discount: [:id, {:product_ids => []}]})
  elsif action_name.to_s == "create_many_items"
    params.permit({:cart_item => [:product_ids]})
  else

    params.permit({cart_item: [:product_id,:discount_code,:quantity]},:id)

  end


end

#showObject



57
58
59
60
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 57

def show
  not_found if @auth_shopping_cart_item.nil?
  respond_with @auth_shopping_cart_item 
end

#updateObject

only permits the quantity to be changed, transaction id is internally assigned and can never be changed by the external world.



48
49
50
51
52
53
54
55
# File 'app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb', line 48

def update
  check_for_update(@auth_shopping_cart_item)
  @auth_shopping_cart_item.assign_attributes(@auth_shopping_cart_item_params)
  @auth_shopping_cart_item = add_owner_and_signed_in_resource(@auth_shopping_cart_item)  
  @auth_shopping_cart_item.save
  
  respond_with @auth_shopping_cart_item
end