Skip to content

Commit a1d2460

Browse files
committed
Named variants should be defined using block syntax
1 parent 1adea93 commit a1d2460

File tree

7 files changed

+43
-23
lines changed

7 files changed

+43
-23
lines changed

activestorage/CHANGELOG.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
```ruby
44
class User < ActiveRecord::Base
5-
has_one_attached :avatar, variants: {
6-
thumb: { resize: "100x100" },
7-
medium: { resize: "300x300", monochrome: true }
8-
}
5+
has_one_attached :avatar do |attachable|
6+
attachable.variant :thumb, resize: "100x100"
7+
attachable.variant :medium, resize: "300x300", monochrome: true
8+
end
99
end
1010

1111
class Gallery < ActiveRecord::Base
12-
has_many_attached :photos, variants: {
13-
thumb: { resize: "100x100" },
14-
medium: { resize: "300x300", monochrome: true }
15-
}
12+
has_many_attached :photos do |attachable|
13+
attachable.variant :thumb, resize: "100x100"
14+
attachable.variant :medium, resize: "300x300", monochrome: true
15+
end
1616
end
1717

1818
<%= image_tag user.avatar.variant(:thumb) %>

activestorage/app/models/active_storage/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def dependent
6666
end
6767

6868
def variants
69-
record.attachment_reflections[name]&.options[:variants]
69+
record.attachment_reflections[name]&.variants
7070
end
7171
end
7272

activestorage/lib/active_storage/attached/model.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Attached::Model
4040
# has_one_attached :avatar, service: :s3
4141
# end
4242
#
43-
def has_one_attached(name, dependent: :purge_later, service: nil, variants: {})
43+
def has_one_attached(name, dependent: :purge_later, service: nil)
4444
validate_service_configuration(name, service)
4545

4646
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
@@ -72,9 +72,10 @@ def #{name}=(attachable)
7272
:has_one_attached,
7373
name,
7474
nil,
75-
{ dependent: dependent, service_name: service, variants: variants },
75+
{ dependent: dependent, service_name: service },
7676
self
7777
)
78+
yield reflection if block_given?
7879
ActiveRecord::Reflection.add_attachment_reflection(self, name, reflection)
7980
end
8081

@@ -110,7 +111,7 @@ def #{name}=(attachable)
110111
# has_many_attached :photos, service: :s3
111112
# end
112113
#
113-
def has_many_attached(name, dependent: :purge_later, service: nil, variants: {})
114+
def has_many_attached(name, dependent: :purge_later, service: nil)
114115
validate_service_configuration(name, service)
115116

116117
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
@@ -159,9 +160,10 @@ def purge_later
159160
:has_many_attached,
160161
name,
161162
nil,
162-
{ dependent: dependent, service_name: service, variants: variants },
163+
{ dependent: dependent, service_name: service },
163164
self
164165
)
166+
yield reflection if block_given?
165167
ActiveRecord::Reflection.add_attachment_reflection(self, name, reflection)
166168
end
167169

activestorage/lib/active_storage/reflection.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22

33
module ActiveStorage
44
module Reflection
5+
class HasAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
6+
def variant(name, transformations)
7+
variants[name] = transformations
8+
end
9+
10+
def variants
11+
@variants ||= {}
12+
end
13+
end
14+
515
# Holds all the metadata about a has_one_attached attachment as it was
616
# specified in the Active Record class.
7-
class HasOneAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
17+
class HasOneAttachedReflection < HasAttachedReflection #:nodoc:
818
def macro
919
:has_one_attached
1020
end
1121
end
1222

1323
# Holds all the metadata about a has_many_attached attachment as it was
1424
# specified in the Active Record class.
15-
class HasManyAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
25+
class HasManyAttachedReflection < HasAttachedReflection #:nodoc:
1626
def macro
1727
:has_many_attached
1828
end

activestorage/test/models/reflection_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ActiveStorage::ReflectionTest < ActiveSupport::TestCase
1414
assert_equal :local, reflection.options[:service_name]
1515

1616
reflection = User.reflect_on_attachment(:avatar_with_variants)
17-
assert_instance_of Hash, reflection.options[:variants]
17+
assert_instance_of Hash, reflection.variants
1818
end
1919

2020
test "reflection on a singular attachment with the same name as an attachment on another model" do
@@ -33,7 +33,7 @@ class ActiveStorage::ReflectionTest < ActiveSupport::TestCase
3333
assert_equal :local, reflection.options[:service_name]
3434

3535
reflection = User.reflect_on_attachment(:highlights_with_variants)
36-
assert_instance_of Hash, reflection.options[:variants]
36+
assert_instance_of Hash, reflection.variants
3737
end
3838

3939
test "reflecting on all attachments" do

activestorage/test/test_helper.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,15 @@ class User < ActiveRecord::Base
119119

120120
has_one_attached :avatar
121121
has_one_attached :cover_photo, dependent: false, service: :local
122-
has_one_attached :avatar_with_variants, variants: { thumb: { resize: "100x100" } }
122+
has_one_attached :avatar_with_variants do |attachable|
123+
attachable.variant :thumb, resize: "100x100"
124+
end
123125

124126
has_many_attached :highlights
125127
has_many_attached :vlogs, dependent: false, service: :local
126-
has_many_attached :highlights_with_variants, variants: { thumb: { resize: "100x100" } }
128+
has_many_attached :highlights_with_variants do |attachable|
129+
attachable.variant :thumb, resize: "100x100"
130+
end
127131
end
128132

129133
class Group < ActiveRecord::Base

guides/source/active_storage_overview.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,13 @@ class User < ApplicationRecord
332332
end
333333
```
334334

335-
You can configure specific variants per attachment using the `variants` option:
335+
You can configure specific variants per attachment by calling the `variant` method on yielded attachable object:
336336

337337
```ruby
338338
class User < ApplicationRecord
339-
has_one_attached :avatar, variants: { thumb: { resize: "100x100" } }
339+
has_one_attached :avatar do |attachable|
340+
attachable.variant :thumb, resize: "100x100"
341+
end
340342
end
341343
```
342344

@@ -396,11 +398,13 @@ class Message < ApplicationRecord
396398
end
397399
```
398400

399-
Configuring specific variants is done the same way as `has_one_attached`, by using the `variants` option:
401+
Configuring specific variants is done the same way as `has_one_attached`, by calling the `variant` method on the yielded attachable object:
400402

401403
```ruby
402404
class Message < ApplicationRecord
403-
has_many_attached :images, variants: { thumb: { resize: "100x100" } }
405+
has_many_attached :images do |attachable|
406+
attachable.variant :thumb, resize: "100x100"
407+
end
404408
end
405409
```
406410

0 commit comments

Comments
 (0)