Class: LesliVault::DescriptorService

Inherits:
Lesli::ApplicationLesliService
  • Object
show all
Defined in:
app/services/lesli_vault/descriptor_service.rb

Instance Method Summary collapse

Instance Method Details

#create(params) ⇒ Object

Parameters:

  • Hash (params)

    of the permitted attributes for a descriptor

Returns:

  • (Object)


110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/services/lesli_vault/descriptor_service.rb', line 110

def create params
    descriptor = current_user..descriptors.new(params)

    if descriptor.save
        self.resource = descriptor
        # TODO: keep track of the activities
    else
        self.error(descriptor.errors.full_messages.to_sentence)
    end

    self
end

#destroyObject

Returns:

  • (Object)


141
142
143
144
145
146
147
148
149
150
# File 'app/services/lesli_vault/descriptor_service.rb', line 141

def destroy

    # TODO: keep track of the activities

    unless self.resource.destroy
        self.error(self.resource.errors.full_messages.to_sentence)
    end

    self
end

#find(id) ⇒ Object

Returns:

  • (Object)


40
41
42
43
# File 'app/services/lesli_vault/descriptor_service.rb', line 40

def find id
    self.resource = self.current_user..descriptors.find_by_id(id)
    self
end

#indexArray

Returns Paginated index of users.

Returns:

  • (Array)

    Paginated index of users.



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
# File 'app/services/lesli_vault/descriptor_service.rb', line 48

def index

    descriptors = current_user..descriptors.where.not(
        :name => ["owner"]
    )
    
    # Count the amount of privileges assigned to every descriptor
    descriptors = descriptors.joins(%(
        left join  (
            select
                count(1) as total,
                descriptor_id
            from lesli_descriptor_privileges
            --where apga.status = TRUE
            group by descriptor_id
        ) as actions
        on actions.descriptor_id = lesli_descriptors.id
    ))

    descriptors = descriptors.select(
        :id,
        :name,
        :method,
        "coalesce(actions.total, 0) as privileges_count",
        Date2.new.date_time.db_timestamps("lesli_descriptors")
    )
    
    # skip native descriptors
    #descriptors = descriptors.where.not("descriptors.name in (?)", ["owner", "sysadmin", "profile"])
    
    # Filter results by search string
    # unless search_string.blank?
    #     descriptors = descriptors.where("(LOWER(descriptors.name) SIMILAR TO ?)", search_string)
    # end
    
    return descriptors
    .page(query[:pagination][:page])
    .per(query[:pagination][:perPage])
    .order("#{query[:order][:by]} #{query[:order][:dir]} NULLS LAST")
end

#showHash

Returns:

  • (Hash)


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/services/lesli_vault/descriptor_service.rb', line 92

def show 
    { 
        :id => resource.id,
        :name => resource.name,
        :category => resource.category,
        :privileges => resource.privileges.joins(system_controller_action: :system_controller).select(
            "descriptor_privileges.id",
            "system_controllers.name as controlle_name",
            "system_controller_actions.name as action_name",
            "descriptor_privileges.created_at"
        )
    }
end

#update(params) ⇒ Object

Parameters:

  • Hash (params)

    of the permitted attributes for a descriptor

Returns:

  • (Object)


127
128
129
130
131
132
133
134
135
136
# File 'app/services/lesli_vault/descriptor_service.rb', line 127

def update params

    # TODO: keep track of the activities

    unless self.resource.update(params)
        self.error(self.resource.errors.full_messages.to_sentence)
    end

    self
end