rails-patterns 0.4.0 → 0.4.1
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 +4 -4
- data/README.md +9 -4
- data/VERSION +1 -1
- data/lib/patterns/form.rb +2 -2
- data/rails-patterns.gemspec +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0527dc116049d1579ba19deadf9ad643b0abf9b
|
4
|
+
data.tar.gz: 52f5f8cae780e21a3a91b7b82602d71814883a3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58005bd76bfb7d2c30d83025343765b201994e1b4161e0e7c04b37a54ed5e88a98180afde2d4c46f8e70e4dd51ad06c0733bd073d47a5647721d7dbda43f0b49
|
7
|
+
data.tar.gz: b461d4fa96a92acb04a56ccc202a41aa79cbdb2948a0c94f5a56ebc3bd228c277a5db17331c6e690a9bdade2b84fe4a6ae008e835c84556ede60b4a038f991b9
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ A collection of lightweight, standardized, rails-oriented patterns.
|
|
4
4
|
|
5
5
|
- [Query - complex querying on active record relation](#query)
|
6
6
|
- [Service - useful for handling processes involving multiple steps](#service)
|
7
|
-
- [Collection - when in need to add a method that relates to the collection
|
7
|
+
- [Collection - when in need to add a method that relates to the collection as whole](#collection)
|
8
8
|
- [Form - when you need a place for callbacks, want to replace strong parameters or handle virtual/composite resources](#form)
|
9
9
|
|
10
10
|
## Installation
|
@@ -82,7 +82,7 @@ end
|
|
82
82
|
### When to use it
|
83
83
|
|
84
84
|
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.
|
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).
|
86
86
|
|
87
87
|
### Assumptions and rules
|
88
88
|
|
@@ -93,6 +93,10 @@ Service objects are also useful for handling processes involving multiple steps.
|
|
93
93
|
* It is recommended for `#call` method to be the only public method of service object (besides state readers)
|
94
94
|
* It is recommended to name service object classes after commands (e.g. `ActivateUser` instead of `UserActivation`)
|
95
95
|
|
96
|
+
### Other
|
97
|
+
|
98
|
+
A bit higher level of abstraction is provided by [business_process gem](https://github.com/Selleo/business_process).
|
99
|
+
|
96
100
|
### Examples
|
97
101
|
|
98
102
|
#### Declaration
|
@@ -162,7 +166,7 @@ class CustomerEventsByTypeCollection < Patterns::Collection
|
|
162
166
|
subject.
|
163
167
|
events.
|
164
168
|
group_by(&:type).
|
165
|
-
transform_values{ |
|
169
|
+
transform_values{ |events| events.map{ |e| e.public_send(options.fetch(:label_method, "description")) }}
|
166
170
|
end
|
167
171
|
end
|
168
172
|
```
|
@@ -183,6 +187,7 @@ Form objects, just like service objects, are commonly used to mitigate problems
|
|
183
187
|
Form objects can also be used as replacement for `ActionController::StrongParameters` strategy, as all writable attributes are re-defined within each form.
|
184
188
|
Finally form objects can be used as wrappers for virtual (with no model representation) or composite (saving multiple models at once) resources.
|
185
189
|
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).
|
186
191
|
|
187
192
|
### Assumptions and rules
|
188
193
|
|
@@ -191,7 +196,7 @@ In the latter case this may act as replacement for `ActiveRecord::NestedAttribut
|
|
191
196
|
* Forms can be initialized using `.new`.
|
192
197
|
* Forms accept optional resource object as first constructor argument.
|
193
198
|
* Forms accept optional attributes hash as latter constructor argument.
|
194
|
-
*
|
199
|
+
* Forms have to implement `#persist` method that returns falsey (if failed) or truthy (if succeeded) value.
|
195
200
|
* Forms provide access to first constructor argument using `#resource`.
|
196
201
|
* Forms are saved using their `#save` or `#save!` methods.
|
197
202
|
* Forms will attempt to pre-populate their fields using `resource#attributes` and public getters for `resource`
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/lib/patterns/form.rb
CHANGED
@@ -50,7 +50,7 @@ module Patterns
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def persisted?
|
53
|
-
if resource
|
53
|
+
if resource.present? && resource.respond_to?(:persisted?)
|
54
54
|
resource.persisted?
|
55
55
|
else
|
56
56
|
false
|
@@ -77,7 +77,7 @@ module Patterns
|
|
77
77
|
|
78
78
|
def param_key
|
79
79
|
param_key = self.class.param_key
|
80
|
-
param_key ||= resource
|
80
|
+
param_key ||= resource.present? && resource.respond_to?(:model_name) && resource.model_name.param_key
|
81
81
|
raise NoParamKey if param_key.blank?
|
82
82
|
param_key
|
83
83
|
end
|
data/rails-patterns.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: rails-patterns 0.4.
|
5
|
+
# stub: rails-patterns 0.4.1 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "rails-patterns".freeze
|
9
|
-
s.version = "0.4.
|
9
|
+
s.version = "0.4.1"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
13
13
|
s.authors = ["Stevo".freeze]
|
14
|
-
s.date = "2017-
|
14
|
+
s.date = "2017-05-09"
|
15
15
|
s.description = "A collection of lightweight, standardized, rails-oriented patterns.".freeze
|
16
16
|
s.email = "b.kosmowski@selleo.com".freeze
|
17
17
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-patterns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stevo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|