app/models/lesli/system_controller.rb in lesli-5.0.0 vs app/models/lesli/system_controller.rb in lesli-5.0.1
- old
+ new
@@ -31,7 +31,72 @@
=end
module Lesli
class SystemController < ApplicationLesliRecord
has_many :actions
+
+ def self.index matrix:false
+
+ # get a matrix of controllers and actions
+ c = SystemController.joins(:actions).select(
+ "lesli_system_controllers.engine as engine",
+ "lesli_system_controllers.reference as controller",
+ "lesli_system_controllers.name as controller_name",
+ "lesli_system_controllers.id as controller_id",
+ "lesli_system_controller_actions.name as action",
+ "lesli_system_controller_actions.id as action_id",
+ "case lesli_system_controller_actions.name
+ when 'index' then 1
+ when 'show' then 2
+ when 'new' then 3
+ when 'edit' then 4
+ when 'create' then 5
+ when 'update' then 6
+ when 'destroy' then 7
+ when 'options' then 8
+ else 9
+ end as importance
+ "
+ )
+ .where("lesli_system_controllers.deleted_at is NULL")
+ #.where("system_controller_actions.name in ('index', 'create', 'update', 'show', 'destroy')")
+ #.order("system_controllers.name, importance, system_controller_actions.name")
+ .order("importance DESC")
+
+ return c unless matrix
+
+ cc = {}
+
+ # convert the matrix to a hash of engines with controllers and available actions as values
+ # example:
+ # my_engine: { my_controller: [ my list of actions ]}
+ c.each do |c|
+
+ engine = c[:engine]
+ controller = c[:controller]
+
+ # create a uniq container for every action that belongs to a specific controller
+ if cc[engine].blank?
+ cc[engine] = {}
+ end
+
+ # create a uniq container for every action that belongs to a specific controller
+ if cc[engine][controller].blank?
+ cc[engine][controller] = {
+ id: c[:controller_id],
+ name: c[:controller_name],
+ actions: []
+ }
+ end
+
+ # push every action to his specic controller
+ cc[engine][controller][:actions].push({
+ id: c[:action_id],
+ action: c[:action]
+ })
+ end
+
+ return cc
+
+ end
end
end