Merge pull request #16 from glooko/customizable_field_prefix
Customizable field name prefix
This commit is contained in:
commit
781f05f707
@ -1,5 +1,6 @@
|
|||||||
require "mongoid/enum/version"
|
require "mongoid/enum/version"
|
||||||
require "mongoid/enum/validators/multiple_validator"
|
require "mongoid/enum/validators/multiple_validator"
|
||||||
|
require "mongoid/enum/configuration"
|
||||||
|
|
||||||
module Mongoid
|
module Mongoid
|
||||||
module Enum
|
module Enum
|
||||||
@ -7,7 +8,7 @@ module Mongoid
|
|||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
|
||||||
def enum(name, values, options = {})
|
def enum(name, values, options = {})
|
||||||
field_name = :"_#{name}"
|
field_name = :"#{Mongoid::Enum.configuration.field_name_prefix}#{name}"
|
||||||
options = default_options(values).merge(options)
|
options = default_options(values).merge(options)
|
||||||
|
|
||||||
set_values_constant name, values
|
set_values_constant name, values
|
||||||
|
19
lib/mongoid/enum/configuration.rb
Normal file
19
lib/mongoid/enum/configuration.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
module Mongoid
|
||||||
|
module Enum
|
||||||
|
class Configuration
|
||||||
|
attr_accessor :field_name_prefix
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
self.field_name_prefix = "_"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.configuration
|
||||||
|
@configuration ||= Configuration.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.configure
|
||||||
|
yield(configuration) if block_given?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
11
spec/mongoid/configuration_spec.rb
Normal file
11
spec/mongoid/configuration_spec.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Mongoid::Enum::Configuration do
|
||||||
|
subject { Mongoid::Enum::Configuration.new }
|
||||||
|
|
||||||
|
describe "field_name_prefix" do
|
||||||
|
it "has '_' as default value" do
|
||||||
|
expect(subject.field_name_prefix).to eq "_"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -16,14 +16,14 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|||||||
context "and value is nil" do
|
context "and value is nil" do
|
||||||
before(:each) { validator.validate_each(record, attribute, nil) }
|
before(:each) { validator.validate_each(record, attribute, nil) }
|
||||||
it "validates" do
|
it "validates" do
|
||||||
expect(record.errors[attribute].empty?).to be_true
|
expect(record.errors[attribute].empty?).to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "and value is []" do
|
context "and value is []" do
|
||||||
before(:each) { validator.validate_each(record, attribute, []) }
|
before(:each) { validator.validate_each(record, attribute, []) }
|
||||||
it "validates" do
|
it "validates" do
|
||||||
expect(record.errors[attribute].empty?).to be_true
|
expect(record.errors[attribute].empty?).to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -32,14 +32,14 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|||||||
context "and value is nil" do
|
context "and value is nil" do
|
||||||
before(:each) { validator.validate_each(record, attribute, nil) }
|
before(:each) { validator.validate_each(record, attribute, nil) }
|
||||||
it "won't validate" do
|
it "won't validate" do
|
||||||
expect(record.errors[attribute].any?).to be_true
|
expect(record.errors[attribute].any?).to be true
|
||||||
expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"]
|
expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
context "and value is []" do
|
context "and value is []" do
|
||||||
before(:each) { validator.validate_each(record, attribute, []) }
|
before(:each) { validator.validate_each(record, attribute, []) }
|
||||||
it "won't validate" do
|
it "won't validate" do
|
||||||
expect(record.errors[attribute].any?).to be_true
|
expect(record.errors[attribute].any?).to be true
|
||||||
expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"]
|
expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -49,7 +49,7 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|||||||
let(:allow_nil) { rand(2).zero? }
|
let(:allow_nil) { rand(2).zero? }
|
||||||
before(:each) { validator.validate_each(record, attribute, [values.sample]) }
|
before(:each) { validator.validate_each(record, attribute, [values.sample]) }
|
||||||
it "validates" do
|
it "validates" do
|
||||||
expect(record.errors[attribute].empty?).to be_true
|
expect(record.errors[attribute].empty?).to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|||||||
let(:allow_nil) { rand(2).zero? }
|
let(:allow_nil) { rand(2).zero? }
|
||||||
before(:each) { validator.validate_each(record, attribute, [:amet]) }
|
before(:each) { validator.validate_each(record, attribute, [:amet]) }
|
||||||
it "won't validate" do
|
it "won't validate" do
|
||||||
expect(record.errors[attribute].any?).to be_true
|
expect(record.errors[attribute].any?).to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|||||||
let(:allow_nil) { rand(2).zero? }
|
let(:allow_nil) { rand(2).zero? }
|
||||||
before(:each) { validator.validate_each(record, attribute, [values.first, values.last]) }
|
before(:each) { validator.validate_each(record, attribute, [values.first, values.last]) }
|
||||||
it "validates" do
|
it "validates" do
|
||||||
expect(record.errors[attribute].empty?).to be_true
|
expect(record.errors[attribute].empty?).to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|||||||
let(:allow_nil) { rand(2).zero? }
|
let(:allow_nil) { rand(2).zero? }
|
||||||
before(:each) { validator.validate_each(record, attribute, [values.first, values.last, :amet]) }
|
before(:each) { validator.validate_each(record, attribute, [values.first, values.last, :amet]) }
|
||||||
it "won't validate" do
|
it "won't validate" do
|
||||||
expect(record.errors[attribute].any?).to be_true
|
expect(record.errors[attribute].any?).to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
require 'mongoid/enum/configuration'
|
||||||
|
|
||||||
class User
|
class User
|
||||||
include Mongoid::Document
|
include Mongoid::Document
|
||||||
@ -21,6 +22,23 @@ describe Mongoid::Enum do
|
|||||||
expect(klass).to have_field(field_name)
|
expect(klass).to have_field(field_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "uses prefix defined in configuration" do
|
||||||
|
old_field_name_prefix = Mongoid::Enum.configuration.field_name_prefix
|
||||||
|
Mongoid::Enum.configure do |config|
|
||||||
|
config.field_name_prefix = "___"
|
||||||
|
end
|
||||||
|
UserWithoutPrefix = Class.new do
|
||||||
|
include Mongoid::Document
|
||||||
|
include Mongoid::Enum
|
||||||
|
|
||||||
|
enum :status, [:awaiting_approval, :approved, :banned]
|
||||||
|
end
|
||||||
|
expect(UserWithoutPrefix).to have_field "___status"
|
||||||
|
Mongoid::Enum.configure do |config|
|
||||||
|
config.field_name_prefix = old_field_name_prefix
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "is aliased" 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}="
|
||||||
@ -56,7 +74,7 @@ describe Mongoid::Enum do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "accessors"do
|
describe "accessors" do
|
||||||
context "when singular" do
|
context "when singular" do
|
||||||
describe "{{value}}!" do
|
describe "{{value}}!" do
|
||||||
it "sets the value" do
|
it "sets the value" do
|
||||||
@ -111,15 +129,15 @@ describe Mongoid::Enum do
|
|||||||
instance.save
|
instance.save
|
||||||
instance.author!
|
instance.author!
|
||||||
instance.editor!
|
instance.editor!
|
||||||
expect(instance.editor?).to be_true
|
expect(instance.editor?).to be true
|
||||||
expect(instance.author?).to be_true
|
expect(instance.author?).to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when {{enum}} does not contain {{value}}" do
|
context "when {{enum}} does not contain {{value}}" do
|
||||||
it "returns false" do
|
it "returns false" do
|
||||||
instance.save
|
instance.save
|
||||||
expect(instance.author?).to be_false
|
expect(instance.author?).to be false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -174,4 +192,21 @@ describe Mongoid::Enum do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".configuration" do
|
||||||
|
it "returns Configuration object" do
|
||||||
|
expect(Mongoid::Enum.configuration)
|
||||||
|
.to be_instance_of Mongoid::Enum::Configuration
|
||||||
|
end
|
||||||
|
it "returns same object when called multiple times" do
|
||||||
|
expect(Mongoid::Enum.configuration).to be Mongoid::Enum.configuration
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".configure" do
|
||||||
|
it "yields configuration if block is given" do
|
||||||
|
expect { |b| Mongoid::Enum.configure &b }
|
||||||
|
.to yield_with_args Mongoid::Enum.configuration
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user