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

Add method AttributeSet#names to return array of all attribute names #379

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tracyloisel
Copy link

Returns all the attributes name defined on a Class

#
# @example
#   class User
#     include Virtus
#
#     attribute :name, String
#     attribute :age,  Integer
#   end
#
# @return [:name, :age]
#
# @api public

@tracyloisel tracyloisel changed the title Add method attribute_names to ClassMethods to collect all attributes in an array Add method AttributeSet#names to return array of all attribute names Jun 23, 2017
@Roman2K
Copy link

Roman2K commented Jun 23, 2017

I think it's bloat as AttributeSet is already an enumerable of attributes that have a #name method. So AttributeSet#names doesn't bring anything not already easily accessible. You can already do:

User.attribute_set.map(&:name)  # => [:name, :age]

@tracyloisel
Copy link
Author

I think a lot of people may find this feature useful so it should be part of the gem.
Once it will a public method in AttributeSet class, it will be great to add the feature on the README.

In our case, we use Virtus in a ruby (ruby only) project. In some factory classes we want to permit the params with the strict list of attributes as described in our virtus classes. The 30 minutes we took to read the source code and write the method could benefit to all the community around Virtus.

  • bisous

@Roman2K
Copy link

Roman2K commented Jun 26, 2017

Params whitelisting is your specific use case. If you find that you repeat SomeClass.attribute_set.map(&:name) in your code, then add a helper method to factor it out, in your code.

Otherwise, what stops you from adding more methods at the Virtus::AttributeSet-level (i.e. #names) for each Attribute-level method (i.e. #name). Why stop at #names when there are many more attribute methods?

That's why IMO, you're bloating a generic library for your specific needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants