techthumb-formtastic 1.rails3.sha
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.textile +584 -0
- data/Rakefile +127 -0
- data/generators/form/USAGE +16 -0
- data/generators/form/form_generator.rb +120 -0
- data/generators/form/templates/view__form.html.erb +5 -0
- data/generators/form/templates/view__form.html.haml +4 -0
- data/generators/formtastic/formtastic_generator.rb +24 -0
- data/generators/formtastic/templates/formtastic.css +131 -0
- data/generators/formtastic/templates/formtastic.rb +54 -0
- data/generators/formtastic/templates/formtastic_changes.css +14 -0
- data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +16 -0
- data/init.rb +5 -0
- data/lib/formtastic/i18n.rb +35 -0
- data/lib/formtastic/layout_helper.rb +10 -0
- data/lib/formtastic/railtie.rb +12 -0
- data/lib/formtastic/util.rb +36 -0
- data/lib/formtastic.rb +1870 -0
- data/lib/generators/formtastic/form/form_generator.rb +86 -0
- data/lib/generators/formtastic/install/install_generator.rb +24 -0
- data/lib/locale/en.yml +8 -0
- data/rails/init.rb +2 -0
- data/spec/buttons_spec.rb +166 -0
- data/spec/commit_button_spec.rb +401 -0
- data/spec/custom_builder_spec.rb +98 -0
- data/spec/defaults_spec.rb +20 -0
- data/spec/error_proc_spec.rb +27 -0
- data/spec/errors_spec.rb +105 -0
- data/spec/form_helper_spec.rb +142 -0
- data/spec/helpers/layout_helper_spec.rb +21 -0
- data/spec/i18n_spec.rb +152 -0
- data/spec/include_blank_spec.rb +74 -0
- data/spec/input_spec.rb +786 -0
- data/spec/inputs/boolean_input_spec.rb +104 -0
- data/spec/inputs/check_boxes_input_spec.rb +426 -0
- data/spec/inputs/country_input_spec.rb +118 -0
- data/spec/inputs/date_input_spec.rb +168 -0
- data/spec/inputs/datetime_input_spec.rb +310 -0
- data/spec/inputs/file_input_spec.rb +34 -0
- data/spec/inputs/hidden_input_spec.rb +78 -0
- data/spec/inputs/numeric_input_spec.rb +44 -0
- data/spec/inputs/password_input_spec.rb +46 -0
- data/spec/inputs/radio_input_spec.rb +243 -0
- data/spec/inputs/select_input_spec.rb +546 -0
- data/spec/inputs/string_input_spec.rb +64 -0
- data/spec/inputs/text_input_spec.rb +34 -0
- data/spec/inputs/time_input_spec.rb +206 -0
- data/spec/inputs/time_zone_input_spec.rb +110 -0
- data/spec/inputs_spec.rb +476 -0
- data/spec/label_spec.rb +89 -0
- data/spec/semantic_errors_spec.rb +98 -0
- data/spec/semantic_fields_for_spec.rb +45 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +289 -0
- data/spec/support/custom_macros.rb +494 -0
- data/spec/support/output_buffer.rb +4 -0
- data/spec/support/test_environment.rb +45 -0
- metadata +235 -0
@@ -0,0 +1,98 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'Formtastic::SemanticFormHelper.builder' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
class MyCustomFormBuilder < ::Formtastic::SemanticFormBuilder
|
9
|
+
def awesome_input(method, options)
|
10
|
+
self.text_field(method)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
before do
|
15
|
+
@output_buffer = ''
|
16
|
+
mock_everything
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'is the Formtastic::SemanticFormBuilder by default' do
|
20
|
+
::Formtastic::SemanticFormHelper.builder.should == ::Formtastic::SemanticFormBuilder
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'can be configured to use your own custom form builder' do
|
24
|
+
# Set it to a custom builder class
|
25
|
+
::Formtastic::SemanticFormHelper.builder = MyCustomFormBuilder
|
26
|
+
::Formtastic::SemanticFormHelper.builder.should == MyCustomFormBuilder
|
27
|
+
|
28
|
+
# Reset it to the default
|
29
|
+
::Formtastic::SemanticFormHelper.builder = ::Formtastic::SemanticFormBuilder
|
30
|
+
::Formtastic::SemanticFormHelper.builder.should == ::Formtastic::SemanticFormBuilder
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should allow custom settings per form builder subclass' do
|
34
|
+
with_config(:all_fields_required_by_default, true) do
|
35
|
+
MyCustomFormBuilder.all_fields_required_by_default = false
|
36
|
+
|
37
|
+
MyCustomFormBuilder.all_fields_required_by_default.should be_false
|
38
|
+
::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should be_true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "when using a custom builder" do
|
43
|
+
|
44
|
+
before do
|
45
|
+
@new_post.stub!(:title)
|
46
|
+
::Formtastic::SemanticFormHelper.builder = MyCustomFormBuilder
|
47
|
+
end
|
48
|
+
|
49
|
+
after do
|
50
|
+
::Formtastic::SemanticFormHelper.builder = ::Formtastic::SemanticFormBuilder
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "semantic_form_for" do
|
54
|
+
|
55
|
+
it "should yield an instance of the custom builder" do
|
56
|
+
semantic_form_for(@new_post) do |builder|
|
57
|
+
builder.class.should == MyCustomFormBuilder
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should allow me to call my custom input" do
|
62
|
+
semantic_form_for(@new_post) do |builder|
|
63
|
+
concat(builder.input(:title, :as => :awesome))
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "semantic_fields_for" do
|
70
|
+
|
71
|
+
it "should yield an instance of the parent form builder" do
|
72
|
+
semantic_form_for(@new_post) do |builder|
|
73
|
+
builder.semantic_fields_for(:author) do |nested_builder|
|
74
|
+
nested_builder.class.should == MyCustomFormBuilder
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "when using a builder passed to form options" do
|
84
|
+
|
85
|
+
describe "semantic_fields_for" do
|
86
|
+
|
87
|
+
it "should yield an instance of the parent form builder" do
|
88
|
+
semantic_form_for(@new_post, :builder => MyCustomFormBuilder) do |builder|
|
89
|
+
builder.semantic_fields_for(:author) do |nested_builder|
|
90
|
+
nested_builder.class.should == MyCustomFormBuilder
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'Formtastic::SemanticFormBuilder-defaults' do
|
5
|
+
|
6
|
+
# Note: This spec might make better sense somewhere else. Just temporary.
|
7
|
+
|
8
|
+
describe "required string" do
|
9
|
+
|
10
|
+
it "should render proc with I18n correctly" do
|
11
|
+
::I18n.backend.store_translations :en, :formtastic => {:required => 'Haha!'}
|
12
|
+
|
13
|
+
required_string = Formtastic::SemanticFormBuilder.required_string
|
14
|
+
required_string = required_string.is_a?(::Proc) ? required_string.call : required_string.to_s
|
15
|
+
required_string.should == %{<abbr title="Haha!">*</abbr>}
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'Rails field_error_proc' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should not be overridden globally for all form builders" do
|
14
|
+
current_field_error_proc = ::ActionView::Base.field_error_proc
|
15
|
+
|
16
|
+
semantic_form_for(@new_post) do |builder|
|
17
|
+
::ActionView::Base.field_error_proc.should_not == current_field_error_proc
|
18
|
+
end
|
19
|
+
|
20
|
+
::ActionView::Base.field_error_proc.should == current_field_error_proc
|
21
|
+
|
22
|
+
form_for(@new_post) do |builder|
|
23
|
+
::ActionView::Base.field_error_proc.should == current_field_error_proc
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/spec/errors_spec.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'SemanticFormBuilder#errors_on' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
@title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome']
|
12
|
+
@errors = mock('errors')
|
13
|
+
@new_post.stub!(:errors).and_return(@errors)
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'when there are errors' do
|
17
|
+
before do
|
18
|
+
@errors.stub!(:[]).with(:title).and_return(@title_errors)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should render a paragraph with the errors joined into a sentence when inline_errors config is :sentence' do
|
22
|
+
::Formtastic::SemanticFormBuilder.inline_errors = :sentence
|
23
|
+
semantic_form_for(@new_post) do |builder|
|
24
|
+
builder.errors_on(:title).should have_tag('p.inline-errors', @title_errors.to_sentence)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should render an unordered list with the class errors when inline_errors config is :list' do
|
29
|
+
::Formtastic::SemanticFormBuilder.inline_errors = :list
|
30
|
+
semantic_form_for(@new_post) do |builder|
|
31
|
+
builder.errors_on(:title).should have_tag('ul.errors')
|
32
|
+
@title_errors.each do |error|
|
33
|
+
builder.errors_on(:title).should have_tag('ul.errors li', error)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should render a paragraph with the first error when inline_errors config is :first' do
|
39
|
+
::Formtastic::SemanticFormBuilder.inline_errors = :first
|
40
|
+
semantic_form_for(@new_post) do |builder|
|
41
|
+
builder.errors_on(:title).should have_tag('p.inline-errors', @title_errors.first)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should return nil when inline_errors config is :none' do
|
46
|
+
::Formtastic::SemanticFormBuilder.inline_errors = :none
|
47
|
+
semantic_form_for(@new_post) do |builder|
|
48
|
+
builder.errors_on(:title).should be_nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'when there are no errors (nil)' do
|
55
|
+
before do
|
56
|
+
@errors.stub!(:[]).with(:title).and_return(nil)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should return nil when inline_errors config is :sentence, :list or :none' do
|
60
|
+
[:sentence, :list, :none].each do |config|
|
61
|
+
::Formtastic::SemanticFormBuilder.inline_errors = config
|
62
|
+
semantic_form_for(@new_post) do |builder|
|
63
|
+
builder.errors_on(:title).should be_nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'when there are no errors (empty array)' do
|
70
|
+
before do
|
71
|
+
@errors.stub!(:[]).with(:title).and_return([])
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should return nil when inline_errors config is :sentence, :list or :none' do
|
75
|
+
[:sentence, :list, :none].each do |config|
|
76
|
+
::Formtastic::SemanticFormBuilder.inline_errors = config
|
77
|
+
semantic_form_for(@new_post) do |builder|
|
78
|
+
builder.errors_on(:title).should be_nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
describe 'when there are errors on the association and column' do
|
86
|
+
|
87
|
+
it "should list all unique errors" do
|
88
|
+
::Formtastic::SemanticFormBuilder.inline_errors = :list
|
89
|
+
::Post.stub!(:reflections).and_return({:author => mock('reflection', :options => {}, :macro => :belongs_to)})
|
90
|
+
|
91
|
+
@errors.stub!(:[]).with(:author).and_return(['must not be blank'])
|
92
|
+
@errors.stub!(:[]).with(:author_id).and_return(['is already taken', 'must not be blank']) # note the duplicate of association
|
93
|
+
|
94
|
+
form = semantic_form_for(@new_post) do |builder|
|
95
|
+
concat(builder.input(:author))
|
96
|
+
end
|
97
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
98
|
+
output_buffer.should have_tag("ul.errors li", /must not be blank/, :count => 1)
|
99
|
+
output_buffer.should have_tag("ul.errors li", /is already taken/, :count => 1)
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'SemanticFormHelper' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#semantic_form_for' do
|
14
|
+
|
15
|
+
it 'yields an instance of SemanticFormBuilder' do
|
16
|
+
semantic_form_for(@new_post, :url => '/hello') do |builder|
|
17
|
+
builder.class.should == ::Formtastic::SemanticFormBuilder
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'adds a class of "formtastic" to the generated form' do
|
22
|
+
form = semantic_form_for(@new_post, :url => '/hello') do |builder|
|
23
|
+
end
|
24
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
25
|
+
output_buffer.should have_tag("form.formtastic")
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'adds class matching the object name to the generated form when a symbol is provided' do
|
29
|
+
form = semantic_form_for(@new_post, :url => '/hello') do |builder|
|
30
|
+
end
|
31
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
32
|
+
output_buffer.should have_tag("form.post")
|
33
|
+
|
34
|
+
form = semantic_form_for(:project, :url => '/hello') do |builder|
|
35
|
+
end
|
36
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
37
|
+
output_buffer.should have_tag("form.project")
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'adds class matching the object\'s class to the generated form when an object is provided' do
|
41
|
+
form = semantic_form_for(@new_post) do |builder|
|
42
|
+
end
|
43
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
44
|
+
output_buffer.should have_tag("form.post")
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'adds a namespaced class to the generated form' do
|
48
|
+
form = semantic_form_for(::Namespaced::Post.new, :url => '/hello') do |builder|
|
49
|
+
end
|
50
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
51
|
+
output_buffer.should have_tag("form.namespaced_post")
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'allows :html options' do
|
55
|
+
before(:each) do
|
56
|
+
@form = semantic_form_for(@new_post, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'to add a id of "something-special" to generated form' do
|
61
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
62
|
+
output_buffer.should have_tag("form#something-special")
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'to add a class of "something-extra" to generated form' do
|
66
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
67
|
+
output_buffer.should have_tag("form.something-extra")
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'to add enctype="multipart/form-data"' do
|
71
|
+
output_buffer.concat(@form) if Formtastic::Util.rails3?
|
72
|
+
output_buffer.should have_tag('form[@enctype="multipart/form-data"]')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'can be called with a resource-oriented style' do
|
77
|
+
semantic_form_for(@new_post) do |builder|
|
78
|
+
builder.object.class.should == ::Post
|
79
|
+
builder.object_name.should == "post"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'can be called with a generic style and instance variable' do
|
84
|
+
if rails3?
|
85
|
+
semantic_form_for(@new_post, :as => :post, :url => new_post_path) do |builder|
|
86
|
+
builder.object.class.should == ::Post
|
87
|
+
builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
|
88
|
+
end
|
89
|
+
end
|
90
|
+
if rails2?
|
91
|
+
semantic_form_for(:post, @new_post, :url => new_post_path) do |builder|
|
92
|
+
builder.object.class.should == ::Post
|
93
|
+
builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'can be called with a generic style and inline object' do
|
99
|
+
semantic_form_for(@new_post, :url => new_post_path) do |builder|
|
100
|
+
builder.object.class.should == ::Post
|
101
|
+
builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "with :builder option" do
|
106
|
+
it "yields an instance of the given builder" do
|
107
|
+
class MyAwesomeCustomBuilder < ::Formtastic::SemanticFormBuilder
|
108
|
+
end
|
109
|
+
semantic_form_for(@new_post, :url => '/hello', :builder => MyAwesomeCustomBuilder) do |builder|
|
110
|
+
builder.class.should == MyAwesomeCustomBuilder
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
describe '#semantic_fields_for' do
|
118
|
+
it 'yields an instance of SemanticFormBuilder' do
|
119
|
+
semantic_fields_for(@new_post, :url => '/hello') do |builder|
|
120
|
+
builder.class.should == ::Formtastic::SemanticFormBuilder
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#semantic_form_remote_for' do
|
126
|
+
it 'yields an instance of SemanticFormBuilder' do
|
127
|
+
semantic_form_remote_for(@new_post, :url => '/hello') do |builder|
|
128
|
+
builder.class.should == ::Formtastic::SemanticFormBuilder
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe '#semantic_form_for_remote' do
|
134
|
+
it 'yields an instance of SemanticFormBuilder' do
|
135
|
+
semantic_remote_form_for(@new_post, :url => '/hello') do |builder|
|
136
|
+
builder.class.should == ::Formtastic::SemanticFormBuilder
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Formtastic::LayoutHelper do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
include Formtastic::LayoutHelper
|
8
|
+
|
9
|
+
describe '#formtastic_stylesheet_link_tag' do
|
10
|
+
|
11
|
+
it 'should render a link to formtastic.css' do
|
12
|
+
formtastic_stylesheet_link_tag.should have_tag("link[@href='/stylesheets/formtastic.css']")
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should render a link to formtastic_changes.css' do
|
16
|
+
formtastic_stylesheet_link_tag.should have_tag("link[@href='/stylesheets/formtastic_changes.css']")
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
data/spec/i18n_spec.rb
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
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',
|
29
|
+
:create => 'Default Create %{model}',
|
30
|
+
:update => 'Default Update %{model}',
|
31
|
+
:custom_scope => {
|
32
|
+
:duck => 'Duck',
|
33
|
+
:duck_pond => '%{ducks} ducks in a pond'
|
34
|
+
}
|
35
|
+
}
|
36
|
+
::I18n.backend.store_translations :en, :formtastic => @formtastic_strings
|
37
|
+
end
|
38
|
+
|
39
|
+
after do
|
40
|
+
::I18n.backend.reload!
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should translate core strings correctly" do
|
44
|
+
::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}}
|
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
|
69
|
+
::I18n.backend.reload!
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should use default strings" do
|
73
|
+
(::Formtastic::I18n::DEFAULT_VALUES.keys).each do |key|
|
74
|
+
::Formtastic::I18n.t(key, :model => '%{model}').should == ::Formtastic::I18n::DEFAULT_VALUES[key]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "I18n string lookups" do
|
81
|
+
|
82
|
+
include FormtasticSpecHelper
|
83
|
+
|
84
|
+
before do
|
85
|
+
@output_buffer = ''
|
86
|
+
mock_everything
|
87
|
+
|
88
|
+
::I18n.backend.store_translations :en, :formtastic => {
|
89
|
+
:labels => {
|
90
|
+
:title => "Hello world!",
|
91
|
+
:post => {:title => "Hello post!"},
|
92
|
+
:project => {:title => "Hello project!", :task => {:name => "Hello task name!"}},
|
93
|
+
:line_item => {:name => "Hello line item name!"}
|
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
|
103
|
+
::I18n.backend.reload!
|
104
|
+
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
|
105
|
+
end
|
106
|
+
|
107
|
+
it "lookup scopes should be defined" do
|
108
|
+
lambda { ::Formtastic::I18n::SCOPES }.should_not raise_error(::NameError)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should be able to translate with namespaced object" do
|
112
|
+
form = semantic_form_for(@new_post) do |builder|
|
113
|
+
concat(builder.input(:title))
|
114
|
+
end
|
115
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
116
|
+
output_buffer.should have_tag("form label", /Hello post!/)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should be able to translate without form-object" do
|
120
|
+
form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
121
|
+
concat(builder.input(:title))
|
122
|
+
end
|
123
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
124
|
+
output_buffer.should have_tag("form label", /Hello project!/)
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should be able to translate nested objects with nested translations' do
|
128
|
+
form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
|
129
|
+
builder.semantic_fields_for(:task) do |f|
|
130
|
+
concat(f.input(:name))
|
131
|
+
end
|
132
|
+
end
|
133
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
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
|
138
|
+
form = semantic_form_for(:order, :url => 'http://test.host') do |builder|
|
139
|
+
builder.semantic_fields_for(:line_item) do |f|
|
140
|
+
concat(f.input(:name))
|
141
|
+
end
|
142
|
+
end
|
143
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
144
|
+
output_buffer.should have_tag("form label", /Hello line item name!/)
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
# TODO: Add spec for namespaced models?
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "*select: options[:include_blank]" do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ''
|
10
|
+
mock_everything
|
11
|
+
|
12
|
+
@new_post.stub!(:author_id).and_return(nil)
|
13
|
+
@new_post.stub!(:publish_at).and_return(nil)
|
14
|
+
|
15
|
+
@select_input_types = {
|
16
|
+
:select => :author,
|
17
|
+
:datetime => :publish_at,
|
18
|
+
:date => :publish_at,
|
19
|
+
:time => :publish_at
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'when :include_blank is not set' do
|
24
|
+
it 'blank value should be included if the default value specified in config is true' do
|
25
|
+
::Formtastic::SemanticFormBuilder.include_blank_for_select_by_default = true
|
26
|
+
@select_input_types.each do |as, attribute|
|
27
|
+
form = semantic_form_for(@new_post) do |builder|
|
28
|
+
concat(builder.input(attribute, :as => as))
|
29
|
+
end
|
30
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
31
|
+
output_buffer.should have_tag("form li select option[@value='']", "")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'blank value should not be included if the default value specified in config is false' do
|
36
|
+
::Formtastic::SemanticFormBuilder.include_blank_for_select_by_default = false
|
37
|
+
@select_input_types.each do |as, attribute|
|
38
|
+
form = semantic_form_for(@new_post) do |builder|
|
39
|
+
concat(builder.input(attribute, :as => as))
|
40
|
+
end
|
41
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
42
|
+
output_buffer.should_not have_tag("form li select option[@value='']", "")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
after do
|
47
|
+
::Formtastic::SemanticFormBuilder.include_blank_for_select_by_default = true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'when :include_blank is set to false' do
|
52
|
+
it 'should not have a blank option' do
|
53
|
+
@select_input_types.each do |as, attribute|
|
54
|
+
form = semantic_form_for(@new_post) do |builder|
|
55
|
+
concat(builder.input(attribute, :as => as, :include_blank => false))
|
56
|
+
end
|
57
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
58
|
+
output_buffer.should_not have_tag("form li select option[@value='']", "")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'when :include_blank => true is set' do
|
64
|
+
it 'should have a blank select option' do
|
65
|
+
@select_input_types.each do |as, attribute|
|
66
|
+
form = semantic_form_for(@new_post) do |builder|
|
67
|
+
concat(builder.input(attribute, :as => as, :include_blank => true))
|
68
|
+
end
|
69
|
+
output_buffer.concat(form) if Formtastic::Util.rails3?
|
70
|
+
output_buffer.should have_tag("form li select option[@value='']", "")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|