Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #62 from nulogy/add_foreign_key_support

adds rails-compatible support for foreign keys
  • Loading branch information...
commit 6183b04b94658794a886b92b846bac82e3c31c27 2 parents 0ce1891 + 26daa13
@grosser authored
View
4 lib/gettext_i18n_rails.rb
@@ -29,5 +29,9 @@ module GettextI18nRails
ActiveRecord::Base.extend GettextI18nRails::ActiveRecord
end
+if not defined?(Rails::Railtie) and defined?(ActiveModel)
+ require 'gettext_i18n_rails/active_model'
+end
+
require 'gettext_i18n_rails/action_controller' if defined?(ActionController) # so that bundle console can work in a rails project
require 'gettext_i18n_rails/railtie'
View
7 lib/gettext_i18n_rails/active_model.rb
@@ -0,0 +1,7 @@
+module ActiveModel
+ class Name < String
+ def human(options={})
+ _(@klass.humanize_class_name(self))
+ end
+ end
+end
View
13 lib/gettext_i18n_rails/active_record.rb
@@ -5,15 +5,20 @@ def human_attribute_name(attribute, *args)
end
# CarDealer -> _('car dealer')
+ # method deprecated in Rails 3.1
def human_name(*args)
- _(self.human_name_without_translation)
+ _(self.humanize_class_name(self.to_s))
end
- def human_name_without_translation
- self.to_s.underscore.gsub('_',' ')
+ def humanize_class_name(name)
+ name.underscore.humanize
end
def gettext_translation_for_attribute_name(attribute)
- "#{self}|#{attribute.to_s.split('.').map! {|a| a.gsub('_',' ').capitalize }.join('|')}"
+ if attribute.to_s.ends_with?('_id')
+ humanize_class_name(attribute)
+ else
+ "#{self}|#{attribute.to_s.split('.').map! {|a| a.humanize }.join('|')}"
+ end
end
end
View
4 lib/gettext_i18n_rails/railtie.rb
@@ -15,6 +15,10 @@ class Railtie < ::Rails::Railtie
ActiveSupport.on_load :active_record do
extend GettextI18nRails::ActiveRecord
end
+
+ ActiveSupport.on_load :active_model do
+ require 'gettext_i18n_rails/active_model.rb'
+ end
end
end
end
View
19 spec/gettext_i18n_rails/active_model_spec.rb
@@ -0,0 +1,19 @@
+# encoding: utf-8
+require "spec_helper"
+
+class NameTest < ActiveRecord::Base
+end
+
+describe ActiveModel::Name do
+ before do
+ FastGettext.current_cache = {}
+ end
+
+ describe 'human' do
+ it "is translated through FastGettext" do
+ name = ActiveModel::Name.new(NameTest)
+ name.should_receive(:_).with('Name test').and_return('Autositz')
+ name.human.should == 'Autositz'
+ end
+ end
+end
View
8 spec/gettext_i18n_rails/active_record_spec.rb
@@ -38,7 +38,7 @@ class Part < ActiveRecord::Base
describe :human_name do
it "is translated through FastGettext" do
- CarSeat.should_receive(:_).with('car seat').and_return('Autositz')
+ CarSeat.should_receive(:_).with('Car seat').and_return('Autositz')
CarSeat.human_name.should == 'Autositz'
end
end
@@ -55,6 +55,12 @@ class Part < ActiveRecord::Base
end
end
+ describe :gettext_translation_for_attribute_name do
+ it "translates foreign keys to model name keys" do
+ Part.gettext_translation_for_attribute_name('car_seat_id').should == 'Car seat'
+ end
+ end
+
describe 'error messages' do
let(:model){
c = CarSeat.new
Please sign in to comment.
Something went wrong with that request. Please try again.