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
4e007d9e
Commit
4e007d9e
authored
Sep 15, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Shotengai::Controller::Base
parent
0b76f7be
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
98 additions
and
111 deletions
+98
-111
template_controller.rb
lib/generators/templates/controllers/template_controller.rb
+1
-1
base.rb
lib/shotengai/controllers/base.rb
+53
-44
carts_controller.rb
lib/shotengai/controllers/customer/carts_controller.rb
+1
-7
orders_controller.rb
lib/shotengai/controllers/customer/orders_controller.rb
+8
-7
product_series_controller.rb
...otengai/controllers/customer/product_series_controller.rb
+4
-6
product_snapshots_controller.rb
...ngai/controllers/customer/product_snapshots_controller.rb
+7
-10
products_controller.rb
lib/shotengai/controllers/customer/products_controller.rb
+4
-8
orders_controller.rb
lib/shotengai/controllers/merchant/orders_controller.rb
+4
-8
product_series_controller.rb
...otengai/controllers/merchant/product_series_controller.rb
+3
-7
product_snapshots_controller.rb
...ngai/controllers/merchant/product_snapshots_controller.rb
+5
-5
products_controller.rb
lib/shotengai/controllers/merchant/products_controller.rb
+8
-8
No files found.
lib/generators/templates/controllers/template_controller.rb
View file @
4e007d9e
class
<
%= @controller_prefix %><%=
@key
.
classify
.
pluralize
%>Controller < Shotengai::Controller::<%= "#{@role}/#{@key}".camelize %>
Controller
class
<
%= @controller_prefix %><%=
@key
.
classify
.
pluralize
%>Controller < Shotengai::Controller::<%= "#{@role}/#{@key}".camelize %>
Controller
self
.
resources
=
<
%= @klass_name %>
self
.
base_
resources
=
<
%= @klass_name %>
end
end
lib/shotengai/controllers/base.rb
View file @
4e007d9e
module
Shotengai
module
Shotengai
module
Controller
module
Controller
class
Base
<
ApplicationController
class
Base
<
ApplicationController
class
<<
self
#
#
# The
resources of this controller
# The base_
resources of this controller
# ActiveRecord::Relation or ActiveRecord::Base
# ActiveRecord::Relation or ActiveRecord::Base
#
cattr_accessor
:resources
def
base_resources
=
resources
class_eval
%Q{
def add_base_resources
@base_resources = ::
#{
resources
}
end
}
end
#
#
# The view template dir
# The view template dir
# respond_with @products, template: "#{self.class.template_dir}/index"
# respond_with @products, template: "#{self.class.template_dir}/index"
#
cattr_accessor
:template_dir
@@index_query
=
nil
@@default_query
=
nil
class
<<
self
def
template_dir
=
template_dir
# Add the index query to custom the @@index_resouces on the base of @@resources
class_eval
%Q{
# Foe example:
def add_template_dir
#
@template_dir = "
#{
template_dir
}
"
# index_query do |klass, params|
# klass.where(product: params[:product_id]).order('desc')
# end
#
def
index_query
&
block
# could not get params here
@@index_query
=
block
end
end
}
# @@default_query would be useful for create & set_resource
# Just like:
# one_product.series.create! => default_query where(product_id: params[:product_id])
#
def
default_query
&
block
@@default_query
=
block
end
end
# Refuse to search methods in superclasses
# Refuse to search methods in superclasses
def
remove_actions
*
actions
def
remove_actions
*
actions
actions
.
each
{
|
name
|
remove_possible_method
name
}
actions
.
each
{
|
name
|
remove_possible_method
name
}
end
end
# def resources= klass_name
# retries ||= 1
# @@resources = klass_name.constantize
# rescue NameError
# # If Product havent been load, ProductSeries or ProductSnapshot would not exists
# unless (retries =- 1) < 0
# klass_name.remove('Series', 'Snapshot').constantize
# retry
# else
# raise
# end
# end
end
end
before_action
:set_resource
,
except:
[
:index
,
:create
]
before_action
:set_resource
,
except:
[
:index
,
:create
]
...
@@ -78,17 +55,17 @@ module Shotengai
...
@@ -78,17 +55,17 @@ module Shotengai
page
=
params
[
:page
]
||
1
page
=
params
[
:page
]
||
1
per_page
=
params
[
:per_page
]
||
10
per_page
=
params
[
:per_page
]
||
10
@resources
=
index_resources
.
paginate
(
page:
page
,
per_page:
per_page
)
@resources
=
index_resources
.
paginate
(
page:
page
,
per_page:
per_page
)
respond_with
@resources
,
template:
"
#{
@
@
template_dir
}
/index"
respond_with
@resources
,
template:
"
#{
@template_dir
}
/index"
end
end
def
show
def
show
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
end
end
def
create
def
create
@resource
=
default_resources
.
create!
(
resource_params
)
@resource
=
default_resources
.
create!
(
resource_params
)
# head 201
# head 201
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
,
status:
201
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
,
status:
201
end
end
def
update
def
update
...
@@ -101,13 +78,44 @@ module Shotengai
...
@@ -101,13 +78,44 @@ module Shotengai
head
204
head
204
end
end
def
initialize
super
try
(
:add_base_resources
)
try
(
:add_template_dir
)
end
private
private
# Instance method: default_query
# example:
# def default_query resources
# resources.where(product_id: params[:product_id])
# end
# default_query would be useful for create & set_resource
# Just like:
# one_product.series.create! => default_query where(product_id: params[:product_id])
#
# def default_query resources
# end
# Add the index query to custom the @@index_resouces on the base of @@resources
# Foe example:
#
# def index_query resources
# resources.where(product: params[:product_id]).order('desc')
# end
#
# def index_query resources
# end
def
index_resources
def
index_resources
@@index_query
&
.
call
(
default_resources
,
params
,
request
)
||
default_resources
try
(
:index_query
,
default_resources
)
||
default_resources
end
end
def
default_resources
def
default_resources
@@default_query
&
.
call
(
self
.
class
.
resources
,
params
,
request
)
||
self
.
class
.
resources
try
(
:default_query
,
@base_resources
)
||
@base_
resources
end
end
def
resource_key
def
resource_key
...
@@ -122,6 +130,7 @@ module Shotengai
...
@@ -122,6 +130,7 @@ module Shotengai
# def resource_params
# def resource_params
# super&.merge params.require(:some_key)
# super&.merge params.require(:some_key)
# end
# end
def
resource_params
def
resource_params
params
.
require
(
resource_key
).
permit!
params
.
require
(
resource_key
).
permit!
end
end
...
...
lib/shotengai/controllers/customer/carts_controller.rb
View file @
4e007d9e
...
@@ -2,7 +2,7 @@ module Shotengai
...
@@ -2,7 +2,7 @@ module Shotengai
module
Controller
module
Controller
module
Customer
module
Customer
class
CartsController
<
Shotengai
::
Controller
::
Base
class
CartsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
Cart
self
.
base_
resources
=
Cart
self
.
template_dir
=
'shotengai/customer/cart'
self
.
template_dir
=
'shotengai/customer/cart'
before_action
:buyer_auth
before_action
:buyer_auth
...
@@ -11,12 +11,6 @@ module Shotengai
...
@@ -11,12 +11,6 @@ module Shotengai
remove_actions
:index
,
:create
,
:destroy
remove_actions
:index
,
:create
,
:destroy
default_query
do
|
resource
,
params
,
request
|
end
index_query
do
|
resource
,
params
,
request
|
end
private
private
def
buyer_auth
def
buyer_auth
@buyer
=
params
[
:buyer_type
].
constantize
.
find
(
params
[
:buyer_id
])
@buyer
=
params
[
:buyer_type
].
constantize
.
find
(
params
[
:buyer_id
])
...
...
lib/shotengai/controllers/customer/orders_controller.rb
View file @
4e007d9e
...
@@ -2,7 +2,7 @@ module Shotengai
...
@@ -2,7 +2,7 @@ module Shotengai
module
Controller
module
Controller
module
Customer
module
Customer
class
OrdersController
<
Shotengai
::
Controller
::
Base
class
OrdersController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
Order
self
.
base_
resources
=
Order
self
.
template_dir
=
'shotengai/customer/orders/'
self
.
template_dir
=
'shotengai/customer/orders/'
before_action
:buyer_auth
before_action
:buyer_auth
...
@@ -10,21 +10,22 @@ module Shotengai
...
@@ -10,21 +10,22 @@ module Shotengai
remove_actions
:destroy
remove_actions
:destroy
default_query
do
|
resource
,
params
,
request
|
def
default_query
resources
resources
.
where
(
buyer:
@buyer
)
end
end
index_query
do
|
resource
,
params
,
request
|
def
index_query
resources
resource
.
status_is
(
params
[
:status
])
resource
s
.
status_is
(
params
[
:status
])
end
end
def
create
# Use :series_id & :count
def
create
# Use :series_id & :count
@resource
=
@buyer
.
buy_it_immediately
(
snapshot_params
,
resource_params
)
@resource
=
@buyer
.
buy_it_immediately
(
snapshot_params
,
resource_params
)
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
,
status:
201
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
,
status:
201
end
end
def
pay
def
pay
@resource
.
pay!
@resource
.
pay!
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
end
end
def
destroy
def
destroy
...
@@ -34,7 +35,7 @@ module Shotengai
...
@@ -34,7 +35,7 @@ module Shotengai
def
confirm
def
confirm
@resource
.
confirm!
@resource
.
confirm!
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
end
end
private
private
...
...
lib/shotengai/controllers/customer/product_series_controller.rb
View file @
4e007d9e
...
@@ -2,18 +2,16 @@ module Shotengai
...
@@ -2,18 +2,16 @@ module Shotengai
module
Controller
module
Controller
module
Customer
module
Customer
class
ProductSeriesController
<
Shotengai
::
Controller
::
Base
class
ProductSeriesController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
ProductSeries
self
.
base_
resources
=
ProductSeries
self
.
template_dir
=
'shotengai/customer/series/'
self
.
template_dir
=
'shotengai/customer/series/'
remove_actions
:create
,
:update
,
:destroy
remove_actions
:create
,
:update
,
:destroy
def
ault_query
do
|
resource
,
params
,
request
|
def
default_query
resources
resource
.
where
(
resource
s
.
where
(
params
[
:product_id
]
&&
{
shotengai_product_id:
params
[
:product_id
]
}
params
[
:product_id
]
&&
{
shotengai_product_id:
params
[
:product_id
]
}
)
)
end
end
index_query
do
|
resource
,
params
,
request
|
end
end
end
end
end
end
end
...
...
lib/shotengai/controllers/customer/product_snapshots_controller.rb
View file @
4e007d9e
...
@@ -2,34 +2,31 @@ module Shotengai
...
@@ -2,34 +2,31 @@ module Shotengai
module
Controller
module
Controller
module
Customer
module
Customer
class
ProductSnapshotsController
<
Shotengai
::
Controller
::
Base
class
ProductSnapshotsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
ProductSnapshot
self
.
base_
resources
=
ProductSnapshot
self
.
template_dir
=
'shotengai/customer/snapshots/'
self
.
template_dir
=
'shotengai/customer/snapshots/'
before_action
:buyer_auth
before_action
:buyer_auth
before_action
:edit_only_unpaid
,
except:
[
:index
,
:show
,
:create
]
before_action
:edit_only_unpaid
,
except:
[
:index
,
:show
,
:create
]
self
.
default_query
do
|
resource
,
params
,
request
|
def
default_query
resources
# /orders/:order_id/snapshots
# /orders/:order_id/snapshots
# /series/:series_id/snapshots
# /series/:series_id/snapshots
buyer
=
params
[
:buyer_type
].
constantize
.
find
(
params
[
:buyer_id
])
order_id
=
request
.
path_info
.
include?
(
'cart'
)
?
@buyer
.
order_cart
.
id
:
params
[
:order_id
]
order_id
=
request
.
path_info
.
include?
(
'cart'
)
?
buyer
.
order_cart
.
id
:
params
[
:order_id
]
resources
.
where
(
resource
.
where
(
order_id
&&
{
shotengai_order_id:
order_id
}
order_id
&&
{
shotengai_order_id:
order_id
}
).
count
).
count
resource
.
where
(
resource
s
.
where
(
order_id
&&
{
shotengai_order_id:
order_id
}
order_id
&&
{
shotengai_order_id:
order_id
}
).
where
(
).
where
(
params
[
:series_id
]
&&
{
shotengai_series_id:
params
[
:series_id
]
}
params
[
:series_id
]
&&
{
shotengai_series_id:
params
[
:series_id
]
}
)
)
end
end
index_query
do
|
resource
,
params
,
request
|
end
# 不指定 order 时,默认创建在 cart 中
# 不指定 order 时,默认创建在 cart 中
# TODO: WARNING: snapshots
# TODO: WARNING: snapshots
def
create
def
create
@resource
=
default_resources
.
create!
(
resource_params
)
@resource
=
default_resources
.
create!
(
resource_params
)
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
,
status:
201
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
,
status:
201
end
end
private
private
...
...
lib/shotengai/controllers/customer/products_controller.rb
View file @
4e007d9e
...
@@ -2,18 +2,14 @@ module Shotengai
...
@@ -2,18 +2,14 @@ module Shotengai
module
Controller
module
Controller
module
Customer
module
Customer
class
ProductsController
<
Shotengai
::
Controller
::
Base
class
ProductsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
Product
self
.
base_
resources
=
Product
self
.
template_dir
=
'shotengai/customer/products/'
self
.
template_dir
=
'shotengai/customer/products/'
remove_actions
:create
,
:update
,
:destroy
remove_actions
:create
,
:update
,
:destroy
default_query
do
|
resource
,
params
,
request
|
end
index_query
do
|
resource
,
params
,
request
|
def
index_query
resources
params
[
:catalog_list
]
?
params
[
:catalog_list
]
?
resource
.
tagged_with
(
params
[:
catalog_list
],
on: :catalogs
)
:
resource
s
.
tagged_with
(
params
[:
catalog_list
],
on: :catalogs
)
:
resource
resource
s
end
end
end
end
end
end
...
...
lib/shotengai/controllers/merchant/orders_controller.rb
View file @
4e007d9e
...
@@ -2,22 +2,18 @@ module Shotengai
...
@@ -2,22 +2,18 @@ module Shotengai
module
Controller
module
Controller
module
Merchant
module
Merchant
class
OrdersController
<
Shotengai
::
Controller
::
Base
class
OrdersController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
Order
self
.
base_resources
=
::
Order
self
.
template_dir
=
'shotengai/merchant/orders/'
self
.
template_dir
=
'shotengai/merchant/orders/'
remove_actions
:create
,
:destroy
remove_actions
:create
,
:destroy
default_query
do
|
resource
,
params
,
request
|
def
index_query
resources
resources
.
status_is
(
params
[
:status
])
end
index_query
do
|
resource
,
params
,
request
|
resource
.
status_is
(
params
[
:status
])
end
end
def
send_out
def
send_out
@resource
.
send_out!
@resource
.
send_out!
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
end
end
private
private
...
...
lib/shotengai/controllers/merchant/product_series_controller.rb
View file @
4e007d9e
...
@@ -2,19 +2,15 @@ module Shotengai
...
@@ -2,19 +2,15 @@ module Shotengai
module
Controller
module
Controller
module
Merchant
module
Merchant
class
ProductSeriesController
<
Shotengai
::
Controller
::
Base
class
ProductSeriesController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
ProductSeries
self
.
base_
resources
=
ProductSeries
self
.
template_dir
=
'shotengai/merchant/series/'
self
.
template_dir
=
'shotengai/merchant/series/'
def
ault_query
do
|
resource
,
params
,
request
|
def
default_query
resources
resource
.
where
(
resource
s
.
where
(
params
[
:product_id
]
&&
{
shotengai_product_id:
params
[
:product_id
]
}
params
[
:product_id
]
&&
{
shotengai_product_id:
params
[
:product_id
]
}
)
)
end
end
index_query
do
|
resource
,
params
,
request
|
end
private
private
def
resource_params
def
resource_params
spec
=
params
.
require
(
resource_key
).
fetch
(
:spec
,
nil
).
try
(
:permit!
)
spec
=
params
.
require
(
resource_key
).
fetch
(
:spec
,
nil
).
try
(
:permit!
)
...
...
lib/shotengai/controllers/merchant/product_snapshots_controller.rb
View file @
4e007d9e
...
@@ -2,18 +2,18 @@ module Shotengai
...
@@ -2,18 +2,18 @@ module Shotengai
module
Controller
module
Controller
module
Merchant
module
Merchant
class
ProductSnapshotsController
<
Shotengai
::
Controller
::
Base
class
ProductSnapshotsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
ProductSnapshot
self
.
base_
resources
=
ProductSnapshot
self
.
template_dir
=
'shotengai/merchant/snapshots/'
self
.
template_dir
=
'shotengai/merchant/snapshots/'
remove_actions
:create
,
:destroy
remove_actions
:create
,
:destroy
before_action
:edit_only_unpaid
,
only: :update
before_action
:edit_only_unpaid
,
only: :update
def
ault_query
do
|
resource
,
params
,
request
|
def
default_query
resources
resource
.
in_order
resource
s
.
in_order
end
end
index_query
do
|
resource
,
params
,
request
|
def
index_query
resources
resource
.
where
(
resource
s
.
where
(
params
[
:order_id
]
&&
{
shotengai_order_id:
params
[
:order_id
]
}
params
[
:order_id
]
&&
{
shotengai_order_id:
params
[
:order_id
]
}
).
where
(
).
where
(
params
[
:product_series_id
]
&&
{
shotengai_series_id:
params
[
:product_series_id
]
}
params
[
:product_series_id
]
&&
{
shotengai_series_id:
params
[
:product_series_id
]
}
...
...
lib/shotengai/controllers/merchant/products_controller.rb
View file @
4e007d9e
...
@@ -2,29 +2,29 @@ module Shotengai
...
@@ -2,29 +2,29 @@ module Shotengai
module
Controller
module
Controller
module
Merchant
module
Merchant
class
ProductsController
<
Shotengai
::
Controller
::
Base
class
ProductsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
Product
self
.
base_
resources
=
Product
self
.
template_dir
=
'shotengai/merchant/products/'
self
.
template_dir
=
'shotengai/merchant/products/'
before_action
:manager_auth
before_action
:manager_auth
def
ault_query
do
|
resource
,
params
,
request
|
def
default_query
resources
resource
.
where
(
manager:
@manager
)
resource
s
.
where
(
@manager
&&
{
manager:
@manager
}
)
end
end
index_query
do
|
resource
,
params
,
request
|
def
index_query
resources
params
[
:catalog_list
]
?
params
[
:catalog_list
]
?
resource
.
tagged_with
(
params
[:
catalog_list
],
on: :catalogs
)
:
resource
s
.
tagged_with
(
params
[:
catalog_list
],
on: :catalogs
)
:
resource
resource
s
end
end
def
put_on_shelf
def
put_on_shelf
@resource
.
put_on_shelf!
@resource
.
put_on_shelf!
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
,
status:
200
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
,
status:
200
end
end
def
sold_out
def
sold_out
@resource
.
sold_out!
@resource
.
sold_out!
respond_with
@resource
,
template:
"
#{
@
@
template_dir
}
/show"
,
status:
200
respond_with
@resource
,
template:
"
#{
@template_dir
}
/show"
,
status:
200
end
end
def
destroy
def
destroy
...
...
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