Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion lib/active_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def self.define(&block)
end

def self.storage_class
@@storage_class ||= "ActiveEnum::Storage::#{storage.to_s.classify}Store".constantize
@@storage_class ||= get_storage_class(storage)
end

def self.get_storage_class(storage)
"ActiveEnum::Storage::#{storage.to_s.classify}Store".constantize
end

private
Expand Down
4 changes: 4 additions & 0 deletions lib/active_enum/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def meta(index)
end
row[2] || {} if row
end

def storage=(storage)
@store = ActiveEnum.get_storage_class(storage).new(self, @order || :asc, ActiveEnum.storage_options)
end

private

Expand Down
5 changes: 3 additions & 2 deletions lib/active_enum/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def enumerate(*attributes, &block)
attributes.each do |attribute|
begin
if block_given?
enum = define_implicit_enum_class_for_attribute(attribute, block)
enum = define_implicit_enum_class_for_attribute(attribute, options.slice(:storage), block)
else
enum = options[:with] || attribute.to_s.camelize.constantize
end
Expand All @@ -60,10 +60,11 @@ def define_active_enum_methods_for_attribute(attribute)
define_active_enum_question_method(attribute)
end

def define_implicit_enum_class_for_attribute(attribute, block)
def define_implicit_enum_class_for_attribute(attribute, options = {}, block)
enum_class_name = "#{name}::#{attribute.to_s.camelize}"
eval("class #{enum_class_name} < ActiveEnum::Base; end")
enum = enum_class_name.constantize
enum.storage = options[:storage] if options.key? :storage
enum.class_eval &block
enum
end
Expand Down
30 changes: 29 additions & 1 deletion spec/active_enum/extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Accepted < ActiveEnum::Base
end
Person.active_enum_for(:sex).should == ::Person::Sex
end

it 'should raise error if implicit enumeration class cannot be found' do
expect {
Person.enumerate :first_name
Expand Down Expand Up @@ -206,5 +206,33 @@ class Accepted < ActiveEnum::Base
end

end

context "with specified storage" do
let(:person) { Person.new(:sex => 1) }

before do
@default_locale = I18n.locale
I18n.backend.store_translations :en, :active_enum => { :person => { :sex => { 'male' => 'Male', 'female' => 'Female' } } }
I18n.locale = :en

reset_class Person do
enumerate :sex, storage: :i18n do
value 1 => 'male'
value 2 => 'female'
end
end
end

after do
I18n.locale = @default_locale
end

it 'should load the specified storage class instance' do
Person.active_enum_for(:sex).send(:store).should be_instance_of(ActiveEnum::Storage::I18nStore)
end

it 'should return text name value for attribute' do
person.sex(:name).should == 'Male'
end
end
end