Skip to content
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

introduce all_attribute_names #23324

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/extensions/ar_visible_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ def hide_attribute(attribute)
self.hidden_attribute_names += [attribute.to_s]
end

# @return Array[String] name of hidden and attributes visible in the api
# This includes both attribute names and column aliases
def all_attribute_names
attribute_names | (try(:attribute_aliases)&.keys || [])
jrafanie marked this conversation as resolved.
Show resolved Hide resolved
end

# @return Array[String] attribute names that can be advertised in the api and reporting
# Other attributes are accessible, they are just no longer in our public api (or never were)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this sentence mean? Can you provide an example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original text says "non-hidden attributes"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, let's merge it and if you think of a better way to describe it, we can edit it later. I don't have a better suggestion.

def visible_attribute_names
attribute_names - hidden_attribute_names
all_attribute_names - hidden_attribute_names
end
end
end
11 changes: 11 additions & 0 deletions spec/lib/extensions/ar_visible_attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,15 @@
end
end
end

context ".all_attribute_names" do
it "returns columns" do
expect(klass.all_attribute_names).to include("name")
end

it "returns aliases" do
klass.alias_attribute :name2, :name
expect(klass.all_attribute_names).to include("name2")
end
end
end