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
177e7179
Commit
177e7179
authored
Aug 18, 2023
by
Ivan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: SimpleController::GroupIndex
parent
2fabf855
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
3 deletions
+83
-3
simple_controller.rb
lib/simple_controller.rb
+4
-3
base_controller.rb
lib/simple_controller/base_controller.rb
+1
-0
group_index.rb
lib/simple_controller/group_index.rb
+78
-0
No files found.
lib/simple_controller.rb
View file @
177e7179
...
@@ -8,9 +8,10 @@ require 'pundit'
...
@@ -8,9 +8,10 @@ require 'pundit'
require
'ransack_mongo'
require
'ransack_mongo'
module
SimpleController
module
SimpleController
autoload
:VERSION
,
'simple_controller/version'
autoload
:VERSION
,
'simple_controller/version'
autoload
:BaseController
,
'simple_controller/base_controller'
autoload
:BaseController
,
'simple_controller/base_controller'
autoload
:Exportable
,
'simple_controller/exportable'
autoload
:Exportable
,
'simple_controller/exportable'
autoload
:Importable
,
'simple_controller/importable'
autoload
:Importable
,
'simple_controller/importable'
autoload
:Responder
,
'simple_controller/responder'
autoload
:GroupIndex
,
'simple_controller/group_index'
autoload
:Responder
,
'simple_controller/responder'
end
end
lib/simple_controller/base_controller.rb
View file @
177e7179
class
SimpleController
::
BaseController
<
::
InheritedResources
::
Base
class
SimpleController
::
BaseController
<
::
InheritedResources
::
Base
include
Pundit
::
Authorization
include
Pundit
::
Authorization
include
SimpleController
::
GroupIndex
self
.
responder
=
SimpleController
::
Responder
self
.
responder
=
SimpleController
::
Responder
respond_to
:json
respond_to
:json
...
...
lib/simple_controller/group_index.rb
0 → 100644
View file @
177e7179
module
SimpleController::GroupIndex
def
group_index
keys
=
Array
(
params
[
:q
]
&
.
[
](
:group_keys
)
||
[])
# keys = ["priority", "own_tokens_user_of_User_type_name", "state"]
ransack_options
=
keys
.
reduce
({})
do
|
options
,
key
|
options
.
merge
(
"
#{
key
}
_eq"
:
"some thing"
)
end
search
=
collection
.
ransack
(
ransack_options
)
group_configs
=
search
.
base
.
values
.
map
do
|
ransack_condition
|
arel
=
ransack_condition
.
arel_predicate
group_key
=
[
arel
.
left
.
relation
.
name
,
arel
.
left
.
name
].
join
(
'.'
)
{
group_key:
group_key
,
ransack_key:
ransack_condition
.
key
,
}
end
group_keys
=
group_configs
.
map
{
|
config
|
config
[
:group_key
]
}
statistics
=
search
.
object
.
group
(
group_keys
).
count
process_statistics_key
=
proc
do
|
ary
,
i
|
result
=
ary
.
group_by
{
|
key
|
key
[
key
.
length
-
i
-
1
]
}
if
(
i
-
1
).
positive?
(
result
.
map
do
|
k
,
v
|
[
k
,
process_statistics_key
.
call
(
v
,
i
-
1
)]
end
).
to_h
else
result
end
end
# {
# nil=>
# {
# "戴华杰"=>[[nil, "戴华杰", "starting"], [nil, "戴华杰", "pending"], [nil, "戴华杰", "completed"]],
# nil=>[[nil, nil, "completed"], [nil, nil, "terminated"], [nil, nil, "starting"], [nil, nil, "pending"]],
# "张祥"=>[[nil, "张祥", "completed"]],
# },
# "A"=>{nil=>[["A", nil, "pending"]], "李龙贺"=>[["A", "李龙贺", "pending"]]},
# "C"=>{nil=>[["C", nil, "completed"]]},
# "B"=>{nil=>[["B", nil, "pending"]]}
# }
tree_result
=
process_statistics_key
.
call
(
statistics
.
keys
,
keys
.
size
-
1
)
data
=
tree_result_mount_data
(
tree_result
,
statistics
,
group_configs
)
render
json:
{
current_page:
1
,
total_pages:
1
,
total_count:
data
.
count
,
records:
data
},
status:
200
end
def
tree_result_mount_data
(
tree_result
,
statistics
,
group_configs
,
deep
=
0
)
tree_result
.
map
do
|
key
,
value
|
{
count:
tree_result_get_count_sum
(
value
,
statistics
),
children:
value
.
is_a?
(
Hash
)
?
tree_result_mount_data
(
value
,
statistics
,
group_configs
,
deep
+
1
)
:
[],
}.
merge
(
key
.
nil?
?
{
ransack_key:
group_configs
[
deep
][:
ransack_key
].
gsub
(
/_eq$/
,
'_null'
),
ransack_value:
true
,
}
:
{
ransack_key:
group_configs
[
deep
][
:ransack_key
],
ransack_value:
key
,
}
)
end
end
def
tree_result_get_count_sum
(
tree_result
,
statistics
)
if
tree_result
.
is_a?
(
Hash
)
tree_result
.
reduce
(
0
)
do
|
sum
,
(
k
,
v
)
|
sum
+
tree_result_get_count_sum
(
v
,
statistics
)
end
elsif
tree_result
.
is_a?
(
Array
)
tree_result
.
reduce
(
0
)
{
|
sum
,
key_ary
|
sum
+
statistics
[
key_ary
].
to_i
}
else
0
end
end
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