Git Product home page Git Product logo

Comments (5)

fatkodima avatar fatkodima commented on May 26, 2024

I did not get what the problem is?

Rails generates both methods ("pretty" and "not pretty") on purpose -

pairs.each do |label, value|
enum_values[label] = value
label = label.to_s
value_method_name = "#{prefix}#{label}#{suffix}"
value_method_names << value_method_name
define_enum_methods(name, value_method_name, value, scopes, instance_methods)
method_friendly_label = label.gsub(/[\W&&[:ascii:]]+/, "_")
value_method_alias = "#{prefix}#{method_friendly_label}#{suffix}"
if value_method_alias != value_method_name && !value_method_names.include?(value_method_alias)
value_method_names << value_method_alias
define_enum_methods(name, value_method_alias, value, scopes, instance_methods)
end
end

from rails.

maniSHarma7575 avatar maniSHarma7575 commented on May 26, 2024

I did not get what the problem is?

Rails generates both methods ("pretty" and "not pretty") on purpose -

pairs.each do |label, value|
enum_values[label] = value
label = label.to_s
value_method_name = "#{prefix}#{label}#{suffix}"
value_method_names << value_method_name
define_enum_methods(name, value_method_name, value, scopes, instance_methods)
method_friendly_label = label.gsub(/[\W&&[:ascii:]]+/, "_")
value_method_alias = "#{prefix}#{method_friendly_label}#{suffix}"
if value_method_alias != value_method_name && !value_method_names.include?(value_method_alias)
value_method_names << value_method_alias
define_enum_methods(name, value_method_alias, value, scopes, instance_methods)
end
end

@fatkodima Upon reviewing the command output below, it's evident that the method name US West (Oregon)! contains spaces and brackets, which deviates from standard programming naming conventions. I've noticed this discrepancy, and it seems there might be an issue with adhering to the naming convention for methods.

Additionally, we recently integrated an enum into our application with the following values. However, we discovered that calling the method for the corresponding keys is not possible without utilizing 'send', primarily due to the naming convention.

Could you kindly clarify if Rails intentionally maintains this naming convention? and Why?

Additionally, @fatkodima, it's worth noting that this change was introduced after the merge of #40992. In previous versions before this merge, we only had the pretty method.

[1] pry(#<BugTest>)> john.methods.select { |method| method.to_s.include? "US"}
=> [:"US West (Oregon)!", :US_West_Oregon_?, :US_West_Oregon_!, :"US West (Oregon)?"]

from rails.

fatkodima avatar fatkodima commented on May 26, 2024

As you can see from the removed tests in the PR you opened, this is intentional. It keeps original enum key names to be able to use exact key names (via send, for example). And a mangled name to be able to little easier use it in the code, like for:
User.enum foo: {"foo bar" => 1} you can call User.foo_bar.

For enums with keys like in the description for this issue, it is a pain to use either of the approaches. But this is how it is done and removing this behavior is at least breaking, without saying that it is not needed.

from rails.

manishElitmus avatar manishElitmus commented on May 26, 2024

As you can see from the removed tests in the PR you opened, this is intentional. It keeps original enum key names to be able to use exact key names (via send, for example). And a mangled name to be able to little easier use it in the code, like for: User.enum foo: {"foo bar" => 1} you can call User.foo_bar.

For enums with keys like in the description for this issue, it is a pain to use either of the approaches. But this is how it is done and removing this behavior is at least breaking, without saying that it is not needed.

@fatkodima, I suggest that instead of using either approach, we consider restricting the definition of enum keys to follow a convention friendly to programming methods.

And if you look at pretty method that are generated by Rails they are also not as per the naming convention.

For example: US_West_Oregon_?

  1. Method name starts with the capital letter.
  2. Method name ends with _ because actual key name ends with ")". These pretty methods are also not name friendly.

from rails.

rafaelfranca avatar rafaelfranca commented on May 26, 2024

Like @fatkodima the current behavior is intentional. I'd actually suggest no use enums like this instead, but if you want to, you will get the current behavior of having two methods to the same enum key.

from rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.