Skip to content

Commit

Permalink
Merge pull request #23324 from kbrock/available_attributes
Browse files Browse the repository at this point in the history
introduce all_attribute_names
  • Loading branch information
jrafanie authored Feb 4, 2025
2 parents 9bd969e + 3aa83d2 commit bd85bc0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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 || [])
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)
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

0 comments on commit bd85bc0

Please sign in to comment.