Commit 85054a87 by ivan Lan

Add methods about buy product with order to Buyer & Improve README about it

parent a72652f0
...@@ -74,15 +74,34 @@ This will copy shotengai example views to your application under 'app/views/shot ...@@ -74,15 +74,34 @@ This will copy shotengai example views to your application under 'app/views/shot
## Methods ## Methods
### Order ### Buy Product with Order
Add snapshots into order: Use methods in buyer model with series_id:
``` ruby Add into cart:
Order.create.incr_snapshot_ids= [ some_snapshot_ids ] ``` ruby
``` # snapshot_params = params.require(:snapshot).permit(
Add snapshot into cart (a new snapshot or a snapshot in order): # :shotengai_series_id, :count, # .. and so on
``` ruby # )
Order.create.gone_snapshot_ids= [ some_snapshot_ids ] @user.add_to_order_cart(snapshot_params)
``` ```
Create order by series_id immediately:
```
@user.buy_it_immediately(snapshot_params, order_params)
```
You can also use methods in order model here by adding params like
``` ruby
{ incr_snapshot_ids: [1, 2, 3] }
```
Or use methods in order model with snapshot_id
Add snapshots into order:
``` ruby
Order.create.incr_snapshot_ids= [ some_snapshot_ids ]
```
Add snapshots into cart (a new snapshot or snapshots in order unpaid):
``` ruby
Order.create.gone_snapshot_ids= [ some_snapshot_ids ]
```
## Development ## Development
......
...@@ -19,13 +19,31 @@ module Shotengai ...@@ -19,13 +19,31 @@ module Shotengai
has_one cart_name.to_sym, class_name: klass.cart_class.name, as: :buyer has_one cart_name.to_sym, class_name: klass.cart_class.name, as: :buyer
# User.new Cart 相关 # User.new Cart 相关
class_eval(" class_eval("
after_create :create_#{cart_name}
def #{cart_name} def #{cart_name}
super || create_#{cart_name} super || create_#{cart_name}
end end
def add_to_#{cart_name} snapshot def add_to_#{cart_name} snapshot_params
snapshot.update!(test_order_cart: self.#{cart_name}) Shotengai::Series.find(snapshot_params[:shotengai_series_id]).snapshots.create!(
end snapshot_params.merge({
shotengai_order_id: self.#{cart_name}.id,
})
)
end
def buy_it_immediately snapshot_params, order_params
ActiveRecord::Base.transaction do
order = self.#{collection_name}.create!(order_params)
Shotengai::Series.find(snapshot_params[:shotengai_series_id]).snapshots.create!(
snapshot_params.merge({
shotengai_order: order
})
)
order
end
end
") ")
end end
end end
......
...@@ -25,18 +25,12 @@ module Shotengai ...@@ -25,18 +25,12 @@ module Shotengai
end end
def add_to_cart def add_to_cart
snapshot = @buyer.order_cart.product_snapshots.create!(snapshot_params) snapshot = @buyer.add_to_order_cart(snapshot_params)
respond_with @resource = snapshot, template: 'shotengai/customer/snapshots/show', status: 201 respond_with @resource = snapshot, template: 'shotengai/customer/snapshots/show', status: 201
end end
def create_directly # using :series_id & :count def create_directly # using :series_id & :count
ActiveRecord::Base.transaction do @resource = @buyer.buy_it_immediately(snapshot_params, resource_params)
@resource = @buyer.orders.create!(resource_params)
Shotengai::Series.find(snapshot_params[:shotengai_series_id]).snapshots.create!(
count: snapshot_params[:count],
shotengai_order: @resource
)
end
respond_with @resource, template: "#{@@template_dir}/show", status: 201 respond_with @resource, template: "#{@@template_dir}/show", status: 201
end end
......
...@@ -102,7 +102,8 @@ module Shotengai ...@@ -102,7 +102,8 @@ module Shotengai
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
ids.each { |id| ids.each { |id|
# using update(shotengai_order_id: id) can not get self.id before save # using update(shotengai_order_id: id) can not get self.id before save
Shotengai::Snapshot.find(id).update!(shotengai_order: self) Shotengai::Snapshot.find(id).update!(shotengai_order_id: self.id)
} }
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment