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 prefix option to enum definition #19813

Merged
merged 1 commit into from Jun 12, 2015
Merged

Add prefix option to enum definition #19813

merged 1 commit into from Jun 12, 2015

Conversation

igas
Copy link
Contributor

@igas igas commented Apr 19, 2015

Fixes #17511 and #17415

/cc @dgsan @chancancode @agis-

@@ -119,8 +132,9 @@ def serialize(value)
attr_reader :name, :mapping
end

def enum(definitions)
def enum(definitions, options = {})
Copy link
Contributor

Choose a reason for hiding this comment

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

Methods with double hashes are ugly as hell, when you have to surround the first hash with braces. Let's turn prefix into an optional keyword argument and avoid that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I agree, but first hash is current api =(
Sure, I can change it to keyword arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kaspth can you please explain how you see api with keyword arguments? cause if we don't want to brake api we can't avoid brackets because any function with more than one argument and first argument is hash need them. Sure we can do def enum(prefix: nil, **definitions) but with that we reserve prefix and user can not use it as enum.

Copy link
Contributor

Choose a reason for hiding this comment

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

The way I see it prefix will have to go as a possible enum name.
We'll need feedback from others on this though. @rafaelfranca @matthewd @sgrif

Copy link
Contributor

Choose a reason for hiding this comment

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

Either prefix (and presumably suffix) will have to go as possible enum names, or we'll need to use double hashes. I'd prefer to go the double hash route. It's slightly uglier, but the point of this feature is to reduce the number of reserved words. In which case it'd be def enum(definitions, prefix: false, suffix: false)

Copy link
Member

Choose a reason for hiding this comment

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

Double hash is definitely a no-go. I'm OK with prefix/suffix being reserved keywords for this method under the circumstances.

@s-aida
Copy link
Contributor

s-aida commented Apr 19, 2015

I think enum is supposed to get multiple definitions, so options here is kinda global rather than attribute-specific, but passing the prefix for the specific attribute within options is kinda odd.

Of course, you just need to call enum per attribute though.

@igas
Copy link
Contributor Author

igas commented Apr 19, 2015

@shunsukeaida from my experience most of the time people use enum for each attribute and don't know about ability to define multiple. Anyway it's "global" per call, each definition get same prefix. If you don't need prefix or need different you can add more calls. As you mention.

@s-aida
Copy link
Contributor

s-aida commented Apr 19, 2015

Fair enough, as I remember until recently Rails even didn't have a test for the multiple enum definitions at once 😏

@rxx
Copy link

rxx commented Apr 28, 2015

That really has been and closed #13433

@igas
Copy link
Contributor Author

igas commented Apr 28, 2015

@mli-max actually we have updated version =) #17511 (comment)

As @kaspth mention previously we are waiting some feedback from @rafaelfranca @matthewd

I'm personally agree with @sgrif suggestion, we can't avoid current api and best way imo is def enum(definitions, prefix: false, suffix: false)

If everybody agree I'll update PR.

@rxx
Copy link

rxx commented Apr 28, 2015

@igas Maybe we should add some option to prevent generation methods for a field enum? I need some fields as enum only for inclusion validation for example

@kaspth
Copy link
Contributor

kaspth commented Apr 28, 2015

@dhh I remember you saying you didn't want double hash APIs anymore, so what's your over/under on this?

@kaspth
Copy link
Contributor

kaspth commented Jun 7, 2015

Hey @igas, are you still up for making this happen?

@igas
Copy link
Contributor Author

igas commented Jun 8, 2015

@kaspth yeah, sorry for delay, I'll take a look on this week.

@igas
Copy link
Contributor Author

igas commented Jun 12, 2015

@kaspth hey, can you take a look, please?

@sgrif
Copy link
Contributor

sgrif commented Jun 12, 2015

@igas Can you rebase and squash your commits?

@igas
Copy link
Contributor Author

igas commented Jun 12, 2015

@sgrif done

sgrif added a commit that referenced this pull request Jun 12, 2015
Add prefix option to enum definition
@sgrif sgrif merged commit 0158f92 into rails:master Jun 12, 2015
@kaspth
Copy link
Contributor

kaspth commented Jun 12, 2015

@igas nice! ❤️

@@ -75,6 +75,22 @@ module ActiveRecord
#
# Conversation.where("status <> ?", Conversation.statuses[:archived])
#
# You can use <tt>:enum_prefix</tt>/<tt>:enum_suffix</tt> option then you need
Copy link
Contributor

Choose a reason for hiding this comment

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

Am I the only one who thinks it'd rather be when than then?

Copy link

Choose a reason for hiding this comment

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

Right, when would be better, since after this word you have explanation for its usage.

@dhh
Copy link
Member

dhh commented Jun 15, 2015

Sorry for the late reply here, but I actually think we should just go with _prefix and _postfix. It looks better given the line already starts with enum and it's even clearer that it's a reserved word.

@s-aida
Copy link
Contributor

s-aida commented Jun 16, 2015

(...prefix_ and _postfix are better imo)

kamipo added a commit to kamipo/rails that referenced this pull request Feb 19, 2021
Unlike other features built on Attribute API, reserved options for
`enum` has leading `_`.

* `_prefix`/`_suffix`: rails#19813, rails#20999
* `_scopes`: rails#34605
* `_default`: rails#39820

That is due to `enum` takes one hash argument only, which contains both
enum definitions and reserved options.

I propose new syntax for `enum` to avoid leading `_` from reserved
options, by allowing `enum(attr_name, ..., **options)` more Attribute
API like syntax.

Before:

```ruby
class Book < ActiveRecord::Base
  enum status: [ :proposed, :written ], _prefix: true, _scopes: false
  enum cover: [ :hard, :soft ], _suffix: true, _default: :hard
end
```

After:

```ruby
class Book < ActiveRecord::Base
  enum :status, [ :proposed, :written ], prefix: true, scopes: false
  enum :cover, [ :hard, :soft ], suffix: true, default: :hard
end
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Active Record Enum - Same values, different columns
8 participants