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

Protection from passing wrong symbols/strings to enums. #96

Closed
mainameiz opened this issue Apr 8, 2015 · 11 comments
Closed

Protection from passing wrong symbols/strings to enums. #96

mainameiz opened this issue Apr 8, 2015 · 11 comments

Comments

@mainameiz
Copy link
Contributor

First of all, thank you for writing such a good gem. :-)

class Model < AR
  as_enum :state, {
    accept: 0,
    reject: 1
  }
end

Model.states[:wrong_state] => nil

It seems to be better to raise an exception in such cases.

@matthewrudy
Copy link

maybe we just want Model.states.fetch(:wrong_state)
and behave like Hash#fetch

@lwe
Copy link
Owner

lwe commented Apr 27, 2015

You can use the whiny accessor, that works like that

class User < ActiveRecord::Base
  as_enum :gender, %w{male female}, accessor: :whiny
end
User.new(gender: "dunno") # => raises ArgumentError

@lwe
Copy link
Owner

lwe commented Apr 27, 2015

and you are welcome :)

@mainameiz
Copy link
Contributor Author

@lwe Yes, whiny accessor works great, but image we write a scope

-> { where(state_cd: self.states[:wrong_state]) }

It will silently paste nil in sql query. To protect from such cases I suggest to raise an error or to implement #fetch method as @matthewrudy suggests. I cant image situations where it can be prefered to return nil for not existing enums.

Thank you for writing such a good gem :-)

@lwe
Copy link
Owner

lwe commented Apr 27, 2015

Ah, now I get it, sorry. Yes, please, feel free to provide a pull request with a fetch implementation, would gladly accept it.

@mainameiz
Copy link
Contributor Author

@lwe ok!

@mainameiz
Copy link
Contributor Author

Done! #104

@lwe
Copy link
Owner

lwe commented Jun 8, 2015

Hey, thanks!

@lwe
Copy link
Owner

lwe commented Jun 8, 2015

FYI, I'm closing this issue now, ok?

@lwe lwe closed this as completed Jun 8, 2015
@mainameiz
Copy link
Contributor Author

Ok, thanks you! :-)

@aayush-practo
Copy link

aayush-practo commented Aug 31, 2016

Hey, @mainameiz
Check the two situations:

  • my first attribute is settlement_status and second attribute status. Here are the enums
class ModelA <AR
   as_enum :settlement_status, [:initiated, :settlementInProgress, :settled, :failure, :queuedInNextCycle], map: :string, source: :settlementStatus, accessor: :whiny
   alias_attribute :settlement_status, :settlementStatus
end

class ModelB < AR
  validates :status, presence: true
  as_enum :status, [:active, :expired], map: :string, source: :status, accessor: :whiny
end

When making new object of ModelB with invalid value for status, it throws error, but does nothing for ModelA, it creates the object and commits it to the database

for example

@model_a = ModelA.new(settlement_status: "abc")
@model_a.save

it is committed successfully

for

 @model_b = ModelB.new(status: "abc")
ArgumentError: abc is not a valid enum value for status

I also tried validating presence of settlement_status but still got the same response
I am using simple_enum (2.3.0) and rails 4.2.6
Please help me with it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants