Skip to content

Commit

Permalink
Merge pull request #13 from johnnyshields/fix-empty-values
Browse files Browse the repository at this point in the history
Value pass-through + fix using locales not present in translations hash
  • Loading branch information
tiagogodinho committed May 8, 2015
2 parents bacf361 + 99bddb8 commit d066033
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/localized_fields/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def create_field(attribute, options)

value = translations.has_key?(language.to_s) ? translations[language.to_s] : nil

options = options.merge(value: value, id: "#{object_name}_#{attribute}_translations_#{language}", name: "#{object_name}[#{attribute}_translations][#{language}]")
options = options.merge(id: "#{object_name}_#{attribute}_translations_#{language}", name: "#{object_name}[#{attribute}_translations][#{language}]")
else
value = @object ? @object[attribute.to_s] : nil

options = options.merge(value: value)
end

options[:value] ||= value || ''

return attribute, options
end
end
Expand Down
72 changes: 60 additions & 12 deletions spec/localized_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

expected = %{<dl class="field">}
expected << %{<dt><label for="post_title_translations_en">Title</label></dt>}
expected << %{<dd><input id="post_title_translations_en" name="post[title_translations][en]" type="text" /></dd>}
expected << %{<dd><input id="post_title_translations_en" name="post[title_translations][en]" value="" type="text" /></dd>}
expected << %{</dl>}
expected << %{<dl class="field">}
expected << %{<dt><label for="post_title_translations_pt">Title</label></dt>}
expected << %{<dd><input id="post_title_translations_pt" name="post[title_translations][pt]" type="text" /></dd>}
expected << %{<dd><input id="post_title_translations_pt" name="post[title_translations][pt]" value="" type="text" /></dd>}
expected << %{</dl>}

expect(output).to eq(expected)
Expand All @@ -42,8 +42,8 @@
%{<div>#{localized_field.text_field(:title).html_safe}</div>}.html_safe
end

expected = %{<div><input id="post_title_translations_en" name="post[title_translations][en]" type="text" /></div>}
expected << %{<div><input id="post_title_translations_pt" name="post[title_translations][pt]" type="text" /></div>}
expected = %{<div><input id="post_title_translations_en" name="post[title_translations][en]" value="" type="text" /></div>}
expected << %{<div><input id="post_title_translations_pt" name="post[title_translations][pt]" value="" type="text" /></div>}

expect(output).to eq(expected)
end
Expand All @@ -53,7 +53,7 @@
%{<div>#{localized_field.text_field(:en).html_safe}</div>}.html_safe
end

expected = %{<div><input type="text" name="post[title_translations][en]" id="post_title_translations_en" /></div>}
expected = %{<div><input value="" type="text" name="post[title_translations][en]" id="post_title_translations_en" /></div>}

expect(output).to eq(expected)
end
Expand Down Expand Up @@ -131,7 +131,7 @@
localized_field.text_field :en
end

expected = %{<input type="text" name="post[title_translations][en]" id="post_title_translations_en" />}
expected = %{<input value="" type="text" name="post[title_translations][en]" id="post_title_translations_en" />}

expect(output).to eq(expected)
end
Expand All @@ -141,8 +141,8 @@
localized_field.text_field :title
end

expected = %{<input id="post_title_translations_en" name="post[title_translations][en]" type="text" />}
expected << %{<input id="post_title_translations_pt" name="post[title_translations][pt]" type="text" />}
expected = %{<input id="post_title_translations_en" name="post[title_translations][en]" value="" type="text" />}
expected << %{<input id="post_title_translations_pt" name="post[title_translations][pt]" value="" type="text" />}

expect(output).to eq(expected)
end
Expand All @@ -152,21 +152,58 @@
localized_field.text_field :title, class: 'field'
end

expected = %{<input class="field" id="post_title_translations_en" name="post[title_translations][en]" type="text" />}
expected << %{<input class="field" id="post_title_translations_pt" name="post[title_translations][pt]" type="text" />}
expected = %{<input class="field" id="post_title_translations_en" name="post[title_translations][en]" value="" type="text" />}
expected << %{<input class="field" id="post_title_translations_pt" name="post[title_translations][pt]" value="" type="text" />}

expect(output).to eq(expected)
end

context 'post with values' do
before do
post.stub(:title_translations).and_return({ 'en' => 'title en', 'pt' => 'title pt' })
end

it 'returns a text_area tag for en' do
output = builder.localized_fields(:title) do |localized_field|
localized_field.text_field :en
end

expected = %{<input value="title en" type="text" name="post[title_translations][en]" id="post_title_translations_en" />}

expect(output).to eq(expected)
end

it 'returns a text_area tag for ja' do
output = builder.localized_fields(:title) do |localized_field|
localized_field.text_field :ja
end

expected = %{<input value="" type="text" name="post[title_translations][ja]" id="post_title_translations_ja" />}

expect(output).to eq(expected)
end

it 'returns a text_area tag for all languages' do
output = builder.localized_fields do |localized_field|
localized_field.text_field :title
end

expected = %{<input id="post_title_translations_en" name="post[title_translations][en]" value="title en" type="text" />}
expected << %{<input id="post_title_translations_pt" name="post[title_translations][pt]" value="title pt" type="text" />}

expect(output).to eq(expected)
end
end
end

describe 'text_area' do
it 'returns a text_area tag for en' do
output = builder.localized_fields(:title) do |localized_field|
localized_field.text_area :en, value: 'text'
localized_field.text_area :en, value: 'My Value'
end

expected = %{<textarea name="post[title_translations][en]" id="post_title_translations_en">\n}
expected << %{</textarea>}
expected << %{My Value</textarea>}

expect(output).to eq(expected)
end
Expand Down Expand Up @@ -213,6 +250,17 @@
expect(output).to eq(expected)
end

it 'returns a text_area tag for ja' do
output = builder.localized_fields(:title) do |localized_field|
localized_field.text_area :ja
end

expected = %{<textarea name="post[title_translations][ja]" id="post_title_translations_ja">\n}
expected << %{</textarea>}

expect(output).to eq(expected)
end

it 'returns a text_area tag for all languages' do
output = builder.localized_fields do |localized_field|
localized_field.text_area :title
Expand Down

0 comments on commit d066033

Please sign in to comment.