Skip to content

Commit

Permalink
Fix locale for ActiveRecord getting added to the i18n load path multi…
Browse files Browse the repository at this point in the history
…ple times
  • Loading branch information
mixr authored and obrie committed Jun 5, 2009
1 parent b578e79 commit 01fd1c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,6 @@
== master

* Fix locale for ActiveRecord getting added to the i18n load path multiple times [Reiner Dieterich]
* Fix callbacks, guards, and state-driven behaviors not always working on tainted classes [Brandon Dimcheff]
* Use Ruby 1.9's built-in Object#instance_exec for bound callbacks when it's available
* Improve performance of cached dynamic state lookups by 25%
Expand Down
6 changes: 5 additions & 1 deletion lib/state_machine/integrations/active_record.rb
Expand Up @@ -274,7 +274,11 @@ def self.matches?(klass)
# Loads additional files specific to ActiveRecord
def self.extended(base) #:nodoc:
require 'state_machine/integrations/active_record/observer'
I18n.load_path << "#{File.dirname(__FILE__)}/active_record/locale.rb" if Object.const_defined?(:I18n)

if Object.const_defined?(:I18n)
locale = "#{File.dirname(__FILE__)}/active_record/locale.rb"
I18n.load_path << locale unless I18n.load_path.include?(locale)
end
end

# Adds a validation error to the given object
Expand Down
25 changes: 17 additions & 8 deletions test/unit/integrations/active_record_test.rb
Expand Up @@ -965,16 +965,25 @@ def test_should_invalidate_using_customized_i18n_key_if_specified
machine.invalidate(record, :state, :invalid_transition, [[:event, :ignite]])
assert_equal 'cannot ignite', record.errors.on(:state)
end
end

def test_should_invalidate_using_customized_i18n_string_if_specified
machine = StateMachine::Machine.new(@model, :messages => {:invalid_transition => 'cannot {{event}}'})
machine.state :parked, :idling

record = @model.new(:state => 'idling')
def test_should_invalidate_using_customized_i18n_string_if_specified
machine = StateMachine::Machine.new(@model, :messages => {:invalid_transition => 'cannot {{event}}'})
machine.state :parked, :idling

record = @model.new(:state => 'idling')

machine.invalidate(record, :state, :invalid_transition, [[:event, :ignite]])
assert_equal 'cannot ignite', record.errors.on(:state)
end

machine.invalidate(record, :state, :invalid_transition, [[:event, :ignite]])
assert_equal 'cannot ignite', record.errors.on(:state)
def test_should_only_add_locale_once_in_load_path
assert_equal 1, I18n.load_path.select {|path| path =~ %r{state_machine/integrations/active_record/locale\.rb$}}.length

# Create another ActiveRecord model that will triger the i18n feature
new_model

assert_equal 1, I18n.load_path.select {|path| path =~ %r{state_machine/integrations/active_record/locale\.rb$}}.length
end
end
else
$stderr.puts 'Skipping ActiveRecord I18n tests. `gem install active_record` >= v2.2.0 and try again.'
Expand Down

0 comments on commit 01fd1c4

Please sign in to comment.