Skip to content

Commit

Permalink
Use I18n for link_to_*_association texts
Browse files Browse the repository at this point in the history
  • Loading branch information
ViliusLuneckas committed Aug 7, 2015
1 parent 7ca848c commit 15f96d7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/cocoon/view_helpers.rb
Expand Up @@ -19,6 +19,12 @@ def link_to_remove_association(*args, &block)
html_options = args.second || {}
name = capture(&block)
link_to_remove_association(name, f, html_options)
elsif args[0].respond_to?(:object)
f = args.first
html_options = args.second || {}
association = f.object.model_name.to_s.tableize
name = I18n.translate("cocoon.#{association}.remove", default: I18n.translate('cocoon.defaults.remove'))
link_to_remove_association(name, f, html_options)
else
name = args[0]
f = args[1]
Expand Down Expand Up @@ -71,6 +77,12 @@ def link_to_add_association(*args, &block)
association = args[1]
html_options = args[2] || {}
link_to_add_association(capture(&block), f, association, html_options)
elsif args[0].respond_to?(:object)
f = args[0]
association = args[1]
html_options = args[2] || {}
name = I18n.translate("cocoon.#{association}.add", default: I18n.translate('cocoon.defaults.add'))
link_to_add_association(name, f, association, html_options)
else
name = args[0]
f = args[1]
Expand Down
44 changes: 44 additions & 0 deletions spec/cocoon_spec.rb
Expand Up @@ -33,6 +33,28 @@ class TestClass < ActionView::Base
it_behaves_like "a correctly rendered add link", {}
end

context 'and no name given' do
context 'custom translation exists' do
before do
I18n.backend.store_translations(:en, :cocoon => { :comments => { :add => 'Add comment' } })

@html = @tester.link_to_add_association(@form_obj, :comments)
end

it_behaves_like "a correctly rendered add link", { text: 'Add comment' }
end

context 'uses default translation' do
before do
I18n.backend.store_translations(:en, :cocoon => { :defaults => { :add => 'Add' } })

@html = @tester.link_to_add_association(@form_obj, :comments)
end

it_behaves_like "a correctly rendered add link", { text: 'Add' }
end
end

context "and given html options to pass them to link_to" do
before do
@html = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'})
Expand Down Expand Up @@ -267,6 +289,28 @@ class TestClass < ActionView::Base
it_behaves_like "a correctly rendered remove link", {}
end

context 'no name given' do
context 'custom translation exists' do
before do
I18n.backend.store_translations(:en, :cocoon => { :posts => { :remove => 'Remove post' } })

@html = @tester.link_to_remove_association(@form_obj)
end

it_behaves_like "a correctly rendered remove link", { text: 'Remove post' }
end

context 'uses default translation' do
before do
I18n.backend.store_translations(:en, :cocoon => { :defaults => { :remove => 'Remove' } })

@html = @tester.link_to_remove_association(@form_obj)
end

it_behaves_like "a correctly rendered remove link", { text: 'Remove' }
end
end

context "accepts html options and pass them to link_to" do
before do
@html = @tester.link_to_remove_association('remove something', @form_obj, {:class => 'add_some_class', :'data-something' => 'bla'})
Expand Down
3 changes: 3 additions & 0 deletions spec/support/i18n.rb
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.after(:each) { I18n.reload! }
end

0 comments on commit 15f96d7

Please sign in to comment.