Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shotengai
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
shotengai
Commits
85054a87
Commit
85054a87
authored
Sep 08, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add methods about buy product with order to Buyer & Improve README about it
parent
a72652f0
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
18 deletions
+50
-18
README.md
README.md
+26
-7
buyer.rb
lib/shotengai/buyer.rb
+20
-2
orders_controller.rb
lib/shotengai/controllers/customer/orders_controller.rb
+2
-8
order.rb
lib/shotengai/order.rb
+2
-1
No files found.
README.md
View file @
85054a87
...
...
@@ -74,15 +74,34 @@ This will copy shotengai example views to your application under 'app/views/shot
## Methods
### Order
Add snapshots into order:
```
ruby
### 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 snapshot into cart (a new snapshot or a snapshot in order
):
```
ruby
```
Add snapshots into cart (a new snapshot or snapshots in order unpaid
):
``` ruby
Order.create.gone_snapshot_ids= [ some_snapshot_ids ]
```
```
## Development
...
...
lib/shotengai/buyer.rb
View file @
85054a87
...
...
@@ -19,12 +19,30 @@ 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
}
)
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
...
...
lib/shotengai/controllers/customer/orders_controller.rb
View file @
85054a87
...
...
@@ -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
...
...
lib/shotengai/order.rb
View file @
85054a87
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment