Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActiveRecord translations support #35

Closed
wants to merge 3 commits into from
Closed
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
15 changes: 14 additions & 1 deletion lib/reform/form/active_model/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ def self.included(includer)
class << self
extend Uber::Delegates
# # Hooray! Delegate translation back to Reform's Validator class which contains AM::Validations.
delegates :active_model_really_sucks, :human_attribute_name, :lookup_ancestors, :i18n_scope # Rails 3.1.
delegates :active_model_really_sucks, :human_attribute_name, :lookup_ancestors # Rails 3.1.

def validation_group_class
Group
end

def i18n_scope
:activemodel
end

# this is to allow calls like Form::human_attribute_name (note that this is on the CLASS level) to be resolved.
# those calls happen when adding errors in a custom validation method, which is defined on the form (as an instance method).
def active_model_really_sucks
Class.new(Validator).tap do |v|
v.model_name = model_name
v.i18n_scope = i18n_scope
end
end
end
Expand Down Expand Up @@ -78,6 +83,14 @@ def model_name=(name)
@_active_model_sucks = name
end

def i18n_scope
@i18n_scope || super
end

def i18n_scope=(value)
@i18n_scope = value
end

def validates(*args, &block)
super(*Declarative::DeepDup.(args), &block)
end
Expand Down
9 changes: 6 additions & 3 deletions lib/reform/form/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ def self.included(base)
include Reform::Form::ActiveModel
include Reform::Form::ORM
extend ClassMethods

class << self
def i18n_scope
:activerecord
end
end
end
end

Expand All @@ -15,9 +21,6 @@ def validates_uniqueness_of(attribute, options={})
options = options.merge(:attributes => [attribute])
validates_with(UniquenessValidator, options)
end
def i18n_scope
:activerecord
end
end

def to_nested_hash(*)
Expand Down
2 changes: 1 addition & 1 deletion lib/reform/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def active_model!
Reform::Form.class_eval do
include Reform::Form::ActiveModel
include Reform::Form::ActiveModel::FormBuilderMethods
include Reform::Form::ActiveModel::Validations
include Reform::Form::ActiveRecord if defined?(ActiveRecord)
include Reform::Form::Mongoid if defined?(Mongoid)
include Reform::Form::ActiveModel::Validations
end
end

Expand Down
28 changes: 28 additions & 0 deletions test/active_model_translations_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'test_helper'

class ActiveModelTranslationsTest < MiniTest::Spec
class SongForm < Reform::Form
property :title
property :created_at
end

before do
I18n.backend.store_translations(:en,
activemodel: {
attributes: {
'active_model_translations_test/song' => {
title: 'Song title',
}
}
}
)
end

it 'translate attribute with I18n' do
SongForm.human_attribute_name(:title).must_equal 'Song title'
end

it 'translate attribute without I18n' do
SongForm.human_attribute_name(:created_at).must_equal 'Created at'
end
end
3 changes: 0 additions & 3 deletions test/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,3 @@ class ActiveModelAlbumForm < Reform::Form
end
end
end



32 changes: 32 additions & 0 deletions test/active_record_translations_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'test_helper'
require 'reform/active_record'

class ActiveRecordTranslationsTest < MiniTest::Spec
class SongForm < Reform::Form
include Reform::Form::ActiveModel::Validations
include Reform::Form::ActiveRecord

property :title
property :created_at
end

before do
I18n.backend.store_translations(:en,
activerecord: {
attributes: {
'active_record_translations_test/song' => {
title: 'Song title',
}
}
}
)
end

it 'translate attribute with I18n' do
SongForm.human_attribute_name(:title).must_equal 'Song title'
end

it 'translate attribute without I18n' do
SongForm.human_attribute_name(:created_at).must_equal 'Created at'
end
end