Class: Lesli::ApplicationLesliService
- Inherits:
-
Object
- Object
- Lesli::ApplicationLesliService
- Defined in:
- app/services/lesli/application_lesli_service.rb
Direct Known Subclasses
Instance Method Summary collapse
- #cache_key_for_account(method = nil) ⇒ Object
- #cache_key_for_user(method = nil) ⇒ Object
-
#create(params) ⇒ Object
Standard method to create new resource into the database.
-
#delete ⇒ Object
Standard method to delete data from the database.
-
#error(error) ⇒ Object
Register a new error for the current service object.
-
#errors ⇒ Object
Get the list of erros.
-
#errors_as_sentence ⇒ Object
Get the list of erros as single string.
-
#find(resource = nil) ⇒ Object
Find an specific resource through the main id.
-
#found? ⇒ Boolean
Method to check if the service object has data available this method ment to be used together and after the find method.
-
#index(params = nil) ⇒ Object
Standard method to index data from the database.
-
#initialize(current_user = nil, query = {}) ⇒ ApplicationLesliService
constructor
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.
-
#list(params = nil) ⇒ Object
Standard method to list data from the database.
-
#result ⇒ Object
Return the resource as a result.
-
#show ⇒ Object
Standard method to show data from the database.
-
#successful? ⇒ Boolean
Check if the service object has errors.
-
#update(params) ⇒ Object
Standard method to update resources into the database.
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 cache_key_for_account(method=nil) cache_key_builder('account',current_user.account.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 |
#delete ⇒ Object
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 |
#errors ⇒ Object
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_sentence ⇒ Object
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
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 |
#result ⇒ Object
Return the resource as a result
147 148 149 |
# File 'app/services/lesli/application_lesli_service.rb', line 147 def result self.resource end |
#show ⇒ Object
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
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 |