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
## Methods
### Order
Add snapshots into order:
``` ruby
Order.create.incr_snapshot_ids= [ some_snapshot_ids ]
```
Add snapshot into cart (a new snapshot or a snapshot in order):
``` ruby
Order.create.gone_snapshot_ids= [ some_snapshot_ids ]
```
### Buy Product with Order
Use methods in buyer model with series_id:
Add into cart:
``` ruby
# snapshot_params = params.require(:snapshot).permit(
# :shotengai_series_id, :count, # .. and so on
# )
@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
......
......@@ -19,13 +19,31 @@ module Shotengai
has_one cart_name.to_sym, class_name: klass.cart_class.name, as: :buyer
# User.new Cart 相关
class_eval("
after_create :create_#{cart_name}
def #{cart_name}
super || create_#{cart_name}
end
def add_to_#{cart_name} snapshot
snapshot.update!(test_order_cart: self.#{cart_name})
end
def add_to_#{cart_name} snapshot_params
Shotengai::Series.find(snapshot_params[:shotengai_series_id]).snapshots.create!(
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
......
......@@ -25,18 +25,12 @@ module Shotengai
end
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
end
def create_directly # using :series_id & :count
ActiveRecord::Base.transaction do
@resource = @buyer.orders.create!(resource_params)
Shotengai::Series.find(snapshot_params[:shotengai_series_id]).snapshots.create!(
count: snapshot_params[:count],
shotengai_order: @resource
)
end
@resource = @buyer.buy_it_immediately(snapshot_params, resource_params)
respond_with @resource, template: "#{@@template_dir}/show", status: 201
end
......
......@@ -102,7 +102,8 @@ module Shotengai
ActiveRecord::Base.transaction do
ids.each { |id|
# 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
......
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