Coder Social home page Coder Social logo

Comments (14)

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024 1

Released as part of 0.4.0, let me know if you face any issues

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

Hi @destructobeam, thank you for the interest to the gem!

I've tried, and looks like there is no way to do it easily, because ActiveRecord::Enum does not really work outside ActiveRecord+ActiveModel classes (StoreModel::Model uses only a part of ActiveModel).

There are some more complex options of getting the behaviour you need:

  1. We can define a custom type for enums and use it to perform the conversion between value and integer (but it won't generate any additional methods):
class User
  include StoreModel::Model

  attribute :status, StoreModel::Types::Enum[active: 1, inactive: 0]
end
  1. Alternatively, we can mimic the functionality of the built-in enum and keep the syntax. I guess, in this case we need to generate only status? (we cannot have status! because hash cannot save itself), am I right?

from store_model.

destructobeam avatar destructobeam commented on May 25, 2024

Thanks for taking a look Dmitry.

I am currently just doing all this manually at the moment by storing the string values. But if you think it’s a good feature it would be pretty cool to have.

Replicating the Rails API I think would look like this:

attribute :status, :enum => [:active, :inactive], :default => 0

attribute :status, :enum => { :inactive => 1, :active => 0 }

model.status = 0
model.status
# => :active
model.active?
# => true
model.inactive?
# => false

Seems like a bit of effort there though? What do you think?

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

I've played with it a bit more. Unfortunately we cannot get enums from attribute, because it comes with Rails Attributes API and it won't allow me to add any new methods, only type conversion.

I'm going to do something like enum :status, values: %i[active inactive], default: :active, which will define the attribute along with methods status (=> :active), status_value (=> 0), active? (=> true), inactive (=> :false). What do you think? Will it cover your case?

from store_model.

destructobeam avatar destructobeam commented on May 25, 2024

Had a look through that pull request you linked and it is looking really good!

I like what you have with the spec where you don’t need the :values key so it’s defined as enum :status => [:active, :inactive], I think it’s a bit cleaner than a seperate ‘values` option, but up to you of course.

I think that creating the status_value method is a good idea also. If you make a branch or something I am happy to test when you are happy with it.

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

I'd like to get rid of values: too, but in this case we won't be able to set up the default using kwargs when hash values are used: enum :status, active: 0, inactive: 1, default: :active. The only way to make it work is to define options explicitly (like enum :status, { active: 0, inactive: 1 }, default: :active) but it looks more error-prone.

Rails enum does not have default option at all, they urge us to use DB default, so we cannot steal any ideas from there. The place we can steal ideas is enumerize - they have enumerize :role, in: [:user, :admin], default: :user too

from store_model.

destructobeam avatar destructobeam commented on May 25, 2024

I like it without kwargs to be honest, as you would usually only use the array syntax, and the hash for edge cases I would think. Then you can use kwargs for options?

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

I can imagine the situation when values are important (like enum :rating, in: { excellent: 100, good: 75, bad: 25, awful: 0 }). I think we could take the best from two worlds and allow to use both in/default and array/default

from store_model.

destructobeam avatar destructobeam commented on May 25, 2024

Oh yeah, hadn’t thought of that, I would probably relegate something like that to a decorator personally, as it isn’t really an enum concern.

But if you want to support that use case I think :in keyword is a good second best to me 👍

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

Okay, I think it works, could you please use the branch enums to give it a shot before I merge? As I planned, we can use enum :status, %i[active archived], default: :active, enum :status, active: 0, archived: 1 (when we need hash) or enum :rating, in: { excellent: 100, okay: 50, bad: 25, awful: 10 }, default: :okay (when we need hash + default)

from store_model.

destructobeam avatar destructobeam commented on May 25, 2024

Have given the branch a quick run through in my current project and it looks really good, working as expected on my end.

Thanks so much for all your work on this, really appreciate it!

from store_model.

destructobeam avatar destructobeam commented on May 25, 2024

One thing I have noticed, not sure if this is expected behaviour or not, is I am not getting any errors or invalid state when I set the enum to an out of range int or a random string.

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

Good catch! I've fixed the issue–cast was called only when the value is accessed (and not during the assignment)

from store_model.

destructobeam avatar destructobeam commented on May 25, 2024

Sorry, been flat out the last few days, thanks for getting this out! Will do.

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.