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
fc40c834
Commit
fc40c834
authored
Aug 25, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Shotengai::Controller::Base & Add some controller
parent
a8975486
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
153 additions
and
110 deletions
+153
-110
shotengai.rb
lib/shotengai.rb
+12
-2
base.rb
lib/shotengai/controllers/base.rb
+80
-66
orders_controller.rb
lib/shotengai/controllers/merchant/orders_controller.rb
+9
-0
product_series_controller.rb
...otengai/controllers/merchant/product_series_controller.rb
+26
-0
product_snapshots_controller.rb
...ngai/controllers/merchant/product_snapshots_controller.rb
+9
-0
products_controller.rb
lib/shotengai/controllers/merchant/products_controller.rb
+17
-42
No files found.
lib/shotengai.rb
View file @
fc40c834
require
"shotengai/version"
require
"shotengai/version"
require
'rails'
require
'rails'
#
require 'active_record'
require
'active_record'
module
Shotengai
module
Shotengai
# Your code goes here...
autoload
:Product
,
'shotengai/product'
autoload
:Product
,
'shotengai/product'
autoload
:Series
,
'shotengai/series'
autoload
:Series
,
'shotengai/series'
autoload
:Snapshot
,
'shotengai/snapshot'
autoload
:Snapshot
,
'shotengai/snapshot'
...
@@ -16,6 +15,17 @@ module Shotengai
...
@@ -16,6 +15,17 @@ module Shotengai
autoload
:Engine
,
'shotengai/engine'
autoload
:Engine
,
'shotengai/engine'
module
Controller
module
Controller
autoload
:Base
,
'shotengai/controllers/base'
module
Merchant
autoload
:ProductsController
,
'shotengai/controllers/merchant/products_controller'
autoload
:ProductSnapshotsController
,
'shotengai/controllers/merchant/product_snapshots_controller'
autoload
:ProductSeriesController
,
'shotengai/controllers/merchant/product_series_controller'
autoload
:OrdersController
,
'shotengai/controllers/merchant/orders_controller'
end
module
Customer
end
end
end
end
end
lib/shotengai/controller.rb
→
lib/shotengai/controller
s/base
.rb
View file @
fc40c834
module
Shotengai
module
Shotengai
class
Controller
<
ApplicationController
module
Controller
# The resources of this controller
class
Base
<
ApplicationController
# ActiveRecord::Relation or ActiveRecord::Base
# The resources of this controller
cattr_accessor
:resources
# ActiveRecord::Relation or ActiveRecord::Base
# The view template dir
cattr_accessor
:resources
# respond_with @products, template: "#{self.class.template_dir}/index"
# The view template dir
cattr_accessor
:template_dir
# respond_with @products, template: "#{self.class.template_dir}/index"
cattr_accessor
:template_dir
class
<<
self
# Add the index query to custom the @@index_resouces on the base of @@resources
class
<<
self
# Foe example:
# Add the index query to custom the @@index_resouces on the base of @@resources
#
# Foe example:
# index_query do |klass|
#
# klass.where(product: params[:product_id]).order('desc')
# index_query do |klass|
# end
# klass.where(product: params[:product_id]).order('desc')
#
# end
def
index_query
&
block
#
# 为了保证 self 为Controller instance variable ?? 才能有params 方法??
def
index_query
&
block
@@index_query
=
block
# 为了保证 self 为Controller instance variable ?? 才能有params 方法??
# self.index_resouces = block.call(self.resources)
@@index_query
=
block
end
# self.index_resouces = block.call(self.resources)
end
def
remove_methods
*
method_names
method_names
.
each
{
|
name
|
self
.
remove_method
name
}
end
end
before_action
:set_resource
,
except:
[
:index
,
:create
]
respond_to
:json
def
index
page
=
params
[
:page
]
||
1
per_page
=
params
[
:per_page
]
||
10
@resources
=
index_resources
.
paginate
(
page:
page
,
per_page:
per_page
)
respond_with
@resources
,
template:
"
#{
@@template_dir
}
/index"
end
def
show
def
remove_methods
*
method_names
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
method_names
.
each
{
|
name
|
self
.
remove_method
name
}
end
end
def
create
def
resources
=
klass_name
default_resources
.
create!
(
resource_params
)
retries
||=
1
head
201
@@resources
=
klass_name
.
constantize
end
rescue
NameError
# If Product havent been load, ProductSeries or ProductSnapshot would not exists
def
update
unless
(
retries
=-
1
)
<
0
@resource
.
update!
(
resource_params
)
klass_name
.
remove
(
'Series'
,
'Snapshot'
).
constantize
head
200
retry
end
else
raise
def
destroy
end
@resource
.
destroy!
end
head
204
end
end
before_action
:set_resource
,
except:
[
:index
,
:create
]
respond_to
:json
private
def
index
def
index_resources
page
=
params
[
:page
]
||
1
@@index_query
&
.
call
(
default_resources
)
||
default_resources
per_page
=
params
[
:per_page
]
||
10
@resources
=
index_resources
.
paginate
(
page:
page
,
per_page:
per_page
)
respond_with
@resources
,
template:
"
#{
@@template_dir
}
/index"
end
end
def
default_resources
def
show
self
.
class
.
resources
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
end
end
def
resource_key
def
create
(
default_resources
.
try
(
:klass
)
||
default_resources
).
model_name
.
singular
.
to_sym
default_resources
.
create!
(
resource_params
)
head
201
end
end
def
set_resource
def
update
@resource
=
default_resources
.
find
(
params
[
:id
])
@resource
.
update!
(
resource_params
)
head
200
end
end
def
resource_params
def
destroy
params
.
requrie
(
resource_key
)
@resource
.
destroy!
head
204
end
end
private
def
index_resources
@@index_query
&
.
call
(
default_resources
)
||
default_resources
end
def
default_resources
self
.
class
.
resources
end
def
resource_key
(
default_resources
.
try
(
:klass
)
||
default_resources
).
model_name
.
singular
.
to_sym
end
def
set_resource
@resource
=
default_resources
.
find
(
params
[
:id
])
end
def
resource_params
params
.
requrie
(
resource_key
)
end
end
end
end
end
end
\ No newline at end of file
lib/shotengai/controllers/merchant/orders_controller.rb
0 → 100644
View file @
fc40c834
module
Shotengai
module
Controller
module
Merchant
class
OrdersController
<
Shotengai
::
Controller
::
Base
end
end
end
end
lib/shotengai/controllers/merchant/product_series_controller.rb
0 → 100644
View file @
fc40c834
module
Shotengai
module
Controller
module
Merchant
class
ProductSeriesController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
'ProductSeries'
self
.
template_dir
=
'shoutengai/merchant/product_series/'
private
def
resource_params
# resource_key = self.resource.model_name.singular.to_sym
# # QUESTION: need these ?
# spec = params.fetch(:spec, nil).try(:permit!)
# datail = params.fetch(:datail, nil).try(:permit!)
# meta = params.fetch(:meta, nil).try(:permit!)
# params.permit(
# :price, :original_price,
# ).merge(
# { spec: spec, detail: detail, meta: meta }
# )
end
end
end
end
end
lib/shotengai/controllers/merchant/product_snapshots_controller.rb
0 → 100644
View file @
fc40c834
module
Shotengai
module
Controller
module
Merchant
class
SnapshotsController
<
Shotengai
::
Controller
::
Base
end
end
end
end
lib/shotengai/controllers/
product
.rb
→
lib/shotengai/controllers/
merchant/products_controller
.rb
View file @
fc40c834
module
Shotengai
module
Shotengai
module
Controller
module
Controller
class
ProductsMasterController
module
Merchant
cattr_accessor
:resource
{
'Product'
}
# 默认值
class
ProductsController
<
Shotengai
::
Controller
::
Base
# respond_with @products, template: "#{self.template_dir}/index"
self
.
resources
=
::
Product
cattr_accessor
:template_dir
{
'products/'
}
self
.
template_dir
=
'shoutengai/merchant/products/'
class
<<
self
index_query
do
|
resource
|
def
resource
=
resource_name
# params[:catalogs] nil 返回所有
klass
=
Object
.
const_get
resource_name
resource
.
tagged_with
(
params
[
:catalogs
])
raise
ArgumentError
.
new
(
'The resource of Product Controller should be a class inherited from Shotengai::Product'
)
unless
Shotengai
::
Product
===
klass
@@resource
=
klass
end
end
end
before_action
:set_product
,
expect:
[
:index
,
:create
]
def
put_on_shelf
respond_to
:json
@resource
.
put_on_shelf!
head
200
def
index
@products
=
self
.
resource
.
all
respond_with
@products
,
template:
"
#{
self
.
template_dir
}
/index"
end
def
show
@product
=
self
.
resource
.
find
(
params
[
:id
])
respond_with
@product
,
template:
"
#{
self
.
template_dir
}
/show"
# need series list here
end
def
create
self
.
resource
.
create!
(
product_params
)
head
201
end
end
def
update
def
sold_out
self
.
resource
.
update!
(
product_params
)
@resource
.
sold_out!
head
200
head
200
end
end
resource
.
aasm
.
state_machine
.
events
.
map
(
&
:first
).
each
do
|
event
|
def
destroy
define_method
(
event
)
do
@resource
.
soft_delete!
@product
.
send
(
"
#{
event
}
!"
)
head
204
head
200
end
end
end
private
private
def
set_resource
def
resource_params
@product
=
self
.
resource
.
find
(
params
[
:id
])
end
def
product_params
resource_key
=
self
.
resource
.
model_name
.
singular
.
to_sym
resource_key
=
self
.
resource
.
model_name
.
singular
.
to_sym
# QUESTION: need these ?
# QUESTION: need these ?
spec
=
params
.
require
(
resource_key
).
fetch
(
:spec
,
nil
).
try
(
:permit!
)
spec
=
params
.
require
(
resource_key
).
fetch
(
:spec
,
nil
).
try
(
:permit!
)
...
...
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