formtastic-rails3 0.9.7
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 +558 -0
- data/Rakefile +103 -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 +144 -0
- data/generators/formtastic/templates/formtastic.rb +54 -0
- data/generators/formtastic/templates/formtastic_changes.css +10 -0
- data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +16 -0
- data/lib/formtastic.rb +1724 -0
- data/lib/formtastic/i18n.rb +32 -0
- data/lib/formtastic/layout_helper.rb +10 -0
- data/lib/generators/formtastic/form/form_generator.rb +85 -0
- data/lib/generators/formtastic/form/templates/_form.html.erb +5 -0
- data/lib/generators/formtastic/form/templates/_form.html.haml +4 -0
- data/lib/generators/formtastic/install/install_generator.rb +23 -0
- data/lib/generators/formtastic/install/templates/formtastic.css +144 -0
- data/lib/generators/formtastic/install/templates/formtastic.rb +58 -0
- data/lib/generators/formtastic/install/templates/formtastic_changes.css +10 -0
- data/lib/locale/en.yml +8 -0
- data/spec/buttons_spec.rb +149 -0
- data/spec/commit_button_spec.rb +377 -0
- data/spec/custom_builder_spec.rb +62 -0
- data/spec/custom_macros.rb +460 -0
- data/spec/defaults_spec.rb +20 -0
- data/spec/error_proc_spec.rb +27 -0
- data/spec/errors_spec.rb +85 -0
- data/spec/form_helper_spec.rb +110 -0
- data/spec/i18n_spec.rb +131 -0
- data/spec/include_blank_spec.rb +70 -0
- data/spec/input_spec.rb +632 -0
- data/spec/inputs/boolean_input_spec.rb +93 -0
- data/spec/inputs/check_boxes_input_spec.rb +167 -0
- data/spec/inputs/country_input_spec.rb +80 -0
- data/spec/inputs/currency_input_spec.rb +80 -0
- data/spec/inputs/date_input_spec.rb +143 -0
- data/spec/inputs/datetime_input_spec.rb +259 -0
- data/spec/inputs/file_input_spec.rb +33 -0
- data/spec/inputs/hidden_input_spec.rb +52 -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 +152 -0
- data/spec/inputs/select_input_spec.rb +470 -0
- data/spec/inputs/string_input_spec.rb +47 -0
- data/spec/inputs/text_input_spec.rb +33 -0
- data/spec/inputs/time_input_spec.rb +128 -0
- data/spec/inputs/time_zone_input_spec.rb +102 -0
- data/spec/inputs_spec.rb +395 -0
- data/spec/label_spec.rb +48 -0
- data/spec/layout_helper_spec.rb +42 -0
- data/spec/semantic_errors_spec.rb +98 -0
- data/spec/semantic_fields_for_spec.rb +44 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +238 -0
- metadata +205 -0
@@ -0,0 +1,32 @@
|
|
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}}.{{action}}.{{attribute}}',
|
14
|
+
'{{model}}.{{attribute}}',
|
15
|
+
'{{attribute}}'
|
16
|
+
]
|
17
|
+
|
18
|
+
class << self
|
19
|
+
|
20
|
+
def translate(*args)
|
21
|
+
key = args.shift.to_sym
|
22
|
+
options = args.extract_options!
|
23
|
+
options.reverse_merge!(:default => DEFAULT_VALUES[key])
|
24
|
+
options[:scope] = [DEFAULT_SCOPE, options[:scope]].flatten.compact
|
25
|
+
::I18n.translate(key, *(args << options))
|
26
|
+
end
|
27
|
+
alias :t :translate
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,85 @@
|
|
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
|
+
class_option :haml, :type => :boolean, :default => false, :group => :formtastic,
|
9
|
+
:desc => "Generate HAML instead of ERB"
|
10
|
+
|
11
|
+
class_option :partial, :type => :boolean, :default => false, :group => :formtastic,
|
12
|
+
:desc => 'Generate a form partial in the model views path, i.e. "_form.html.erb" or "_form.html.haml"'
|
13
|
+
|
14
|
+
class_option :controller, :type => :string, :default => false, :group => :formtastic,
|
15
|
+
:desc => 'Generate for custom controller/view path - in case model and controller namespace is different, i.e. "admin/posts"'
|
16
|
+
|
17
|
+
def self.source_root
|
18
|
+
@source_root ||= File.expand_path("../templates", __FILE__)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.banner
|
22
|
+
"rails generate formtastic:form ExistingModelName [options]"
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_or_show
|
26
|
+
if options[:partial]
|
27
|
+
empty_directory "app/views/#{controller_path}"
|
28
|
+
template "_form.html.#{template_type}", "app/views/#{controller_path}/_form.html.#{template_type}"
|
29
|
+
else
|
30
|
+
template = File.read("#{self.class.source_root}/_form.html.#{template_type}")
|
31
|
+
erb = ERB.new(template, nil, '-')
|
32
|
+
generated_code = erb.result(binding).strip rescue nil
|
33
|
+
|
34
|
+
puts "# ---------------------------------------------------------"
|
35
|
+
puts "# GENERATED FORMTASTIC CODE"
|
36
|
+
puts "# ---------------------------------------------------------"
|
37
|
+
puts
|
38
|
+
puts generated_code || "Nothing could be generated - model exists?"
|
39
|
+
puts
|
40
|
+
puts "# ---------------------------------------------------------"
|
41
|
+
puts "Copied to clipboard - just paste it!" if save_to_clipboard(generated_code)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
IGNORED_COLUMNS = [:updated_at, :created_at].freeze
|
48
|
+
|
49
|
+
def template_type
|
50
|
+
@template_type ||= options[:haml] ? :haml : :erb
|
51
|
+
end
|
52
|
+
|
53
|
+
def controller_path
|
54
|
+
@controller_path ||= if options[:controller]
|
55
|
+
options[:controller].underscore
|
56
|
+
else
|
57
|
+
name.underscore.pluralize
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def columns
|
62
|
+
@columns ||= self.name.camelize.constantize.content_columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) }
|
63
|
+
end
|
64
|
+
|
65
|
+
def save_to_clipboard(data)
|
66
|
+
return unless data
|
67
|
+
|
68
|
+
begin
|
69
|
+
case RUBY_PLATFORM
|
70
|
+
when /win32/
|
71
|
+
require 'win32/clipboard'
|
72
|
+
::Win32::Clipboard.data = data
|
73
|
+
when /darwin/ # mac
|
74
|
+
`echo "#{data}" | pbcopy`
|
75
|
+
else # linux/unix
|
76
|
+
`echo "#{data}" | xsel --clipboard` || `echo "#{data}" | xclip`
|
77
|
+
end
|
78
|
+
rescue LoadError
|
79
|
+
false
|
80
|
+
else
|
81
|
+
true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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
|
+
@source_root ||= File.expand_path("../templates", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.banner
|
11
|
+
"rails generate formtastic:install [options]"
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_files
|
15
|
+
empty_directory 'config/initializers'
|
16
|
+
template 'formtastic.rb', 'config/initializers/formtastic.rb'
|
17
|
+
|
18
|
+
empty_directory 'public/stylesheets'
|
19
|
+
template 'formtastic.css', 'public/stylesheets/formtastic.css'
|
20
|
+
template 'formtastic_changes.css', 'public/stylesheets/formtastic_changes.css'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
/* -------------------------------------------------------------------------------------------------
|
2
|
+
|
3
|
+
It's *strongly* suggested that you don't modify this file. Instead, load a new stylesheet after
|
4
|
+
this one in your layouts (eg formtastic_changes.css) and override the styles to suit your needs.
|
5
|
+
This will allow you to update formtastic.css with new releases without clobbering your own changes.
|
6
|
+
|
7
|
+
This stylesheet forms part of the Formtastic Rails Plugin
|
8
|
+
(c) 2008 Justin French
|
9
|
+
|
10
|
+
--------------------------------------------------------------------------------------------------*/
|
11
|
+
|
12
|
+
|
13
|
+
/* NORMALIZE AND RESET - obviously inspired by Yahoo's reset.css, but scoped to just form.formtastic
|
14
|
+
--------------------------------------------------------------------------------------------------*/
|
15
|
+
form.formtastic, form.formtastic ul, form.formtastic ol, form.formtastic li, form.formtastic fieldset, form.formtastic legend, form.formtastic input, form.formtastic textarea, form.formtastic select, form.formtastic p { margin:0; padding:0; }
|
16
|
+
form.formtastic fieldset { border:0; }
|
17
|
+
form.formtastic em, form.formtastic strong { font-style:normal; font-weight:normal; }
|
18
|
+
form.formtastic ol, form.formtastic ul { list-style:none; }
|
19
|
+
form.formtastic abbr, form.formtastic acronym { border:0; font-variant:normal; }
|
20
|
+
form.formtastic input, form.formtastic textarea, form.formtastic select { font-family:inherit; font-size:inherit; font-weight:inherit; }
|
21
|
+
form.formtastic input, form.formtastic textarea, form.formtastic select { font-size:100%; }
|
22
|
+
form.formtastic legend { color:#000; }
|
23
|
+
|
24
|
+
|
25
|
+
/* SEMANTIC ERRORS
|
26
|
+
--------------------------------------------------------------------------------------------------*/
|
27
|
+
form.formtastic ul.errors { color:#cc0000; margin:0.5em 0 1.5em 25%; list-style:square; }
|
28
|
+
form.formtastic ul.errors li { padding:0; border:none; display:list-item; }
|
29
|
+
|
30
|
+
|
31
|
+
/* FIELDSETS & LISTS
|
32
|
+
--------------------------------------------------------------------------------------------------*/
|
33
|
+
form.formtastic fieldset { }
|
34
|
+
form.formtastic fieldset.inputs { }
|
35
|
+
form.formtastic fieldset.buttons { padding-left:25%; }
|
36
|
+
form.formtastic fieldset ol { }
|
37
|
+
form.formtastic fieldset.buttons li { float:left; padding-right:0.5em; }
|
38
|
+
|
39
|
+
/* clearfixing the fieldsets */
|
40
|
+
form.formtastic fieldset { display: inline-block; }
|
41
|
+
form.formtastic fieldset:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
|
42
|
+
html[xmlns] form.formtastic fieldset { display: block; }
|
43
|
+
* html form.formtastic fieldset { height: 1%; }
|
44
|
+
|
45
|
+
|
46
|
+
/* INPUT LIs
|
47
|
+
--------------------------------------------------------------------------------------------------*/
|
48
|
+
form.formtastic fieldset ol li { margin-bottom:1.5em; }
|
49
|
+
|
50
|
+
/* clearfixing the li's */
|
51
|
+
form.formtastic fieldset ol li { display: inline-block; }
|
52
|
+
form.formtastic fieldset ol li:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
|
53
|
+
html[xmlns] form.formtastic fieldset ol li { display: block; }
|
54
|
+
* html form.formtastic fieldset ol li { height: 1%; }
|
55
|
+
|
56
|
+
form.formtastic fieldset ol li.required { }
|
57
|
+
form.formtastic fieldset ol li.optional { }
|
58
|
+
form.formtastic fieldset ol li.error { }
|
59
|
+
|
60
|
+
|
61
|
+
/* LABELS
|
62
|
+
--------------------------------------------------------------------------------------------------*/
|
63
|
+
form.formtastic fieldset ol li label { display:block; width:25%; float:left; padding-top:.2em; }
|
64
|
+
form.formtastic fieldset ol li li label { line-height:100%; padding-top:0; }
|
65
|
+
form.formtastic fieldset ol li li label input { line-height:100%; vertical-align:middle; margin-top:-0.1em;}
|
66
|
+
|
67
|
+
|
68
|
+
/* NESTED FIELDSETS AND LEGENDS (radio, check boxes and date/time inputs use nested fieldsets)
|
69
|
+
--------------------------------------------------------------------------------------------------*/
|
70
|
+
form.formtastic fieldset ol li fieldset { position:relative; }
|
71
|
+
form.formtastic fieldset ol li fieldset legend { position:absolute; width:25%; padding-top:0.1em; }
|
72
|
+
form.formtastic fieldset ol li fieldset legend span { position:absolute; }
|
73
|
+
form.formtastic fieldset ol li fieldset legend.label label { position:absolute; }
|
74
|
+
form.formtastic fieldset ol li fieldset ol { float:left; width:74%; margin:0; padding:0 0 0 25%; }
|
75
|
+
form.formtastic fieldset ol li fieldset ol li { padding:0; border:0; }
|
76
|
+
|
77
|
+
|
78
|
+
/* INLINE HINTS
|
79
|
+
--------------------------------------------------------------------------------------------------*/
|
80
|
+
form.formtastic fieldset ol li p.inline-hints { color:#666; margin:0.5em 0 0 25%; }
|
81
|
+
|
82
|
+
|
83
|
+
/* INLINE ERRORS
|
84
|
+
--------------------------------------------------------------------------------------------------*/
|
85
|
+
form.formtastic fieldset ol li p.inline-errors { color:#cc0000; margin:0.5em 0 0 25%; }
|
86
|
+
form.formtastic fieldset ol li ul.errors { color:#cc0000; margin:0.5em 0 0 25%; list-style:square; }
|
87
|
+
form.formtastic fieldset ol li ul.errors li { padding:0; border:none; display:list-item; }
|
88
|
+
|
89
|
+
|
90
|
+
/* STRING & NUMERIC OVERRIDES
|
91
|
+
--------------------------------------------------------------------------------------------------*/
|
92
|
+
form.formtastic fieldset ol li.string input { width:74%; }
|
93
|
+
form.formtastic fieldset ol li.password input { width:74%; }
|
94
|
+
form.formtastic fieldset ol li.numeric input { width:74%; }
|
95
|
+
|
96
|
+
|
97
|
+
/* TEXTAREA OVERRIDES
|
98
|
+
--------------------------------------------------------------------------------------------------*/
|
99
|
+
form.formtastic fieldset ol li.text textarea { width:74%; }
|
100
|
+
|
101
|
+
|
102
|
+
/* HIDDEN OVERRIDES
|
103
|
+
--------------------------------------------------------------------------------------------------*/
|
104
|
+
form.formtastic fieldset ol li.hidden { display:none; }
|
105
|
+
|
106
|
+
|
107
|
+
/* BOOLEAN OVERRIDES
|
108
|
+
--------------------------------------------------------------------------------------------------*/
|
109
|
+
form.formtastic fieldset ol li.boolean label { padding-left:25%; width:auto; }
|
110
|
+
form.formtastic fieldset ol li.boolean label input { margin:0 0.5em 0 0.2em; }
|
111
|
+
|
112
|
+
|
113
|
+
/* RADIO OVERRIDES
|
114
|
+
--------------------------------------------------------------------------------------------------*/
|
115
|
+
form.formtastic fieldset ol li.radio { }
|
116
|
+
form.formtastic fieldset ol li.radio fieldset ol { margin-bottom:-0.6em; }
|
117
|
+
form.formtastic fieldset ol li.radio fieldset ol li { margin:0.1em 0 0.5em 0; }
|
118
|
+
form.formtastic fieldset ol li.radio fieldset ol li label { float:none; width:100%; }
|
119
|
+
form.formtastic fieldset ol li.radio fieldset ol li label input { margin-right:0.2em; }
|
120
|
+
|
121
|
+
|
122
|
+
/* CHECK BOXES (COLLECTION) OVERRIDES
|
123
|
+
--------------------------------------------------------------------------------------------------*/
|
124
|
+
form.formtastic fieldset ol li.check_boxes { }
|
125
|
+
form.formtastic fieldset ol li.check_boxes fieldset ol { margin-bottom:-0.6em; }
|
126
|
+
form.formtastic fieldset ol li.check_boxes fieldset ol li { margin:0.1em 0 0.5em 0; }
|
127
|
+
form.formtastic fieldset ol li.check_boxes fieldset ol li label { float:none; width:100%; }
|
128
|
+
form.formtastic fieldset ol li.check_boxes fieldset ol li label input { margin-right:0.2em; }
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
/* DATE & TIME OVERRIDES
|
133
|
+
--------------------------------------------------------------------------------------------------*/
|
134
|
+
form.formtastic fieldset ol li.date fieldset ol li,
|
135
|
+
form.formtastic fieldset ol li.time fieldset ol li,
|
136
|
+
form.formtastic fieldset ol li.datetime fieldset ol li { float:left; width:auto; margin:0 .3em 0 0; }
|
137
|
+
|
138
|
+
form.formtastic fieldset ol li.date fieldset ol li label,
|
139
|
+
form.formtastic fieldset ol li.time fieldset ol li label,
|
140
|
+
form.formtastic fieldset ol li.datetime fieldset ol li label { display:none; }
|
141
|
+
|
142
|
+
form.formtastic fieldset ol li.date fieldset ol li label input,
|
143
|
+
form.formtastic fieldset ol li.time fieldset ol li label input,
|
144
|
+
form.formtastic fieldset ol li.datetime fieldset ol li label input { display:inline; margin:0; padding:0; }
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Set the default text field size when input is a string. Default is 50.
|
2
|
+
# Formtastic::SemanticFormBuilder.default_text_field_size = 50
|
3
|
+
|
4
|
+
# Set the default text area height when input is a text. Default is 20.
|
5
|
+
# Formtastic::SemanticFormBuilder.default_text_area_height = 5
|
6
|
+
|
7
|
+
# Should all fields be considered "required" by default?
|
8
|
+
# Defaults to true, see ValidationReflection notes below.
|
9
|
+
# Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
|
10
|
+
|
11
|
+
# Should select fields have a blank option/prompt by default?
|
12
|
+
# Defaults to true.
|
13
|
+
# Formtastic::SemanticFormBuilder.include_blank_for_select_by_default = true
|
14
|
+
|
15
|
+
# Set the string that will be appended to the labels/fieldsets which are required
|
16
|
+
# It accepts string or procs and the default is a localized version of
|
17
|
+
# '<abbr title="required">*</abbr>'. In other words, if you configure formtastic.required
|
18
|
+
# in your locale, it will replace the abbr title properly. But if you don't want to use
|
19
|
+
# abbr tag, you can simply give a string as below
|
20
|
+
# Formtastic::SemanticFormBuilder.required_string = "(required)"
|
21
|
+
|
22
|
+
# Set the string that will be appended to the labels/fieldsets which are optional
|
23
|
+
# Defaults to an empty string ("") and also accepts procs (see required_string above)
|
24
|
+
# Formtastic::SemanticFormBuilder.optional_string = "(optional)"
|
25
|
+
|
26
|
+
# Set the way inline errors will be displayed.
|
27
|
+
# Defaults to :sentence, valid options are :sentence, :list and :none
|
28
|
+
# Formtastic::SemanticFormBuilder.inline_errors = :sentence
|
29
|
+
|
30
|
+
# Set the method to call on label text to transform or format it for human-friendly
|
31
|
+
# reading when formtastic is user without object. Defaults to :humanize.
|
32
|
+
# Formtastic::SemanticFormBuilder.label_str_method = :humanize
|
33
|
+
|
34
|
+
# Set the array of methods to try calling on parent objects in :select and :radio inputs
|
35
|
+
# for the text inside each @<option>@ tag or alongside each radio @<input>@. The first method
|
36
|
+
# that is found on the object will be used.
|
37
|
+
# Defaults to ["to_label", "display_name", "full_name", "name", "title", "username", "login", "value", "to_s"]
|
38
|
+
# Formtastic::SemanticFormBuilder.collection_label_methods = [
|
39
|
+
# "to_label", "display_name", "full_name", "name", "title", "username", "login", "value", "to_s"]
|
40
|
+
|
41
|
+
# Formtastic by default renders inside li tags the input, hints and then
|
42
|
+
# errors messages. Sometimes you want the hints to be rendered first than
|
43
|
+
# the input, in the following order: hints, input and errors. You can
|
44
|
+
# customize it doing just as below:
|
45
|
+
# Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
|
46
|
+
|
47
|
+
# Formtastic by default renders inside li tags you can customize it to render
|
48
|
+
# with div tags if required
|
49
|
+
# Formtastic::SemanticFormBuilder.item_separator = :div
|
50
|
+
|
51
|
+
# Specifies if labels/hints for input fields automatically be looked up using I18n.
|
52
|
+
# Default value: false. Overridden for specific fields by setting value to true,
|
53
|
+
# i.e. :label => true, or :hint => true (or opposite depending on initialized value)
|
54
|
+
# Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
|
55
|
+
|
56
|
+
# You can add custom inputs or override parts of Formtastic by subclassing SemanticFormBuilder and
|
57
|
+
# specifying that class here. Defaults to SemanticFormBuilder.
|
58
|
+
# Formtastic::SemanticFormHelper.builder = MyCustomBuilder
|
@@ -0,0 +1,10 @@
|
|
1
|
+
/* -------------------------------------------------------------------------------------------------
|
2
|
+
|
3
|
+
Load this stylesheet after formtastic.css in your layouts to override the CSS to suit your needs.
|
4
|
+
This will allow you to update formtastic.css with new releases without clobbering your own changes.
|
5
|
+
|
6
|
+
For example, to make the inline hint paragraphs a little darker in color than the standard #666:
|
7
|
+
|
8
|
+
form.formtastic fieldset ol li p.inline-hints { color:#333; }
|
9
|
+
|
10
|
+
--------------------------------------------------------------------------------------------------*/
|
data/lib/locale/en.yml
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
3
|
+
|
4
|
+
describe 'SemanticFormBuilder#buttons' do
|
5
|
+
|
6
|
+
include FormtasticSpecHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
@output_buffer = ActiveSupport::SafeBuffer.new
|
10
|
+
mock_everything
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'with a block' do
|
14
|
+
describe 'when no options are provided' do
|
15
|
+
before do
|
16
|
+
semantic_form_for(@new_post) do |builder|
|
17
|
+
builder.buttons do
|
18
|
+
concat('hello')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should render a fieldset inside the form, with a class of "inputs"' do
|
24
|
+
output_buffer.should have_tag("form fieldset.buttons")
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should render an ol inside the fieldset' do
|
28
|
+
output_buffer.should have_tag("form fieldset.buttons ol")
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should render the contents of the block inside the ol' do
|
32
|
+
output_buffer.should have_tag("form fieldset.buttons ol", /hello/)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should not render a legend inside the fieldset' do
|
36
|
+
output_buffer.should_not have_tag("form fieldset.buttons legend")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'when a :name option is provided' do
|
41
|
+
before do
|
42
|
+
@legend_text = "Advanced options"
|
43
|
+
|
44
|
+
semantic_form_for(@new_post) do |builder|
|
45
|
+
builder.buttons :name => @legend_text do
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
it 'should render a fieldset inside the form' do
|
50
|
+
output_buffer.should have_tag("form fieldset legend", /#{@legend_text}/)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'when other options are provided' do
|
55
|
+
before do
|
56
|
+
@id_option = 'advanced'
|
57
|
+
@class_option = 'wide'
|
58
|
+
|
59
|
+
semantic_form_for(@new_post) do |builder|
|
60
|
+
builder.buttons :id => @id_option, :class => @class_option do
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
it 'should pass the options into the fieldset tag as attributes' do
|
65
|
+
output_buffer.should have_tag("form fieldset##{@id_option}")
|
66
|
+
output_buffer.should have_tag("form fieldset.#{@class_option}")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'without a block' do
|
73
|
+
|
74
|
+
describe 'with no args (default buttons)' do
|
75
|
+
|
76
|
+
before do
|
77
|
+
semantic_form_for(@new_post) do |builder|
|
78
|
+
concat(builder.buttons)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should render a form' do
|
83
|
+
output_buffer.should have_tag('form')
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'should render a buttons fieldset inside the form' do
|
87
|
+
output_buffer.should have_tag('form fieldset.buttons')
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should not render a legend in the fieldset' do
|
91
|
+
output_buffer.should_not have_tag('form fieldset.buttons legend')
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should render an ol in the fieldset' do
|
95
|
+
output_buffer.should have_tag('form fieldset.buttons ol')
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should render a list item in the ol for each default button' do
|
99
|
+
output_buffer.should have_tag('form fieldset.buttons ol li', :count => 1)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should render a commit list item for the commit button' do
|
103
|
+
output_buffer.should have_tag('form fieldset.buttons ol li.commit')
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
describe 'with button names as args' do
|
109
|
+
|
110
|
+
before do
|
111
|
+
semantic_form_for(@new_post) do |builder|
|
112
|
+
concat(builder.buttons(:commit))
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should render a form with a fieldset containing a list item for each button arg' do
|
117
|
+
output_buffer.should have_tag('form > fieldset.buttons > ol > li', :count => 1)
|
118
|
+
output_buffer.should have_tag('form > fieldset.buttons > ol > li.commit')
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
describe 'with button names as args and an options hash' do
|
124
|
+
|
125
|
+
before do
|
126
|
+
semantic_form_for(@new_post) do |builder|
|
127
|
+
concat(builder.buttons(:commit, :name => "Now click a button", :id => "my-id"))
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should render a form with a fieldset containing a list item for each button arg' do
|
132
|
+
output_buffer.should have_tag('form > fieldset.buttons > ol > li', :count => 1)
|
133
|
+
output_buffer.should have_tag('form > fieldset.buttons > ol > li.commit', :count => 1)
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'should pass the options down to the fieldset' do
|
137
|
+
output_buffer.should have_tag('form > fieldset#my-id.buttons')
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should use the special :name option as a text for the legend tag' do
|
141
|
+
output_buffer.should have_tag('form > fieldset#my-id.buttons > legend', /Now click a button/)
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|