Git Product home page Git Product logo

Comments (3)

fatkodima avatar fatkodima commented on September 27, 2024 1

Ok, I see that is already implemented in #50333

from rails.

fatkodima avatar fatkodima commented on September 27, 2024

validate: ... is an implemented syntax, not a _validate: .... Why do you expect it to work?

from rails.

james-em avatar james-em commented on September 27, 2024

validate: ... is an implemented syntax, not a _validate: .... Why do you expect it to work?

Hi,

This is just how enums are built.

You can do the following:

enum :my_enum, [:value, :value2], prefix: true, suffix: false, instance_methods: false, validate: true

enum another_enum2: { value: "something" }, _prefix: true, _suffix: false, _instance_methods: false

But in the second instance you can't do the following: _validate: true

It simply appears to have been forgotten, don't you agree?

By looking at the implementation, it seems we can declare many enums at once:

enum enum1: { value: "something" }, enum2: { hello: "world" }, _prefix: true

So because _validate: true isn't explicitly in the slice!(...)ActiveRecord thinks it's another enum to define.

Edit: To those interested, this initializer fix the behavior in the mean time

module ValidatableEnum
  extend ActiveSupport::Concern

  included do
    def self.enum(name = nil, values = nil, **options)
      return super if name

      definitions = options.slice!(:_prefix, :_suffix, :_scopes, :_default, :_instance_methods, :_validate)
      options.transform_keys! { |key| :"#{key[1..]}" }

      definitions.each { |name, values| _enum(name, values, **options) }
    end
  end
end

ActiveSupport.on_load(:active_record) { include ValidatableEnum }

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.