thetron / mongoid-enum Goto Github PK
View Code? Open in Web Editor NEWSweet enum sugar for your Mongoid documents
License: MIT License
Sweet enum sugar for your Mongoid documents
License: MIT License
I reached a problem when doing
Model.find_or_initialize_by(enum_field: 'value')
it's not matching it.
I saw that if I use _enum_field
instead of enum_field
it's working as expected.
My environment is:
ruby (2.1.6)
rails (3.2.15)
mongoid (3.1.6)
mongoid-enum (0.1.1)
What is the reason for using underscore in fieldname?
> Meeting.new.attributes
=> {"_id"=>BSON::ObjectId('549d746563616c2168060000'), "_meeting_quality"=>:hd, "_status"=>:inactive}
Actually we get another attribute that differ one we created
Hi Thetron,
I just have a simple question, I have an enum with option multiple equal to true. What is the correct way to send the params for autosave?
I mean my form is sending day => [ 'day1', 'day2' ] but the i have error with the validation.
Hi
Are there any plans to support configurable prefixes for the accessors?
Thanks
Error msg while running 'bundle install':
Bundler could not find compatible versions for gem "mongoid":
In Gemfile:
mongoid-enum (>= 0) ruby depends on
mongoid (~> 3.1) ruby
mongoid (4.0.0.alpha2)
Gemfile:
gem 'mongoid', git: 'git://github.com/mongoid/mongoid.git'
gem 'mongoid-enum', git: 'git://github.com/thetron/mongoid-enum.git'
Is there something wrong with the version comparing?
In the process of converting a project from using symbolize/mongoid
to mongoid-enum
, I had a model that declares the following enum:
# old symbolize/mongoid declaration
# symbolize :status, in: [:new, :success, :fail], default: :new
# new mongoid-enum declaration
enum :status, [:new, :success, :fail]
The enum declaration creates scope methods for the enum values:
MyModel.new # => Mongoid::Criteria
MyModel.success # => Mongoid::Criteria
MyModel.fail # => Mongoid::Criteria
So now mongoid-enum has overridden the new
method, which blows up calls to MyModel.create
or MyModel.create!
.
There are a couple of ways this can be fixed (ideally, both could be implemented).
Add a new option to disable scopes for an enum:
enum :status, [:new, :success, :fail], :scopes => false
Add a scope method prefix configuration option to avoid namespace collisions (e.g., scope_
or s_
for brevity):
module Mongoid
module Enum
class Configuration
attr_accessor :field_name_prefix
attr_accessor :scope_method_prefix
def initialize
self.field_name_prefix = "_"
self.scope_method_prefix = "s_"
end
end
def self.configuration
@configuration ||= Configuration.new
end
def self.configure
yield(configuration) if block_given?
end
end
end
Way before DHH enum arrived there was a lot of solutions for this and one I took to maintain (AR pain) was Symbolize.
A nice feature I've ported from it is i18n helpers: _text
to get the value translated quickly and _values
to get all translations (good for drop down).
They are pretty cheap oneliners, but very handy (at least for my apps).
If you would like I'll create a pull request.
https://github.com/nofxx/mongoid-enum/tree/merge
The "symbol" type is deprecated in BSON and in Mongo 3.2.
https://docs.mongodb.com/manual/reference/bson-types/
I believe mongoid-enum should be updated to use Strings instead of Symbols for storing the enum value to reduce future data migrations when Symbol is finally removed as a supported type.
Alternatively, mongoid-enum could be made to be indifferent as to whether strings or symbols are stored - including in the auto-generated accessor methods.
What is the correct syntax for creating an index on an enum field?
I've already created an enum named action like this.
enum :action, Enum::Task::ACTION, default: :something
I wanted to know if the correct syntax is index _action: 1
or index action: 1
.
This is a feature request.
I am waiting for the compatibility support for Mongoid 6
I had https://github.com/mongoid kindly donated to the community by the nice folks at MongoDB. The three people who have org level admin access are currently @durran, @estolfo and myself.
I want to invite this project to be moved to the org. I think it makes good sense to group mongoid projects together and have continuous support from MongoDB, the company, over the long term. All admins of this project will continue having admin access of course, but this would reduce the bus factor since currently only @thetron can add maintainers here.
If you think it's a good idea, please transfer this project to me and I'll move it into the org and please add [email protected]
, [email protected]
and [email protected]
to rubygems.
I have error
SyntaxError (/home/silver/ProRails/triip/app/models/lapor.rb:12: syntax error, unexpected tCONSTANT, expecting ']'
...tan, :Pelanggaran, :Lampu Jalan Rusak, :Sampah, :Jalan,
SyntaxError (/home/silver/ProRails/triip/app/models/lapor.rb:12: syntax error, unexpected tCONSTANT, expecting ']'
...tan, :Pelanggaran, :Lampu Jalan Rusak, :Sampah, :Jalan,
Here source code Model :
enum :category, [:Kemacetan, :Pelanggaran, :Lampu Jalan Rusak, :Sampah, :Jalan,
:Fasilitas, :Parkir, :Pengemis, :Kaki, :Kriminal,
:Banjir,:Pohon,:Kebakaran,:Pajak,:PelanggaranIzinBangunan,
:PotensiTeroris,:LansiaHilang,
I've just updated to mongoid5 and mongoid-enum 0.4, and noticed a change in how criteria is built.
In emongoid-enum 0.2 Model.where(enum_name: :value)
would translate to the mongo query for {_enum_name: "value"}
that is, it added the field prefix to the query. That doesn't seem to be working anymore.
Is this the expected behaviour?
Help !!!
base) ➜ raceday git:(master) ✗ bundle
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "mongoid":
In snapshot (Gemfile.lock):
mongoid (= 7.0.2)
In Gemfile:
mongoid (~> 7.0, >= 7.0.2)
mongoid_enum was resolved to 1.1.0, which depends on
mongoid (~> 5.0)
Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
I've just realized new mongoid 5.0.0 showed up. Do you plan for next version support?
P.S. Thanks for the great gem.
After including enum to model class mongoid-rspec validation tests are failing.
Would be happy to help maintain this on behalf of the https://github.com/mongoid organization. Propose to move this gem into there.
I can take care of pending PRs for compatibility with newer versions of mongoid.
When i have a inherited model and a enum in the superclass, I need to repeat the translation. E.g
class Employee
enum :foo, [:bar, :baz, :zaz]
class Manager < Employee
and in translation .yml file I need to have
enums:
employee:
foo:
bar: BAR
baz: BAZ
zaz: ZAZ
employees/manager:
foo:
bar: BAR
baz: BAZ
zaz: ZAZ
So if I don't have the employees/manager defined, its should search in the employee
I defined enum :lang, [:'zh-TW', :jp]
and I got exception.
.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-enum-0.3.0/lib/mongoid/enum.rb:69:in `class_eval': (eval):1: syntax error, unexpected '-', expecting ';' or '\n'
def zh-TW?() self._lang == :zh-TW end
^
(eval):1: syntax error, unexpected keyword_self, expecting end-of-input
def zh-TW?() self._lang == :zh-TW end
^ (SyntaxError)
I have the code like
enum :gender, [:male, :female], default: ''
doesn't work.
If I don't set a value, I got
The following errors were found: gender is not included in the list
If you have an enum that is the name of an existing method, the method is clobbered. Example an enum of :all
is quite destructive ;)
enum, :roles => [:basic, :manager, :administrator], :multiple => true?
I have a sintax bug on my rails app. I'm using rails 4.
How should I test enum setup in my app?
Just looking through and noticing that there has not been a commit in 2 years, and PRs that seem to have been ignored for some time as well. Does this gem need new maintainers? Is there an alternative people have switched to?
I get an error when trying to map enum values explicitly (as can be done with ActiveRecord::Enum
).
For example:
# see https://en.wikipedia.org/wiki/ISO/IEC_5218
enum :sex, {
unknown: 0,
male: 1,
female: 2,
not_applicable: 9
}
The error is in create_validations
:
undefined method `to_sym' for [:unknown, 0]:Array
i using mongoid 7
ruby 2.6.1
rails 5.2.2
when i run bundle on my gem i get these error
In snapshot (Gemfile.lock):
mongoid (= 7.0.2)
In Gemfile:
mongoid-enum was resolved to 0.1.1, which depends on
mongoid (~> 3.1)
mongoid-paperclip was resolved to 0.0.11, which depends on
mongoid
Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
what shoul d i do
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.