Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
simple_controller
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
simple_controller
Commits
ad7a6282
Commit
ad7a6282
authored
Mar 10, 2022
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 重构association chain的各个逻辑,抽象对于q参数的特殊处理
parent
7e5eca2c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
33 deletions
+22
-33
base_controller.rb
lib/simple_controller/base_controller.rb
+22
-33
No files found.
lib/simple_controller/base_controller.rb
View file @
ad7a6282
...
...
@@ -212,10 +212,6 @@ class SimpleController::BaseController < ::InheritedResources::Base
association
=
ransack_association
(
association
,
params
[
:q
])
unless
self
.
class
.
instance_variable_get
(
:@ransack_off
)
||
params
[
:q
].
blank?
association
=
ransack_association
(
association
,
params
[
:sub_q
])
unless
self
.
class
.
instance_variable_get
(
:@ransack_off
)
||
params
[
:sub_q
].
blank?
association
=
association
.
distinct
unless
self
.
class
.
instance_variable_get
(
:@distinct_off
)
||
!
association
.
respond_to?
(
:distinct
)
||
!
active_record?
if
params
[
:q
][
:refs
].
present?
_refs
=
Array
(
params
[
:q
][
:refs
]).
map
(
&
:to_sym
)
association
=
association
.
includes
(
*
_refs
).
joins
(
*
_refs
)
end
association
=
association
.
paginate
(
page:
params
[
:page
],
per_page:
params
[
:per_page
])
unless
self
.
class
.
instance_variable_get
(
:@paginate_off
)
association
end
...
...
@@ -239,12 +235,7 @@ class SimpleController::BaseController < ::InheritedResources::Base
if
self
.
class
.
instance_variable_get
(
:@ransack_off
)
||
params
[
:q
].
blank?
policy_association_chain
else
if
params
[
:q
][
:scopes
].
present?
association
=
Array
(
params
[
:q
][
:scopes
]).
reduce
(
policy_association_chain
)
{
|
_association
,
_scope
|
_association
.
send
(
_scope
)
}
else
association
=
policy_association_chain
end
association
=
ransack_association
(
association
,
params
[
:q
].
except
(
:scopes
))
ransack_association
(
association
,
params
[
:q
])
end
end
...
...
@@ -278,27 +269,8 @@ class SimpleController::BaseController < ::InheritedResources::Base
end
end
unless
self
.
class
.
instance_variable_get
(
:@ransack_off
)
||
params
[
:sub_q
].
blank?
association
=
Array
(
params
[
:sub_q
][
:scopes
]).
reduce
(
association
)
{
|
_association
,
_scope
|
_association
.
send
(
_scope
)
}
if
params
[
:sub_q
][
:scopes
].
present?
association
=
ransack_association
(
association
,
params
[
:sub_q
].
except
(
:scopes
,
:refs
,
:jorder
))
end
unless
self
.
class
.
instance_variable_get
(
:@ransack_off
)
||
params
[
:q
].
blank?
association
=
Array
(
params
[
:q
][
:scopes
]).
reduce
(
association
)
{
|
_association
,
_scope
|
_association
.
send
(
_scope
)
}
if
params
[
:q
][
:scopes
].
present?
association
=
ransack_association
(
association
,
params
[
:q
].
except
(
:scopes
,
:refs
,
:jorder
))
end
if
params
.
dig
(
:q
,
:refs
).
present?
_refs
=
Array
(
params
[
:q
][
:refs
]).
map
(
&
:to_sym
)
association
=
association
.
includes
(
*
_refs
).
joins
(
*
_refs
)
end
if
params
.
dig
(
:q
,
:jorder
).
present?
order_array
=
Array
(
params
.
dig
(
:q
,
:jorder
))
sql
=
order_array
.
map
do
|
order_string
|
_attr
,
_order
=
order_string
.
split
(
' '
)
_jsonb_attr
=
_attr
.
split
(
'.'
).
map
.
with_index
{
|
a
,
index
|
index
==
0
?
a
:
"'
#{
a
}
'"
}.
join
(
'->'
)
"
#{
_jsonb_attr
}
#{
_order
}
"
end
.
join
(
', '
)
association
=
association
.
order
(
Arel
.
sql
(
sql
))
end
association
=
ransack_association
(
association
,
params
[
:sub_q
])
unless
self
.
class
.
instance_variable_get
(
:@ransack_off
)
||
params
[
:sub_q
].
blank?
association
=
association
.
distinct
unless
self
.
class
.
instance_variable_get
(
:@distinct_off
)
||
!
association
.
respond_to?
(
:distinct
)
||
!
active_record?
||
params
.
dig
(
:q
,
:jorder
).
present?
association
end
...
...
@@ -343,10 +315,27 @@ class SimpleController::BaseController < ::InheritedResources::Base
end
def
ransack_association
(
association
,
query_params
)
# scopes,代表前端直接调用后台的scope过滤
association
=
Array
(
query_params
[
:scopes
]).
reduce
(
association
)
{
|
_association
,
_scope
|
_association
.
send
(
_scope
)
}
if
query_params
[
:scopes
].
present?
end
if
active_record?
association
.
ransack
(
query_params
).
result
association
.
ransack
(
query_params
.
except
(
:scopes
,
:refs
,
:jorder
)).
result
# PG,为了支持distinct和order的操作,需要增加refs,手动includes 和 joins
if
query_params
[
:refs
].
present?
_refs
=
Array
(
query_params
[
:refs
]).
map
(
&
:to_sym
)
association
=
association
.
includes
(
*
_refs
).
joins
(
*
_refs
)
end
if
query_params
.
dig
(
:jorder
).
present?
order_array
=
Array
(
query_params
.
dig
(
:jorder
))
sql
=
order_array
.
map
do
|
order_string
|
_attr
,
_order
=
order_string
.
split
(
' '
)
_jsonb_attr
=
_attr
.
split
(
'.'
).
map
.
with_index
{
|
a
,
index
|
index
==
0
?
a
:
"'
#{
a
}
'"
}.
join
(
'->'
)
"
#{
_jsonb_attr
}
#{
_order
}
"
end
.
join
(
', '
)
association
=
association
.
order
(
Arel
.
sql
(
sql
))
end
else
_params
=
query_params
.
clone
_params
=
query_params
.
clone
.
except
(
:scopes
)
order_params
=
_params
.
delete
(
:s
)
selector
=
RansackMongo
::
Query
.
parse
(
_params
)
order_params
.
present?
?
...
...
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