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
9a4747f0
Commit
9a4747f0
authored
Aug 01, 2021
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support ransack with sub_q for statistic
parent
0e80481e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
2 deletions
+15
-2
index.json.jbuilder
...ors/simple_controller/templates/views/index.json.jbuilder
+1
-0
base_controller.rb
lib/simple_controller/base_controller.rb
+14
-2
No files found.
lib/generators/simple_controller/templates/views/index.json.jbuilder
View file @
9a4747f0
json.current_page @<%= resource_plural %>.current_page
json.total_pages @<%= resource_plural %>.total_pages
json.total_count @<%= resource_plural %>.count
json.statistic @statistic if @statistic.present?
json.records @<%= resource_plural %>, partial: '<%= view_path %>/simple', as: :<%= resource_singular %>
lib/simple_controller/base_controller.rb
View file @
9a4747f0
...
...
@@ -164,8 +164,20 @@ class SimpleController::BaseController < ::InheritedResources::Base
association
end
# ransack q, 这里主要是为了统计
def
query_association_chain
if
self
.
class
.
instance_variable_get
(
:@ransack_off
)
||
params
[
:q
].
blank?
end_of_association_chain
else
end_of_association_chain
.
ransack
(
params
[
:q
].
result
end
end
# 执行sub_q
def
ransack_paginate
(
association
)
association
=
association
.
ransack
(
params
[
:q
]).
result
unless
self
.
class
.
instance_variable_get
(
:@ransack_off
)
@statistic
=
association
.
unscope
(
:order
).
distinct
.
group
(
params
[
:group_keys
]).
count
if
params
[
:group_keys
].
present?
association
=
association
.
ransack
(
params
[
:sub_q
])).
result
unless
self
.
class
.
instance_variable_get
(
:@ransack_off
)
||
params
[
:sub_q
].
blank?
association
=
association
.
distinct
unless
self
.
class
.
instance_variable_get
(
:@distinct_off
)
association
=
association
.
paginate
(
page:
params
[
:page
],
per_page:
params
[
:per_page
])
unless
self
.
class
.
instance_variable_get
(
:@paginate_off
)
association
...
...
@@ -188,7 +200,7 @@ class SimpleController::BaseController < ::InheritedResources::Base
def
collection
get_collection_ivar
||
set_collection_ivar
(
ransack_paginate
(
end_of
_association_chain
)
ransack_paginate
(
query
_association_chain
)
)
end
...
...
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