rails-patterns 0.4.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +33 -0
- data/Gemfile +5 -2
- data/Gemfile.lock +103 -83
- data/README.md +165 -4
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/patterns/calculation.rb +59 -0
- data/lib/patterns/form.rb +26 -9
- data/lib/patterns/query.rb +20 -15
- data/lib/patterns/rule.rb +25 -0
- data/lib/patterns/ruleset.rb +69 -0
- data/lib/patterns/service.rb +10 -6
- data/lib/patterns/strong_ruleset.rb +19 -0
- data/lib/rails-patterns.rb +4 -0
- data/rails-patterns.gemspec +26 -10
- data/spec/helpers/custom_calculation.rb +16 -0
- data/spec/helpers/custom_calculation_script.rb +4 -0
- data/spec/helpers/rails_redis_cache_mock.rb +5 -0
- data/spec/patterns/calculation_spec.rb +212 -0
- data/spec/patterns/form_spec.rb +96 -50
- data/spec/patterns/rule_spec.rb +44 -0
- data/spec/patterns/ruleset_spec.rb +260 -0
- data/spec/patterns/service_spec.rb +16 -1
- data/spec/patterns/strong_ruleset_spec.rb +79 -0
- data/spec/spec_helper.rb +6 -1
- metadata +39 -14
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f61449cb2feed58c4d3636c7e8cb06b9d6f93f7192861eb5c4567e114054610f
|
|
4
|
+
data.tar.gz: '08e88e618593983f3fd0b82af0f18b42792e4413a40c2711befffcf06be29106'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0aac7fae367e8d81c4340f36680671f5ed4f292f72c956f8d1f3b28e9af87cb2c5d3257996be507af299dc0f27e45e07a848d7d3492b9c3f3c8a0a1a8278a1be
|
|
7
|
+
data.tar.gz: 2279865d364019b49ed9621eb325459f70c7a5f63d873e147ec16700d81248fcc6dbc5108e27cf919e554d4a5fc23a7c710fece598b5255223ce3fe838843c82
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
2
|
+
# They are provided by a third-party and are governed by
|
|
3
|
+
# separate terms of service, privacy policy, and support
|
|
4
|
+
# documentation.
|
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
|
7
|
+
|
|
8
|
+
name: Ruby
|
|
9
|
+
|
|
10
|
+
on:
|
|
11
|
+
push:
|
|
12
|
+
branches: [ master ]
|
|
13
|
+
pull_request:
|
|
14
|
+
branches: [ master ]
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
test:
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
strategy:
|
|
20
|
+
fail-fast: false
|
|
21
|
+
matrix:
|
|
22
|
+
ruby: [ '2.5', '2.6', '2.7' ]
|
|
23
|
+
name: RSpec for Ruby version ${{ matrix.ruby }}
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v2
|
|
26
|
+
- uses: supercharge/redis-github-action@1.1.0
|
|
27
|
+
- name: Set up Ruby
|
|
28
|
+
uses: actions/setup-ruby@v1
|
|
29
|
+
with:
|
|
30
|
+
ruby-version: ${{ matrix.ruby }}
|
|
31
|
+
- run: gem install bundler
|
|
32
|
+
- run: bundle install
|
|
33
|
+
- run: bundle exec rspec
|
data/Gemfile
CHANGED
|
@@ -3,16 +3,19 @@ source "https://rubygems.org"
|
|
|
3
3
|
gem "activerecord", ">= 4.2.6"
|
|
4
4
|
gem "actionpack", ">= 4.2.6"
|
|
5
5
|
gem "virtus"
|
|
6
|
+
gem "ruby2_keywords"
|
|
6
7
|
|
|
7
8
|
# Add dependencies to develop your gem here.
|
|
8
9
|
# Include everything needed to run rake, tests, features, etc.
|
|
9
10
|
|
|
10
11
|
group :development do
|
|
11
12
|
gem "rspec"
|
|
12
|
-
gem "bundler", "~>
|
|
13
|
-
gem "juwelier"
|
|
13
|
+
gem "bundler", "~> 2.0"
|
|
14
|
+
gem "juwelier"
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
group "test" do
|
|
17
18
|
gem "pry-rails"
|
|
19
|
+
gem "rspec_junit_formatter"
|
|
20
|
+
gem "redis"
|
|
18
21
|
end
|
data/Gemfile.lock
CHANGED
|
@@ -1,127 +1,144 @@
|
|
|
1
1
|
GEM
|
|
2
2
|
remote: https://rubygems.org/
|
|
3
3
|
specs:
|
|
4
|
-
actionpack (
|
|
5
|
-
actionview (=
|
|
6
|
-
activesupport (=
|
|
7
|
-
rack (~> 2.0)
|
|
8
|
-
rack-test (
|
|
4
|
+
actionpack (6.0.3.2)
|
|
5
|
+
actionview (= 6.0.3.2)
|
|
6
|
+
activesupport (= 6.0.3.2)
|
|
7
|
+
rack (~> 2.0, >= 2.0.8)
|
|
8
|
+
rack-test (>= 0.6.3)
|
|
9
9
|
rails-dom-testing (~> 2.0)
|
|
10
|
-
rails-html-sanitizer (~> 1.0, >= 1.0
|
|
11
|
-
actionview (
|
|
12
|
-
activesupport (=
|
|
10
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
11
|
+
actionview (6.0.3.2)
|
|
12
|
+
activesupport (= 6.0.3.2)
|
|
13
13
|
builder (~> 3.1)
|
|
14
|
-
|
|
14
|
+
erubi (~> 1.4)
|
|
15
15
|
rails-dom-testing (~> 2.0)
|
|
16
|
-
rails-html-sanitizer (~> 1.
|
|
17
|
-
activemodel (
|
|
18
|
-
activesupport (=
|
|
19
|
-
activerecord (
|
|
20
|
-
activemodel (=
|
|
21
|
-
activesupport (=
|
|
22
|
-
|
|
23
|
-
activesupport (5.0.2)
|
|
16
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
17
|
+
activemodel (6.0.3.2)
|
|
18
|
+
activesupport (= 6.0.3.2)
|
|
19
|
+
activerecord (6.0.3.2)
|
|
20
|
+
activemodel (= 6.0.3.2)
|
|
21
|
+
activesupport (= 6.0.3.2)
|
|
22
|
+
activesupport (6.0.3.2)
|
|
24
23
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
25
|
-
i18n (
|
|
24
|
+
i18n (>= 0.7, < 2)
|
|
26
25
|
minitest (~> 5.1)
|
|
27
26
|
tzinfo (~> 1.1)
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
|
28
|
+
addressable (2.7.0)
|
|
29
|
+
public_suffix (>= 2.0.2, < 5.0)
|
|
30
30
|
axiom-types (0.1.1)
|
|
31
31
|
descendants_tracker (~> 0.0.4)
|
|
32
32
|
ice_nine (~> 0.11.0)
|
|
33
33
|
thread_safe (~> 0.3, >= 0.3.1)
|
|
34
|
-
builder (3.2.
|
|
35
|
-
coderay (1.1.
|
|
34
|
+
builder (3.2.4)
|
|
35
|
+
coderay (1.1.2)
|
|
36
36
|
coercible (1.0.0)
|
|
37
37
|
descendants_tracker (~> 0.0.1)
|
|
38
|
-
concurrent-ruby (1.
|
|
38
|
+
concurrent-ruby (1.1.6)
|
|
39
|
+
crass (1.0.6)
|
|
39
40
|
descendants_tracker (0.0.4)
|
|
40
41
|
thread_safe (~> 0.3, >= 0.3.1)
|
|
41
42
|
diff-lcs (1.3)
|
|
42
43
|
equalizer (0.0.11)
|
|
43
|
-
|
|
44
|
-
faraday (
|
|
44
|
+
erubi (1.9.0)
|
|
45
|
+
faraday (1.3.0)
|
|
46
|
+
faraday-net_http (~> 1.0)
|
|
45
47
|
multipart-post (>= 1.2, < 3)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
ruby2_keywords
|
|
49
|
+
faraday-net_http (1.0.1)
|
|
50
|
+
git (1.8.1)
|
|
51
|
+
rchardet (~> 1.8)
|
|
52
|
+
github_api (0.19.0)
|
|
53
|
+
addressable (~> 2.4)
|
|
49
54
|
descendants_tracker (~> 0.0.4)
|
|
50
|
-
faraday (
|
|
51
|
-
hashie (>= 3.
|
|
52
|
-
mime-types (>= 1.16, < 3.0)
|
|
55
|
+
faraday (>= 0.8, < 2)
|
|
56
|
+
hashie (~> 3.5, >= 3.5.2)
|
|
53
57
|
oauth2 (~> 1.0)
|
|
54
|
-
hashie (3.
|
|
55
|
-
highline (
|
|
56
|
-
i18n (
|
|
58
|
+
hashie (3.6.0)
|
|
59
|
+
highline (2.0.3)
|
|
60
|
+
i18n (1.8.3)
|
|
61
|
+
concurrent-ruby (~> 1.0)
|
|
57
62
|
ice_nine (0.11.2)
|
|
58
|
-
juwelier (2.
|
|
63
|
+
juwelier (2.4.9)
|
|
59
64
|
builder
|
|
60
|
-
bundler
|
|
61
|
-
git
|
|
65
|
+
bundler
|
|
66
|
+
git
|
|
62
67
|
github_api
|
|
63
|
-
highline
|
|
64
|
-
|
|
68
|
+
highline
|
|
69
|
+
kamelcase (~> 0)
|
|
70
|
+
nokogiri
|
|
71
|
+
psych
|
|
65
72
|
rake
|
|
66
73
|
rdoc
|
|
67
|
-
|
|
68
|
-
jwt (
|
|
69
|
-
|
|
74
|
+
semver2
|
|
75
|
+
jwt (2.2.3)
|
|
76
|
+
kamelcase (0.0.2)
|
|
77
|
+
semver2 (~> 3)
|
|
78
|
+
loofah (2.6.0)
|
|
79
|
+
crass (~> 1.0.2)
|
|
70
80
|
nokogiri (>= 1.5.9)
|
|
71
|
-
method_source (0.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
multi_json (1.12.1)
|
|
81
|
+
method_source (0.9.0)
|
|
82
|
+
mini_portile2 (2.5.3)
|
|
83
|
+
minitest (5.14.1)
|
|
84
|
+
multi_json (1.15.0)
|
|
76
85
|
multi_xml (0.6.0)
|
|
77
|
-
multipart-post (2.
|
|
78
|
-
nokogiri (1.7
|
|
79
|
-
mini_portile2 (~> 2.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
86
|
+
multipart-post (2.1.1)
|
|
87
|
+
nokogiri (1.11.7)
|
|
88
|
+
mini_portile2 (~> 2.5.0)
|
|
89
|
+
racc (~> 1.4)
|
|
90
|
+
oauth2 (1.4.7)
|
|
91
|
+
faraday (>= 0.8, < 2.0)
|
|
92
|
+
jwt (>= 1.0, < 3.0)
|
|
83
93
|
multi_json (~> 1.3)
|
|
84
94
|
multi_xml (~> 0.5)
|
|
85
95
|
rack (>= 1.2, < 3)
|
|
86
|
-
pry (0.
|
|
96
|
+
pry (0.11.3)
|
|
87
97
|
coderay (~> 1.1.0)
|
|
88
|
-
method_source (~> 0.
|
|
89
|
-
slop (~> 3.4)
|
|
98
|
+
method_source (~> 0.9.0)
|
|
90
99
|
pry-rails (0.3.6)
|
|
91
100
|
pry (>= 0.10.4)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
rails-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
101
|
+
psych (4.0.1)
|
|
102
|
+
public_suffix (4.0.6)
|
|
103
|
+
racc (1.5.2)
|
|
104
|
+
rack (2.2.3)
|
|
105
|
+
rack-test (1.1.0)
|
|
106
|
+
rack (>= 1.0, < 3)
|
|
107
|
+
rails-dom-testing (2.0.3)
|
|
108
|
+
activesupport (>= 4.2.0)
|
|
109
|
+
nokogiri (>= 1.6)
|
|
110
|
+
rails-html-sanitizer (1.3.0)
|
|
111
|
+
loofah (~> 2.3)
|
|
112
|
+
rake (13.0.3)
|
|
113
|
+
rchardet (1.8.0)
|
|
114
|
+
rdoc (6.3.1)
|
|
115
|
+
redis (4.1.4)
|
|
116
|
+
rspec (3.7.0)
|
|
117
|
+
rspec-core (~> 3.7.0)
|
|
118
|
+
rspec-expectations (~> 3.7.0)
|
|
119
|
+
rspec-mocks (~> 3.7.0)
|
|
120
|
+
rspec-core (3.7.1)
|
|
121
|
+
rspec-support (~> 3.7.0)
|
|
122
|
+
rspec-expectations (3.7.0)
|
|
109
123
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
110
|
-
rspec-support (~> 3.
|
|
111
|
-
rspec-mocks (3.
|
|
124
|
+
rspec-support (~> 3.7.0)
|
|
125
|
+
rspec-mocks (3.7.0)
|
|
112
126
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
113
|
-
rspec-support (~> 3.
|
|
114
|
-
rspec-support (3.
|
|
115
|
-
|
|
116
|
-
|
|
127
|
+
rspec-support (~> 3.7.0)
|
|
128
|
+
rspec-support (3.7.1)
|
|
129
|
+
rspec_junit_formatter (0.3.0)
|
|
130
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
|
131
|
+
ruby2_keywords (0.0.2)
|
|
132
|
+
semver2 (3.4.2)
|
|
117
133
|
thread_safe (0.3.6)
|
|
118
|
-
tzinfo (1.2.
|
|
134
|
+
tzinfo (1.2.7)
|
|
119
135
|
thread_safe (~> 0.1)
|
|
120
136
|
virtus (1.0.5)
|
|
121
137
|
axiom-types (~> 0.1)
|
|
122
138
|
coercible (~> 1.0)
|
|
123
139
|
descendants_tracker (~> 0.0, >= 0.0.3)
|
|
124
140
|
equalizer (~> 0.0, >= 0.0.9)
|
|
141
|
+
zeitwerk (2.3.0)
|
|
125
142
|
|
|
126
143
|
PLATFORMS
|
|
127
144
|
ruby
|
|
@@ -129,11 +146,14 @@ PLATFORMS
|
|
|
129
146
|
DEPENDENCIES
|
|
130
147
|
actionpack (>= 4.2.6)
|
|
131
148
|
activerecord (>= 4.2.6)
|
|
132
|
-
bundler (~>
|
|
133
|
-
juwelier
|
|
149
|
+
bundler (~> 2.0)
|
|
150
|
+
juwelier
|
|
134
151
|
pry-rails
|
|
152
|
+
redis
|
|
135
153
|
rspec
|
|
154
|
+
rspec_junit_formatter
|
|
155
|
+
ruby2_keywords
|
|
136
156
|
virtus
|
|
137
157
|
|
|
138
158
|
BUNDLED WITH
|
|
139
|
-
1.
|
|
159
|
+
2.1.4
|
data/README.md
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
1
3
|
# Pattern
|
|
2
4
|
|
|
3
|
-
A collection of lightweight, standardized, rails-oriented patterns.
|
|
5
|
+
A collection of lightweight, standardized, rails-oriented patterns used by [RubyOnRails Developers @ Selleo](https://selleo.com/ruby-on-rails)
|
|
4
6
|
|
|
5
7
|
- [Query - complex querying on active record relation](#query)
|
|
6
8
|
- [Service - useful for handling processes involving multiple steps](#service)
|
|
7
9
|
- [Collection - when in need to add a method that relates to the collection as whole](#collection)
|
|
8
10
|
- [Form - when you need a place for callbacks, want to replace strong parameters or handle virtual/composite resources](#form)
|
|
11
|
+
- [Calculation - when you need a place for calculating a simple value (numeric, array, hash) and/or cache it](#calculation)
|
|
12
|
+
- [Rule and Ruleset - when you need a place for conditional logic](#rule-and-ruleset)
|
|
9
13
|
|
|
10
14
|
## Installation
|
|
11
15
|
|
|
@@ -27,6 +31,7 @@ One should consider using query objects pattern when in need to perform complex
|
|
|
27
31
|
Usually one should avoid using scopes for such purpose.
|
|
28
32
|
As a rule of thumb, if scope interacts with more than one column and/or joins in other tables, it should be moved to query object.
|
|
29
33
|
Also whenever a chain of scopes is to be used, one should consider using query object too.
|
|
34
|
+
Some more information on using query objects can be found in [this article](https://medium.com/@blazejkosmowski/essential-rubyonrails-patterns-part-2-query-objects-4b253f4f4539).
|
|
30
35
|
|
|
31
36
|
### Assumptions and rules
|
|
32
37
|
|
|
@@ -73,7 +78,7 @@ RecentlyActivatedUsersQuery.call(date_range: Date.today.beginning_of_day..Date.t
|
|
|
73
78
|
RecentlyActivatedUsersQuery.call(User.without_test_users, date_range: Date.today.beginning_of_day..Date.today.end_of_day)
|
|
74
79
|
|
|
75
80
|
class User < ApplicationRecord
|
|
76
|
-
scope :
|
|
81
|
+
scope :recently_activated, RecentlyActivatedUsersQuery
|
|
77
82
|
end
|
|
78
83
|
```
|
|
79
84
|
|
|
@@ -82,7 +87,7 @@ end
|
|
|
82
87
|
### When to use it
|
|
83
88
|
|
|
84
89
|
Service objects are commonly used to mitigate problems with model callbacks that interact with external classes ([read more...](http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/)).
|
|
85
|
-
Service objects are also useful for handling processes involving multiple steps. E.g. a controller that performs more than one operation on its subject (usually a model instance) is a possible candidate for Extract ServiceObject (or Extract FormObject) refactoring. In many cases service object can be used as scaffolding for [replace method with object refactoring](https://sourcemaking.com/refactoring/replace-method-with-method-object).
|
|
90
|
+
Service objects are also useful for handling processes involving multiple steps. E.g. a controller that performs more than one operation on its subject (usually a model instance) is a possible candidate for Extract ServiceObject (or Extract FormObject) refactoring. In many cases service object can be used as scaffolding for [replace method with object refactoring](https://sourcemaking.com/refactoring/replace-method-with-method-object). Some more information on using services can be found in [this article](https://medium.com/selleo/essential-rubyonrails-patterns-part-1-service-objects-1af9f9573ca1).
|
|
86
91
|
|
|
87
92
|
### Assumptions and rules
|
|
88
93
|
|
|
@@ -187,7 +192,7 @@ Form objects, just like service objects, are commonly used to mitigate problems
|
|
|
187
192
|
Form objects can also be used as replacement for `ActionController::StrongParameters` strategy, as all writable attributes are re-defined within each form.
|
|
188
193
|
Finally form objects can be used as wrappers for virtual (with no model representation) or composite (saving multiple models at once) resources.
|
|
189
194
|
In the latter case this may act as replacement for `ActiveRecord::NestedAttributes`.
|
|
190
|
-
In some cases FormObject can be used as scaffolding for [replace method with object refactoring](https://sourcemaking.com/refactoring/replace-method-with-method-object).
|
|
195
|
+
In some cases FormObject can be used as scaffolding for [replace method with object refactoring](https://sourcemaking.com/refactoring/replace-method-with-method-object). Some more information on using form objects can be found in [this article](https://medium.com/selleo/essential-rubyonrails-patterns-form-objects-b199aada6ec9).
|
|
191
196
|
|
|
192
197
|
### Assumptions and rules
|
|
193
198
|
|
|
@@ -274,6 +279,162 @@ ReportConfigurationForm.new
|
|
|
274
279
|
ReportConfigurationForm.new({ include_extra_data: true, dump_as_csv: true })
|
|
275
280
|
```
|
|
276
281
|
|
|
282
|
+
## Calculation
|
|
283
|
+
|
|
284
|
+
### When to use it
|
|
285
|
+
|
|
286
|
+
Calculation objects provide a place to calculate simple values (i.e. numeric, arrays, hashes), especially when calculations require interacting with multiple classes, and thus do not fit into any particular one.
|
|
287
|
+
Calculation objects also provide simple abstraction for caching their results.
|
|
288
|
+
|
|
289
|
+
### Assumptions and rules
|
|
290
|
+
|
|
291
|
+
* Calculations have to implement `#result` method that returns any value (result of calculation).
|
|
292
|
+
* Calculations do provide `.set_cache_expiry_every` method, that allows defining caching period.
|
|
293
|
+
* When `.set_cache_expiry_every` is not used, result is not being cached.
|
|
294
|
+
* Calculations return result by calling any of following methods: `.calculate`, `.result_for` or `.result`.
|
|
295
|
+
* First argument passed to calculation is accessible by `#subject` private method.
|
|
296
|
+
* Arguments hash passed to calculation is accessible by `#options` private method.
|
|
297
|
+
* Caching takes into account arguments passed when building cache key.
|
|
298
|
+
* To build cache key, `#cache_key` of each argument value is used if possible.
|
|
299
|
+
* By default `Rails.cache` is used as cache store.
|
|
300
|
+
|
|
301
|
+
### Examples
|
|
302
|
+
|
|
303
|
+
#### Declaration
|
|
304
|
+
|
|
305
|
+
```ruby
|
|
306
|
+
class AverageHotelDailyRevenue < Patterns::Calculation
|
|
307
|
+
set_cache_expiry_every 1.day
|
|
308
|
+
|
|
309
|
+
private
|
|
310
|
+
|
|
311
|
+
def result
|
|
312
|
+
reservations.sum(:price) / days_in_year
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def reservations
|
|
316
|
+
Reservation.where(
|
|
317
|
+
date: (beginning_of_year..end_of_year),
|
|
318
|
+
hotel_id: subject.id
|
|
319
|
+
)
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def days_in_year
|
|
323
|
+
end_of_year.yday
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def year
|
|
327
|
+
options.fetch(:year, Date.current.year)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def beginning_of_year
|
|
331
|
+
Date.new(year).beginning_of_year
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def end_of_year
|
|
335
|
+
Date.new(year).end_of_year
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
#### Usage
|
|
341
|
+
|
|
342
|
+
```ruby
|
|
343
|
+
hotel = Hotel.find(123)
|
|
344
|
+
AverageHotelDailyRevenue.result_for(hotel)
|
|
345
|
+
AverageHotelDailyRevenue.result_for(hotel, year: 2015)
|
|
346
|
+
|
|
347
|
+
TotalCurrentRevenue.calculate
|
|
348
|
+
AverageDailyRevenue.result
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
## Rule and Ruleset
|
|
352
|
+
|
|
353
|
+
### When to use it
|
|
354
|
+
|
|
355
|
+
Rule objects provide a place for dislocating/extracting conditional logic.
|
|
356
|
+
|
|
357
|
+
Use it when:
|
|
358
|
+
- given complex condition is duplicated in multiple places in your codebase
|
|
359
|
+
- part of condition logic can be reused in some other place
|
|
360
|
+
- there is a need to instantiate condition itself for some reason (i.e. to represent it in the interface)
|
|
361
|
+
- responsibility of your class is blurred by complex conditional logic, and as a result...
|
|
362
|
+
- ...tests for your class require multiple condition branches / nested contexts
|
|
363
|
+
|
|
364
|
+
### Assumptions and rules
|
|
365
|
+
|
|
366
|
+
* Rule has `#satisfied?`, `#applicable?`, `#not_applicable?` and `#forceable?` methods available.
|
|
367
|
+
* Rule has to implement at least `#satisfied?` method. `#not_applicable?` and `#forceable?` are meant to be overridable.
|
|
368
|
+
* `#forceable?` makes sense in scenario where condition is capable of being force-satisfied regardless if its actually satisfied or not. Is `true` by default.
|
|
369
|
+
* Override `#not_applicable?` when method is applicable only under some specific conditions. Is `false` by default.
|
|
370
|
+
* Rule requires a subject as first argument.
|
|
371
|
+
* Multiple rules and rulesets can be combined into new ruleset as both share same interface and can be used interchangeably (composite pattern).
|
|
372
|
+
|
|
373
|
+
#### Forcing rules
|
|
374
|
+
|
|
375
|
+
On some occasions there is a situation in which some condition should be overridable.
|
|
376
|
+
Let's say we may want send shipping notification even though given order was not paid for and under regular circumstances such notification should not be sent.
|
|
377
|
+
In this case, while regular logic with some automated process would not trigger delivery, an action triggered by user from UI could do it, by passing `force: true` option to `#satisified?` methods.
|
|
378
|
+
|
|
379
|
+
It might be good idea to test for `#forceable?` on the UI level to control visibility of such link/button.
|
|
380
|
+
|
|
381
|
+
Overriding `#forceable` can be useful to prevent some edge cases, i.e. `ContactInformationProvidedRule` might check if customer for given order has provided any contact means by which a notification could be delivered.
|
|
382
|
+
If not, ruleset containing such rule (and the rule itself) would not be "forceable" and UI could reflect that by querying `#forceable?`.
|
|
383
|
+
|
|
384
|
+
#### Regular and strong rulesets
|
|
385
|
+
|
|
386
|
+
While regular `Ruleset` can be satisfied or forced if any of its rules in not applicable, the
|
|
387
|
+
`StrongRuleset` is not satisfied and not "forceable" if any of its rules is not applicable.
|
|
388
|
+
|
|
389
|
+
#### `#not_applicable?` vs `#applicable?`
|
|
390
|
+
|
|
391
|
+
It might be surprising that is is the negated version of the `#applicable?` predicate methods that is overridable.
|
|
392
|
+
However, from the actual usage perspective, it usually easier to conceptually define when condition makes no sense than other way around.
|
|
393
|
+
|
|
394
|
+
### Examples
|
|
395
|
+
|
|
396
|
+
#### Declaration
|
|
397
|
+
|
|
398
|
+
```ruby
|
|
399
|
+
class OrderIsSentRule < Rule
|
|
400
|
+
def satisfied?
|
|
401
|
+
subject.sent?
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
class OrderIsPaidRule < Rule
|
|
406
|
+
def satisfied?
|
|
407
|
+
subject.paid?
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def forceable?
|
|
411
|
+
true
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
OrderCompletedNotificationRuleset = Class.new(Ruleset)
|
|
416
|
+
OrderCompletedNotificationRuleset.
|
|
417
|
+
add_rule(:order_is_sent_rule).
|
|
418
|
+
add_rule(:order_is_paid_rule)
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
#### Usage
|
|
422
|
+
|
|
423
|
+
```ruby
|
|
424
|
+
OrderIsPaidRule.new(order).satisfied?
|
|
425
|
+
OrderCompletedNotificationRuleset.new(order).satisfied?
|
|
426
|
+
|
|
427
|
+
ResendOrderNotification.call(order) if OrderCompletedNotificationRuleset.new(order).satisfied?(force: true)
|
|
428
|
+
```
|
|
429
|
+
|
|
277
430
|
## Further reading
|
|
278
431
|
|
|
279
432
|
* [7 ways to decompose fat active record models](http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/)
|
|
433
|
+
|
|
434
|
+
## About Selleo
|
|
435
|
+
|
|
436
|
+

|
|
437
|
+
|
|
438
|
+
Software development teams with an entrepreneurial sense of ownership at their core delivering great digital products and building culture people want to belong to. We are a community of engaged co-workers passionate about crafting impactful web solutions which transform the way our clients do business.
|
|
439
|
+
|
|
440
|
+
All names and logos for [Selleo](https://selleo.com/about) are trademark of Selleo sp. z o. o. sp. k.
|