Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 152 lines (120 sloc) 5.193 kb
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
1 # coding: utf-8
480e559 @yabawock Make tests use require 'spec_helper'
yabawock authored
2 require 'spec_helper'
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
3
4 describe 'Formtastic::I18n' do
5
6 FORMTASTIC_KEYS = [:required, :yes, :no, :create, :update].freeze
7
8 it "should be defined" do
9 lambda { ::Formtastic::I18n }.should_not raise_error(::NameError)
10 end
11
12 describe "default translations" do
13 it "should be defined" do
14 lambda { ::Formtastic::I18n::DEFAULT_VALUES }.should_not raise_error(::NameError)
15 ::Formtastic::I18n::DEFAULT_VALUES.is_a?(::Hash).should == true
16 end
17
18 it "should exists for the core I18n lookup keys" do
19 (::Formtastic::I18n::DEFAULT_VALUES.keys & FORMTASTIC_KEYS).size.should == FORMTASTIC_KEYS.size
20 end
21 end
22
23 describe "when I18n locales are available" do
24
25 before do
26 @formtastic_strings = {
27 :yes => 'Default Yes',
28 :no => 'Default No',
743b26b @yabawock Revert "Reverting two i18n patches pulled in from the rails3 branch whic...
yabawock authored
29 :create => 'Default Create %{model}',
30 :update => 'Default Update %{model}',
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
31 :custom_scope => {
32 :duck => 'Duck',
743b26b @yabawock Revert "Reverting two i18n patches pulled in from the rails3 branch whic...
yabawock authored
33 :duck_pond => '%{ducks} ducks in a pond'
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
34 }
35 }
36 ::I18n.backend.store_translations :en, :formtastic => @formtastic_strings
37 end
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
38
39 after do
40 ::I18n.backend.reload!
41 end
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
42
43 it "should translate core strings correctly" do
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
44 ::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}}
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
45 ::Formtastic::I18n.t(:required).should == "Default Required"
46 ::Formtastic::I18n.t(:yes).should == "Default Yes"
47 ::Formtastic::I18n.t(:no).should == "Default No"
48 ::Formtastic::I18n.t(:create, :model => 'Post').should == "Default Create Post"
49 ::Formtastic::I18n.t(:update, :model => 'Post').should == "Default Update Post"
50 end
51
52 it "should all belong to scope 'formtastic'" do
53 ::Formtastic::I18n.t(:duck, :scope => [:custom_scope]).should == 'Duck'
54 end
55
56 it "should override default I18n lookup args if these are specified" do
57 ::Formtastic::I18n.t(:duck_pond, :scope => [:custom_scope], :ducks => 15).should == '15 ducks in a pond'
58 end
59
60 it "should be possible to override default values" do
61 ::Formtastic::I18n.t(:required, :default => 'Nothing found!').should == 'Nothing found!'
62 end
63
64 end
65
66 describe "when no I18n locales are available" do
67
68 before do
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
69 ::I18n.backend.reload!
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
70 end
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
71
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
72 it "should use default strings" do
73 (::Formtastic::I18n::DEFAULT_VALUES.keys).each do |key|
743b26b @yabawock Revert "Reverting two i18n patches pulled in from the rails3 branch whic...
yabawock authored
74 ::Formtastic::I18n.t(key, :model => '%{model}').should == ::Formtastic::I18n::DEFAULT_VALUES[key]
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
75 end
76 end
77
78 end
79
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
80 describe "I18n string lookups" do
81
82 include FormtasticSpecHelper
83
84 before do
f9c1a8a @yabawock Revert all the changes introduced in commits 8d17ca4..8d8c50
yabawock authored
85 @output_buffer = ''
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
86 mock_everything
87
88 ::I18n.backend.store_translations :en, :formtastic => {
89 :labels => {
90 :title => "Hello world!",
91 :post => {:title => "Hello post!"},
6b53be0 @tjtuom Translations for nested models.
tjtuom authored
92 :project => {:title => "Hello project!", :task => {:name => "Hello task name!"}},
93 :line_item => {:name => "Hello line item name!"}
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
94 }
95 }
96 ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
97
98 @new_post.stub!(:title)
99 @new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 255))
100 end
101
102 after do
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
103 ::I18n.backend.reload!
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
104 ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
105 end
106
f22b7a1 @grimen I18N_SCOPES => I18n::SCOPES
grimen authored
107 it "lookup scopes should be defined" do
108 lambda { ::Formtastic::I18n::SCOPES }.should_not raise_error(::NameError)
109 end
110
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
111 it "should be able to translate with namespaced object" do
d7a729d @yabawock Capture returns from formtastic and append them to the output_buffer if ...
yabawock authored
112 form = semantic_form_for(@new_post) do |builder|
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
113 concat(builder.input(:title))
114 end
ae3d5bc @yabawock Update spec suite to use new helper to check for rails3
yabawock authored
115 output_buffer.concat(form) if Formtastic::Util.rails3?
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
116 output_buffer.should have_tag("form label", /Hello post!/)
117 end
118
119 it "should be able to translate without form-object" do
d7a729d @yabawock Capture returns from formtastic and append them to the output_buffer if ...
yabawock authored
120 form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
121 concat(builder.input(:title))
122 end
ae3d5bc @yabawock Update spec suite to use new helper to check for rails3
yabawock authored
123 output_buffer.concat(form) if Formtastic::Util.rails3?
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
124 output_buffer.should have_tag("form label", /Hello project!/)
125 end
126
6b53be0 @tjtuom Translations for nested models.
tjtuom authored
127 it 'should be able to translate nested objects with nested translations' do
c96809c @yabawock Fix tests for nested objects
yabawock authored
128 form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
6b53be0 @tjtuom Translations for nested models.
tjtuom authored
129 builder.semantic_fields_for(:task) do |f|
130 concat(f.input(:name))
131 end
132 end
c96809c @yabawock Fix tests for nested objects
yabawock authored
133 output_buffer.concat(form) if Formtastic::Util.rails3?
6b53be0 @tjtuom Translations for nested models.
tjtuom authored
134 output_buffer.should have_tag("form label", /Hello task name!/)
135 end
136
137 it 'should be able to translated nested objects with top level translations' do
c96809c @yabawock Fix tests for nested objects
yabawock authored
138 form = semantic_form_for(:order, :url => 'http://test.host') do |builder|
6b53be0 @tjtuom Translations for nested models.
tjtuom authored
139 builder.semantic_fields_for(:line_item) do |f|
140 concat(f.input(:name))
141 end
142 end
c96809c @yabawock Fix tests for nested objects
yabawock authored
143 output_buffer.concat(form) if Formtastic::Util.rails3?
6b53be0 @tjtuom Translations for nested models.
tjtuom authored
144 output_buffer.should have_tag("form label", /Hello line item name!/)
145 end
146
147
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
148 # TODO: Add spec for namespaced models?
149
150 end
151
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
152 end
Something went wrong with that request. Please try again.