mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Named variants should be defined using block syntax
This commit is contained in:
parent
1adea93265
commit
a1d2460cdf
7 changed files with 43 additions and 23 deletions
|
@ -2,17 +2,17 @@
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
has_one_attached :avatar, variants: {
|
has_one_attached :avatar do |attachable|
|
||||||
thumb: { resize: "100x100" },
|
attachable.variant :thumb, resize: "100x100"
|
||||||
medium: { resize: "300x300", monochrome: true }
|
attachable.variant :medium, resize: "300x300", monochrome: true
|
||||||
}
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Gallery < ActiveRecord::Base
|
class Gallery < ActiveRecord::Base
|
||||||
has_many_attached :photos, variants: {
|
has_many_attached :photos do |attachable|
|
||||||
thumb: { resize: "100x100" },
|
attachable.variant :thumb, resize: "100x100"
|
||||||
medium: { resize: "300x300", monochrome: true }
|
attachable.variant :medium, resize: "300x300", monochrome: true
|
||||||
}
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
<%= image_tag user.avatar.variant(:thumb) %>
|
<%= image_tag user.avatar.variant(:thumb) %>
|
||||||
|
|
|
@ -66,7 +66,7 @@ class ActiveStorage::Attachment < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def variants
|
def variants
|
||||||
record.attachment_reflections[name]&.options[:variants]
|
record.attachment_reflections[name]&.variants
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ module ActiveStorage
|
||||||
# has_one_attached :avatar, service: :s3
|
# has_one_attached :avatar, service: :s3
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def has_one_attached(name, dependent: :purge_later, service: nil, variants: {})
|
def has_one_attached(name, dependent: :purge_later, service: nil)
|
||||||
validate_service_configuration(name, service)
|
validate_service_configuration(name, service)
|
||||||
|
|
||||||
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
||||||
|
@ -72,9 +72,10 @@ module ActiveStorage
|
||||||
:has_one_attached,
|
:has_one_attached,
|
||||||
name,
|
name,
|
||||||
nil,
|
nil,
|
||||||
{ dependent: dependent, service_name: service, variants: variants },
|
{ dependent: dependent, service_name: service },
|
||||||
self
|
self
|
||||||
)
|
)
|
||||||
|
yield reflection if block_given?
|
||||||
ActiveRecord::Reflection.add_attachment_reflection(self, name, reflection)
|
ActiveRecord::Reflection.add_attachment_reflection(self, name, reflection)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -110,7 +111,7 @@ module ActiveStorage
|
||||||
# has_many_attached :photos, service: :s3
|
# has_many_attached :photos, service: :s3
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def has_many_attached(name, dependent: :purge_later, service: nil, variants: {})
|
def has_many_attached(name, dependent: :purge_later, service: nil)
|
||||||
validate_service_configuration(name, service)
|
validate_service_configuration(name, service)
|
||||||
|
|
||||||
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
||||||
|
@ -159,9 +160,10 @@ module ActiveStorage
|
||||||
:has_many_attached,
|
:has_many_attached,
|
||||||
name,
|
name,
|
||||||
nil,
|
nil,
|
||||||
{ dependent: dependent, service_name: service, variants: variants },
|
{ dependent: dependent, service_name: service },
|
||||||
self
|
self
|
||||||
)
|
)
|
||||||
|
yield reflection if block_given?
|
||||||
ActiveRecord::Reflection.add_attachment_reflection(self, name, reflection)
|
ActiveRecord::Reflection.add_attachment_reflection(self, name, reflection)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,19 @@
|
||||||
|
|
||||||
module ActiveStorage
|
module ActiveStorage
|
||||||
module Reflection
|
module Reflection
|
||||||
|
class HasAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
|
||||||
|
def variant(name, transformations)
|
||||||
|
variants[name] = transformations
|
||||||
|
end
|
||||||
|
|
||||||
|
def variants
|
||||||
|
@variants ||= {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Holds all the metadata about a has_one_attached attachment as it was
|
# Holds all the metadata about a has_one_attached attachment as it was
|
||||||
# specified in the Active Record class.
|
# specified in the Active Record class.
|
||||||
class HasOneAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
|
class HasOneAttachedReflection < HasAttachedReflection #:nodoc:
|
||||||
def macro
|
def macro
|
||||||
:has_one_attached
|
:has_one_attached
|
||||||
end
|
end
|
||||||
|
@ -12,7 +22,7 @@ module ActiveStorage
|
||||||
|
|
||||||
# Holds all the metadata about a has_many_attached attachment as it was
|
# Holds all the metadata about a has_many_attached attachment as it was
|
||||||
# specified in the Active Record class.
|
# specified in the Active Record class.
|
||||||
class HasManyAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc:
|
class HasManyAttachedReflection < HasAttachedReflection #:nodoc:
|
||||||
def macro
|
def macro
|
||||||
:has_many_attached
|
:has_many_attached
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,7 +14,7 @@ class ActiveStorage::ReflectionTest < ActiveSupport::TestCase
|
||||||
assert_equal :local, reflection.options[:service_name]
|
assert_equal :local, reflection.options[:service_name]
|
||||||
|
|
||||||
reflection = User.reflect_on_attachment(:avatar_with_variants)
|
reflection = User.reflect_on_attachment(:avatar_with_variants)
|
||||||
assert_instance_of Hash, reflection.options[:variants]
|
assert_instance_of Hash, reflection.variants
|
||||||
end
|
end
|
||||||
|
|
||||||
test "reflection on a singular attachment with the same name as an attachment on another model" do
|
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
|
||||||
assert_equal :local, reflection.options[:service_name]
|
assert_equal :local, reflection.options[:service_name]
|
||||||
|
|
||||||
reflection = User.reflect_on_attachment(:highlights_with_variants)
|
reflection = User.reflect_on_attachment(:highlights_with_variants)
|
||||||
assert_instance_of Hash, reflection.options[:variants]
|
assert_instance_of Hash, reflection.variants
|
||||||
end
|
end
|
||||||
|
|
||||||
test "reflecting on all attachments" do
|
test "reflecting on all attachments" do
|
||||||
|
|
|
@ -119,11 +119,15 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
has_one_attached :avatar
|
has_one_attached :avatar
|
||||||
has_one_attached :cover_photo, dependent: false, service: :local
|
has_one_attached :cover_photo, dependent: false, service: :local
|
||||||
has_one_attached :avatar_with_variants, variants: { thumb: { resize: "100x100" } }
|
has_one_attached :avatar_with_variants do |attachable|
|
||||||
|
attachable.variant :thumb, resize: "100x100"
|
||||||
|
end
|
||||||
|
|
||||||
has_many_attached :highlights
|
has_many_attached :highlights
|
||||||
has_many_attached :vlogs, dependent: false, service: :local
|
has_many_attached :vlogs, dependent: false, service: :local
|
||||||
has_many_attached :highlights_with_variants, variants: { thumb: { resize: "100x100" } }
|
has_many_attached :highlights_with_variants do |attachable|
|
||||||
|
attachable.variant :thumb, resize: "100x100"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Group < ActiveRecord::Base
|
class Group < ActiveRecord::Base
|
||||||
|
|
|
@ -332,11 +332,13 @@ class User < ApplicationRecord
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
You can configure specific variants per attachment using the `variants` option:
|
You can configure specific variants per attachment by calling the `variant` method on yielded attachable object:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
has_one_attached :avatar, variants: { thumb: { resize: "100x100" } }
|
has_one_attached :avatar do |attachable|
|
||||||
|
attachable.variant :thumb, resize: "100x100"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -396,11 +398,13 @@ class Message < ApplicationRecord
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
Configuring specific variants is done the same way as `has_one_attached`, by using the `variants` option:
|
Configuring specific variants is done the same way as `has_one_attached`, by calling the `variant` method on the yielded attachable object:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Message < ApplicationRecord
|
class Message < ApplicationRecord
|
||||||
has_many_attached :images, variants: { thumb: { resize: "100x100" } }
|
has_many_attached :images do |attachable|
|
||||||
|
attachable.variant :thumb, resize: "100x100"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue