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
bd678c51
Commit
bd678c51
authored
Aug 26, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add default_query to Shotengai::Controller::Base & Fix foreign_key
parent
e97b8d05
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
52 additions
and
40 deletions
+52
-40
cart.rb
lib/shotengai/cart.rb
+1
-1
base.rb
lib/shotengai/controllers/base.rb
+24
-13
product_series_controller.rb
...otengai/controllers/merchant/product_series_controller.rb
+15
-14
products_controller.rb
lib/shotengai/controllers/merchant/products_controller.rb
+1
-1
order.rb
lib/shotengai/order.rb
+3
-3
product.rb
lib/shotengai/product.rb
+1
-1
series.rb
lib/shotengai/series.rb
+4
-4
snapshot.rb
lib/shotengai/snapshot.rb
+1
-1
shotengai_series.rb
spec/factories/shotengai_series.rb
+1
-1
shotengai_snapshots.rb
spec/factories/shotengai_snapshots.rb
+1
-1
No files found.
lib/shotengai/cart.rb
View file @
bd678c51
...
@@ -46,7 +46,7 @@ module Shotengai
...
@@ -46,7 +46,7 @@ module Shotengai
self
.
model_name
.
singular
.
to_sym
,
self
.
model_name
.
singular
.
to_sym
,
class_name:
self
.
name
,
class_name:
self
.
name
,
optional:
true
,
optional:
true
,
foreign_key: :shotengai_order
s
_id
foreign_key: :shotengai_order_id
)
)
end
end
end
end
...
...
lib/shotengai/controllers/base.rb
View file @
bd678c51
...
@@ -12,6 +12,9 @@ module Shotengai
...
@@ -12,6 +12,9 @@ module Shotengai
#
#
cattr_accessor
:template_dir
cattr_accessor
:template_dir
@@index_query
||=
nil
@@default_scope
||=
nil
class
<<
self
class
<<
self
# Add the index query to custom the @@index_resouces on the base of @@resources
# Add the index query to custom the @@index_resouces on the base of @@resources
# Foe example:
# Foe example:
...
@@ -25,22 +28,30 @@ module Shotengai
...
@@ -25,22 +28,30 @@ module Shotengai
@@index_query
=
block
@@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
def
remove_methods
*
method_names
def
remove_methods
*
method_names
method_names
.
each
{
|
name
|
self
.
remove_method
name
}
method_names
.
each
{
|
name
|
self
.
remove_method
name
}
end
end
def
resources
=
klass_name
#
def resources= klass_name
retries
||=
1
#
retries ||= 1
@@resources
=
klass_name
.
constantize
#
@@resources = klass_name.constantize
rescue
NameError
#
rescue NameError
# If Product havent been load, ProductSeries or ProductSnapshot would not exists
#
# If Product havent been load, ProductSeries or ProductSnapshot would not exists
unless
(
retries
=-
1
)
<
0
#
unless (retries =- 1) < 0
klass_name
.
remove
(
'Series'
,
'Snapshot'
).
constantize
#
klass_name.remove('Series', 'Snapshot').constantize
retry
#
retry
else
#
else
raise
#
raise
end
#
end
end
#
end
end
end
before_action
:set_resource
,
except:
[
:index
,
:create
]
before_action
:set_resource
,
except:
[
:index
,
:create
]
...
@@ -84,7 +95,7 @@ module Shotengai
...
@@ -84,7 +95,7 @@ module Shotengai
end
end
def
default_resources
def
default_resources
self
.
class
.
resources
@@default_query
&
.
call
(
self
.
class
.
resources
,
params
)
||
self
.
class
.
resources
end
end
def
resource_key
def
resource_key
...
...
lib/shotengai/controllers/merchant/product_series_controller.rb
View file @
bd678c51
...
@@ -2,23 +2,24 @@ module Shotengai
...
@@ -2,23 +2,24 @@ module Shotengai
module
Controller
module
Controller
module
Merchant
module
Merchant
class
ProductSeriesController
<
Shotengai
::
Controller
::
Base
class
ProductSeriesController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
'ProductSeries'
self
.
resources
=
ProductSeries
self
.
template_dir
=
'sho
utengai/merchant/product_
series/'
self
.
template_dir
=
'sho
tengai/merchant/
series/'
private
default_query
do
|
klass
,
params
|
klass
.
where
(
shotengai_product_id:
params
)
end
private
def
resource_params
def
resource_params
# resource_key = self.resource.model_name.singular.to_sym
spec
=
params
.
require
(
resource_key
).
fetch
(
:spec
,
nil
).
try
(
:permit!
)
# # QUESTION: need these ?
meta
=
params
.
require
(
resource_key
).
fetch
(
:meta
,
nil
).
try
(
:permit!
)
# spec = params.fetch(:spec, nil).try(:permit!)
params
.
require
(
resource_key
).
permit
(
# datail = params.fetch(:datail, nil).try(:permit!)
:original_price
,
:price
,
:stock
# meta = params.fetch(:meta, nil).try(:permit!)
).
merge
(
{
spec:
spec
,
meta:
meta
}
# params.permit(
).
merge
(
# :price, :original_price,
other_resource_params
# ).merge(
)
# { spec: spec, detail: detail, meta: meta }
# )
end
end
end
end
end
end
...
...
lib/shotengai/controllers/merchant/products_controller.rb
View file @
bd678c51
...
@@ -2,7 +2,7 @@ module Shotengai
...
@@ -2,7 +2,7 @@ module Shotengai
module
Controller
module
Controller
module
Merchant
module
Merchant
class
ProductsController
<
Shotengai
::
Controller
::
Base
class
ProductsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
'Product'
self
.
resources
=
Product
self
.
template_dir
=
'shotengai/merchant/products/'
self
.
template_dir
=
'shotengai/merchant/products/'
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
|
...
...
lib/shotengai/order.rb
View file @
bd678c51
...
@@ -95,20 +95,20 @@ module Shotengai
...
@@ -95,20 +95,20 @@ module Shotengai
has_many
:snapshots
,
->
{
has_many
:snapshots
,
->
{
where
(
type:
good_classes
.
map
{
|
good_class
|
"
#{
good_class
.
name
}
Snapshot"
})
where
(
type:
good_classes
.
map
{
|
good_class
|
"
#{
good_class
.
name
}
Snapshot"
})
},
class_name:
'Shotengai::Snapshot'
,
},
class_name:
'Shotengai::Snapshot'
,
foreign_key: :shotengai_order
s
_id
foreign_key: :shotengai_order_id
good_classes
.
each
do
|
klass
|
good_classes
.
each
do
|
klass
|
has_many
(
has_many
(
klass
.
snapshot_class
.
model_name
.
collection
.
to_sym
,
klass
.
snapshot_class
.
model_name
.
collection
.
to_sym
,
class_name:
klass
.
snapshot_class
.
name
,
class_name:
klass
.
snapshot_class
.
name
,
foreign_key: :shotengai_order
s
_id
foreign_key: :shotengai_order_id
)
)
# optional: true 允许父对象不存在
# optional: true 允许父对象不存在
klass
.
snapshot_class
.
belongs_to
(
klass
.
snapshot_class
.
belongs_to
(
self
.
model_name
.
singular
.
to_sym
,
self
.
model_name
.
singular
.
to_sym
,
class_name:
self
.
name
,
class_name:
self
.
name
,
optional:
true
,
optional:
true
,
foreign_key: :shotengai_order
s
_id
foreign_key: :shotengai_order_id
)
)
end
end
...
...
lib/shotengai/product.rb
View file @
bd678c51
...
@@ -64,7 +64,7 @@ module Shotengai
...
@@ -64,7 +64,7 @@ module Shotengai
end
end
def
add_associations
subclass
def
add_associations
subclass
subclass
.
has_many
:series
,
class_name:
subclass
.
series_class
.
name
,
foreign_key:
'shotengai_product
s
_id'
subclass
.
has_many
:series
,
class_name:
subclass
.
series_class
.
name
,
foreign_key:
'shotengai_product_id'
subclass
.
has_many
:snapshots
,
class_name:
subclass
.
snapshot_class
.
name
,
through: :series
,
source: :snapshots
subclass
.
has_many
:snapshots
,
class_name:
subclass
.
snapshot_class
.
name
,
through: :series
,
source: :snapshots
end
end
...
...
lib/shotengai/series.rb
View file @
bd678c51
...
@@ -16,14 +16,14 @@ module Shotengai
...
@@ -16,14 +16,14 @@ module Shotengai
#
#
# Indexes
# Indexes
#
#
# index_shotengai_series_on_shotengai_product
s_id (shotengai_products
_id)
# index_shotengai_series_on_shotengai_product
_id (shotengai_product
_id)
# index_shotengai_series_on_type (type)
# index_shotengai_series_on_type (type)
#
#
class
Series
<
ActiveRecord
::
Base
class
Series
<
ActiveRecord
::
Base
self
.
table_name
=
'shotengai_series'
self
.
table_name
=
'shotengai_series'
validate
:check_spec
,
if: :spec
validate
:check_spec
,
if: :spec
validates_uniqueness_of
:spec
,
scope: :shotengai_product
s
_id
validates_uniqueness_of
:spec
,
scope: :shotengai_product_id
delegate
:detail
,
:banners
,
:cover_image
,
to: :product
delegate
:detail
,
:banners
,
:cover_image
,
to: :product
...
@@ -37,8 +37,8 @@ module Shotengai
...
@@ -37,8 +37,8 @@ module Shotengai
def
add_associations
def
add_associations
# belongs to Product
# belongs to Product
@subclass
.
belongs_to
:product
,
foreign_key: :shotengai_product
s
_id
,
class_name:
@product_name
@subclass
.
belongs_to
:product
,
foreign_key: :shotengai_product_id
,
class_name:
@product_name
@subclass
.
belongs_to
@product_name
.
underscore
.
to_sym
,
foreign_key: :shotengai_product
s
_id
,
class_name:
@product_name
@subclass
.
belongs_to
@product_name
.
underscore
.
to_sym
,
foreign_key: :shotengai_product_id
,
class_name:
@product_name
# has many snapshot
# has many snapshot
@subclass
.
has_many
:snapshots
,
class_name:
"
#{
@product_name
}
Snapshot"
,
foreign_key: :shotengai_series_id
@subclass
.
has_many
:snapshots
,
class_name:
"
#{
@product_name
}
Snapshot"
,
foreign_key: :shotengai_series_id
end
end
...
...
lib/shotengai/snapshot.rb
View file @
bd678c51
...
@@ -20,7 +20,7 @@ module Shotengai
...
@@ -20,7 +20,7 @@ module Shotengai
#
#
# Indexes
# Indexes
#
#
# index_shotengai_snapshots_on_shotengai_order
s_id (shotengai_orders
_id)
# index_shotengai_snapshots_on_shotengai_order
_id (shotengai_order
_id)
# index_shotengai_snapshots_on_shotengai_series_id (shotengai_series_id)
# index_shotengai_snapshots_on_shotengai_series_id (shotengai_series_id)
# index_shotengai_snapshots_on_type (type)
# index_shotengai_snapshots_on_type (type)
#
#
...
...
spec/factories/shotengai_series.rb
View file @
bd678c51
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
# spec :json
# spec :json
# type :string(255)
# type :string(255)
# meta :json
# meta :json
# shotengai_product
s
_id :integer
# shotengai_product_id :integer
# created_at :datetime not null
# created_at :datetime not null
# updated_at :datetime not null
# updated_at :datetime not null
#
#
...
...
spec/factories/shotengai_snapshots.rb
View file @
bd678c51
...
@@ -13,7 +13,7 @@
...
@@ -13,7 +13,7 @@
# type :string(255)
# type :string(255)
# meta :json
# meta :json
# shotengai_series_id :integer
# shotengai_series_id :integer
# shotengai_order
s
_id :integer
# shotengai_order_id :integer
# created_at :datetime not null
# created_at :datetime not null
# updated_at :datetime not null
# updated_at :datetime not null
#
#
...
...
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