New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveStorage reflection #33018
Merged
Merged
ActiveStorage reflection #33018
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorage | ||
module Reflection | ||
# Holds all the metadata about a has_one_attached attachment as it was | ||
# specified in the Active Record class. | ||
class HasOneAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc: | ||
def macro | ||
:has_one_attached | ||
end | ||
end | ||
|
||
# Holds all the metadata about a has_many_attached attachment as it was | ||
# specified in the Active Record class. | ||
class HasManyAttachedReflection < ActiveRecord::Reflection::MacroReflection #:nodoc: | ||
def macro | ||
:has_many_attached | ||
end | ||
end | ||
|
||
module ReflectionExtension | ||
def reflection_class_for(macro) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should be private |
||
case macro | ||
when :has_one_attached | ||
HasOneAttachedReflection | ||
when :has_many_attached | ||
HasManyAttachedReflection | ||
else | ||
super | ||
end | ||
end | ||
|
||
def add_attachment_reflection(ar, name, reflection) | ||
ar.attachment_reflections.merge!(name.to_s => reflection) | ||
end | ||
end | ||
|
||
module ActiveRecordExtensions | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :attachment_reflections, instance_writer: false, default: {} | ||
end | ||
|
||
module ClassMethods | ||
# Returns an array of reflection objects for all the attachments in the | ||
# class. | ||
def reflect_on_all_attachments | ||
attachment_reflections.values | ||
end | ||
|
||
# Returns the reflection object for the named +attachment+. | ||
# | ||
# User.reflect_on_attachment(:avatar) | ||
# # => the avatar reflection | ||
# | ||
def reflect_on_attachment(attachment) | ||
attachment_reflections[attachment.to_s] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class ActiveStorage::ReflectionTest < ActiveSupport::TestCase | ||
test "allows reflecting for all attachment" do | ||
expected_classes = | ||
User.reflect_on_all_attachments.all? do |reflection| | ||
reflection.is_a?(ActiveStorage::Reflection::HasOneAttachedReflection) || | ||
reflection.is_a?(ActiveStorage::Reflection::HasManyAttachedReflection) | ||
end | ||
|
||
assert expected_classes | ||
end | ||
|
||
test "allows reflecting on a singular has_one_attached attachment" do | ||
reflection = User.reflect_on_attachment(:avatar) | ||
|
||
assert_equal :avatar, reflection.name | ||
assert_equal :has_one_attached, reflection.macro | ||
end | ||
|
||
test "allows reflecting on a singular has_many_attached attachment" do | ||
reflection = User.reflect_on_attachment(:highlights) | ||
|
||
assert_equal :highlights, reflection.name | ||
assert_equal :has_many_attached, reflection.macro | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this one should be
:nodoc:
like all methods in Reflection are.