From db0bc477233f9ed2462f0e2dc696f08950ebbaed Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Thu, 13 Jan 2011 15:58:19 +0600 Subject: [PATCH 01/15] added human_state instance method for translating states --- lib/aasm/aasm.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index b08c522..ba8a62c 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -41,7 +41,7 @@ module AASM aasm_current_state == name end end - + def aasm_event(name, options = {}, &block) sm = AASM::StateMachine[self] @@ -105,6 +105,17 @@ module AASM events.map {|event| event.name} end + def human_state + begin + var_model = self.class.model_name + var = var_model.respond_to?(:i18n_key) ? var_model.i18n_key : var_model.underscore + key = "activerecord.attributes.#{var}_#{self.class.aasm_column}.#{aasm_current_state}" + translation = I18n.translate(key, :raise => true) + rescue I18n::MissingTranslationData + aasm_current_state.to_s.humanize + end + end + private def set_aasm_current_state_with_persistence(state) @@ -193,4 +204,4 @@ module AASM event.execute_error_callback(self, e) end end -end +end \ No newline at end of file From 41b191698e40d4228c788ceed94842e956095d70 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Thu, 13 Jan 2011 20:58:12 +0600 Subject: [PATCH 02/15] rewrite human_state method to lookup ancestor's translation if inherited --- lib/aasm/aasm.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index ba8a62c..9029530 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -107,10 +107,12 @@ module AASM def human_state begin - var_model = self.class.model_name - var = var_model.respond_to?(:i18n_key) ? var_model.i18n_key : var_model.underscore - key = "activerecord.attributes.#{var}_#{self.class.aasm_column}.#{aasm_current_state}" - translation = I18n.translate(key, :raise => true) + defaults = self.class.lookup_ancestors.map do |klass| + klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + :"#{klass_human}.#{aasm_current_state}" + end + scope = "#{self.class.i18n_scope}.attributes" + I18n.translate(defaults.shift, :default => defaults, :scope => scope, :raise => true) rescue I18n::MissingTranslationData aasm_current_state.to_s.humanize end From 73127e76b6986227e1ebcdc00c87df02169a60fa Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Fri, 14 Jan 2011 13:42:35 +0600 Subject: [PATCH 03/15] added human_event_name() method --- lib/aasm/aasm.rb | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 9029530..92b4f10 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -71,7 +71,7 @@ module AASM end end - + # Instance methods def aasm_current_state return @aasm_current_state if @aasm_current_state @@ -104,20 +104,27 @@ module AASM events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) } events.map {|event| event.name} end - + def human_state - begin - defaults = self.class.lookup_ancestors.map do |klass| - klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{klass_human}.#{aasm_current_state}" - end - scope = "#{self.class.i18n_scope}.attributes" - I18n.translate(defaults.shift, :default => defaults, :scope => scope, :raise => true) - rescue I18n::MissingTranslationData - aasm_current_state.to_s.humanize + defaults = self.class.lookup_ancestors.map do |klass| + klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + :"#{self.class.i18n_scope}.attributes.#{klass_human}.#{aasm_current_state}" end + defaults << aasm_current_state.to_s.humanize + + I18n.translate(defaults.shift, :default => defaults, :raise => true) end - + + def human_event_name(event) + defaults = self.class.lookup_ancestors.map do |klass| + klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + :"#{self.class.i18n_scope}.events.#{klass_human}.#{event}" + end + defaults << event.to_s.humanize + + I18n.translate(defaults.shift, :default => defaults, :raise => true) + end + private def set_aasm_current_state_with_persistence(state) From 651d0209b26d28231af0ed6f497da37651b97b0b Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Fri, 14 Jan 2011 14:29:40 +0600 Subject: [PATCH 04/15] move i18n methods to the own module --- lib/aasm.rb | 1 + lib/aasm/aasm.rb | 40 +++++++++++++++++++++------------------- lib/aasm/i18n.rb | 23 +++++++++++++++++++++++ 3 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 lib/aasm/i18n.rb diff --git a/lib/aasm.rb b/lib/aasm.rb index 659d1d1..ebc0cd3 100644 --- a/lib/aasm.rb +++ b/lib/aasm.rb @@ -7,3 +7,4 @@ require File.join(File.dirname(__FILE__), 'aasm', 'supporting_classes') require File.join(File.dirname(__FILE__), 'aasm', 'state_machine') require File.join(File.dirname(__FILE__), 'aasm', 'persistence') require File.join(File.dirname(__FILE__), 'aasm', 'aasm') +require File.join(File.dirname(__FILE__), 'aasm', 'i18n') diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 92b4f10..00f9201 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -7,6 +7,8 @@ module AASM def self.included(base) #:nodoc: base.extend AASM::ClassMethods + base.send(:include, AASM::I18n::InstanceMethods) + AASM::Persistence.set_persistence(base) unless AASM::StateMachine[base] AASM::StateMachine[base] = AASM::StateMachine.new('') @@ -105,25 +107,25 @@ module AASM events.map {|event| event.name} end - def human_state - defaults = self.class.lookup_ancestors.map do |klass| - klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{self.class.i18n_scope}.attributes.#{klass_human}.#{aasm_current_state}" - end - defaults << aasm_current_state.to_s.humanize - - I18n.translate(defaults.shift, :default => defaults, :raise => true) - end - - def human_event_name(event) - defaults = self.class.lookup_ancestors.map do |klass| - klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{self.class.i18n_scope}.events.#{klass_human}.#{event}" - end - defaults << event.to_s.humanize - - I18n.translate(defaults.shift, :default => defaults, :raise => true) - end + # def human_state + # defaults = self.class.lookup_ancestors.map do |klass| + # klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + # :"#{self.class.i18n_scope}.attributes.#{klass_human}.#{aasm_current_state}" + # end + # defaults << aasm_current_state.to_s.humanize + # + # I18n.translate(defaults.shift, :default => defaults, :raise => true) + # end + # + # def human_event_name(event) + # defaults = self.class.lookup_ancestors.map do |klass| + # klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + # :"#{self.class.i18n_scope}.events.#{klass_human}.#{event}" + # end + # defaults << event.to_s.humanize + # + # I18n.translate(defaults.shift, :default => defaults, :raise => true) + # end private diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb new file mode 100644 index 0000000..029b057 --- /dev/null +++ b/lib/aasm/i18n.rb @@ -0,0 +1,23 @@ +module AASM::I18n + module InstanceMethods + def human_state + _translate("attributes", aasm_current_state) + end + + def human_event_name(event) + _translate("events", event) + end + + private + + def _translate(local_scope, attribute) + defaults = self.class.lookup_ancestors.map do |klass| + klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + :"#{self.class.i18n_scope}.#{local_scope}.#{klass_human}.#{attribute}" + end + defaults << attribute.to_s.humanize + + I18n.translate(defaults.shift, :default => defaults, :raise => true) + end + end +end From a4ffa6351132f9c585b4b5ed2c93c5bf27496156 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Fri, 14 Jan 2011 14:33:14 +0600 Subject: [PATCH 05/15] woopse. forgot to delete this --- lib/aasm/aasm.rb | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 00f9201..161af06 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -106,27 +106,7 @@ module AASM events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) } events.map {|event| event.name} end - - # def human_state - # defaults = self.class.lookup_ancestors.map do |klass| - # klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - # :"#{self.class.i18n_scope}.attributes.#{klass_human}.#{aasm_current_state}" - # end - # defaults << aasm_current_state.to_s.humanize - # - # I18n.translate(defaults.shift, :default => defaults, :raise => true) - # end - # - # def human_event_name(event) - # defaults = self.class.lookup_ancestors.map do |klass| - # klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - # :"#{self.class.i18n_scope}.events.#{klass_human}.#{event}" - # end - # defaults << event.to_s.humanize - # - # I18n.translate(defaults.shift, :default => defaults, :raise => true) - # end - + private def set_aasm_current_state_with_persistence(state) From 5e645cc0fcdb13cec6b5068a4c15ad3b295c1559 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Fri, 14 Jan 2011 15:48:37 +0600 Subject: [PATCH 06/15] temporary hack --- lib/aasm/i18n.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index 029b057..e77cb4f 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -13,10 +13,10 @@ module AASM::I18n def _translate(local_scope, attribute) defaults = self.class.lookup_ancestors.map do |klass| klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{self.class.i18n_scope}.#{local_scope}.#{klass_human}.#{attribute}" + :"#{self.class.i18n_scope}.#{local_scope}.#{klass_human}.state_enum.#{attribute}" end defaults << attribute.to_s.humanize - + I18n.translate(defaults.shift, :default => defaults, :raise => true) end end From c0d6926a13d9cf4d8e21ca6ec09d6fb99e84538d Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Fri, 14 Jan 2011 17:11:33 +0600 Subject: [PATCH 07/15] realize own version of lookup_ancestors due to problem if ancestor class is abstract --- lib/aasm/i18n.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index e77cb4f..56b4025 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -11,7 +11,10 @@ module AASM::I18n private def _translate(local_scope, attribute) - defaults = self.class.lookup_ancestors.map do |klass| + lookuped_ancestors = self.class.ancestors.select do |x| + x.respond_to?(:model_name) unless x == ActiveRecord::Base + end + defaults = lookuped_ancestors.map do |klass| klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore :"#{self.class.i18n_scope}.#{local_scope}.#{klass_human}.state_enum.#{attribute}" end From 20c15f827e8aba7c52aa212cdbf7e18e1262672a Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Fri, 14 Jan 2011 17:15:02 +0600 Subject: [PATCH 08/15] remove temprorary hack --- lib/aasm/i18n.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index 56b4025..f7b1287 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -16,7 +16,7 @@ module AASM::I18n end defaults = lookuped_ancestors.map do |klass| klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{self.class.i18n_scope}.#{local_scope}.#{klass_human}.state_enum.#{attribute}" + :"#{self.class.i18n_scope}.#{local_scope}.#{klass_human}.#{attribute}" end defaults << attribute.to_s.humanize From d43889d7ceaa14e52797fadaf94ae442199c5547 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Fri, 14 Jan 2011 18:12:10 +0600 Subject: [PATCH 09/15] seems this is working solution, but refactoring needed --- lib/aasm/i18n.rb | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index f7b1287..f04746a 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -1,24 +1,29 @@ module AASM::I18n module InstanceMethods def human_state - _translate("attributes", aasm_current_state) + ancestors = self.class.ancestors.select do |x| + x.respond_to?(:model_name) unless x == ActiveRecord::Base + end + + defaults = ancestors.map do |klass| + human_klass = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + :"#{self.class.i18n_scope}.attributes.#{human_klass}.#{self.class.aasm_column}.#{aasm_current_state}" + end + defaults << aasm_current_state.to_s.humanize + + I18n.translate(defaults.shift, :default => defaults, :raise => true) end def human_event_name(event) - _translate("events", event) - end - - private - - def _translate(local_scope, attribute) - lookuped_ancestors = self.class.ancestors.select do |x| + ancestors = self.class.ancestors.select do |x| x.respond_to?(:model_name) unless x == ActiveRecord::Base end - defaults = lookuped_ancestors.map do |klass| - klass_human = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{self.class.i18n_scope}.#{local_scope}.#{klass_human}.#{attribute}" + + defaults = ancestors.map do |klass| + human_klass = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + :"#{self.class.i18n_scope}.events.#{human_klass}.#{event}" end - defaults << attribute.to_s.humanize + defaults << event.to_s.humanize I18n.translate(defaults.shift, :default => defaults, :raise => true) end From b7852e29f854db358e5cddb83269f19b7e9d1cee Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Sat, 12 Feb 2011 23:10:39 +0700 Subject: [PATCH 10/15] added spec for i18n --- spec/en.yml | 10 +++++++ spec/unit/i18n_spec.rb | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 spec/en.yml create mode 100644 spec/unit/i18n_spec.rb diff --git a/spec/en.yml b/spec/en.yml new file mode 100644 index 0000000..5a1a6e5 --- /dev/null +++ b/spec/en.yml @@ -0,0 +1,10 @@ +en: + activerecord: + events: + i18n_test_model: + close: "Let's close it!" + + attributes: + i18n_test_model: + aasm_state: + open: "It's opened now!" diff --git a/spec/unit/i18n_spec.rb b/spec/unit/i18n_spec.rb new file mode 100644 index 0000000..2ac43d3 --- /dev/null +++ b/spec/unit/i18n_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' +require 'active_record' +require 'logger' +require 'i18n' + +ActiveRecord::Base.logger = Logger.new(STDERR) + +class Connection +end + +class I18nTestModel < ActiveRecord::Base + include AASM + + attr_accessor :aasm_state + + aasm_initial_state :open + aasm_state :opened + aasm_state :closed + + aasm_event :close + aasm_event :open +end + +describe AASM::I18n do + before(:all) do + I18n.load_path << 'spec/en.yml' + I18n.default_locale = :en + end + + after(:all) { I18n.load_path.clear } + + before do + connection = mock(Connection, :columns => []) + I18nTestModel.stub!(:connection).and_return(connection) + end + + let (:foo_opened) { I18nTestModel.new } + let (:foo_closed) { I18nTestModel.new.tap {|x| x.aasm_state = :closed } } + + context '.human_state' do + it 'should return translated state value' do + foo_opened.human_state.should == "It's opened now!" + end + + it 'should return humanized value if not localized' do + foo_closed.human_state.should == "Closed" + end + end + + context '.human_event_name' do + it 'should return translated event name' do + I18nTestModel.human_event_name(:close).should == "Let's close it!" + end + + it 'should return humanized event name' do + I18nTestModel.human_event_name(:open).should == "Open" + end + end +end From 898a7dc9ef53b4cd4b9f3a131463b02dc96c9423 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Sat, 12 Feb 2011 23:11:56 +0700 Subject: [PATCH 11/15] human_event_name should be class method --- lib/aasm/aasm.rb | 3 ++- lib/aasm/i18n.rb | 30 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 161af06..93aa0b7 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -7,6 +7,7 @@ module AASM def self.included(base) #:nodoc: base.extend AASM::ClassMethods + base.extend AASM::I18n::ClassMethods base.send(:include, AASM::I18n::InstanceMethods) AASM::Persistence.set_persistence(base) @@ -195,4 +196,4 @@ module AASM event.execute_error_callback(self, e) end end -end \ No newline at end of file +end diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index f04746a..899bdc4 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -1,4 +1,20 @@ module AASM::I18n + module ClassMethods + def human_event_name(event) + ancestors = self.ancestors.select do |x| + x.respond_to?(:model_name) unless x == ActiveRecord::Base + end + + defaults = ancestors.map do |klass| + human_klass = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + :"#{self.i18n_scope}.events.#{human_klass}.#{event}" + end + defaults << event.to_s.humanize + + I18n.translate(defaults.shift, :default => defaults, :raise => true) + end + end + module InstanceMethods def human_state ancestors = self.class.ancestors.select do |x| @@ -13,19 +29,5 @@ module AASM::I18n I18n.translate(defaults.shift, :default => defaults, :raise => true) end - - def human_event_name(event) - ancestors = self.class.ancestors.select do |x| - x.respond_to?(:model_name) unless x == ActiveRecord::Base - end - - defaults = ancestors.map do |klass| - human_klass = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{self.class.i18n_scope}.events.#{human_klass}.#{event}" - end - defaults << event.to_s.humanize - - I18n.translate(defaults.shift, :default => defaults, :raise => true) - end end end From f209c1f992f0bba2cbdf934858eaf0aedfb5d138 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Sun, 13 Feb 2011 02:09:41 +0700 Subject: [PATCH 12/15] refactored module and spec --- lib/aasm/aasm.rb | 9 ++++++-- lib/aasm/i18n.rb | 51 ++++++++++++++++++++---------------------- spec/unit/i18n_spec.rb | 2 +- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 93aa0b7..049fdc3 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -7,8 +7,6 @@ module AASM def self.included(base) #:nodoc: base.extend AASM::ClassMethods - base.extend AASM::I18n::ClassMethods - base.send(:include, AASM::I18n::InstanceMethods) AASM::Persistence.set_persistence(base) unless AASM::StateMachine[base] @@ -73,6 +71,9 @@ module AASM AASM::StateMachine[self].states.map { |state| state.for_select } end + def human_event_name(event) + AASM::I18n.new.human_event_name(self, event) + end end # Instance methods @@ -108,6 +109,10 @@ module AASM events.map {|event| event.name} end + def human_state + AASM::I18n.new.human_state(self) + end + private def set_aasm_current_state_with_persistence(state) diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index 899bdc4..31044ce 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -1,33 +1,30 @@ -module AASM::I18n - module ClassMethods - def human_event_name(event) - ancestors = self.ancestors.select do |x| - x.respond_to?(:model_name) unless x == ActiveRecord::Base - end - - defaults = ancestors.map do |klass| - human_klass = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{self.i18n_scope}.events.#{human_klass}.#{event}" - end - defaults << event.to_s.humanize - - I18n.translate(defaults.shift, :default => defaults, :raise => true) - end +class AASM::I18n + def human_event_name(klass, event) + defaults = ancestors_list(klass).map do |ancestor| + :"#{klass.i18n_scope}.events.#{i18n_klass(ancestor)}.#{event}" + end << event.to_s.humanize + + I18n.translate(defaults.shift, :default => defaults, :raise => true) end - module InstanceMethods - def human_state - ancestors = self.class.ancestors.select do |x| - x.respond_to?(:model_name) unless x == ActiveRecord::Base - end - - defaults = ancestors.map do |klass| - human_klass = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore - :"#{self.class.i18n_scope}.attributes.#{human_klass}.#{self.class.aasm_column}.#{aasm_current_state}" - end - defaults << aasm_current_state.to_s.humanize + def human_state(obj) + klass = obj.class + defaults = ancestors_list(klass).map do |ancestor| + :"#{klass.i18n_scope}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}.#{obj.aasm_current_state}" + end << obj.aasm_current_state.to_s.humanize - I18n.translate(defaults.shift, :default => defaults, :raise => true) + I18n.translate(defaults.shift, :default => defaults, :raise => true) + end + + private + + def i18n_klass(klass) + klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore + end + + def ancestors_list(klass) + klass.ancestors.select do |ancestor| + ancestor.respond_to?(:model_name) unless ancestor == ActiveRecord::Base end end end diff --git a/spec/unit/i18n_spec.rb b/spec/unit/i18n_spec.rb index 2ac43d3..3c707b6 100644 --- a/spec/unit/i18n_spec.rb +++ b/spec/unit/i18n_spec.rb @@ -35,7 +35,7 @@ describe AASM::I18n do end let (:foo_opened) { I18nTestModel.new } - let (:foo_closed) { I18nTestModel.new.tap {|x| x.aasm_state = :closed } } + let (:foo_closed) { I18nTestModel.new.tap { |x| x.aasm_state = :closed } } context '.human_state' do it 'should return translated state value' do From 29ec3399b5e99ac590ac73341ba11b1349418fb1 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Sun, 13 Feb 2011 02:18:53 +0700 Subject: [PATCH 13/15] rails ~> 2.3.0 compatibility --- lib/aasm/i18n.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index 31044ce..bc69ec3 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -1,7 +1,7 @@ class AASM::I18n def human_event_name(klass, event) defaults = ancestors_list(klass).map do |ancestor| - :"#{klass.i18n_scope}.events.#{i18n_klass(ancestor)}.#{event}" + :"#{i18n_scope(klass)}.events.#{i18n_klass(ancestor)}.#{event}" end << event.to_s.humanize I18n.translate(defaults.shift, :default => defaults, :raise => true) @@ -10,7 +10,7 @@ class AASM::I18n def human_state(obj) klass = obj.class defaults = ancestors_list(klass).map do |ancestor| - :"#{klass.i18n_scope}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}.#{obj.aasm_current_state}" + :"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}.#{obj.aasm_current_state}" end << obj.aasm_current_state.to_s.humanize I18n.translate(defaults.shift, :default => defaults, :raise => true) @@ -18,6 +18,10 @@ class AASM::I18n private + def i18n_scope(klass) + klass.respond_to?(:i18n_scope) ? klass.i18n_scope : :activerecord + end + def i18n_klass(klass) klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore end From 09efa3e579d05e6d30e66c43a62954d4e278f0cf Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Sun, 13 Feb 2011 02:20:36 +0700 Subject: [PATCH 14/15] added comments --- lib/aasm/i18n.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index bc69ec3..ad938ea 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -18,10 +18,12 @@ class AASM::I18n private + # added for rails 2.x compatibility def i18n_scope(klass) klass.respond_to?(:i18n_scope) ? klass.i18n_scope : :activerecord end + # added for rails < 3.0.3 compatibility def i18n_klass(klass) klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore end From 8e7921c67ddb5aa4620389a9711939dc24c96ed1 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Mon, 4 Jul 2011 02:09:17 +0700 Subject: [PATCH 15/15] whitespace cleanup --- lib/aasm/i18n.rb | 2 +- spec/unit/i18n_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/aasm/i18n.rb b/lib/aasm/i18n.rb index ad938ea..0776d38 100644 --- a/lib/aasm/i18n.rb +++ b/lib/aasm/i18n.rb @@ -3,7 +3,7 @@ class AASM::I18n defaults = ancestors_list(klass).map do |ancestor| :"#{i18n_scope(klass)}.events.#{i18n_klass(ancestor)}.#{event}" end << event.to_s.humanize - + I18n.translate(defaults.shift, :default => defaults, :raise => true) end diff --git a/spec/unit/i18n_spec.rb b/spec/unit/i18n_spec.rb index 3c707b6..04448f6 100644 --- a/spec/unit/i18n_spec.rb +++ b/spec/unit/i18n_spec.rb @@ -10,7 +10,7 @@ end class I18nTestModel < ActiveRecord::Base include AASM - + attr_accessor :aasm_state aasm_initial_state :open @@ -26,7 +26,7 @@ describe AASM::I18n do I18n.load_path << 'spec/en.yml' I18n.default_locale = :en end - + after(:all) { I18n.load_path.clear } before do