sensis-formtastic-rails3 1.d4e5326

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.textile +584 -0
  3. data/Rakefile +127 -0
  4. data/generators/form/USAGE +16 -0
  5. data/generators/form/form_generator.rb +120 -0
  6. data/generators/form/templates/view__form.html.erb +5 -0
  7. data/generators/form/templates/view__form.html.haml +4 -0
  8. data/generators/formtastic/formtastic_generator.rb +24 -0
  9. data/generators/formtastic/templates/formtastic.css +131 -0
  10. data/generators/formtastic/templates/formtastic.rb +54 -0
  11. data/generators/formtastic/templates/formtastic_changes.css +14 -0
  12. data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +16 -0
  13. data/init.rb +5 -0
  14. data/lib/formtastic.rb +1870 -0
  15. data/lib/formtastic/i18n.rb +35 -0
  16. data/lib/formtastic/layout_helper.rb +10 -0
  17. data/lib/formtastic/railtie.rb +12 -0
  18. data/lib/formtastic/util.rb +36 -0
  19. data/lib/generators/formtastic/form/form_generator.rb +86 -0
  20. data/lib/generators/formtastic/install/install_generator.rb +24 -0
  21. data/lib/locale/en.yml +8 -0
  22. data/rails/init.rb +2 -0
  23. data/spec/buttons_spec.rb +166 -0
  24. data/spec/commit_button_spec.rb +401 -0
  25. data/spec/custom_builder_spec.rb +98 -0
  26. data/spec/defaults_spec.rb +20 -0
  27. data/spec/error_proc_spec.rb +27 -0
  28. data/spec/errors_spec.rb +105 -0
  29. data/spec/form_helper_spec.rb +142 -0
  30. data/spec/helpers/layout_helper_spec.rb +21 -0
  31. data/spec/i18n_spec.rb +152 -0
  32. data/spec/include_blank_spec.rb +74 -0
  33. data/spec/input_spec.rb +786 -0
  34. data/spec/inputs/boolean_input_spec.rb +104 -0
  35. data/spec/inputs/check_boxes_input_spec.rb +426 -0
  36. data/spec/inputs/country_input_spec.rb +118 -0
  37. data/spec/inputs/date_input_spec.rb +168 -0
  38. data/spec/inputs/datetime_input_spec.rb +310 -0
  39. data/spec/inputs/file_input_spec.rb +34 -0
  40. data/spec/inputs/hidden_input_spec.rb +78 -0
  41. data/spec/inputs/numeric_input_spec.rb +44 -0
  42. data/spec/inputs/password_input_spec.rb +46 -0
  43. data/spec/inputs/radio_input_spec.rb +243 -0
  44. data/spec/inputs/select_input_spec.rb +546 -0
  45. data/spec/inputs/string_input_spec.rb +64 -0
  46. data/spec/inputs/text_input_spec.rb +34 -0
  47. data/spec/inputs/time_input_spec.rb +206 -0
  48. data/spec/inputs/time_zone_input_spec.rb +110 -0
  49. data/spec/inputs_spec.rb +476 -0
  50. data/spec/label_spec.rb +89 -0
  51. data/spec/semantic_errors_spec.rb +98 -0
  52. data/spec/semantic_fields_for_spec.rb +45 -0
  53. data/spec/spec.opts +2 -0
  54. data/spec/spec_helper.rb +289 -0
  55. data/spec/support/custom_macros.rb +494 -0
  56. data/spec/support/output_buffer.rb +4 -0
  57. data/spec/support/test_environment.rb +45 -0
  58. metadata +234 -0
