alassek / activerecord-pg_enum

Integrate PostgreSQL's enumerated types with the Rails enum feature
MIT License
168 stars 10 forks source link

Support Rails enum options #5

Closed AndrewSpeed closed 4 years ago

AndrewSpeed commented 5 years ago

👋 I ran into another use case which activerecord-pg_enum doesn't seem to support yet, and wanted to start a discussion about whether we would want to support it, and if so how it would be implemented.

As with #3, I'm happy to attempt implementing this change, if it's decided that we want to support it.

Example

When using activerecord-pg_enum, we can't specify any of the options supported by the ActiveRecord::Enum class, e.g. prefix and suffix.

This means that two enums with the same keys will collide when it comes to the autogenerated methods provided by this class, for example:

Rails enum

class Conversation < ActiveRecord::Base
  enum status: [:active, :archived], _suffix: true
  enum comments_status: [:active, :inactive], _prefix: :comments
end
conversation.active_status!
conversation.archived_status? # => false

conversation.comments_inactive!
conversation.comments_active? # => false

Proposed activerecord-pg_enum implementation

One option to implement this behaviour could be to accept any options to the include and pass them through to the ActiveRecord::Enum in the module definition

class Conversation < ActiveRecord::Base
  include PGEnum(status: [:active, :archived], _suffix: true)
  include PGEnum(status: [:active, :archived], _prefix: :comments)
end
conversation.active_status!
conversation.archived_status? # => false

conversation.comments_inactive!
conversation.comments_active? # => false
AndrewSpeed commented 5 years ago

It turns out the gem does support this behaviour :man_facepalming:

I've added a PR containing a test covering this functionality and updated the README with an example

alassek commented 4 years ago

@AndrewSpeed Sorry for the slow responses 😕 My GitHub notifications are so noisy it's hard to see anything.

AndrewSpeed commented 4 years ago

No problem at all @alassek, mine are exactly the same way 😄

Thanks for merging the PR 🎉