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
7 years ago
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
master
No related merge requests found
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
21 deletions
+53
-21
README.md
README.md
+28
-9
buyer.rb
lib/shotengai/buyer.rb
+21
-3
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
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
lib/shotengai/buyer.rb
View file @
85054a87
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
lib/shotengai/controllers/customer/orders_controller.rb
View file @
85054a87
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
lib/shotengai/order.rb
View file @
85054a87
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
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