service_record 1.0.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +26 -0
- data/Gemfile.lock +11 -11
- data/README.md +18 -9
- data/lib/service_record.rb +0 -1
- data/lib/service_record/base.rb +4 -6
- data/lib/service_record/version.rb +1 -1
- data/service_record.gemspec +1 -1
- metadata +7 -7
- data/.travis.yml +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fce5a993bb3878ab4264d9e3d95e5f103bc8fcddb3c79f9f0b0067676f5a1c0
|
4
|
+
data.tar.gz: 2bab1660dc7fba8727bc93b43c37c39d7d4d8017a9183c1316605de8db3e6f75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d2393518ed0c59f47d52515c265123a361094b87ce464635f243c6fedf42104c70acae4e86f6343620dbb2f74640b06c5427eda25fb2362a906798abbd6ed9d
|
7
|
+
data.tar.gz: cdca60c091b1f82f4e585e4ba4fa1dc2b7050e5606c863f9697eaf920a71a55ed999b607ebdfde3d33b5f119d08da74f52426f50c703a7fda72108df933b8da8
|
@@ -0,0 +1,26 @@
|
|
1
|
+
name: Continuous Integration
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ main ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ main ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
|
13
|
+
steps:
|
14
|
+
- name: Checkout
|
15
|
+
uses: actions/checkout@v2
|
16
|
+
|
17
|
+
- name: Set up Ruby
|
18
|
+
uses: ruby/setup-ruby@v1
|
19
|
+
with:
|
20
|
+
ruby-version: 2.7
|
21
|
+
|
22
|
+
- name: Install dependencies
|
23
|
+
run: bundle install
|
24
|
+
|
25
|
+
- name: Run tests
|
26
|
+
run: bundle exec rake
|
data/Gemfile.lock
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
service_record (1.
|
5
|
-
activemodel (
|
4
|
+
service_record (1.2.0)
|
5
|
+
activemodel (>= 6.0.3.1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (6.0.
|
11
|
-
activesupport (= 6.0.
|
12
|
-
activesupport (6.0.
|
10
|
+
activemodel (6.0.3.4)
|
11
|
+
activesupport (= 6.0.3.4)
|
12
|
+
activesupport (6.0.3.4)
|
13
13
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
14
|
i18n (>= 0.7, < 2)
|
15
15
|
minitest (~> 5.1)
|
16
16
|
tzinfo (~> 1.1)
|
17
|
-
zeitwerk (~> 2.2)
|
18
|
-
concurrent-ruby (1.1.
|
17
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
18
|
+
concurrent-ruby (1.1.7)
|
19
19
|
diff-lcs (1.3)
|
20
|
-
i18n (1.8.
|
20
|
+
i18n (1.8.5)
|
21
21
|
concurrent-ruby (~> 1.0)
|
22
|
-
minitest (5.14.
|
22
|
+
minitest (5.14.2)
|
23
23
|
rake (12.3.3)
|
24
24
|
rspec (3.9.0)
|
25
25
|
rspec-core (~> 3.9.0)
|
@@ -35,9 +35,9 @@ GEM
|
|
35
35
|
rspec-support (~> 3.9.0)
|
36
36
|
rspec-support (3.9.2)
|
37
37
|
thread_safe (0.3.6)
|
38
|
-
tzinfo (1.2.
|
38
|
+
tzinfo (1.2.7)
|
39
39
|
thread_safe (~> 0.1)
|
40
|
-
zeitwerk (2.
|
40
|
+
zeitwerk (2.4.0)
|
41
41
|
|
42
42
|
PLATFORMS
|
43
43
|
ruby
|
data/README.md
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# ServiceRecord
|
2
2
|
|
3
|
+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/uxxman/service_record/Continuous%20Integration)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/7634ecae285ff14e6bd6/maintainability)](https://codeclimate.com/github/uxxman/service_record/maintainability)
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/service_record.svg)](https://badge.fury.io/rb/service_record)
|
6
|
+
|
3
7
|
An ActiveRecord lookalike but for business model requirements, a.k.a Service Objects.
|
4
8
|
|
5
|
-
Rails is packed with
|
9
|
+
Rails is packed with amazing tools to get you started with building your new awesome project and enforces reliable and battle-tested guidelines. One of those guideline is "**thin controllers and fat models**", but sometimes (actually most of the time) its difficult to follow because most business requirements are not that simple like most CRUD operations.
|
6
10
|
|
7
11
|
Enters, ServiceRecord. Its similar to ActiveRecord models but their sole purpose is to perform a big/complex/muilt-step task without bloating the controllers or models.
|
8
12
|
|
@@ -133,14 +137,12 @@ end
|
|
133
137
|
|
134
138
|
## Validations
|
135
139
|
|
136
|
-
ServiceRecord extends on `ActiveModel::Validations`, so, everything that you can do there can be done inside a service class and ServiceRecord will make sure that a service only runs the perform function when all validations are passed, otherwise `errors` will contain details about the validation issues.
|
137
|
-
|
138
|
-
You can also define callbacks like `before_validation` and `after_validation` just like you do inside an ActiveRecord class.
|
140
|
+
ServiceRecord extends on `ActiveModel::Validations`, so, everything that you can do there can be done inside a service class and ServiceRecord will make sure that a service only runs the perform function when all validations are passed, otherwise `errors` will contain details about the validation issues.
|
139
141
|
|
140
142
|
|
141
143
|
## Custom Errors
|
142
144
|
|
143
|
-
Just like validation errors, you can also add custom errors that you want to be reported.
|
145
|
+
Just like validation errors, you can also add custom errors that you want to be reported. Use them to handle errors which are not related to input parameters validation. E.g.
|
144
146
|
|
145
147
|
```ruby
|
146
148
|
errors.add :authentication, 'invalid credentials'
|
@@ -158,12 +160,13 @@ class SampleService < ApplicationService
|
|
158
160
|
end
|
159
161
|
|
160
162
|
private
|
161
|
-
|
162
|
-
|
163
|
+
|
164
|
+
def do_something
|
165
|
+
end
|
163
166
|
end
|
164
167
|
```
|
165
168
|
|
166
|
-
Availble callbacks are `before_perform`, `after_perform` and `around_perform`. If a `before_perform` calls `throw :abort`, the callback chain is hallted and perform function
|
169
|
+
Availble callbacks are `before_perform`, `after_perform` and `around_perform`. If a `before_perform` calls `throw :abort`, the callback chain is hallted and perform function will not be called.
|
167
170
|
|
168
171
|
|
169
172
|
## Development
|
@@ -174,7 +177,13 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
174
177
|
|
175
178
|
## Contributing
|
176
179
|
|
177
|
-
|
180
|
+
1. Fork it
|
181
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
182
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
183
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
184
|
+
5. Create new Pull Request
|
185
|
+
|
186
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/uxxman/service_record.
|
178
187
|
|
179
188
|
|
180
189
|
## License
|
data/lib/service_record.rb
CHANGED
data/lib/service_record/base.rb
CHANGED
@@ -6,17 +6,15 @@ module ServiceRecord
|
|
6
6
|
include ActiveModel::Attributes
|
7
7
|
include ActiveModel::Validations
|
8
8
|
include ActiveModel::AttributeAssignment
|
9
|
-
include ActiveModel::Validations::Callbacks
|
10
9
|
|
11
10
|
def self.perform(args = {})
|
12
11
|
new.tap do |service|
|
13
12
|
service.attributes = args
|
13
|
+
break service unless service.valid?
|
14
14
|
|
15
|
-
|
16
|
-
service.
|
17
|
-
|
18
|
-
service.result = nil if service.failure?
|
19
|
-
end
|
15
|
+
service.run_callbacks :perform do
|
16
|
+
service.result = service.perform
|
17
|
+
service.result = nil if service.failure?
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
data/service_record.gemspec
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: service_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Muhammad Usman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 6.0.3.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 6.0.3.1
|
27
27
|
description: ActiveRecord lookalike but for business model requirements
|
28
28
|
email:
|
29
29
|
- uxman.sherwani@gmail.com
|
@@ -31,9 +31,9 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- ".github/workflows/ci.yml"
|
34
35
|
- ".gitignore"
|
35
36
|
- ".rspec"
|
36
|
-
- ".travis.yml"
|
37
37
|
- Gemfile
|
38
38
|
- Gemfile.lock
|
39
39
|
- LICENSE.txt
|