decorator_dryer 0.1.0 → 0.1.2
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/Gemfile.lock +2 -1
- data/README.md +72 -3
- data/lib/decorator_dryer/active_storage_shortcuts.rb +7 -1
- data/lib/decorator_dryer/rspec/matchers.rb +125 -0
- data/lib/decorator_dryer/rspec.rb +5 -0
- data/lib/decorator_dryer/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: acaab9b600b806da72034503fd1248a8fd1a04cd9ce40da3b7a2ace4ae152f9c
|
|
4
|
+
data.tar.gz: b27d7230005a389d8d386032d9b754bc587f9544194254c917d47c00a3a93197
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f53634e3a21b84ddd288c19ccb4042654793372619c16bfdd984c1a8cc824430baf185950bd606a4ecf90de73b5ad1f22484efaae530b0f1a1890d1883aa851c
|
|
7
|
+
data.tar.gz: 30d393ba4083a7bbd7b748752bad269db75c1e51dbe34272a676c1a7e78851e47ff10c352e56f530559a56b26d3d2823e5a51d3ff65198ad9a83255a5c363089
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -223,21 +223,90 @@ class PersonDecorator < Draper::Decorator
|
|
|
223
223
|
end
|
|
224
224
|
```
|
|
225
225
|
|
|
226
|
+
## Testing
|
|
227
|
+
|
|
228
|
+
### RSpec Matchers
|
|
229
|
+
|
|
230
|
+
Decorator Dryer provides RSpec matchers to help you test your decorators. To use them, require the matchers in your `spec_helper.rb` or `rails_helper.rb`:
|
|
231
|
+
|
|
232
|
+
```ruby
|
|
233
|
+
require 'decorator_dryer/rspec'
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
The following matchers are available:
|
|
237
|
+
|
|
238
|
+
#### format_field_to_name
|
|
239
|
+
|
|
240
|
+
Verifies that a decorator defines a method to retrieve the name value from an association:
|
|
241
|
+
|
|
242
|
+
```ruby
|
|
243
|
+
it { should format_field_to_name(:person) }
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
#### format_field_to_datetime
|
|
247
|
+
|
|
248
|
+
Verifies that a decorator defines a datetime format method for a given attribute:
|
|
249
|
+
|
|
250
|
+
```ruby
|
|
251
|
+
it { should format_field_to_datetime(:moment_of_birth) }
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### format_field_to_time
|
|
255
|
+
|
|
256
|
+
Verifies that a decorator defines a time format method for a given attribute:
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
it { should format_field_to_time(:lunch_time) }
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
#### format_field_to_date
|
|
263
|
+
|
|
264
|
+
Verifies that a decorator defines a date format method for a given attribute:
|
|
265
|
+
|
|
266
|
+
```ruby
|
|
267
|
+
it { should format_field_to_date(:moment_of_birth) }
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
#### format_field_to_precision_number
|
|
271
|
+
|
|
272
|
+
Verifies that a decorator defines a precision number format method for a given attribute:
|
|
273
|
+
|
|
274
|
+
```ruby
|
|
275
|
+
it { should format_field_to_precision_number(:salary, 2) }
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
#### delegate_field
|
|
279
|
+
|
|
280
|
+
Verifies that a decorator defines a method to delegate a given attribute to a given association:
|
|
281
|
+
|
|
282
|
+
```ruby
|
|
283
|
+
it { should delegate_field(:job_title, :employee) }
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
#### delegate_field_with_prefix
|
|
287
|
+
|
|
288
|
+
Verifies that a decorator defines a method to delegate a given attribute to a given association, using the association as the method prefix:
|
|
289
|
+
|
|
290
|
+
```ruby
|
|
291
|
+
it { should delegate_field_with_prefix(:job_title, :employee) }
|
|
292
|
+
```
|
|
293
|
+
|
|
226
294
|
## To Do
|
|
227
295
|
|
|
228
296
|
* Enable formats to be overridden when calling shortcuts.
|
|
229
297
|
* Add support for additional attachment libraries.
|
|
230
298
|
* Add a more flexible replacement for the `to_name` shortcut.
|
|
299
|
+
* Add a generator for the initialiser.
|
|
231
300
|
|
|
232
301
|
## Development
|
|
233
302
|
|
|
234
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `spec` to run the tests.
|
|
303
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
|
235
304
|
|
|
236
305
|
To release a new version of this gem, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
237
306
|
|
|
238
307
|
## Contributing
|
|
239
308
|
|
|
240
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/CodeTectonics/decorator_dryer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/CodeTectonics/decorator_dryer/blob/
|
|
309
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/CodeTectonics/decorator_dryer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/CodeTectonics/decorator_dryer/blob/main/CODE_OF_CONDUCT.md).
|
|
241
310
|
|
|
242
311
|
## License
|
|
243
312
|
|
|
@@ -245,4 +314,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
|
245
314
|
|
|
246
315
|
## Code of Conduct
|
|
247
316
|
|
|
248
|
-
Everyone interacting in the DecoratorDryer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/CodeTectonics/decorator_dryer/blob/
|
|
317
|
+
Everyone interacting in the DecoratorDryer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/CodeTectonics/decorator_dryer/blob/main/CODE_OF_CONDUCT.md).
|
|
@@ -22,7 +22,13 @@ module DecoratorDryer
|
|
|
22
22
|
define_method(method_name) do
|
|
23
23
|
attachment = object.send(attr)
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
if attachment.attached?
|
|
26
|
+
if attachment.content_type == "image/heic"
|
|
27
|
+
url_for(attachment.variant(format: :jpg).processed)
|
|
28
|
+
else
|
|
29
|
+
url_for(attachment)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
26
32
|
end
|
|
27
33
|
end
|
|
28
34
|
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DecoratorDryer
|
|
4
|
+
module RSpec
|
|
5
|
+
module Matchers
|
|
6
|
+
# Matcher to verify that a decorator defines a method to return the name attribute
|
|
7
|
+
# of a field.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# expect(user).to format_field_to_name(:role)
|
|
11
|
+
RSpec::Matchers.define :format_field_to_name do |field|
|
|
12
|
+
match do |subject|
|
|
13
|
+
subject.decorate.send("#{field}_name") == subject.send(field).try(:name)
|
|
14
|
+
end
|
|
15
|
+
description do
|
|
16
|
+
"format #{field} to display its name attribute"
|
|
17
|
+
end
|
|
18
|
+
failure_message do |subject|
|
|
19
|
+
"expected #{subject.class.name} decorator to format #{field} with a _name suffix method"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Matcher to verify that a decorator formats a datetime field with format '%Y-%m-%d %H:%M'.
|
|
24
|
+
#
|
|
25
|
+
# @example
|
|
26
|
+
# expect(post).to format_field_to_datetime(:published_at)
|
|
27
|
+
RSpec::Matchers.define :format_field_to_datetime do |field|
|
|
28
|
+
match do |subject|
|
|
29
|
+
subject.decorate.send(field) == subject.send(field).try(:strftime, '%Y-%m-%d %H:%M')
|
|
30
|
+
end
|
|
31
|
+
description do
|
|
32
|
+
"format #{field} as a datetime with format '%Y-%m-%d %H:%M'"
|
|
33
|
+
end
|
|
34
|
+
failure_message do |subject|
|
|
35
|
+
"expected #{subject.class.name} decorator to format #{field} as '%Y-%m-%d %H:%M'"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Matcher to verify that a decorator formats a date field with format '%Y-%m-%d'.
|
|
40
|
+
#
|
|
41
|
+
# @example
|
|
42
|
+
# expect(event).to format_field_to_date(:start_date)
|
|
43
|
+
RSpec::Matchers.define :format_field_to_date do |field|
|
|
44
|
+
match do |subject|
|
|
45
|
+
subject.decorate.send(field) == subject.send(field).try(:strftime, '%Y-%m-%d')
|
|
46
|
+
end
|
|
47
|
+
description do
|
|
48
|
+
"format #{field} as a date with format '%Y-%m-%d'"
|
|
49
|
+
end
|
|
50
|
+
failure_message do |subject|
|
|
51
|
+
"expected #{subject.class.name} decorator to format #{field} as '%Y-%m-%d'"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Matcher to verify that a decorator formats a time field with format '%H:%M'.
|
|
56
|
+
#
|
|
57
|
+
# @example
|
|
58
|
+
# expect(schedule).to format_field_to_time(:start_time)
|
|
59
|
+
RSpec::Matchers.define :format_field_to_time do |field|
|
|
60
|
+
match do |subject|
|
|
61
|
+
subject.decorate.send(field) == subject.send(field).try(:strftime, '%H:%M')
|
|
62
|
+
end
|
|
63
|
+
description do
|
|
64
|
+
"format #{field} as a time with format '%H:%M'"
|
|
65
|
+
end
|
|
66
|
+
failure_message do |subject|
|
|
67
|
+
"expected #{subject.class.name} decorator to format #{field} as '%H:%M'"
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Matcher to verify that a decorator formats a number field with a specific precision.
|
|
72
|
+
#
|
|
73
|
+
# @example
|
|
74
|
+
# expect(product).to format_field_to_precision_number(:price, 2)
|
|
75
|
+
RSpec::Matchers.define :format_field_to_precision_number do |field, precision|
|
|
76
|
+
match do |subject|
|
|
77
|
+
subject.decorate.send(field) == number_with_precision(subject.send(field), precision: precision)
|
|
78
|
+
end
|
|
79
|
+
description do
|
|
80
|
+
"format #{field} as a number with precision #{precision}"
|
|
81
|
+
end
|
|
82
|
+
failure_message do |subject|
|
|
83
|
+
"expected #{subject.class.name} decorator to format #{field} with precision #{precision}"
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Matcher to verify that a decorator delegates a field to an associated object.
|
|
88
|
+
#
|
|
89
|
+
# @example
|
|
90
|
+
# expect(order).to delegate_field(:email, :customer)
|
|
91
|
+
RSpec::Matchers.define :delegate_field do |field, association|
|
|
92
|
+
match do |subject|
|
|
93
|
+
subject.decorate.send(field) == subject.send(association).decorate.send(field)
|
|
94
|
+
end
|
|
95
|
+
description do
|
|
96
|
+
"delegate #{field} to #{association}"
|
|
97
|
+
end
|
|
98
|
+
failure_message do |subject|
|
|
99
|
+
"expected #{subject.class.name} to delegate #{field} to #{association}"
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Matcher to verify that a decorator delegates a field to an associated object with
|
|
104
|
+
# a prefix based on the association name.
|
|
105
|
+
#
|
|
106
|
+
# @example
|
|
107
|
+
# expect(order).to delegate_field_with_prefix(:email, :customer)
|
|
108
|
+
RSpec::Matchers.define :delegate_field_with_prefix do |field, association|
|
|
109
|
+
match do |subject|
|
|
110
|
+
subject.decorate.send("#{association}_#{field}") ==
|
|
111
|
+
subject.send(association).decorate.send(field)
|
|
112
|
+
end
|
|
113
|
+
description do
|
|
114
|
+
"delegate #{field} to #{association} with prefix"
|
|
115
|
+
end
|
|
116
|
+
failure_message do |subject|
|
|
117
|
+
"expected #{subject.class.name} to delegate #{field} to #{association}"
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Auto-include matchers when RSpec is loaded
|
|
125
|
+
::RSpec.configure { |config| config.include DecoratorDryer::RSpec::Matchers }
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: decorator_dryer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mark Harbison
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-08-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -76,6 +76,8 @@ files:
|
|
|
76
76
|
- lib/decorator_dryer/active_storage_shortcuts.rb
|
|
77
77
|
- lib/decorator_dryer/configuration.rb
|
|
78
78
|
- lib/decorator_dryer/formatters.rb
|
|
79
|
+
- lib/decorator_dryer/rspec.rb
|
|
80
|
+
- lib/decorator_dryer/rspec/matchers.rb
|
|
79
81
|
- lib/decorator_dryer/shortcuts.rb
|
|
80
82
|
- lib/decorator_dryer/version.rb
|
|
81
83
|
- sig/decorator_dryer.rbs
|
|
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
105
107
|
- !ruby/object:Gem::Version
|
|
106
108
|
version: '0'
|
|
107
109
|
requirements: []
|
|
108
|
-
rubygems_version: 3.4.
|
|
110
|
+
rubygems_version: 3.4.19
|
|
109
111
|
signing_key:
|
|
110
112
|
specification_version: 4
|
|
111
113
|
summary: Shortcuts for concise view models.
|