@@ -0,0 +1,35 @@
1
+ module Formtastic
2
+ module I18n
3
+
4
+ DEFAULT_SCOPE = [:formtastic].freeze
5
+ DEFAULT_VALUES = {
6
+ :required => 'required',
7
+ :yes => 'Yes',
8
+ :no => 'No',
9
+ :create => 'Create %{model}',
10
+ :update => 'Update %{model}'
11
+ }.freeze
12
+ SCOPES = [
13
+ '%{model}.%{nested_model}.%{action}.%{attribute}',
14
+ '%{model}.%{action}.%{attribute}',
15
+ '%{model}.%{nested_model}.%{attribute}',
16
+ '%{model}.%{attribute}',
17
+ '%{nested_model}.%{attribute}',
18
+ '%{attribute}'
19
+ ]
20
+
21
+ class << self
22
+
23
+ def translate(*args)
24
+ key = args.shift.to_sym
25
+ options = args.extract_options!
26
+ options.reverse_merge!(:default => DEFAULT_VALUES[key])
27
+ options[:scope] = [DEFAULT_SCOPE, options[:scope]].flatten.compact
28
+ ::I18n.translate(key, *(args << options))
29
+ end
30
+ alias :t :translate
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ module Formtastic
2
+ module LayoutHelper
3
+
4
+ def formtastic_stylesheet_link_tag
5
+ stylesheet_link_tag("formtastic") +
6
+ stylesheet_link_tag("formtastic_changes")
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ require 'formtastic'
2
+ require 'formtastic/layout_helper'
3
+ require 'rails'
4
+
5
+ module Formtastic
6
+ class Railtie < Rails::Railtie
7
+ initializer 'formtastic.initialize', :after => :after_initialize do
8
+ ActionView::Base.send :include, Formtastic::SemanticFormHelper
9
+ ActionView::Base.send(:include, Formtastic::LayoutHelper)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,36 @@
1
+ # Adapted from the rails3 compatibility shim in Haml 2.2
2
+ module Formtastic
3
+ module Util
4
+ extend self
5
+ ## Rails XSS Safety
6
+
7
+ # Returns the given text, marked as being HTML-safe.
8
+ # With older versions of the Rails XSS-safety mechanism,
9
+ # this destructively modifies the HTML-safety of `text`.
10
+ #
11
+ # @param text [String]
12
+ # @return [String] `text`, marked as HTML-safe
13
+ def html_safe(text)
14
+ return text if text.nil?
15
+ return text.html_safe if defined?(ActiveSupport::SafeBuffer)
16
+ return text.html_safe! if text.respond_to?(:html_safe!)
17
+ text
18
+ end
19
+
20
+ def rails_safe_buffer_class
21
+ # It's important that we check ActiveSupport first,
22
+ # because in Rails 2.3.6 ActionView::SafeBuffer exists
23
+ # but is a deprecated proxy object.
24
+ return ActiveSupport::SafeBuffer if defined?(ActiveSupport::SafeBuffer)
25
+ return ActionView::SafeBuffer
26
+ end
27
+
28
+ def rails3?
29
+ version=
30
+ if defined?(ActionPack::VERSION::MAJOR)
31
+ ActionPack::VERSION::MAJOR
32
+ end
33
+ !version.blank? && version >= 3
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,86 @@
1
+ # coding: utf-8
2
+ module Formtastic
3
+ class FormGenerator < Rails::Generators::NamedBase
4
+ desc "Generates formtastic form code based on an existing model. By default the " <<
5
+ "generated code will be printed out directly in the terminal, and also copied " <<
6
+ "to clipboard. Can optionally be saved into partial directly."
7
+
8
+ argument :name, :type => :string, :required => true, :banner => 'ExistingModelName'
9
+ argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'
10
+
11
+ class_option :haml, :type => :boolean, :default => false, :group => :formtastic,
12
+ :desc => "Generate HAML instead of ERB"
13
+
14
+ class_option :partial, :type => :boolean, :default => false, :group => :formtastic,
15
+ :desc => 'Generate a form partial in the model views path, i.e. "_form.html.erb" or "_form.html.haml"'
16
+
17
+ class_option :controller, :type => :string, :default => false, :group => :formtastic,
18
+ :desc => 'Generate for custom controller/view path - in case model and controller namespace is different, i.e. "admin/posts"'
19
+
20
+ def self.source_root
21
+ # Set source directory for the templates to the rails2 generator template directory
22
+ @source_root ||= File.expand_path(File.join('..', '..', '..', '..', 'generators', 'form', 'templates'), File.dirname(__FILE__))
23
+ end
24
+
25
+ def create_or_show
26
+ @attributes = self.columns if @attributes.empty?
27
+ if options[:partial]
28
+ empty_directory "app/views/#{controller_path}"
29
+ template "view__form.html.#{template_type}", "app/views/#{controller_path}/_form.html.#{template_type}"
30
+ else
31
+ template = File.read("#{self.class.source_root}/view__form.html.#{template_type}")
32
+ erb = ERB.new(template, nil, '-')
33
+ generated_code = erb.result(binding).strip rescue nil
34
+
35
+ puts "# ---------------------------------------------------------"
36
+ puts "# GENERATED FORMTASTIC CODE"
37
+ puts "# ---------------------------------------------------------"
38
+ puts
39
+ puts generated_code || "Nothing could be generated - model exists?"
40
+ puts
41
+ puts "# ---------------------------------------------------------"
42
+ puts "Copied to clipboard - just paste it!" if save_to_clipboard(generated_code)
43
+ end
44
+ end
45
+
46
+ protected
47
+
48
+ IGNORED_COLUMNS = [:updated_at, :created_at].freeze
49
+
50
+ def template_type
51
+ @template_type ||= options[:haml] ? :haml : :erb
52
+ end
53
+
54
+ def controller_path
55
+ @controller_path ||= if options[:controller]
56
+ options[:controller].underscore
57
+ else
58
+ name.underscore.pluralize
59
+ end
60
+ end
61
+
62
+ def columns
63
+ @columns ||= self.name.camelize.constantize.content_columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) }
64
+ end
65
+
66
+ def save_to_clipboard(data)
67
+ return unless data
68
+
69
+ begin
70
+ case RUBY_PLATFORM
71
+ when /win32/
72
+ require 'win32/clipboard'
73
+ ::Win32::Clipboard.data = data
74
+ when /darwin/ # mac
75
+ `echo "#{data}" | pbcopy`
76
+ else # linux/unix
77
+ `echo "#{data}" | xsel --clipboard` || `echo "#{data}" | xclip`
78
+ end
79
+ rescue LoadError
80
+ false
81
+ else
82
+ true
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ module Formtastic
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copies formtastic.css and formtastic_changes.css to public/stylesheets/ and a config initializer to config/initializers/formtastic_config.rb"
5
+
6
+ def self.source_root
7
+ # Set source directory for the templates to the rails2 generator template directory
8
+ @source_root ||= File.expand_path(File.join('..', '..', '..', '..', 'generators', 'formtastic', 'templates'), File.dirname(__FILE__))
9
+ end
10
+
11
+ def self.banner
12
+ "rails generate formtastic:install [options]"
13
+ end
14
+
15
+ def copy_files
16
+ empty_directory 'config/initializers'
17
+ template 'formtastic.rb', 'config/initializers/formtastic.rb'
18
+
19
+ empty_directory 'public/stylesheets'
20
+ template 'formtastic.css', 'public/stylesheets/formtastic.css'
21
+ template 'formtastic_changes.css', 'public/stylesheets/formtastic_changes.css'
22
+ end
23
+ end
24
+ end
data/lib/locale/en.yml ADDED
@@ -0,0 +1,8 @@
1
+ en:
2
+ formtastic:
3
+ :yes: 'Yes'
4
+ :no: 'No'
5
+ create: 'Create'
6
+ save: 'Save'
7
+ submit: 'Submit'
8
+ required: 'Required'
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # coding: utf-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "init"))
@@ -0,0 +1,166 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'SemanticFormBuilder#buttons' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe 'with a block' do
14
+ describe 'when no options are provided' do
15
+ before do
16
+ @form = semantic_form_for(@new_post) do |builder|
17
+ buttons = builder.buttons do
18
+ concat('hello')
19
+ end
20
+ concat(buttons)
21
+ end
22
+ end
23
+
24
+ it 'should render a fieldset inside the form, with a class of "inputs"' do
25
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
26
+ output_buffer.should have_tag("form fieldset.buttons")
27
+ end
28
+
29
+ it 'should render an ol inside the fieldset' do
30
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
31
+ output_buffer.should have_tag("form fieldset.buttons ol")
32
+ end
33
+
34
+ it 'should render the contents of the block inside the ol' do
35
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
36
+ output_buffer.should have_tag("form fieldset.buttons ol", /hello/)
37
+ end
38
+
39
+ it 'should not render a legend inside the fieldset' do
40
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
41
+ output_buffer.should_not have_tag("form fieldset.buttons legend")
42
+ end
43
+ end
44
+
45
+ describe 'when a :name option is provided' do
46
+ before do
47
+ @legend_text = "Advanced options"
48
+
49
+ @form = semantic_form_for(@new_post) do |builder|
50
+ builder.buttons :name => @legend_text do
51
+ end
52
+ end
53
+ end
54
+ it 'should render a fieldset inside the form' do
55
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
56
+ output_buffer.should have_tag("form fieldset legend", /#{@legend_text}/)
57
+ end
58
+ end
59
+
60
+ describe 'when other options are provided' do
61
+ before do
62
+ @id_option = 'advanced'
63
+ @class_option = 'wide'
64
+
65
+ @form = semantic_form_for(@new_post) do |builder|
66
+ builder.buttons :id => @id_option, :class => @class_option do
67
+ end
68
+ end
69
+ end
70
+ it 'should pass the options into the fieldset tag as attributes' do
71
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
72
+ output_buffer.should have_tag("form fieldset##{@id_option}")
73
+ output_buffer.should have_tag("form fieldset.#{@class_option}")
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ describe 'without a block' do
80
+
81
+ describe 'with no args (default buttons)' do
82
+
83
+ before do
84
+ @form = semantic_form_for(@new_post) do |builder|
85
+ concat(builder.buttons)
86
+ end
87
+ end
88
+
89
+ it 'should render a form' do
90
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
91
+ output_buffer.should have_tag('form')
92
+ end
93
+
94
+ it 'should render a buttons fieldset inside the form' do
95
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
96
+ output_buffer.should have_tag('form fieldset.buttons')
97
+ end
98
+
99
+ it 'should not render a legend in the fieldset' do
100
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
101
+ output_buffer.should_not have_tag('form fieldset.buttons legend')
102
+ end
103
+
104
+ it 'should render an ol in the fieldset' do
105
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
106
+ output_buffer.should have_tag('form fieldset.buttons ol')
107
+ end
108
+
109
+ it 'should render a list item in the ol for each default button' do
110
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
111
+ output_buffer.should have_tag('form fieldset.buttons ol li', :count => 1)
112
+ end
113
+
114
+ it 'should render a commit list item for the commit button' do
115
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
116
+ output_buffer.should have_tag('form fieldset.buttons ol li.commit')
117
+ end
118
+
119
+ end
120
+
121
+ describe 'with button names as args' do
122
+
123
+ before do
124
+ @form = semantic_form_for(@new_post) do |builder|
125
+ concat(builder.buttons(:commit))
126
+ end
127
+ end
128
+
129
+ it 'should render a form with a fieldset containing a list item for each button arg' do
130
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
131
+ output_buffer.should have_tag('form > fieldset.buttons > ol > li', :count => 1)
132
+ output_buffer.should have_tag('form > fieldset.buttons > ol > li.commit')
133
+ end
134
+
135
+ end
136
+
137
+ describe 'with button names as args and an options hash' do
138
+
139
+ before do
140
+ @form = semantic_form_for(@new_post) do |builder|
141
+ concat(builder.buttons(:commit, :name => "Now click a button", :id => "my-id"))
142
+ end
143
+ end
144
+
145
+ it 'should render a form with a fieldset containing a list item for each button arg' do
146
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
147
+ output_buffer.should have_tag('form > fieldset.buttons > ol > li', :count => 1)
148
+ output_buffer.should have_tag('form > fieldset.buttons > ol > li.commit', :count => 1)
149
+ end
150
+
151
+ it 'should pass the options down to the fieldset' do
152
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
153
+ output_buffer.should have_tag('form > fieldset#my-id.buttons')
154
+ end
155
+
156
+ it 'should use the special :name option as a text for the legend tag' do
157
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
158
+ output_buffer.should have_tag('form > fieldset#my-id.buttons > legend', /Now click a button/)
159
+ end
160
+
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+
@@ -0,0 +1,401 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'SemanticFormBuilder#commit_button' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe 'when used on any record' do
14
+
15
+ before do
16
+ @new_post.stub!(:new_record?).and_return(false)
17
+ @form = semantic_form_for(@new_post) do |builder|
18
+ concat(builder.commit_button)
19
+ end
20
+ end
21
+
22
+ it 'should render a commit li' do
23
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
24
+ output_buffer.should have_tag('li.commit')
25
+ end
26
+
27
+ it 'should render an input with a type attribute of "submit"' do
28
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
29
+ output_buffer.should have_tag('li.commit input[@type="submit"]')
30
+ end
31
+
32
+ it 'should render an input with a name attribute of "commit"' do
33
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
34
+ output_buffer.should have_tag('li.commit input[@name="commit"]')
35
+ end
36
+
37
+ it 'should pass options given in :button_html to the button' do
38
+ @new_post.stub!(:new_record?).and_return(false)
39
+ form = semantic_form_for(@new_post) do |builder|
40
+ concat(builder.commit_button('text', :button_html => {:class => 'my_class', :id => 'my_id'}))
41
+ end
42
+
43
+ output_buffer.concat(form) if Formtastic::Util.rails3?
44
+ output_buffer.should have_tag('li.commit input#my_id')
45
+ output_buffer.should have_tag('li.commit input.my_class')
46
+ end
47
+
48
+ end
49
+
50
+ describe "its accesskey" do
51
+
52
+ it 'should allow nil default' do
53
+ with_config :default_commit_button_accesskey, nil do
54
+ output_buffer.should_not have_tag('li.commit input[@accesskey]')
55
+ end
56
+ end
57
+
58
+ it 'should use the default if set' do
59
+ with_config :default_commit_button_accesskey, 's' do
60
+ @new_post.stub!(:new_record?).and_return(false)
61
+ form = semantic_form_for(@new_post) do |builder|
62
+ concat(builder.commit_button('text', :button_html => {}))
63
+ end
64
+ output_buffer.concat(form) if Formtastic::Util.rails3?
65
+ output_buffer.should have_tag('li.commit input[@accesskey="s"]')
66
+ end
67
+ end
68
+
69
+ it 'should use the value set in options over the default' do
70
+ with_config :default_commit_button_accesskey, 's' do
71
+ @new_post.stub!(:new_record?).and_return(false)
72
+ form = semantic_form_for(@new_post) do |builder|
73
+ concat(builder.commit_button('text', :accesskey => 'o'))
74
+ end
75
+ output_buffer.concat(form) if Formtastic::Util.rails3?
76
+ output_buffer.should_not have_tag('li.commit input[@accesskey="s"]')
77
+ output_buffer.should have_tag('li.commit input[@accesskey="o"]')
78
+ end
79
+ end
80
+
81
+ it 'should use the value set in button_html over options' do
82
+ with_config :default_commit_button_accesskey, 's' do
83
+ @new_post.stub!(:new_record?).and_return(false)
84
+ form = semantic_form_for(@new_post) do |builder|
85
+ concat(builder.commit_button('text', :accesskey => 'o', :button_html => {:accesskey => 't'}))
86
+ end
87
+ output_buffer.concat(form) if Formtastic::Util.rails3?
88
+ output_buffer.should_not have_tag('li.commit input[@accesskey="s"]')
89
+ output_buffer.should_not have_tag('li.commit input[@accesskey="o"]')
90
+ output_buffer.should have_tag('li.commit input[@accesskey="t"]')
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ describe 'when the first option is a string and the second is a hash' do
97
+
98
+ before do
99
+ @new_post.stub!(:new_record?).and_return(false)
100
+ @form = semantic_form_for(@new_post) do |builder|
101
+ concat(builder.commit_button("a string", :button_html => { :class => "pretty"}))
102
+ end
103
+ end
104
+
105
+ it "should render the string as the value of the button" do
106
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
107
+ output_buffer.should have_tag('li input[@value="a string"]')
108
+ end
109
+
110
+ it "should deal with the options hash" do
111
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
112
+ output_buffer.should have_tag('li input.pretty')
113
+ end
114
+
115
+ end
116
+
117
+ describe 'when the first option is a hash' do
118
+
119
+ before do
120
+ @new_post.stub!(:new_record?).and_return(false)
121
+ @form = semantic_form_for(@new_post) do |builder|
122
+ concat(builder.commit_button(:button_html => { :class => "pretty"}))
123
+ end
124
+ end
125
+
126
+ it "should deal with the options hash" do
127
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
128
+ output_buffer.should have_tag('li input.pretty')
129
+ end
130
+
131
+ end
132
+
133
+ describe 'label' do
134
+
135
+ # No object
136
+ describe 'when used without object' do
137
+ describe 'when explicit label is provided' do
138
+ it 'should render an input with the explicitly specified label' do
139
+ form = semantic_form_for(:post, :url => 'http://example.com') do |builder|
140
+ concat(builder.commit_button("Click!"))
141
+ end
142
+ output_buffer.concat(form) if Formtastic::Util.rails3?
143
+ output_buffer.should have_tag('li.commit input[@value="Click!"][@class~="submit"]')
144
+ end
145
+ end
146
+
147
+ describe 'when no explicit label is provided' do
148
+ describe 'when no I18n-localized label is provided' do
149
+ before do
150
+ ::I18n.backend.store_translations :en, :formtastic => {:submit => 'Submit %{model}'}
151
+ end
152
+
153
+ after do
154
+ ::I18n.backend.reload!
155
+ end
156
+
157
+ it 'should render an input with default I18n-localized label (fallback)' do
158
+ form = semantic_form_for(:post, :url => 'http://example.com') do |builder|
159
+ concat(builder.commit_button)
160
+ end
161
+ output_buffer.concat(form) if Formtastic::Util.rails3?
162
+ output_buffer.should have_tag('li.commit input[@value="Submit Post"][@class~="submit"]')
163
+ end
164
+ end
165
+
166
+ describe 'when I18n-localized label is provided' do
167
+ before do
168
+ ::I18n.backend.store_translations :en,
169
+ :formtastic => {
170
+ :actions => {
171
+ :submit => 'Custom Submit',
172
+ }
173
+ }
174
+ ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
175
+ end
176
+
177
+ after do
178
+ ::I18n.backend.reload!
179
+ end
180
+
181
+ it 'should render an input with localized label (I18n)' do
182
+ ::I18n.backend.store_translations :en,
183
+ :formtastic => {
184
+ :actions => {
185
+ :post => {
186
+ :submit => 'Custom Submit %{model}'
187
+ }
188
+ }
189
+ }
190
+ form = semantic_form_for(:post, :url => 'http://example.com') do |builder|
191
+ concat(builder.commit_button)
192
+ end
193
+ output_buffer.concat(form) if Formtastic::Util.rails3?
194
+ output_buffer.should have_tag(%Q{li.commit input[@value="Custom Submit Post"][@class~="submit"]})
195
+ end
196
+
197
+ it 'should render an input with anoptional localized label (I18n) - if first is not set' do
198
+ form = semantic_form_for(:post, :url => 'http://example.com') do |builder|
199
+ concat(builder.commit_button)
200
+ end
201
+ output_buffer.concat(form) if Formtastic::Util.rails3?
202
+ output_buffer.should have_tag(%Q{li.commit input[@value="Custom Submit"][@class~="submit"]})
203
+ end
204
+
205
+ end
206
+ end
207
+ end
208
+
209
+ # New record
210
+ describe 'when used on a new record' do
211
+ before do
212
+ @new_post.stub!(:new_record?).and_return(true)
213
+ end
214
+
215
+ describe 'when explicit label is provided' do
216
+ it 'should render an input with the explicitly specified label' do
217
+ form = semantic_form_for(@new_post) do |builder|
218
+ concat(builder.commit_button("Click!"))
219
+ end
220
+ output_buffer.concat(form) if Formtastic::Util.rails3?
221
+ output_buffer.should have_tag('li.commit input[@value="Click!"][@class~="create"]')
222
+ end
223
+ end
224
+
225
+ describe 'when no explicit label is provided' do
226
+ describe 'when no I18n-localized label is provided' do
227
+ before do
228
+ ::I18n.backend.store_translations :en, :formtastic => {:create => 'Create %{model}'}
229
+ end
230
+
231
+ after do
232
+ ::I18n.backend.reload!
233
+ end
234
+
235
+ it 'should render an input with default I18n-localized label (fallback)' do
236
+ form = semantic_form_for(@new_post) do |builder|
237
+ concat(builder.commit_button)
238
+ end
239
+ output_buffer.concat(form) if Formtastic::Util.rails3?
240
+ output_buffer.should have_tag('li.commit input[@value="Create Post"][@class~="create"]')
241
+ end
242
+ end
243
+
244
+ describe 'when I18n-localized label is provided' do
245
+ before do
246
+ ::I18n.backend.store_translations :en,
247
+ :formtastic => {
248
+ :actions => {
249
+ :create => 'Custom Create',
250
+ }
251
+ }
252
+ ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
253
+ end
254
+
255
+ after do
256
+ ::I18n.backend.reload!
257
+ end
258
+
259
+ it 'should render an input with localized label (I18n)' do
260
+ ::I18n.backend.store_translations :en,
261
+ :formtastic => {
262
+ :actions => {
263
+ :post => {
264
+ :create => 'Custom Create %{model}'
265
+ }
266
+ }
267
+ }
268
+ form = semantic_form_for(@new_post) do |builder|
269
+ concat(builder.commit_button)
270
+ end
271
+ output_buffer.concat(form) if Formtastic::Util.rails3?
272
+ output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create Post"][@class~="create"]})
273
+ end
274
+
275
+ it 'should render an input with anoptional localized label (I18n) - if first is not set' do
276
+ form = semantic_form_for(@new_post) do |builder|
277
+ concat(builder.commit_button)
278
+ end
279
+ output_buffer.concat(form) if Formtastic::Util.rails3?
280
+ output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create"][@class~="create"]})
281
+ end
282
+
283
+ end
284
+ end
285
+ end
286
+
287
+ # Existing record
288
+ describe 'when used on an existing record' do
289
+ before do
290
+ @new_post.stub!(:new_record?).and_return(false)
291
+ end
292
+
293
+ describe 'when explicit label is provided' do
294
+ it 'should render an input with the explicitly specified label' do
295
+ form = semantic_form_for(@new_post) do |builder|
296
+ concat(builder.commit_button("Click!"))
297
+ end
298
+ output_buffer.concat(form) if Formtastic::Util.rails3?
299
+ output_buffer.should have_tag('li.commit input[@value="Click!"][@class~="update"]')
300
+ end
301
+ end
302
+
303
+ describe 'when no explicit label is provided' do
304
+ describe 'when no I18n-localized label is provided' do
305
+ before do
306
+ ::I18n.backend.store_translations :en, :formtastic => {:update => 'Save %{model}'}
307
+ end
308
+
309
+ after do
310
+ ::I18n.backend.reload!
311
+ end
312
+
313
+ it 'should render an input with default I18n-localized label (fallback)' do
314
+ form = semantic_form_for(@new_post) do |builder|
315
+ concat(builder.commit_button)
316
+ end
317
+ output_buffer.concat(form) if Formtastic::Util.rails3?
318
+ output_buffer.should have_tag('li.commit input[@value="Save Post"][@class~="update"]')
319
+ end
320
+ end
321
+
322
+ describe 'when I18n-localized label is provided' do
323
+ before do
324
+ ::I18n.backend.reload!
325
+ ::I18n.backend.store_translations :en,
326
+ :formtastic => {
327
+ :actions => {
328
+ :update => 'Custom Save',
329
+ }
330
+ }
331
+ ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
332
+ end
333
+
334
+ after do
335
+ ::I18n.backend.reload!
336
+ end
337
+
338
+ it 'should render an input with localized label (I18n)' do
339
+ ::I18n.backend.store_translations :en,
340
+ :formtastic => {
341
+ :actions => {
342
+ :post => {
343
+ :update => 'Custom Save %{model}'
344
+ }
345
+ }
346
+ }
347
+ form = semantic_form_for(@new_post) do |builder|
348
+ concat(builder.commit_button)
349
+ end
350
+ output_buffer.concat(form) if Formtastic::Util.rails3?
351
+ output_buffer.should have_tag(%Q{li.commit input[@value="Custom Save Post"][@class~="update"]})
352
+ end
353
+
354
+ it 'should render an input with anoptional localized label (I18n) - if first is not set' do
355
+ form = semantic_form_for(@new_post) do |builder|
356
+ concat(builder.commit_button)
357
+ end
358
+ output_buffer.concat(form) if Formtastic::Util.rails3?
359
+ output_buffer.should have_tag(%Q{li.commit input[@value="Custom Save"][@class~="update"]})
360
+ ::I18n.backend.store_translations :en, :formtastic => {}
361
+ end
362
+
363
+ end
364
+ end
365
+ end
366
+ end
367
+
368
+ describe 'when the model is two words' do
369
+ before do
370
+ output_buffer = ''
371
+ class ::UserPost
372
+ extend ActiveModel::Naming if defined?(ActiveModel::Naming)
373
+ include ActiveModel::Conversion if defined?(ActiveModel::Conversion)
374
+
375
+ def id
376
+ end
377
+
378
+ def persisted?
379
+ end
380
+
381
+ # Rails does crappy human_name
382
+ def self.human_name
383
+ "User post"
384
+ end
385
+ end
386
+ @new_user_post = ::UserPost.new
387
+
388
+ @new_user_post.stub!(:new_record?).and_return(true)
389
+ @form = semantic_form_for(@new_user_post, :url => '') do |builder|
390
+ concat(builder.commit_button())
391
+ end
392
+ end
393
+
394
+ it "should render the string as the value of the button" do
395
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
396
+ output_buffer.should have_tag('li input[@value="Create User post"]')
397
+ end
398
+
399
+ end
400
+
401
+ end