Class: Lesli::SystemController

Inherits:
ApplicationLesliRecord show all
Defined in:
app/models/lesli/system_controller.rb

Defined Under Namespace

Classes: Action

Class Method Summary collapse

Class Method Details

.index(matrix: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/lesli/system_controller.rb', line 37

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.route as route",
        "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], 
                route: c[:route], 
                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