Class: Lesli::ApplicationLesliService

Inherits:
Object
  • Object
show all
Defined in:
app/services/lesli/application_lesli_service.rb

Direct Known Subclasses

ResourceService, RoleService, UserService

Instance Method Summary collapse

Constructor Details

#initialize(current_user = nil, query = {}) ⇒ ApplicationLesliService

Service constructor current_user is always required to initialize a service object current user is used to get the data only from the account context



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/lesli/application_lesli_service.rb', line 48

def initialize current_user=nil, query={}

    # make the current user globaly available in the service object
    @current_user = current_user

    # stores the resources from the database as result of the active record queries
    @resource = nil

    # stores any error found during the life-cycle of the service object
    @failures = []

    # standard conditions to query the database
    @query = query
end

Instance Method Details

#cache_key_for_account(method = nil) ⇒ Object



151
152
153
# File 'app/services/lesli/application_lesli_service.rb', line 151

def (method=nil)
    cache_key_builder('account',current_user..id,method)
end

#cache_key_for_user(method = nil) ⇒ Object



155
156
157
# File 'app/services/lesli/application_lesli_service.rb', line 155

def cache_key_for_user(method=nil)
    cache_key_builder('user',current_user.id,method)
end

#create(params) ⇒ Object

Standard method to create new resource into the database



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/lesli/application_lesli_service.rb', line 91

def create params
    # Example:
    # user = current_user.account.users.new(params)

    # if user.save
    #     self.resource = user
    # else
    #     self.error(user.errors.full_messages.to_sentence)
    # end
    self.resource = params
    self
end

#deleteObject

Standard method to delete data from the database



111
112
# File 'app/services/lesli/application_lesli_service.rb', line 111

def delete
end

#error(error) ⇒ Object

Register a new error for the current service object



141
142
143
# File 'app/services/lesli/application_lesli_service.rb', line 141

def error error
    self.failures.push(error)
end

#errorsObject

Get the list of erros



129
130
131
# File 'app/services/lesli/application_lesli_service.rb', line 129

def errors
    self.failures
end

#errors_as_sentenceObject

Get the list of erros as single string



135
136
137
# File 'app/services/lesli/application_lesli_service.rb', line 135

def errors_as_sentence
    self.failures.to_sentence
end

#find(resource = nil) ⇒ Object

Find an specific resource through the main id



65
66
67
68
69
70
71
72
# File 'app/services/lesli/application_lesli_service.rb', line 65

def find resource = nil
    # Look for the resource in the database
    # self.resource = current_user.account.users.find_by_id(id) # example

    # Should always return self
    self.resource = resource if resource
    self
end

#found?Boolean

Method to check if the service object has data available this method ment to be used together and after the find method

Returns:

  • (Boolean)


117
118
119
# File 'app/services/lesli/application_lesli_service.rb', line 117

def found?
    !self.resource.blank?
end

#index(params = nil) ⇒ Object

Standard method to index data from the database



81
82
# File 'app/services/lesli/application_lesli_service.rb', line 81

def index params=nil
end

#list(params = nil) ⇒ Object

Standard method to list data from the database



76
77
# File 'app/services/lesli/application_lesli_service.rb', line 76

def list params=nil
end

#resultObject

Return the resource as a result



147
148
149
# File 'app/services/lesli/application_lesli_service.rb', line 147

def result
    self.resource
end

#showObject

Standard method to show data from the database



86
87
# File 'app/services/lesli/application_lesli_service.rb', line 86

def show
end

#successful?Boolean

Check if the service object has errors

Returns:

  • (Boolean)


123
124
125
# File 'app/services/lesli/application_lesli_service.rb', line 123

def successful?
    self.failures.empty?
end

#update(params) ⇒ Object

Standard method to update resources into the database



106
107
# File 'app/services/lesli/application_lesli_service.rb', line 106

def update params
end