Class: Lesli::ResourceService

Inherits:
ApplicationLesliService show all
Defined in:
app/services/lesli/resource_service.rb

Constant Summary collapse

DEVISE_CONTROLLERS =
[
    "users/registrations",
    "users/sessions",
    "users/passwords",
    "users/confirmations"
]

Instance Method Summary collapse

Methods inherited from ApplicationLesliService

#cache_key_for_account, #cache_key_for_user, #create, #delete, #error, #errors, #errors_as_sentence, #find, #found?, #list, #result, #show, #successful?, #update

Constructor Details

#initializeResourceService

Returns a new instance of ResourceService.



43
44
# File 'app/services/lesli/resource_service.rb', line 43

def initialize 
end

Instance Method Details

#buildObject

Scan new routes added and create role privileges



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/services/lesli/resource_service.rb', line 112

def build

    # get all the engines, controllers and actions
    engines = scan_for_engine_controllers

    # Register descriptors and privileges for all the accounts
    engines.each do |engine, controllers|

        controllers.each do |controller_route, controller_actions|

            # Build a strig with the standard name of a Rails controller from the standard routes
            # Examples: 
            #   users converts to Users 
            #   cloud_bell/notifications converts to CloudBell::Notifications
            # sometimes we need a second split to deal with third level deep of controllers
            # Example: "Account::Currency::ExchangeRatesController" from "account/currency/exchange_rates"
            identifier = controller_route
            .camelize
            .gsub('/', '::')

            name = identifier.demodulize.humanize

            controller = Lesli::Resource.find_or_initialize_by(route: controller_route)
            controller.assign_attributes(
                label: name,
                engine: engine,
                identifier: identifier
            )
            controller.save!

            controller_actions.each do |action_name|
                controller.children.find_or_create_by!(
                    action: action_name,
                    identifier: action_name,
                    label: action_name.humanize
                )
            end
        end
    end
end

#index(matrix: false) ⇒ Object



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
103
104
105
106
107
108
109
# File 'app/services/lesli/resource_service.rb', line 46

def index matrix:false

    # get a matrix of controllers and actions
    c = Resource.actions.joins(:parent).select(
        "parents_lesli_resources.engine as engine",
        "parents_lesli_resources.route as route",
        "parents_lesli_resources.identifier as controller",
        "parents_lesli_resources.label as controller_name",
        "parents_lesli_resources.id as controller_id",
        "lesli_resources.action as action",
        "lesli_resources.id as action_id",
        "case lesli_resources.action
            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("parents_lesli_resources.deleted_at is NULL")
    .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