Coder Social home page Coder Social logo

Array of enums about store_model HOT 9 OPEN

dmitrytsepelev avatar dmitrytsepelev commented on May 25, 2024
Array of enums

from store_model.

Comments (9)

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

Hi, @23tux!

Right now StoreModel focuses on isolating JSON(B) data from the parent model. In your example an array (where it is stored?) is used inside the ApplicationRecord subclass. Probably custom Rails validator will help here:

class User < ApplicationRecord
  ...
  validates :roles, array: { inclusion: { in: ROLES }
end

from store_model.

FunkyloverOne avatar FunkyloverOne commented on May 25, 2024

I guess that enumerize gem could deal with it. I didn't try though :)

from store_model.

23tux avatar 23tux commented on May 25, 2024

@DmitryTsepelev #roles is a JSON column on the users table. I thought because you have support for enum types on single attributes (in the example here https://github.com/DmitryTsepelev/store_model/blob/master/docs/enums.md it is the key status), it would be a good fit to extend this to support an array of enums.

Consider this example, maybe it's clearer this way:

class Configuration
  include StoreModel::Model

  enum :role, %i[admin user reporter], default: :user
end

class User < ApplicationRecord
  attribute :configuration, Configuration.to_type
end

My user has a configuration with an enum of role that defaults to :user. What if I want multiple roles for a user? I thought an array of enums would be a good solution for this. What do you think?

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

Aha, in this case having the built-in inclusion validator for arrays makes sense to me!

from store_model.

23tux avatar 23tux commented on May 25, 2024

@FunkyloverOne you are right, enumerize has built in support for Arrays when you serialize them or use mongodb. I still have to find out if it works with json array columns though. But because we already have the store_model gem in our Gemfile, adding another dependency for this simple use case isn't something I'm very happy about

from store_model.

23tux avatar 23tux commented on May 25, 2024

@DmitryTsepelev How would you implement this? If you can give me a push in the right direction, I'm happy to try for a PR.

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

@23tux This is what should be done to make it work:

  1. Take a look at docs about custom validators
  2. Add a new custom validator (we already have one) called ArrayValidator
  3. Implement the validator (there is a good example)
  4. Add specs
  5. Bonus point: add docs 🙂

As a result, it will be possible to add a validation to the StoreModel in a following way:

class Configuration
  include StoreModel::Model
  ROLES = %i[admin user reporter]
  enum :role, ROLES, default: :user
  validates :roles, array: { inclusion: { in: ROLES }
end

class User < ApplicationRecord
  attribute :configuration, Configuration.to_type
end

Bonus point: it might be helpful to have a shortcut for the enum method to use this validation by default enum :role, ROLES, default: :user, validate: true

from store_model.

23tux avatar 23tux commented on May 25, 2024

@DmitryTsepelev thanks for the kick off!

I'm not sure if I understand the need of a validator when using enums. The current implementation raises an error when an invalid value is assigned, e.g. from your examples

class Configuration
  include StoreModel::Model

  enum :status, %i[active archived], default: :active
end

config = Configuration.new
config.status = "foo"
# ArgumentError: invalid value 'foo' is assigned
# from /usr/local/bundle/gems/store_model-0.7.0/lib/store_model/types/enum_type.rb:56:in `raise_invalid_value!'

I would suggest to keep the same behaviour for an array of enums: When you push an invalid value into the array, it should raise an error. Do you know what I mean?

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

@23tux Sorry for the long response, that's a valid point! We don't need to use a validator in the current implementation (however, there is a chance that someone would need to make this behaviour optional in the future)

from store_model.

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.