Class: Lesli::ApplicationLesliService

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

Instance Method Summary collapse

Constructor Details

#initialize(current_user, 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



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

def initialize current_user, 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

#create(resource) ⇒ Object

Standard method to create new resource into the database



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

def create resource
    # 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 = resource if resource
    self
end

#deleteObject

Standard method to delete data from the database



113
114
# File 'app/services/lesli/application_lesli_service.rb', line 113

def delete
end

#error(error) ⇒ Object

Register a new error for the current service object



143
144
145
# File 'app/services/lesli/application_lesli_service.rb', line 143

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

#errorsObject

Get the list of erros



131
132
133
# File 'app/services/lesli/application_lesli_service.rb', line 131

def errors
    self.failures
end

#errors_as_sentenceObject

Get the list of erros as single string



137
138
139
# File 'app/services/lesli/application_lesli_service.rb', line 137

def errors_as_sentence
    self.failures.to_sentence
end

#find(resource = nil) ⇒ Object

Find an specific resource through the main id



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

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)


119
120
121
# File 'app/services/lesli/application_lesli_service.rb', line 119

def found?
    !self.resource.blank?
end

#index(params = nil) ⇒ Object

Standard method to index data from the database



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

def index params=nil
end

#list(params = nil) ⇒ Object

Standard method to list data from the database



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

def list params=nil
end

#resultObject

Return the resource as a result



149
150
151
# File 'app/services/lesli/application_lesli_service.rb', line 149

def result
    self.resource
end

#showObject

Standard method to show data from the database



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

def show
end

#successful?Boolean

Check if the service object has errors

Returns:

  • (Boolean)


125
126
127
# File 'app/services/lesli/application_lesli_service.rb', line 125

def successful?
    self.failures.empty?
end

#update(params) ⇒ Object

Standard method to update resources into the database



108
109
# File 'app/services/lesli/application_lesli_service.rb', line 108

def update params
end