Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
script: "bundle exec rspec spec"

sudo: false
language: ruby

rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.1.1
- 2.1.2
- jruby-19mode

services:
- mongodb
15 changes: 7 additions & 8 deletions lib/mongoid/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def enum(name, values, options = {})

create_field field_name, options

create_validations field_name, values, options
create_validations name, field_name, options
define_value_scopes_and_accessors field_name, values, options
define_field_accessor name, field_name, options
end
Expand All @@ -40,12 +40,11 @@ def create_field(field_name, options)
field field_name, :type => type, :default => options[:default]
end

def create_validations(field_name, values, options)
def create_validations(name, field_name, options)
if options[:multiple] && options[:validate]
validates field_name, :'mongoid/enum/validators/multiple' => { :in => values.map(&:to_sym), :allow_nil => !options[:required] }
#FIXME: Shouldn't this be `elsif options[:validate]` ???
elsif validate
validates field_name, :inclusion => {:in => values.map(&:to_sym)}, :allow_nil => !options[:required]
validates field_name, :'mongoid/enum/validators/multiple' => { :in => self.const_get(name.to_s.upcase), :allow_nil => !options[:required] }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [149/80]
Use the new Ruby 1.9 hash syntax.
Redundant self detected.

elsif options[:validate]
validates field_name, :inclusion => {:in => self.const_get(name.to_s.upcase)}, :allow_nil => !options[:required]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [122/80]
Use the new Ruby 1.9 hash syntax.
Redundant self detected.
Space inside { missing.
Space inside } missing.

end
end

Expand All @@ -71,12 +70,12 @@ def define_field_accessor(name, field_name, options)

def define_array_field_accessor(name, field_name)
class_eval "def #{name}=(vals) self.write_attribute(:#{field_name}, Array(vals).compact.map(&:to_sym)) end"
class_eval "def #{name}() self.read_attribute(:#{field_name}) end"
class_eval "def #{name}() self.send(:#{field_name}).map{ |i| i.try(:to_sym) } end"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [90/80]

end

def define_string_field_accessor(name, field_name)
class_eval "def #{name}=(val) self.write_attribute(:#{field_name}, val && val.to_sym || nil) end"
class_eval "def #{name}() self.read_attribute(:#{field_name}) end"
class_eval "def #{name}() self.send(:#{field_name}) end"
end

def define_array_accessor(field_name, value)
Expand Down