Compare commits
No commits in common. "master" and "v0.3.0" have entirely different histories.
1
.gitignore
vendored
1
.gitignore
vendored
@ -15,4 +15,3 @@ spec/reports
|
||||
test/tmp
|
||||
test/version_tmp
|
||||
tmp
|
||||
.idea
|
||||
|
@ -1,8 +0,0 @@
|
||||
# A sample Guardfile
|
||||
# More info at https://github.com/guard/guard#readme
|
||||
|
||||
guard :rspec, cmd: "bundle exec rspec" do
|
||||
watch(%r{^spec/.+_spec\.rb$})
|
||||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
||||
watch('spec/spec_helper.rb') { "spec" }
|
||||
end
|
@ -14,10 +14,10 @@ module Mongoid
|
||||
set_values_constant name, values
|
||||
|
||||
create_field field_name, options
|
||||
alias_attribute name, field_name
|
||||
|
||||
create_validations field_name, values, options
|
||||
define_value_scopes_and_accessors field_name, values, options
|
||||
define_field_accessor name, field_name, options
|
||||
end
|
||||
|
||||
private
|
||||
@ -42,10 +42,9 @@ module Mongoid
|
||||
|
||||
def create_validations(field_name, values, 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]` ???
|
||||
validates field_name, :'mongoid/enum/validators/multiple' => { :in => values, :allow_nil => !options[:required] }
|
||||
elsif validate
|
||||
validates field_name, :inclusion => {:in => values.map(&:to_sym)}, :allow_nil => !options[:required]
|
||||
validates field_name, :inclusion => {:in => values}, :allow_nil => !options[:required]
|
||||
end
|
||||
end
|
||||
|
||||
@ -61,24 +60,6 @@ module Mongoid
|
||||
end
|
||||
end
|
||||
|
||||
def define_field_accessor(name, field_name, options)
|
||||
if options[:multiple]
|
||||
define_array_field_accessor name, field_name
|
||||
else
|
||||
define_string_field_accessor name, field_name
|
||||
end
|
||||
end
|
||||
|
||||
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"
|
||||
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"
|
||||
end
|
||||
|
||||
def define_array_accessor(field_name, value)
|
||||
class_eval "def #{value}?() self.#{field_name}.include?(:#{value}) end"
|
||||
class_eval "def #{value}!() update_attributes! :#{field_name} => (self.#{field_name} || []) + [:#{value}] end"
|
||||
|
@ -1,5 +1,5 @@
|
||||
module Mongoid
|
||||
module Enum
|
||||
VERSION = "0.4.6"
|
||||
VERSION = "0.3.0"
|
||||
end
|
||||
end
|
||||
|
@ -6,11 +6,11 @@ require 'mongoid/enum/version'
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = "mongoid-enum"
|
||||
spec.version = Mongoid::Enum::VERSION
|
||||
spec.authors = ["Mike Sutton"]
|
||||
spec.email = ["tech@wizewerx.com"]
|
||||
spec.description = %q{Forked from https://github.com/thetron/mongoid-enum/commits?author=thetron.}
|
||||
spec.authors = ["Nicholas Bruning"]
|
||||
spec.email = ["nicholas@bruning.com.au"]
|
||||
spec.description = %q{Heavily inspired by DDH's ActiveRecord::Enum, this little library is there to help you cut down the cruft in your models and make the world a happier place at the same time.}
|
||||
spec.summary = %q{Sweet enum sugar for your Mongoid documents}
|
||||
spec.homepage = "https://git.wizewerx.tech/foss/mongoid-enum"
|
||||
spec.homepage = "https://github.com/thetron/mongoid-enum"
|
||||
spec.license = "MIT"
|
||||
|
||||
spec.files = `git ls-files`.split($/)
|
||||
@ -18,10 +18,12 @@ Gem::Specification.new do |spec|
|
||||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
||||
spec.require_paths = ["lib"]
|
||||
|
||||
spec.add_runtime_dependency "mongoid", "~> 8.0"
|
||||
spec.add_runtime_dependency "mongoid", "~> 4.0"
|
||||
|
||||
spec.add_development_dependency "bundler", "~> 1.3"
|
||||
spec.add_development_dependency "rake"
|
||||
spec.add_development_dependency "rspec", "~> 3.1"
|
||||
spec.add_development_dependency "guard-rspec", "~> 4.6.2"
|
||||
spec.add_development_dependency "mongoid-rspec", "~> 3.0"
|
||||
spec.add_development_dependency "rspec", "~> 2.14"
|
||||
spec.add_development_dependency "guard-rspec", "~> 4.0.3"
|
||||
spec.add_development_dependency "database_cleaner", "~> 1.2.0"
|
||||
spec.add_development_dependency "mongoid-rspec", "~> 1.5.1"
|
||||
end
|
||||
|
@ -42,7 +42,7 @@ describe Mongoid::Enum do
|
||||
it "is aliased" do
|
||||
expect(instance).to respond_to alias_name
|
||||
expect(instance).to respond_to :"#{alias_name}="
|
||||
expect(instance).to respond_to :"#{alias_name}"
|
||||
expect(instance).to respond_to :"#{alias_name}?"
|
||||
end
|
||||
|
||||
describe "type" do
|
||||
@ -68,22 +68,6 @@ describe Mongoid::Enum do
|
||||
end
|
||||
end
|
||||
|
||||
describe "'required' option" do
|
||||
context "when true" do
|
||||
let(:instance) { User.new status: nil }
|
||||
it "is not valid with nil value" do
|
||||
expect(instance).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context "when false" do
|
||||
let(:instance) { User.new roles: nil }
|
||||
it "is valid with nil value" do
|
||||
expect(instance).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "constant" do
|
||||
it "is set to the values" do
|
||||
expect(klass::STATUS).to eq values
|
||||
@ -92,18 +76,6 @@ describe Mongoid::Enum do
|
||||
|
||||
describe "accessors" do
|
||||
context "when singular" do
|
||||
describe "setter" do
|
||||
it "accepts strings" do
|
||||
instance.status = 'banned'
|
||||
expect(instance.status).to eq :banned
|
||||
end
|
||||
|
||||
it "accepts symbols" do
|
||||
instance.status = :banned
|
||||
expect(instance.status).to eq :banned
|
||||
end
|
||||
end
|
||||
|
||||
describe "{{value}}!" do
|
||||
it "sets the value" do
|
||||
instance.save
|
||||
@ -131,33 +103,6 @@ describe Mongoid::Enum do
|
||||
end
|
||||
|
||||
context "when multiple" do
|
||||
describe "setter" do
|
||||
it "accepts strings" do
|
||||
instance.roles = "author"
|
||||
expect(instance.roles).to eq [:author]
|
||||
end
|
||||
|
||||
it "accepts symbols" do
|
||||
instance.roles = :author
|
||||
expect(instance.roles).to eq [:author]
|
||||
end
|
||||
|
||||
it "accepts arrays of strings" do
|
||||
instance.roles = ['author', 'editor']
|
||||
instance.save
|
||||
puts instance.errors.full_messages
|
||||
instance.reload
|
||||
expect(instance.roles).to include(:author)
|
||||
expect(instance.roles).to include(:editor)
|
||||
end
|
||||
|
||||
it "accepts arrays of symbols" do
|
||||
instance.roles = [:author, :editor]
|
||||
expect(instance.roles).to include(:author)
|
||||
expect(instance.roles).to include(:editor)
|
||||
end
|
||||
end
|
||||
|
||||
describe "{{value}}!" do
|
||||
context "when field is nil" do
|
||||
it "creates an array containing the value" do
|
||||
|
@ -1,18 +1,22 @@
|
||||
$: << File.expand_path("../../lib", __FILE__)
|
||||
|
||||
require 'database_cleaner'
|
||||
require 'mongoid'
|
||||
require "mongoid/rspec"
|
||||
require 'mongoid-rspec'
|
||||
require 'mongoid/enum'
|
||||
|
||||
ENV['MONGOID_ENV'] = "test"
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.include Mongoid::Matchers
|
||||
config.before(:suite) do
|
||||
DatabaseCleaner.strategy = :truncation
|
||||
end
|
||||
|
||||
config.before(:each) do
|
||||
config.after(:each) do
|
||||
DatabaseCleaner.clean
|
||||
Mongoid.purge!
|
||||
end
|
||||
end
|
||||
|
||||
Mongoid.load!(File.expand_path("../support/mongoid.yml", __FILE__), :test)
|
||||
Mongo::Logger.logger.level = ::Logger::INFO
|
||||
|
@ -1,5 +1,5 @@
|
||||
test:
|
||||
clients:
|
||||
sessions:
|
||||
default:
|
||||
database: mongoid-enum_test
|
||||
hosts:
|
||||
|
Loading…
x
Reference in New Issue
Block a user