Field prefix is now configurable
This commit is contained in:
parent
1f43e0d4d8
commit
5a956c67e2
@ -1,5 +1,6 @@
|
||||
require "mongoid/enum/version"
|
||||
require "mongoid/enum/validators/multiple_validator"
|
||||
require "mongoid/enum/configuration"
|
||||
|
||||
module Mongoid
|
||||
module Enum
|
||||
@ -7,7 +8,7 @@ module Mongoid
|
||||
module ClassMethods
|
||||
|
||||
def enum(name, values, options = {})
|
||||
field_name = :"_#{name}"
|
||||
field_name = :"#{Mongoid::Enum.configuration.field_name_prefix}#{name}"
|
||||
options = default_options(values).merge(options)
|
||||
|
||||
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
|
@ -1,4 +1,5 @@
|
||||
require 'spec_helper'
|
||||
require 'mongoid/enum/configuration'
|
||||
|
||||
class User
|
||||
include Mongoid::Document
|
||||
@ -21,6 +22,23 @@ describe Mongoid::Enum do
|
||||
expect(klass).to have_field(field_name)
|
||||
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
|
||||
expect(instance).to respond_to alias_name
|
||||
expect(instance).to respond_to :"#{alias_name}="
|
||||
@ -174,4 +192,19 @@ describe Mongoid::Enum do
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user