ShadowBelmolve-formtastic 0.2.1 → 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/README.textile +191 -176
  2. data/Rakefile +65 -22
  3. data/generators/form/USAGE +16 -0
  4. data/generators/form/form_generator.rb +120 -0
  5. data/generators/form/templates/view__form.html.erb +5 -0
  6. data/generators/form/templates/view__form.html.haml +4 -0
  7. data/generators/formtastic/formtastic_generator.rb +24 -0
  8. data/generators/{formtastic_stylesheets → formtastic}/templates/formtastic.css +2 -0
  9. data/generators/formtastic/templates/formtastic.rb +51 -0
  10. data/generators/{formtastic_stylesheets → formtastic}/templates/formtastic_changes.css +0 -0
  11. data/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +5 -10
  12. data/lib/formtastic.rb +1234 -895
  13. data/lib/formtastic/i18n.rb +32 -0
  14. data/lib/locale/en.yml +2 -2
  15. data/rails/init.rb +1 -1
  16. data/spec/buttons_spec.rb +149 -0
  17. data/spec/commit_button_spec.rb +344 -0
  18. data/spec/custom_builder_spec.rb +62 -0
  19. data/spec/custom_macros.rb +561 -0
  20. data/spec/defaults_spec.rb +20 -0
  21. data/spec/error_proc_spec.rb +27 -0
  22. data/spec/errors_spec.rb +85 -0
  23. data/spec/form_helper_spec.rb +120 -0
  24. data/spec/i18n_spec.rb +131 -0
  25. data/spec/include_blank_spec.rb +70 -0
  26. data/spec/input_spec.rb +608 -0
  27. data/spec/inputs/boolean_input_spec.rb +93 -0
  28. data/spec/inputs/check_boxes_input_spec.rb +162 -0
  29. data/spec/inputs/country_input_spec.rb +80 -0
  30. data/spec/inputs/date_input_spec.rb +45 -0
  31. data/spec/inputs/datetime_input_spec.rb +155 -0
  32. data/spec/inputs/file_input_spec.rb +33 -0
  33. data/spec/inputs/hidden_input_spec.rb +52 -0
  34. data/spec/inputs/numeric_input_spec.rb +44 -0
  35. data/spec/inputs/password_input_spec.rb +46 -0
  36. data/spec/inputs/radio_input_spec.rb +149 -0
  37. data/spec/inputs/select_input_spec.rb +459 -0
  38. data/spec/inputs/string_input_spec.rb +47 -0
  39. data/spec/inputs/text_input_spec.rb +33 -0
  40. data/spec/inputs/time_input_spec.rb +44 -0
  41. data/spec/inputs/time_zone_input_spec.rb +102 -0
  42. data/spec/inputs_spec.rb +395 -0
  43. data/spec/label_spec.rb +48 -0
  44. data/spec/nested_forms_spec.rb +50 -0
  45. data/spec/semantic_fields_for_spec.rb +44 -0
  46. data/spec/spec.opts +2 -0
  47. data/spec/spec_helper.rb +212 -0
  48. metadata +121 -16
  49. data/lib/justin_french/formtastic.rb +0 -10
  50. data/spec/formtastic_spec.rb +0 -3072
  51. data/spec/test_helper.rb +0 -14
data/Rakefile CHANGED
@@ -1,15 +1,44 @@
1
+ # coding: utf-8
1
2
  require 'rake'
2
3
  require 'rake/rdoctask'
3
- require 'spec/rake/spectask'
4
4
 
5
5
  begin
6
- GEM = "formtastic"
6
+ require 'spec/rake/spectask'
7
+ rescue LoadError
8
+ begin
9
+ gem 'rspec-rails', '>= 1.0.0'
10
+ require 'spec/rake/spectask'
11
+ rescue LoadError
12
+ puts "[formtastic:] RSpec - or one of it's dependencies - is not available. Install it with: sudo gem install rspec-rails"
13
+ end
14
+ end
15
+
16
+ begin
17
+ GEM = "ShadowBelmolve-formtastic"
7
18
  AUTHOR = "Justin French"
8
19
  EMAIL = "justin@indent.com.au"
9
20
  SUMMARY = "A Rails form builder plugin/gem with semantically rich and accessible markup"
10
- HOMEPAGE = "http://github.com/justinfrench/formtastic/tree/master"
21
+ HOMEPAGE = "http://github.com/ShadowBelmolve/formtastic/tree/master"
22
+ INSTALL_MESSAGE = %q{
23
+ ========================================================================
24
+ Thanks for installing Formtastic!
25
+ ------------------------------------------------------------------------
26
+ You can now (optionally) run the generator to copy some stylesheets and
27
+ a config initializer into your application:
28
+ ./script/generate formtastic
29
+
30
+ To generate some semantic form markup for your exisiting models, just run:
31
+ ./script/generate form MODEL_NAME
32
+
33
+ Find out more and get involved:
34
+ http://github.com/justinfrench/formtastic
35
+ http://groups.google.com.au/group/formtastic
36
+ ========================================================================
37
+ }
11
38
 
39
+ gem 'jeweler', '>= 1.0.0'
12
40
  require 'jeweler'
41
+
13
42
  Jeweler::Tasks.new do |s|
14
43
  s.name = GEM
15
44
  s.summary = SUMMARY
@@ -17,30 +46,30 @@ begin
17
46
  s.homepage = HOMEPAGE
18
47
  s.description = SUMMARY
19
48
  s.author = AUTHOR
49
+ s.post_install_message = INSTALL_MESSAGE
20
50
 
21
51
  s.require_path = 'lib'
22
- s.autorequire = GEM
23
52
  s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,generators,spec}/**/*")
53
+
54
+ # Runtime dependencies: When installing Formtastic these will be checked if they are installed.
55
+ # Will be offered to install these if they are not already installed.
56
+ s.add_dependency 'activesupport', '>= 2.3.0'
57
+ s.add_dependency 'actionpack', '>= 2.3.0'
58
+
59
+ # Development dependencies. Not installed by default.
60
+ # Install with: sudo gem install formtastic --development
61
+ s.add_development_dependency 'rspec-rails', '>= 1.2.6'
62
+ s.add_development_dependency 'rspec_tag_matchers', '>= 1.0.0'
24
63
  end
64
+
65
+ Jeweler::GemcutterTasks.new
25
66
  rescue LoadError
26
- puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
67
+ puts "[formtastic:] Jeweler - or one of its dependencies - is not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
27
68
  end
28
69
 
29
70
  desc 'Default: run unit specs.'
30
71
  task :default => :spec
31
72
 
32
- desc 'Test the formtastic plugin.'
33
- Spec::Rake::SpecTask.new('spec') do |t|
34
- t.spec_files = FileList['spec/**/*_spec.rb']
35
- t.spec_opts = ["-c"]
36
- end
37
-
38
- desc 'Test the formtastic plugin with specdoc formatting and colors'
39
- Spec::Rake::SpecTask.new('specdoc') do |t|
40
- t.spec_files = FileList['spec/**/*_spec.rb']
41
- t.spec_opts = ["--format specdoc", "-c"]
42
- end
43
-
44
73
  desc 'Generate documentation for the formtastic plugin.'
45
74
  Rake::RDocTask.new(:rdoc) do |rdoc|
46
75
  rdoc.rdoc_dir = 'rdoc'
@@ -50,9 +79,23 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
50
79
  rdoc.rdoc_files.include('lib/**/*.rb')
51
80
  end
52
81
 
53
- desc "Run all examples with RCov"
54
- Spec::Rake::SpecTask.new('examples_with_rcov') do |t|
55
- t.spec_files = FileList['spec/**/*_spec.rb']
56
- t.rcov = true
57
- t.rcov_opts = ['--exclude', 'spec,Library']
82
+ if defined?(Spec)
83
+ desc 'Test the formtastic plugin.'
84
+ Spec::Rake::SpecTask.new('spec') do |t|
85
+ t.spec_files = FileList['spec/**/*_spec.rb']
86
+ t.spec_opts = ["-c"]
87
+ end
88
+
89
+ desc 'Test the formtastic plugin with specdoc formatting and colors'
90
+ Spec::Rake::SpecTask.new('specdoc') do |t|
91
+ t.spec_files = FileList['spec/**/*_spec.rb']
92
+ t.spec_opts = ["--format specdoc", "-c"]
93
+ end
94
+
95
+ desc "Run all examples with RCov"
96
+ Spec::Rake::SpecTask.new('examples_with_rcov') do |t|
97
+ t.spec_files = FileList['spec/**/*_spec.rb']
98
+ t.rcov = true
99
+ t.rcov_opts = ['--exclude', 'spec,Library']
100
+ end
58
101
  end
@@ -0,0 +1,16 @@
1
+ NAME
2
+ form - Formtastic form generator.
3
+
4
+ DESCRIPTION
5
+ Generates formtastic form code based on an existing model. By default the generated code will be printed out directly in the terminal, and also copied to clipboard. Can optionally be saved into partial directly.
6
+
7
+ Required:
8
+ ExistingModelName - The name of an existing model for which the generator should generate form code.
9
+
10
+ Options:
11
+ --haml Generate HAML instead of ERB.
12
+ --partial Generate a form partial in the model views path, i.e. "_form.html.erb" or _form.html.haml".
13
+ --controller PATH Generate for custom controller/view path - in case model and controller namespace is different, i.e. "admin/posts".
14
+
15
+ EXAMPLE
16
+ ./script/generate form ExistingModelName [--haml] [--partial]
@@ -0,0 +1,120 @@
1
+ # coding: utf-8
2
+ # Get current OS - needed for clipboard functionality
3
+ case RUBY_PLATFORM
4
+ when /darwin/ then
5
+ CURRENT_OS = :osx
6
+ when /win32/
7
+ CURRENT_OS = :win
8
+ begin
9
+ require 'win32/clipboard'
10
+ rescue LoadError
11
+ # Do nothing
12
+ end
13
+ else
14
+ CURRENT_OS = :x
15
+ end
16
+
17
+ class FormGenerator < Rails::Generator::NamedBase
18
+
19
+ default_options :haml => false,
20
+ :partial => false
21
+
22
+ VIEWS_PATH = File.join('app', 'views').freeze
23
+ IGNORED_COLUMNS = [:updated_at, :created_at].freeze
24
+
25
+ attr_reader :controller_file_name,
26
+ :controller_class_path,
27
+ :controller_class_nesting,
28
+ :controller_class_nesting_depth,
29
+ :controller_class_name,
30
+ :template_type
31
+
32
+ def initialize(runtime_args, runtime_options = {})
33
+ super
34
+ base_name, @controller_class_path = extract_modules(@name.pluralize)
35
+ controller_class_name_without_nesting, @controller_file_name = inflect_names(base_name)
36
+ @template_type = options[:haml] ? :haml : :erb
37
+ end
38
+
39
+ def manifest
40
+ record do |m|
41
+ if options[:partial]
42
+ controller_and_view_path = options[:controller] || File.join(controller_class_path, controller_file_name)
43
+ # Ensure directory exists.
44
+ m.directory File.join(VIEWS_PATH, controller_and_view_path)
45
+ # Create a form partial for the model as "_form" in it's views path.
46
+ m.template "view__form.html.#{template_type}", File.join(VIEWS_PATH, controller_and_view_path, "_form.html.#{template_type}")
47
+ else
48
+ # Load template file, and render without saving to file
49
+ template = File.read(File.join(source_root, "view__form.html.#{template_type}"))
50
+ erb = ERB.new(template, nil, '-')
51
+ generated_code = erb.result(binding).strip rescue nil
52
+
53
+ # Print the result, and copy to clipboard
54
+ puts "# ---------------------------------------------------------"
55
+ puts "# GENERATED FORMTASTIC CODE"
56
+ puts "# ---------------------------------------------------------"
57
+ puts
58
+ puts generated_code || " Nothing could be generated - model exists?"
59
+ puts
60
+ puts "# ---------------------------------------------------------"
61
+ puts " Copied to clipboard - just paste it!" if save_to_clipboard(generated_code)
62
+ end
63
+ end
64
+ end
65
+
66
+ protected
67
+
68
+ # Save to lipboard with multiple OS support.
69
+ def save_to_clipboard(data)
70
+ return unless data
71
+ begin
72
+ case CURRENT_OS
73
+ when :osx
74
+ `echo "#{data}" | pbcopy`
75
+ when :win
76
+ ::Win32::Clipboard.data = data
77
+ else # :linux/:unix
78
+ `echo "#{data}" | xsel --clipboard` || `echo "#{data}" | xclip`
79
+ end
80
+ rescue
81
+ false
82
+ else
83
+ true
84
+ end
85
+ end
86
+
87
+ # Add additional model attributes if specified in args - probably not that common scenario.
88
+ def attributes
89
+ # Get columns for the requested model.
90
+ existing_attributes = @class_name.constantize.content_columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) }
91
+ @args = super + existing_attributes
92
+ end
93
+
94
+ def add_options!(opt)
95
+ opt.separator ''
96
+ opt.separator 'Options:'
97
+
98
+ # Allow option to generate HAML views instead of ERB.
99
+ opt.on('--haml',
100
+ "Generate HAML output instead of the default ERB.") do |v|
101
+ options[:haml] = v
102
+ end
103
+
104
+ # Allow option to generate to partial in model's views path, instead of printing out in terminal.
105
+ opt.on('--partial',
106
+ "Save generated output directly to a form partial (app/views/{resource}/_form.html.*).") do |v|
107
+ options[:partial] = v
108
+ end
109
+
110
+ opt.on('--controller CONTROLLER_PATH',
111
+ "Specify a non-standard controller for the specified model (e.g. admin/posts).") do |v|
112
+ options[:controller] = v if v.present?
113
+ end
114
+ end
115
+
116
+ def banner
117
+ "Usage: #{$0} form ExistingModelName [--haml] [--partial]"
118
+ end
119
+
120
+ end
@@ -0,0 +1,5 @@
1
+ <%% form.inputs do %>
2
+ <% attributes.each do |attribute| -%>
3
+ <%%= form.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>' %>
4
+ <% end -%>
5
+ <%% end %>
@@ -0,0 +1,4 @@
1
+ - form.inputs do
2
+ <% attributes.each do |attribute| -%>
3
+ = form.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>'
4
+ <% end -%>
@@ -0,0 +1,24 @@
1
+ class FormtasticGenerator < Rails::Generator::Base
2
+
3
+ def initialize(*runtime_args)
4
+ super
5
+ end
6
+
7
+ def manifest
8
+ record do |m|
9
+ m.directory File.join('config', 'initializers')
10
+ m.template 'formtastic.rb', File.join('config', 'initializers', 'formtastic.rb')
11
+
12
+ m.directory File.join('public', 'stylesheets')
13
+ m.template 'formtastic.css', File.join('public', 'stylesheets', 'formtastic.css')
14
+ m.template 'formtastic_changes.css', File.join('public', 'stylesheets', 'formtastic_changes.css')
15
+ end
16
+ end
17
+
18
+ protected
19
+
20
+ def banner
21
+ %{Usage: #{$0} #{spec.name}\nCopies formtastic.css and formtastic_changes.css to public/stylesheets/ and a config initializer to config/initializers/formtastic.rb}
22
+ end
23
+
24
+ end
@@ -64,6 +64,7 @@ form.formtastic fieldset ol li li label input { line-height:100%; vertical-align
64
64
  form.formtastic fieldset ol li fieldset { position:relative; }
65
65
  form.formtastic fieldset ol li fieldset legend { position:absolute; width:25%; padding-top:0.1em; }
66
66
  form.formtastic fieldset ol li fieldset legend span { position:absolute; }
67
+ form.formtastic fieldset ol li fieldset legend.label label { position:absolute; }
67
68
  form.formtastic fieldset ol li fieldset ol { float:left; width:74%; margin:0; padding:0 0 0 25%; }
68
69
  form.formtastic fieldset ol li fieldset ol li { padding:0; border:0; }
69
70
 
@@ -83,6 +84,7 @@ form.formtastic fieldset ol li ul.errors li { padding:0; border:none; display:li
83
84
  /* STRING & NUMERIC OVERRIDES
84
85
  --------------------------------------------------------------------------------------------------*/
85
86
  form.formtastic fieldset ol li.string input { width:74%; }
87
+ form.formtastic fieldset ol li.password input { width:74%; }
86
88
  form.formtastic fieldset ol li.numeric input { width:74%; }
87
89
 
88
90
 
@@ -0,0 +1,51 @@
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
+ # Should all fields be considered "required" by default?
5
+ # Defaults to true, see ValidationReflection notes below.
6
+ # Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
7
+
8
+ # Should select fields have a blank option/prompt by default?
9
+ # Defaults to true.
10
+ # Formtastic::SemanticFormBuilder.include_blank_for_select_by_default = true
11
+
12
+ # Set the string that will be appended to the labels/fieldsets which are required
13
+ # It accepts string or procs and the default is a localized version of
14
+ # '<abbr title="required">*</abbr>'. In other words, if you configure formtastic.required
15
+ # in your locale, it will replace the abbr title properly. But if you don't want to use
16
+ # abbr tag, you can simply give a string as below
17
+ # Formtastic::SemanticFormBuilder.required_string = "(required)"
18
+
19
+ # Set the string that will be appended to the labels/fieldsets which are optional
20
+ # Defaults to an empty string ("") and also accepts procs (see required_string above)
21
+ # Formtastic::SemanticFormBuilder.optional_string = "(optional)"
22
+
23
+ # Set the way inline errors will be displayed.
24
+ # Defaults to :sentence, valid options are :sentence, :list and :none
25
+ # Formtastic::SemanticFormBuilder.inline_errors = :sentence
26
+
27
+ # Set the method to call on label text to transform or format it for human-friendly
28
+ # reading when formtastic is user without object. Defaults to :humanize.
29
+ # Formtastic::SemanticFormBuilder.label_str_method = :humanize
30
+
31
+ # Set the array of methods to try calling on parent objects in :select and :radio inputs
32
+ # for the text inside each @<option>@ tag or alongside each radio @<input>@. The first method
33
+ # that is found on the object will be used.
34
+ # Defaults to ["to_label", "display_name", "full_name", "name", "title", "username", "login", "value", "to_s"]
35
+ # Formtastic::SemanticFormBuilder.collection_label_methods = [
36
+ # "to_label", "display_name", "full_name", "name", "title", "username", "login", "value", "to_s"]
37
+
38
+ # Formtastic by default renders inside li tags the input, hints and then
39
+ # errors messages. Sometimes you want the hints to be rendered first than
40
+ # the input, in the following order: hints, input and errors. You can
41
+ # customize it doing just as below:
42
+ # Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
43
+
44
+ # Specifies if labels/hints for input fields automatically be looked up using I18n.
45
+ # Default value: false. Overridden for specific fields by setting value to true,
46
+ # i.e. :label => true, or :hint => true (or opposite depending on initialized value)
47
+ # Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
48
+
49
+ # You can add custom inputs or override parts of Formtastic by subclassing SemanticFormBuilder and
50
+ # specifying that class here. Defaults to SemanticFormBuilder.
51
+ # Formtastic::SemanticFormHelper.builder = MyCustomBuilder
@@ -1,21 +1,16 @@
1
1
  class FormtasticStylesheetsGenerator < Rails::Generator::Base
2
2
 
3
3
  def initialize(*runtime_args)
4
- super
4
+ puts %q{
5
+ ===================================================
6
+ Please run `./script/generate formtastic` instead.
7
+ ===================================================
8
+ }
5
9
  end
6
10
 
7
11
  def manifest
8
12
  record do |m|
9
- m.directory File.join('public', 'stylesheets')
10
- m.template 'formtastic.css', File.join('public', 'stylesheets', 'formtastic.css')
11
- m.template 'formtastic_changes.css', File.join('public', 'stylesheets', 'formtastic_changes.css')
12
13
  end
13
14
  end
14
15
 
15
- protected
16
-
17
- def banner
18
- %{Usage: #{$0} #{spec.name}\nCopies formtastic.css and formtastic_changes.css to public/}
19
- end
20
-
21
16
  end
@@ -1,9 +1,5 @@
1
- # Override the default ActiveRecordHelper behaviour of wrapping the input.
2
- # This gets taken care of semantically by adding an error class to the LI tag
3
- # containing the input.
4
- ActionView::Base.field_error_proc = proc do |html_tag, instance_tag|
5
- html_tag
6
- end
1
+ # coding: utf-8
2
+ require File.join(File.dirname(__FILE__), *%w[formtastic i18n])
7
3
 
8
4
  module Formtastic #:nodoc:
9
5
 
@@ -11,7 +7,8 @@ module Formtastic #:nodoc:
11
7
 
12
8
  @@default_text_field_size = 50
13
9
  @@all_fields_required_by_default = true
14
- @@required_string = proc { %{<abbr title="#{I18n.t 'formtastic.required', :default => 'required'}">*</abbr>} }
10
+ @@include_blank_for_select_by_default = true
11
+ @@required_string = proc { %{<abbr title="#{::Formtastic::I18n.t(:required)}">*</abbr>} }
15
12
  @@optional_string = ''
16
13
  @@inline_errors = :sentence
17
14
  @@label_str_method = :humanize
@@ -20,24 +17,15 @@ module Formtastic #:nodoc:
20
17
  @@file_methods = [ :file?, :public_filename ]
21
18
  @@priority_countries = ["Australia", "Canada", "United Kingdom", "United States"]
22
19
  @@i18n_lookups_by_default = false
20
+ @@default_commit_button_accesskey = nil
23
21
 
24
- cattr_accessor :default_text_field_size, :all_fields_required_by_default, :required_string,
25
- :optional_string, :inline_errors, :label_str_method, :collection_label_methods,
26
- :inline_order, :file_methods, :priority_countries, :i18n_lookups_by_default
22
+ cattr_accessor :default_text_field_size, :all_fields_required_by_default, :include_blank_for_select_by_default,
23
+ :required_string, :optional_string, :inline_errors, :label_str_method, :collection_label_methods,
24
+ :inline_order, :file_methods, :priority_countries, :i18n_lookups_by_default, :default_commit_button_accesskey
27
25
 
28
- I18N_SCOPES = [ '{{model}}.{{action}}.{{attribute}}',
29
- '{{model}}.{{attribute}}',
30
- '{{attribute}}']
26
+ RESERVED_COLUMNS = [:created_at, :updated_at, :created_on, :updated_on, :lock_version, :version]
31
27
 
32
- # Keeps simple mappings in a hash
33
- INPUT_MAPPINGS = {
34
- :string => :text_field,
35
- :password => :password_field,
36
- :numeric => :text_field,
37
- :text => :text_area,
38
- :file => :file_field
39
- }
40
- STRING_MAPPINGS = [ :string, :password, :numeric ]
28
+ INLINE_ERROR_TYPES = [:sentence, :list, :first]
41
29
 
42
30
  attr_accessor :template
43
31
 
@@ -78,7 +66,8 @@ module Formtastic #:nodoc:
78
66
  #
79
67
  # <% semantic_form_for @employee do |form| %>
80
68
  # <% form.inputs do -%>
81
- # <%= form.input :name, :label => "Full Name"%>
69
+ # <%= form.input :secret, :value => "Hello" %>
70
+ # <%= form.input :name, :label => "Full Name" %>
82
71
  # <%= form.input :manager_id, :as => :radio %>
83
72
  # <%= form.input :hired_at, :as => :date, :label => "Date Hired" %>
84
73
  # <%= form.input :phone, :required => false, :hint => "Eg: +1 555 1234" %>
@@ -87,25 +76,24 @@ module Formtastic #:nodoc:
87
76
  #
88
77
  def input(method, options = {})
89
78
  options[:required] = method_required?(method) unless options.key?(:required)
90
- options[:as] ||= default_input_type(method)
79
+ options[:as] ||= default_input_type(method, options)
91
80
 
92
81
  html_class = [ options[:as], (options[:required] ? :required : :optional) ]
93
- html_class << 'error' if @object && @object.respond_to?(:errors) && @object.errors[method.to_sym]
82
+ html_class << 'error' if @object && @object.respond_to?(:errors) && !@object.errors[method.to_sym].blank?
94
83
 
95
84
  wrapper_html = options.delete(:wrapper_html) || {}
96
85
  wrapper_html[:id] ||= generate_html_id(method)
97
86
  wrapper_html[:class] = (html_class << wrapper_html[:class]).flatten.compact.join(' ')
98
87
 
99
- if [:boolean_select, :boolean_radio].include?(options[:as])
100
- ::ActiveSupport::Deprecation.warn(":as => :#{options[:as]} is deprecated, use :as => :#{options[:as].to_s[8..-1]} instead", caller[3..-1])
101
- end
102
-
103
88
  if options[:input_html] && options[:input_html][:id]
104
89
  options[:label_html] ||= {}
105
90
  options[:label_html][:for] ||= options[:input_html][:id]
106
91
  end
107
92
 
108
- list_item_content = @@inline_order.map do |type|
93
+ input_parts = @@inline_order.dup
94
+ input_parts.delete(:errors) if options[:as] == :hidden
95
+
96
+ list_item_content = input_parts.map do |type|
109
97
  send(:"inline_#{type}_for", method, options)
110
98
  end.compact.join("\n")
111
99
 
@@ -150,11 +138,16 @@ module Formtastic #:nodoc:
150
138
  # <%= form.inputs %>
151
139
  # <% end %>
152
140
  #
141
+ # With a few arguments:
142
+ # <% semantic_form_for @post do |form| %>
143
+ # <%= form.inputs "Post details", :title, :body %>
144
+ # <% end %>
145
+ #
153
146
  # === Options
154
147
  #
155
- # All options (with the exception of :name) are passed down to the fieldset as HTML
156
- # attributes (id, class, style, etc). If provided, the :name option is passed into a
157
- # legend tag inside the fieldset (otherwise a legend is not generated).
148
+ # All options (with the exception of :name/:title) are passed down to the fieldset as HTML
149
+ # attributes (id, class, style, etc). If provided, the :name/:title option is passed into a
150
+ # legend tag inside the fieldset.
158
151
  #
159
152
  # # With a block:
160
153
  # <% semantic_form_for @post do |form| %>
@@ -168,6 +161,11 @@ module Formtastic #:nodoc:
168
161
  # <%= form.inputs :title, :body, :name => "Create a new post", :style => "border:1px;" %>
169
162
  # <% end %>
170
163
  #
164
+ # # ...or the equivalent:
165
+ # <% semantic_form_for @post do |form| %>
166
+ # <%= form.inputs "Create a new post", :title, :body, :style => "border:1px;" %>
167
+ # <% end %>
168
+ #
171
169
  # === It's basically a fieldset!
172
170
  #
173
171
  # Instead of hard-coding fieldsets & legends into your form to logically group related fields,
@@ -182,6 +180,9 @@ module Formtastic #:nodoc:
182
180
  # <%= f.input :created_at %>
183
181
  # <%= f.input :user_id, :label => "Author" %>
184
182
  # <% end %>
183
+ # <% f.inputs "Extra" do %>
184
+ # <%= f.input :update_at %>
185
+ # <% end %>
185
186
  # <% end %>
186
187
  #
187
188
  # # Output:
@@ -199,6 +200,12 @@ module Formtastic #:nodoc:
199
200
  # <li class="select">...</li>
200
201
  # </ol>
201
202
  # </fieldset>
203
+ # <fieldset class="inputs">
204
+ # <legend><span>Extra</span></legend>
205
+ # <ol>
206
+ # <li class="datetime">...</li>
207
+ # </ol>
208
+ # </fieldset>
202
209
  # </form>
203
210
  #
204
211
  # === Nested attributes
@@ -240,23 +247,27 @@ module Formtastic #:nodoc:
240
247
  # instead (just as you would do with Rails' form builder).
241
248
  #
242
249
  def inputs(*args, &block)
250
+ title = field_set_title_from_args(*args)
243
251
  html_options = args.extract_options!
244
252
  html_options[:class] ||= "inputs"
245
-
246
- if html_options[:for]
247
- inputs_for_nested_attributes(args, html_options, &block)
253
+ html_options[:name] = title
254
+
255
+ if html_options[:for] # Nested form
256
+ inputs_for_nested_attributes(*(args << html_options), &block)
248
257
  elsif block_given?
249
- field_set_and_list_wrapping(html_options, &block)
258
+ field_set_and_list_wrapping(*(args << html_options), &block)
250
259
  else
251
260
  if @object && args.empty?
252
- args = @object.class.reflections.map { |n,_| n if _.macro == :belongs_to }
253
- args += @object.class.content_columns.map(&:name)
254
- args -= %w[created_at updated_at created_on updated_on lock_version]
261
+ args = self.association_columns(:belongs_to)
262
+ args += self.content_columns
263
+ args -= RESERVED_COLUMNS
255
264
  args.compact!
256
265
  end
257
- contents = args.map { |method| input(method.to_sym) }
258
-
259
- field_set_and_list_wrapping(html_options, contents)
266
+ legend = args.shift if args.first.is_a?(::String)
267
+ contents = args.collect { |method| input(method.to_sym) }
268
+ args.unshift(legend) if legend.present?
269
+
270
+ field_set_and_list_wrapping(*((args << html_options) << contents))
260
271
  end
261
272
  end
262
273
  alias :input_field_set :inputs
@@ -287,18 +298,35 @@ module Formtastic #:nodoc:
287
298
  #
288
299
  # The value of the button text can be overridden:
289
300
  #
290
- # <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" />
301
+ # <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" class="{create|update|submit}" />
302
+ # <%= form.commit_button :label => "Go" %> => <input name="commit" type="submit" value="Go" class="{create|update|submit}" />
291
303
  #
292
304
  # And you can pass html atributes down to the input, with or without the button text:
293
305
  #
294
- # <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" />
295
- # <%= form.commit_button :class => "pretty" %> => <input name="commit" type="submit" value="Save Post" class="pretty" />
296
-
306
+ # <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" class="{create|update|submit}" />
307
+ # <%= form.commit_button :class => "pretty" %> => <input name="commit" type="submit" value="Save Post" class="pretty {create|update|submit}" />
308
+ #
297
309
  def commit_button(*args)
298
- value = args.first.is_a?(String) ? args.shift : save_or_create_button_text
299
- options = args.shift || {}
310
+ options = args.extract_options!
311
+ text = options.delete(:label) || args.shift
312
+
313
+ if @object
314
+ key = @object.new_record? ? :create : :update
315
+ object_name = @object.class.human_name
316
+ else
317
+ key = :submit
318
+ object_name = @object_name.to_s.send(@@label_str_method)
319
+ end
320
+
321
+ text = (self.localized_string(key, text, :action, :model => object_name) ||
322
+ ::Formtastic::I18n.t(key, :model => object_name)) unless text.is_a?(::String)
323
+
300
324
  button_html = options.delete(:button_html) || {}
301
- template.content_tag(:li, self.submit(value, button_html), :class => "commit")
325
+ button_html.merge!(:class => [button_html[:class], key].compact.join(' '))
326
+ element_class = ['commit', options.delete(:class)].compact.join(' ') # TODO: Add class reflecting on form action.
327
+ accesskey = (options.delete(:accesskey) || @@default_commit_button_accesskey) unless button_html.has_key?(:accesskey)
328
+ button_html = button_html.merge(:accesskey => accesskey) if accesskey
329
+ template.content_tag(:li, self.submit(text, button_html), :class => element_class)
302
330
  end
303
331
 
304
332
  # A thin wrapper around #fields_for to set :builder => Formtastic::SemanticFormBuilder
@@ -322,72 +350,11 @@ module Formtastic #:nodoc:
322
350
  #
323
351
  def semantic_fields_for(record_or_name_or_array, *args, &block)
324
352
  opts = args.extract_options!
325
- opts.merge!(:builder => Formtastic::SemanticFormBuilder)
353
+ opts[:builder] ||= Formtastic::SemanticFormHelper.builder
326
354
  args.push(opts)
327
355
  fields_for(record_or_name_or_array, *args, &block)
328
356
  end
329
357
 
330
- # Generates the label for the input. It also accepts the same arguments as
331
- # Rails label method. It has three options that are not supported by Rails
332
- # label method:
333
- #
334
- # * :required - Appends an abbr tag if :required is true
335
- # * :label - An alternative form to give the label content. Whenever label
336
- # is false, a blank string is returned.
337
- # * :as_span - When true returns a span tag with class label instead of a label element
338
- # * :input_name - Gives the input to match for. This is needed when you want to
339
- # to call f.label :authors but it should match :author_ids.
340
- #
341
- # == Examples
342
- #
343
- # f.label :title # like in rails, except that it searches the label on I18n API too
344
- #
345
- # f.label :title, "Your post title"
346
- # f.label :title, :label => "Your post title" # Added for formtastic API
347
- #
348
- # f.label :title, :required => true # Returns <label>Title<abbr title="required">*</abbr></label>
349
- #
350
- def label(method, options_or_text=nil, options=nil)
351
- if options_or_text.is_a?(Hash)
352
- return "" if options_or_text[:label] == false
353
- options = options_or_text
354
- text = options.delete(:label)
355
- else
356
- text = options_or_text
357
- options ||= {}
358
- end
359
-
360
- text = localized_attribute_string(method, text, :label) || humanized_attribute_name(method)
361
- text += required_or_optional_string(options.delete(:required))
362
-
363
- input_name = options.delete(:input_name) || method
364
- if options.delete(:as_span)
365
- options[:class] ||= 'label'
366
- template.content_tag(:span, text, options)
367
- else
368
- super(input_name, text, options)
369
- end
370
- end
371
-
372
- # Generates error messages for the given method. Errors can be shown as list
373
- # or as sentence. If :none is set, no error is shown.
374
- #
375
- # This method is also aliased as errors_on, so you can call on your custom
376
- # inputs as well:
377
- #
378
- # semantic_form_for :post do |f|
379
- # f.text_field(:body)
380
- # f.errors_on(:body)
381
- # end
382
- #
383
- def inline_errors_for(method, options=nil) #:nodoc:
384
- return nil unless @object && @object.respond_to?(:errors) && [:sentence, :list].include?(@@inline_errors)
385
-
386
- errors = @object.errors[method.to_sym]
387
- send("error_#{@@inline_errors}", Array(errors)) unless errors.blank?
388
- end
389
- alias :errors_on :inline_errors_for
390
-
391
358
  #
392
359
  #
393
360
  # Stolen from Attribute_fu (http://github.com/giraffesoft/attribute_fu)
@@ -395,8 +362,7 @@ module Formtastic #:nodoc:
395
362
  #
396
363
  # Dinamically add and remove nested forms for a has_many relation.
397
364
  #
398
-
399
- # Add a link to remove the associated partial
365
+ # Add a link to remove the associated partial
400
366
  # # Example:
401
367
  # <% semantic_form_for @post do |post| %>
402
368
  # <%= post.input :title %>
@@ -412,7 +378,7 @@ module Formtastic #:nodoc:
412
378
  # <%= f.remove_link "Remove" %>
413
379
  # </div>
414
380
  #
415
- # # Output:
381
+ # # Output:
416
382
  # <form ...>
417
383
  # <li class="string"><input type='text' name='post[author][name]' id='post_author_name' /></li>
418
384
  # <fieldset class="inputs" id="authors"><legend><span>Authors</span></legend><ol>
@@ -425,8 +391,9 @@ module Formtastic #:nodoc:
425
391
  # </div>
426
392
  # </ol></fieldset>
427
393
  # </form>
428
- #
429
- # * Opts
394
+ #
395
+ # Opts:
396
+ #
430
397
  # * :selector, id of element that will disappear(.hide()), if no id as give, will use css class
431
398
  # association.class.name last word in downcase #=> '.author'
432
399
  # f.remove_link "Remove", :selector => '#my_own_element_id'
@@ -437,7 +404,6 @@ module Formtastic #:nodoc:
437
404
  #
438
405
  # NOTE: remove_link must be put in the partial, not in main template
439
406
  #
440
-
441
407
  def remove_link(name, *args)
442
408
  options = args.extract_options!
443
409
  css_selector = options.delete(:selector) || ".#{@object.class.name.split("::").last.underscore}"
@@ -486,7 +452,7 @@ module Formtastic #:nodoc:
486
452
  before_function = opts.delete(:before_function) || ''
487
453
  after_function = opts.delete(:after_function) || ''
488
454
 
489
- form = self.render_associated_form(object, :partial => partial)
455
+ form = render_associated_form(object, :partial => partial)
490
456
  form.gsub!(/attributes_(\d+)/, '__idx__')
491
457
  form.gsub!(/\[(\d+)\]/, '__idxx__')
492
458
 
@@ -498,7 +464,9 @@ module Formtastic #:nodoc:
498
464
  end
499
465
 
500
466
  # Render associated form
501
- # # Example:
467
+ #
468
+ # Example:
469
+ #
502
470
  # <% semantic_form_for @post do |post| %>
503
471
  # <%= post.input :title %>
504
472
  # <% post.inputs :name => 'Authors', :id => 'authors' do %>
@@ -507,10 +475,12 @@ module Formtastic #:nodoc:
507
475
  # <% end %>
508
476
  # <% end %>
509
477
  #
510
- # # app/views/authors/_add_author.html.erb
478
+ # Partial: app/views/authors/_add_author.html.erb
479
+ #
511
480
  # <% f.input :name %>
512
481
  #
513
- # # Output:
482
+ # Output:
483
+ #
514
484
  # <form ...>
515
485
  # <li class="string"><input type='text' name='post[author][name]' id='post_author_name' /></li>
516
486
  # <fieldset class="inputs" id="authors"><legend><span>Authors</span></legend><ol>
@@ -520,37 +490,43 @@ module Formtastic #:nodoc:
520
490
  # </ol></fieldset>
521
491
  # </form>
522
492
  #
523
- # Opts
524
- # * :partial, render a partial, if not partial is given, try to find a partial with association
525
- # name in the controller folder.
493
+ # Opts:
494
+ # * :partial => Render a given partial, if no one is given, try to find a partial
495
+ # with association name in the controller folder.
496
+ #
526
497
  # post.render_associated_form :authors, :partial => 'author'
527
498
  # ^ try to render app/views/posts/_author.html.erb
499
+ #
528
500
  # post.render_associated_form :authors, :partial => 'authors/author'
529
501
  # ^ try to render app/views/authors/_author.html.erb
530
- # NOTE: Partial need to use 'f' as formtastic reference
531
- # Example:
502
+ #
503
+ # NOTE: Partial need to use 'f' as formtastic reference. Example:
504
+ #
532
505
  # <%= f.input :name %> #=> render author name association
533
506
  #
534
- # * :new make X empty partials if is a new record
535
- # Examples:
507
+ # * :new => make N empty partials if it is a new record. Example:
508
+ #
536
509
  # post.render_associated_form :authors, :new => 2
537
510
  # ^ make 2 empty authors partials
538
511
  #
539
- # * :edit show only X partial if is editing a record
512
+ # * :edit => show only X partial if is editing a record. Example:
513
+ #
540
514
  # post.render_associated_form :authors, :edit => 3
541
515
  # ^ if record have 1 author, make 2 new empty partials
542
516
  # NOTE: :edit not conflicts with :new; :new is for new records only
543
517
  #
544
- # * :new_in_edit show X new partial if is editing a record
518
+ # * :new_in_edit => show X new partial if is editing a record. Example:
519
+ #
545
520
  # post.render_associated_form :authors, :new_in_edit => 2
546
521
  # ^ make more 2 new partial for record
547
522
  # NOTE: this option is ignored if :edit is seted
548
- # Example:
549
- # post.render_associated_form :authors, :edit => 2, :new_in_edit => 100
550
- # ^ if record have 1 author, make more one partial
551
523
  #
524
+ # Example:
552
525
  #
553
- def render_associated_form(associated, opts = {})
526
+ # post.render_associated_form :authors, :edit => 2, :new_in_edit => 100
527
+ # ^ if record have 1 author, make more one partial
528
+ #
529
+ def render_associated_form(associated, opts = {})
554
530
  associated = @object.send(associated.to_s) if associated.is_a? Symbol
555
531
  associated = associated.is_a?(Array) ? associated : [associated] # preserve association proxy if this is one
556
532
 
@@ -562,7 +538,6 @@ module Formtastic #:nodoc:
562
538
  opts[:new_in_edit].times { associated.build }
563
539
  end
564
540
 
565
-
566
541
  unless associated.empty?
567
542
  name = extract_option_or_class_name(opts, :name, associated.first)
568
543
  partial = opts[:partial] || name
@@ -577,8 +552,6 @@ module Formtastic #:nodoc:
577
552
  end
578
553
  end
579
554
 
580
- protected
581
-
582
555
  def association_name(class_name)
583
556
  @object.respond_to?("#{class_name}_attributes=") ? class_name : class_name.pluralize
584
557
  end
@@ -587,849 +560,1205 @@ module Formtastic #:nodoc:
587
560
  (hash.delete(option) || object.class.name.split('::').last.underscore)
588
561
  end
589
562
 
590
- # Prepare options to be sent to label
563
+ # Generates the label for the input. It also accepts the same arguments as
564
+ # Rails label method. It has three options that are not supported by Rails
565
+ # label method:
591
566
  #
592
- def options_for_label(options)
593
- options.slice(:label, :required).merge!(options.fetch(:label_html, {}))
594
- end
595
-
596
- # Deals with :for option when it's supplied to inputs methods. Additional
597
- # options to be passed down to :for should be supplied using :for_options
598
- # key.
567
+ # * :required - Appends an abbr tag if :required is true
568
+ # * :label - An alternative form to give the label content. Whenever label
569
+ # is false, a blank string is returned.
570
+ # * :input_name - Gives the input to match for. This is needed when you want to
571
+ # to call f.label :authors but it should match :author_ids.
599
572
  #
600
- # It should raise an error if a block with arity zero is given.
573
+ # == Examples
601
574
  #
602
- def inputs_for_nested_attributes(args, options, &block)
603
- args << options.merge!(:parent => { :builder => self, :for => options[:for] })
604
-
605
- fields_for_block = if block_given?
606
- raise ArgumentError, 'You gave :for option with a block to inputs method, ' <<
607
- 'but the block does not accept any argument.' if block.arity <= 0
608
-
609
- proc { |f| f.inputs(*args){ block.call(f) } }
610
- else
611
- proc { |f| f.inputs(*args) }
612
- end
613
-
614
- fields_for_args = [options.delete(:for), options.delete(:for_options) || {}].flatten
615
- semantic_fields_for(*fields_for_args, &fields_for_block)
616
- end
617
-
618
- # Remove any Formtastic-specific options before passing the down options.
575
+ # f.label :title # like in rails, except that it searches the label on I18n API too
619
576
  #
620
- def set_options(options)
621
- options.except(:value_method, :label_method, :collection, :required, :label,
622
- :as, :hint, :input_html, :label_html, :value_as_class)
623
- end
624
-
625
- # Create a default button text. If the form is working with a object, it
626
- # defaults to "Create model" or "Save model" depending if we are working
627
- # with a new_record or not.
577
+ # f.label :title, "Your post title"
578
+ # f.label :title, :label => "Your post title" # Added for formtastic API
628
579
  #
629
- # When not working with models, it defaults to "Submit object".
580
+ # f.label :title, :required => true # Returns <label>Title<abbr title="required">*</abbr></label>
630
581
  #
631
- def save_or_create_button_text(prefix='Submit') #:nodoc:
632
- if @object
633
- prefix = @object.new_record? ? 'Create' : 'Save'
634
- object_name = @object.class.human_name
582
+ def label(method, options_or_text=nil, options=nil)
583
+ if options_or_text.is_a?(Hash)
584
+ return "" if options_or_text[:label] == false
585
+ options = options_or_text
586
+ text = options.delete(:label)
635
587
  else
636
- object_name = @object_name.to_s.send(@@label_str_method)
588
+ text = options_or_text
589
+ options ||= {}
637
590
  end
591
+ text = localized_string(method, text, :label) || humanized_attribute_name(method)
592
+ text += required_or_optional_string(options.delete(:required))
593
+
594
+ # special case for boolean (checkbox) labels, which have a nested input
595
+ text = (options.delete(:label_prefix_for_nested_input) || "") + text
638
596
 
639
- I18n.t(prefix.downcase, :default => prefix, :scope => [:formtastic]) << ' ' << object_name
597
+ input_name = options.delete(:input_name) || method
598
+ super(input_name, text, options)
640
599
  end
641
600
 
642
- # Determins if the attribute (eg :title) should be considered required or not.
643
- #
644
- # * if the :required option was provided in the options hash, the true/false value will be
645
- # returned immediately, allowing the view to override any guesswork that follows:
601
+ # Generates error messages for the given method. Errors can be shown as list,
602
+ # as sentence or just the first error can be displayed. If :none is set, no error is shown.
646
603
  #
647
- # * if the :required option isn't provided in the options hash, and the ValidationReflection
648
- # plugin is installed (http://github.com/redinger/validation_reflection), true is returned
649
- # if the validates_presence_of macro has been used in the class for this attribute, or false
650
- # otherwise.
604
+ # This method is also aliased as errors_on, so you can call on your custom
605
+ # inputs as well:
651
606
  #
652
- # * if the :required option isn't provided, and the plugin isn't available, the value of the
653
- # configuration option @@all_fields_required_by_default is used.
607
+ # semantic_form_for :post do |f|
608
+ # f.text_field(:body)
609
+ # f.errors_on(:body)
610
+ # end
654
611
  #
655
- def method_required?(attribute) #:nodoc:
656
- if @object && @object.class.respond_to?(:reflect_on_all_validations)
657
- attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym
658
-
659
- @object.class.reflect_on_all_validations.any? do |validation|
660
- validation.macro == :validates_presence_of && validation.name == attribute_sym
661
- end
612
+ def inline_errors_for(method, options = nil) #:nodoc:
613
+ if render_inline_errors?
614
+ errors = @object.errors[method.to_sym]
615
+ send(:"error_#{@@inline_errors}", [*errors]) if errors.present?
662
616
  else
663
- @@all_fields_required_by_default
617
+ nil
664
618
  end
665
619
  end
620
+ alias :errors_on :inline_errors_for
666
621
 
667
- # A method that deals with most of inputs (:string, :password, :file,
668
- # :textarea and :numeric). :select, :radio, :boolean and :datetime inputs
669
- # are not handled by this method, since they need more detailed approach.
670
- #
671
- # If input_html is given as option, it's passed down to the input.
672
- #
673
- def input_simple(type, method, options)
674
- html_options = options.delete(:input_html) || {}
675
- html_options = default_string_options(method, type).merge(html_options) if STRING_MAPPINGS.include?(type)
676
-
677
- self.label(method, options_for_label(options)) +
678
- self.send(INPUT_MAPPINGS[type], method, html_options)
679
- end
622
+ protected
680
623
 
681
- # Outputs a hidden field inside the wrapper, which should be hidden with CSS.
682
- # Additionals options can be given and will be sent straight to hidden input
683
- # element.
684
- #
685
- def hidden_input(method, options)
686
- self.hidden_field(method, set_options(options))
687
- end
624
+ def render_inline_errors?
625
+ @object && @object.respond_to?(:errors) && INLINE_ERROR_TYPES.include?(@@inline_errors)
626
+ end
688
627
 
628
+ # Collects content columns (non-relation columns) for the current form object class.
629
+ #
630
+ def content_columns #:nodoc:
631
+ self.model_name.constantize.content_columns.collect { |c| c.name.to_sym }.compact rescue []
632
+ end
689
633
 
690
- # Outputs a label and a select box containing options from the parent
691
- # (belongs_to, has_many, has_and_belongs_to_many) association. If an association
692
- # is has_many or has_and_belongs_to_many the select box will be set as multi-select
693
- # and size = 5
694
- #
695
- # Example (belongs_to):
696
- #
697
- # f.input :author
698
- #
699
- # <label for="book_author_id">Author</label>
700
- # <select id="book_author_id" name="book[author_id]">
701
- # <option value=""></option>
702
- # <option value="1">Justin French</option>
703
- # <option value="2">Jane Doe</option>
704
- # </select>
705
- #
706
- # Example (has_many):
707
- #
708
- # f.input :chapters
709
- #
710
- # <label for="book_chapter_ids">Chapters</label>
711
- # <select id="book_chapter_ids" name="book[chapter_ids]">
712
- # <option value=""></option>
713
- # <option value="1">Chapter 1</option>
714
- # <option value="2">Chapter 2</option>
715
- # </select>
716
- #
717
- # Example (has_and_belongs_to_many):
718
- #
719
- # f.input :authors
720
- #
721
- # <label for="book_author_ids">Authors</label>
722
- # <select id="book_author_ids" name="book[author_ids]">
723
- # <option value=""></option>
724
- # <option value="1">Justin French</option>
725
- # <option value="2">Jane Doe</option>
726
- # </select>
727
- #
728
- #
729
- # You can customize the options available in the select by passing in a collection (an Array or
730
- # Hash) through the :collection option. If not provided, the choices are found by inferring the
731
- # parent's class name from the method name and simply calling find(:all) on it
732
- # (VehicleOwner.find(:all) in the example above).
733
- #
734
- # Examples:
735
- #
736
- # f.input :author, :collection => @authors
737
- # f.input :author, :collection => Author.find(:all)
738
- # f.input :author, :collection => [@justin, @kate]
739
- # f.input :author, :collection => {@justin.name => @justin.id, @kate.name => @kate.id}
740
- # f.input :author, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
741
- #
742
- # Note: This input looks for a label method in the parent association.
743
- #
744
- # You can customize the text label inside each option tag, by naming the correct method
745
- # (:full_name, :display_name, :account_number, etc) to call on each object in the collection
746
- # by passing in the :label_method option. By default the :label_method is whichever element of
747
- # Formtastic::SemanticFormBuilder.collection_label_methods is found first.
748
- #
749
- # Examples:
750
- #
751
- # f.input :author, :label_method => :full_name
752
- # f.input :author, :label_method => :display_name
753
- # f.input :author, :label_method => :to_s
754
- # f.input :author, :label_method => :label
755
- #
756
- # You can also customize the value inside each option tag, by passing in the :value_method option.
757
- # Usage is the same as the :label_method option
758
- #
759
- # Examples:
760
- #
761
- # f.input :author, :value_method => :full_name
762
- # f.input :author, :value_method => :display_name
763
- # f.input :author, :value_method => :to_s
764
- # f.input :author, :value_method => :value
765
- #
766
- # You can pass html_options to the select tag using :input_html => {}
767
- #
768
- # Examples:
769
- #
770
- # f.input :authors, :input_html => {:size => 20, :multiple => true}
771
- #
772
- # By default, all select inputs will have a blank option at the top of the list. You can add
773
- # a prompt with the :prompt option, or disable the blank option with :include_blank => false.
774
- #
775
- def select_input(method, options)
776
- collection = find_collection_for_column(method, options)
777
- html_options = options.delete(:input_html) || {}
634
+ # Collects association columns (relation columns) for the current form object class.
635
+ #
636
+ def association_columns(*by_associations) #:nodoc:
637
+ if @object.present?
638
+ @object.class.reflections.collect do |name, _|
639
+ if by_associations.present?
640
+ name if by_associations.include?(_.macro)
641
+ else
642
+ name
643
+ end
644
+ end.compact
645
+ else
646
+ []
647
+ end
648
+ end
778
649
 
779
- unless options.key?(:include_blank) || options.key?(:prompt)
780
- options[:include_blank] = true
650
+ # Prepare options to be sent to label
651
+ #
652
+ def options_for_label(options) #:nodoc:
653
+ options.slice(:label, :required).merge!(options.fetch(:label_html, {}))
781
654
  end
782
655
 
783
- reflection = find_reflection(method)
784
- if reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro)
785
- options[:include_blank] = false
786
- html_options[:multiple] ||= true
787
- html_options[:size] ||= 5
788
- end
656
+ # Deals with :for option when it's supplied to inputs methods. Additional
657
+ # options to be passed down to :for should be supplied using :for_options
658
+ # key.
659
+ #
660
+ # It should raise an error if a block with arity zero is given.
661
+ #
662
+ def inputs_for_nested_attributes(*args, &block) #:nodoc:
663
+ options = args.extract_options!
664
+ args << options.merge!(:parent => { :builder => self, :for => options[:for] })
665
+
666
+ fields_for_block = if block_given?
667
+ raise ArgumentError, 'You gave :for option with a block to inputs method, ' <<
668
+ 'but the block does not accept any argument.' if block.arity <= 0
669
+
670
+ proc { |f| f.inputs(*args){ block.call(f) } }
671
+ else
672
+ proc { |f| f.inputs(*args) }
673
+ end
789
674
 
790
- input_name = generate_association_input_name(method)
791
- self.label(method, options_for_label(options).merge(:input_name => input_name)) +
792
- self.select(input_name, collection, set_options(options), html_options)
793
- end
794
- alias :boolean_select_input :select_input
675
+ fields_for_args = [options.delete(:for), options.delete(:for_options) || {}].flatten
676
+ semantic_fields_for(*fields_for_args, &fields_for_block)
677
+ end
795
678
 
796
- # Outputs a timezone select input as Rails' time_zone_select helper. You
797
- # can give priority zones as option.
798
- #
799
- # Examples:
800
- #
801
- # f.input :time_zone, :as => :time_zone, :priority_zones => /Australia/
802
- #
803
- def time_zone_input(method, options)
804
- html_options = options.delete(:input_html) || {}
679
+ # Remove any Formtastic-specific options before passing the down options.
680
+ #
681
+ def strip_formtastic_options(options) #:nodoc:
682
+ options.except(:value_method, :label_method, :collection, :required, :label,
683
+ :as, :hint, :input_html, :label_html, :value_as_class)
684
+ end
805
685
 
806
- self.label(method, options_for_label(options)) +
807
- self.time_zone_select(method, options.delete(:priority_zones), set_options(options), html_options)
808
- end
686
+ # Determins if the attribute (eg :title) should be considered required or not.
687
+ #
688
+ # * if the :required option was provided in the options hash, the true/false value will be
689
+ # returned immediately, allowing the view to override any guesswork that follows:
690
+ #
691
+ # * if the :required option isn't provided in the options hash, and the ValidationReflection
692
+ # plugin is installed (http://github.com/redinger/validation_reflection), true is returned
693
+ # if the validates_presence_of macro has been used in the class for this attribute, or false
694
+ # otherwise.
695
+ #
696
+ # * if the :required option isn't provided, and the plugin isn't available, the value of the
697
+ # configuration option @@all_fields_required_by_default is used.
698
+ #
699
+ def method_required?(attribute) #:nodoc:
700
+ if @object && @object.class.respond_to?(:reflect_on_validations_for)
701
+ attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym
702
+
703
+ @object.class.reflect_on_validations_for(attribute_sym).any? do |validation|
704
+ validation.macro == :validates_presence_of &&
705
+ validation.name == attribute_sym &&
706
+ (validation.options.present? ? options_require_validation?(validation.options) : true)
707
+ end
708
+ else
709
+ @@all_fields_required_by_default
710
+ end
711
+ end
809
712
 
810
- # Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list
811
- # items, one for each possible choice in the belongs_to association. Each li contains a
812
- # label and a radio input.
813
- #
814
- # Example:
815
- #
816
- # f.input :author, :as => :radio
817
- #
818
- # Output:
819
- #
820
- # <fieldset>
821
- # <legend><span>Author</span></legend>
822
- # <ol>
823
- # <li>
824
- # <label for="book_author_id_1"><input id="book_author_id_1" name="book[author_id]" type="radio" value="1" /> Justin French</label>
825
- # </li>
826
- # <li>
827
- # <label for="book_author_id_2"><input id="book_author_id_2" name="book[owner_id]" type="radio" value="2" /> Kate French</label>
828
- # </li>
829
- # </ol>
830
- # </fieldset>
831
- #
832
- # You can customize the options available in the select by passing in a collection (an Array or
833
- # Hash) through the :collection option. If not provided, the choices are found by inferring the
834
- # parent's class name from the method name and simply calling find(:all) on it
835
- # (Author.find(:all) in the example above).
836
- #
837
- # Examples:
838
- #
839
- # f.input :author, :as => :radio, :collection => @authors
840
- # f.input :author, :as => :radio, :collection => Author.find(:all)
841
- # f.input :author, :as => :radio, :collection => [@justin, @kate]
842
- # f.input :author, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
843
- #
844
- # You can also customize the text label inside each option tag, by naming the correct method
845
- # (:full_name, :display_name, :account_number, etc) to call on each object in the collection
846
- # by passing in the :label_method option. By default the :label_method is whichever element of
847
- # Formtastic::SemanticFormBuilder.collection_label_methods is found first.
848
- #
849
- # Examples:
850
- #
851
- # f.input :author, :as => :radio, :label_method => :full_name
852
- # f.input :author, :as => :radio, :label_method => :display_name
853
- # f.input :author, :as => :radio, :label_method => :to_s
854
- # f.input :author, :as => :radio, :label_method => :label
855
- #
856
- # Finally, you can set :value_as_class => true if you want that LI wrappers
857
- # contains a class with the wrapped radio input value.
858
- #
859
- def radio_input(method, options)
860
- collection = find_collection_for_column(method, options)
861
- html_options = set_options(options).merge(options.delete(:input_html) || {})
862
-
863
- input_name = generate_association_input_name(method)
864
- value_as_class = options.delete(:value_as_class)
865
-
866
- list_item_content = collection.map do |c|
867
- label = c.is_a?(Array) ? c.first : c
868
- value = c.is_a?(Array) ? c.last : c
869
-
870
- li_content = template.content_tag(:label,
871
- "#{self.radio_button(input_name, value, html_options)} #{label}",
872
- :for => generate_html_id(input_name, value.to_s.downcase)
873
- )
713
+ # Determines whether the given options evaluate to true
714
+ def options_require_validation?(options) #nodoc
715
+ if_condition = !options[:if].nil?
716
+ condition = if_condition ? options[:if] : options[:unless]
874
717
 
875
- li_options = value_as_class ? { :class => value.to_s.downcase } : {}
876
- template.content_tag(:li, li_content, li_options)
718
+ condition = if condition.respond_to?(:call)
719
+ condition.call(@object)
720
+ elsif condition.is_a?(::Symbol) && @object.respond_to?(condition)
721
+ @object.send(condition)
722
+ else
723
+ condition
724
+ end
725
+
726
+ if_condition ? !!condition : !condition
877
727
  end
878
728
 
879
- field_set_and_list_wrapping_for_method(method, options, list_item_content)
880
- end
881
- alias :boolean_radio_input :radio_input
729
+ def basic_input_helper(form_helper_method, type, method, options) #:nodoc:
730
+ html_options = options.delete(:input_html) || {}
731
+ html_options = default_string_options(method, type).merge(html_options) if [:numeric, :string, :password].include?(type)
882
732
 
883
- # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
884
- # items (li), one for each fragment for the date (year, month, day). Each li contains a label
885
- # (eg "Year") and a select box. See date_or_datetime_input for a more detailed output example.
886
- #
887
- # Some of Rails' options for select_date are supported, but not everything yet.
888
- def date_input(method, options)
889
- date_or_datetime_input(method, options.merge(:discard_hour => true))
890
- end
733
+ self.label(method, options_for_label(options)) <<
734
+ self.send(form_helper_method, method, html_options)
735
+ end
891
736
 
737
+ # Outputs a label and standard Rails text field inside the wrapper.
738
+ def string_input(method, options)
739
+ basic_input_helper(:text_field, :string, method, options)
740
+ end
892
741
 
893
- # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
894
- # items (li), one for each fragment for the date (year, month, day, hour, min, sec). Each li
895
- # contains a label (eg "Year") and a select box. See date_or_datetime_input for a more
896
- # detailed output example.
897
- #
898
- # Some of Rails' options for select_date are supported, but not everything yet.
899
- def datetime_input(method, options)
900
- date_or_datetime_input(method, options)
901
- end
742
+ # Outputs a label and standard Rails password field inside the wrapper.
743
+ def password_input(method, options)
744
+ basic_input_helper(:password_field, :password, method, options)
745
+ end
902
746
 
747
+ # Outputs a label and standard Rails text field inside the wrapper.
748
+ def numeric_input(method, options)
749
+ basic_input_helper(:text_field, :numeric, method, options)
750
+ end
903
751
 
904
- # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
905
- # items (li), one for each fragment for the time (hour, minute, second). Each li contains a label
906
- # (eg "Hour") and a select box. See date_or_datetime_input for a more detailed output example.
907
- #
908
- # Some of Rails' options for select_time are supported, but not everything yet.
909
- def time_input(method, options)
910
- date_or_datetime_input(method, options.merge(:discard_year => true, :discard_month => true, :discard_day => true))
911
- end
752
+ # Ouputs a label and standard Rails text area inside the wrapper.
753
+ def text_input(method, options)
754
+ basic_input_helper(:text_area, :text, method, options)
755
+ end
912
756
 
757
+ # Outputs a label and a standard Rails file field inside the wrapper.
758
+ def file_input(method, options)
759
+ basic_input_helper(:file_field, :file, method, options)
760
+ end
913
761
 
914
- # <fieldset>
915
- # <legend>Created At</legend>
916
- # <ol>
917
- # <li>
918
- # <label for="user_created_at_1i">Year</label>
919
- # <select id="user_created_at_1i" name="user[created_at(1i)]">
920
- # <option value="2003">2003</option>
921
- # ...
922
- # <option value="2013">2013</option>
923
- # </select>
924
- # </li>
925
- # <li>
926
- # <label for="user_created_at_2i">Month</label>
927
- # <select id="user_created_at_2i" name="user[created_at(2i)]">
928
- # <option value="1">January</option>
929
- # ...
930
- # <option value="12">December</option>
931
- # </select>
932
- # </li>
933
- # <li>
934
- # <label for="user_created_at_3i">Day</label>
935
- # <select id="user_created_at_3i" name="user[created_at(3i)]">
936
- # <option value="1">1</option>
937
- # ...
938
- # <option value="31">31</option>
939
- # </select>
940
- # </li>
941
- # </ol>
942
- # </fieldset>
943
- #
944
- # This is an absolute abomination, but so is the official Rails select_date().
945
- #
946
- def date_or_datetime_input(method, options)
947
- position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
948
- inputs = options.delete(:order) || I18n.translate(:'date.order') || [:year, :month, :day]
949
-
950
- time_inputs = [:hour, :minute]
951
- time_inputs << [:second] if options[:include_seconds]
952
-
953
- list_items_capture = ""
954
- hidden_fields_capture = ""
955
-
956
- # Gets the datetime object. It can be a Fixnum, Date or Time, or nil.
957
- datetime = @object ? @object.send(method) : nil
958
- html_options = options.delete(:input_html) || {}
959
-
960
- (inputs + time_inputs).each do |input|
961
- html_id = generate_html_id(method, "#{position[input]}i")
962
- field_name = "#{method}(#{position[input]}i)"
963
- if options["discard_#{input}".intern]
964
- break if time_inputs.include?(input)
965
-
966
- hidden_value = datetime.respond_to?(input) ? datetime.send(input) : datetime
967
- hidden_fields_capture << template.hidden_field_tag("#{@object_name}[#{field_name}]", (hidden_value || 1), :id => html_id)
762
+ # Outputs a hidden field inside the wrapper, which should be hidden with CSS.
763
+ # Additionals options can be given and will be sent straight to hidden input
764
+ # element.
765
+ #
766
+ def hidden_input(method, options)
767
+ options ||= {}
768
+ if options[:input_html].present?
769
+ options[:value] = options[:input_html][:value] if options[:input_html][:value].present?
770
+ end
771
+ self.hidden_field(method, strip_formtastic_options(options))
772
+ end
773
+
774
+ # Outputs a label and a select box containing options from the parent
775
+ # (belongs_to, has_many, has_and_belongs_to_many) association. If an association
776
+ # is has_many or has_and_belongs_to_many the select box will be set as multi-select
777
+ # and size = 5
778
+ #
779
+ # Example (belongs_to):
780
+ #
781
+ # f.input :author
782
+ #
783
+ # <label for="book_author_id">Author</label>
784
+ # <select id="book_author_id" name="book[author_id]">
785
+ # <option value=""></option>
786
+ # <option value="1">Justin French</option>
787
+ # <option value="2">Jane Doe</option>
788
+ # </select>
789
+ #
790
+ # Example (has_many):
791
+ #
792
+ # f.input :chapters
793
+ #
794
+ # <label for="book_chapter_ids">Chapters</label>
795
+ # <select id="book_chapter_ids" name="book[chapter_ids]">
796
+ # <option value=""></option>
797
+ # <option value="1">Chapter 1</option>
798
+ # <option value="2">Chapter 2</option>
799
+ # </select>
800
+ #
801
+ # Example (has_and_belongs_to_many):
802
+ #
803
+ # f.input :authors
804
+ #
805
+ # <label for="book_author_ids">Authors</label>
806
+ # <select id="book_author_ids" name="book[author_ids]">
807
+ # <option value=""></option>
808
+ # <option value="1">Justin French</option>
809
+ # <option value="2">Jane Doe</option>
810
+ # </select>
811
+ #
812
+ #
813
+ # You can customize the options available in the select by passing in a collection (an Array or
814
+ # Hash) through the :collection option. If not provided, the choices are found by inferring the
815
+ # parent's class name from the method name and simply calling find(:all) on it
816
+ # (VehicleOwner.find(:all) in the example above).
817
+ #
818
+ # Examples:
819
+ #
820
+ # f.input :author, :collection => @authors
821
+ # f.input :author, :collection => Author.find(:all)
822
+ # f.input :author, :collection => [@justin, @kate]
823
+ # f.input :author, :collection => {@justin.name => @justin.id, @kate.name => @kate.id}
824
+ # f.input :author, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
825
+ #
826
+ # The :label_method option allows you to customize the text label inside each option tag two ways:
827
+ #
828
+ # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc)
829
+ # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together
830
+ #
831
+ # Examples:
832
+ #
833
+ # f.input :author, :label_method => :full_name
834
+ # f.input :author, :label_method => :login
835
+ # f.input :author, :label_method => :full_name_with_post_count
836
+ # f.input :author, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" }
837
+ #
838
+ # The :value_method option provides the same customization of the value attribute of each option tag.
839
+ #
840
+ # Examples:
841
+ #
842
+ # f.input :author, :value_method => :full_name
843
+ # f.input :author, :value_method => :login
844
+ # f.input :author, :value_method => Proc.new { |a| "author_#{a.login}" }
845
+ #
846
+ # You can pre-select a specific option value by passing in the :selected option.
847
+ #
848
+ # Examples:
849
+ #
850
+ # f.input :author, :selected => current_user.id
851
+ # f.input :author, :value_method => :login, :selected => current_user.login
852
+ # f.input :authors, :value_method => :login, :selected => Author.most_popular.collect(&:id)
853
+ # f.input :authors, :value_method => :login, :selected => nil # override any defaults: select none
854
+ #
855
+ # You can pass html_options to the select tag using :input_html => {}
856
+ #
857
+ # Examples:
858
+ #
859
+ # f.input :authors, :input_html => {:size => 20, :multiple => true}
860
+ #
861
+ # By default, all select inputs will have a blank option at the top of the list. You can add
862
+ # a prompt with the :prompt option, or disable the blank option with :include_blank => false.
863
+ #
864
+ #
865
+ # You can group the options in optgroup elements by passing the :group_by option
866
+ # (Note: only tested for belongs_to relations)
867
+ #
868
+ # Examples:
869
+ #
870
+ # f.input :author, :group_by => :continent
871
+ #
872
+ # All the other options should work as expected. If you want to call a custom method on the
873
+ # group item. You can include the option:group_label_method
874
+ # Examples:
875
+ #
876
+ # f.input :author, :group_by => :continents, :group_label_method => :something_different
877
+ #
878
+ def select_input(method, options)
879
+ html_options = options.delete(:input_html) || {}
880
+ options = set_include_blank(options)
881
+ html_options[:multiple] = html_options[:multiple] || options.delete(:multiple)
882
+ html_options.delete(:multiple) if html_options[:multiple].nil?
883
+
884
+ reflection = self.reflection_for(method)
885
+ if reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro)
886
+ options[:include_blank] = false
887
+ html_options[:multiple] = true if html_options[:multiple].nil?
888
+ html_options[:size] ||= 5
889
+ end
890
+ options[:selected] = options[:selected].first if options[:selected].present? && html_options[:multiple] == false
891
+ input_name = generate_association_input_name(method)
892
+
893
+ select_html = if options[:group_by]
894
+ # The grouped_options_select is a bit counter intuitive and not optimised (mostly due to ActiveRecord).
895
+ # The formtastic user however shouldn't notice this too much.
896
+ raw_collection = find_raw_collection_for_column(method, options.reverse_merge(:find_options => { :include => options[:group_by] }))
897
+ label, value = detect_label_and_value_method!(raw_collection)
898
+ group_collection = raw_collection.map { |option| option.send(options[:group_by]) }.uniq
899
+ group_label_method = options[:group_label_method] || detect_label_method(group_collection)
900
+ group_collection = group_collection.sort_by { |group_item| group_item.send(group_label_method) }
901
+ group_association = options[:group_association] || detect_group_association(method, options[:group_by])
902
+
903
+ # Here comes the monster with 8 arguments
904
+ self.grouped_collection_select(input_name, group_collection,
905
+ group_association, group_label_method,
906
+ value, label,
907
+ strip_formtastic_options(options), html_options)
968
908
  else
969
- opts = set_options(options).merge(:prefix => @object_name, :field_name => field_name)
970
- item_label_text = I18n.t(input.to_s, :default => input.to_s.humanize, :scope => [:datetime, :prompts])
909
+ collection = find_collection_for_column(method, options)
971
910
 
972
- list_items_capture << template.content_tag(:li,
973
- template.content_tag(:label, item_label_text, :for => html_id) +
974
- template.send("select_#{input}".intern, datetime, opts, html_options.merge(:id => html_id))
975
- )
911
+ self.select(input_name, collection, strip_formtastic_options(options), html_options)
976
912
  end
977
- end
978
913
 
979
- hidden_fields_capture + field_set_and_list_wrapping_for_method(method, options, list_items_capture)
980
- end
914
+ self.label(method, options_for_label(options).merge(:input_name => input_name)) << select_html
915
+ end
916
+ alias :boolean_select_input :select_input
917
+
918
+ # Outputs a timezone select input as Rails' time_zone_select helper. You
919
+ # can give priority zones as option.
920
+ #
921
+ # Examples:
922
+ #
923
+ # f.input :time_zone, :as => :time_zone, :priority_zones => /Australia/
924
+ #
925
+ # You can pre-select a specific option value by passing in the :selected option.
926
+ # Note: Right now only works if the form object attribute value is not set (nil),
927
+ # because of how the core helper is implemented.
928
+ #
929
+ # Examples:
930
+ #
931
+ # f.input :my_favorite_time_zone, :as => :time_zone, :selected => 'Singapore'
932
+ #
933
+ def time_zone_input(method, options)
934
+ html_options = options.delete(:input_html) || {}
935
+ selected_value = options.delete(:selected)
936
+
937
+ self.label(method, options_for_label(options)) <<
938
+ self.time_zone_select(method, options.delete(:priority_zones),
939
+ strip_formtastic_options(options).merge(:default => selected_value), html_options)
940
+ end
981
941
 
942
+ # Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list
943
+ # items, one for each possible choice in the belongs_to association. Each li contains a
944
+ # label and a radio input.
945
+ #
946
+ # Example:
947
+ #
948
+ # f.input :author, :as => :radio
949
+ #
950
+ # Output:
951
+ #
952
+ # <fieldset>
953
+ # <legend><span>Author</span></legend>
954
+ # <ol>
955
+ # <li>
956
+ # <label for="book_author_id_1"><input id="book_author_id_1" name="book[author_id]" type="radio" value="1" /> Justin French</label>
957
+ # </li>
958
+ # <li>
959
+ # <label for="book_author_id_2"><input id="book_author_id_2" name="book[owner_id]" type="radio" value="2" /> Kate French</label>
960
+ # </li>
961
+ # </ol>
962
+ # </fieldset>
963
+ #
964
+ # You can customize the choices available in the radio button set by passing in a collection (an Array or
965
+ # Hash) through the :collection option. If not provided, the choices are found by reflecting on the association
966
+ # (Author.find(:all) in the example above).
967
+ #
968
+ # Examples:
969
+ #
970
+ # f.input :author, :as => :radio, :collection => @authors
971
+ # f.input :author, :as => :radio, :collection => Author.find(:all)
972
+ # f.input :author, :as => :radio, :collection => [@justin, @kate]
973
+ # f.input :author, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
974
+ #
975
+ # The :label_method option allows you to customize the label for each radio button two ways:
976
+ #
977
+ # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc)
978
+ # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together
979
+ #
980
+ # Examples:
981
+ #
982
+ # f.input :author, :as => :radio, :label_method => :full_name
983
+ # f.input :author, :as => :radio, :label_method => :login
984
+ # f.input :author, :as => :radio, :label_method => :full_name_with_post_count
985
+ # f.input :author, :as => :radio, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" }
986
+ #
987
+ # The :value_method option provides the same customization of the value attribute of each option tag.
988
+ #
989
+ # Examples:
990
+ #
991
+ # f.input :author, :as => :radio, :value_method => :full_name
992
+ # f.input :author, :as => :radio, :value_method => :login
993
+ # f.input :author, :as => :radio, :value_method => Proc.new { |a| "author_#{a.login}" }
994
+ #
995
+ # You can force a particular radio button in the collection to be checked with the :selected option.
996
+ #
997
+ # Examples:
998
+ #
999
+ # f.input :subscribe_to_newsletter, :as => :radio, :selected => true
1000
+ # f.input :subscribe_to_newsletter, :as => :radio, :collection => ["Yeah!", "Nope!"], :selected => "Nope!"
1001
+ #
1002
+ # Finally, you can set :value_as_class => true if you want the li wrapper around each radio
1003
+ # button / label combination to contain a class with the value of the radio button (useful for
1004
+ # applying specific CSS or Javascript to a particular radio button).
1005
+ #
1006
+ def radio_input(method, options)
1007
+ collection = find_collection_for_column(method, options)
1008
+ html_options = strip_formtastic_options(options).merge(options.delete(:input_html) || {})
1009
+
1010
+ input_name = generate_association_input_name(method)
1011
+ value_as_class = options.delete(:value_as_class)
1012
+ input_ids = []
1013
+ selected_option_is_present = [:selected, :checked].any? { |k| options.key?(k) }
1014
+ selected_value = (options.key?(:checked) ? options[:checked] : options[:selected]) if selected_option_is_present
1015
+
1016
+ list_item_content = collection.map do |c|
1017
+ label = c.is_a?(Array) ? c.first : c
1018
+ value = c.is_a?(Array) ? c.last : c
1019
+ input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase)
1020
+ input_ids << input_id
1021
+
1022
+ html_options[:checked] = selected_value == value if selected_option_is_present
1023
+
1024
+ li_content = template.content_tag(:label,
1025
+ "#{self.radio_button(input_name, value, html_options)} #{label}",
1026
+ :for => input_id
1027
+ )
982
1028
 
983
- # Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list
984
- # items, one for each possible choice in the belongs_to association. Each li contains a
985
- # label and a check_box input.
986
- #
987
- # This is an alternative for has many and has and belongs to many associations.
988
- #
989
- # Example:
990
- #
991
- # f.input :author, :as => :check_boxes
992
- #
993
- # Output:
994
- #
995
- # <fieldset>
996
- # <legend><span>Authors</span></legend>
997
- # <ol>
998
- # <li>
999
- # <input type="hidden" name="book[author_id][1]" value="">
1000
- # <label for="book_author_id_1"><input id="book_author_id_1" name="book[author_id][1]" type="checkbox" value="1" /> Justin French</label>
1001
- # </li>
1002
- # <li>
1003
- # <input type="hidden" name="book[author_id][2]" value="">
1004
- # <label for="book_author_id_2"><input id="book_author_id_2" name="book[owner_id][2]" type="checkbox" value="2" /> Kate French</label>
1005
- # </li>
1006
- # </ol>
1007
- # </fieldset>
1008
- #
1009
- # Notice that the value of the checkbox is the same as the id and the hidden
1010
- # field has empty value. You can override the hidden field value using the
1011
- # unchecked_value option.
1012
- #
1013
- # You can customize the options available in the set by passing in a collection (Array) of
1014
- # ActiveRecord objects through the :collection option. If not provided, the choices are found
1015
- # by inferring the parent's class name from the method name and simply calling find(:all) on
1016
- # it (Author.find(:all) in the example above).
1017
- #
1018
- # Examples:
1019
- #
1020
- # f.input :author, :as => :check_boxes, :collection => @authors
1021
- # f.input :author, :as => :check_boxes, :collection => Author.find(:all)
1022
- # f.input :author, :as => :check_boxes, :collection => [@justin, @kate]
1023
- #
1024
- # You can also customize the text label inside each option tag, by naming the correct method
1025
- # (:full_name, :display_name, :account_number, etc) to call on each object in the collection
1026
- # by passing in the :label_method option. By default the :label_method is whichever element of
1027
- # Formtastic::SemanticFormBuilder.collection_label_methods is found first.
1028
- #
1029
- # Examples:
1030
- #
1031
- # f.input :author, :as => :check_boxes, :label_method => :full_name
1032
- # f.input :author, :as => :check_boxes, :label_method => :display_name
1033
- # f.input :author, :as => :check_boxes, :label_method => :to_s
1034
- # f.input :author, :as => :check_boxes, :label_method => :label
1035
- #
1036
- # You can set :value_as_class => true if you want that LI wrappers contains
1037
- # a class with the wrapped checkbox input value.
1038
- #
1039
- def check_boxes_input(method, options)
1040
- collection = find_collection_for_column(method, options)
1041
- html_options = options.delete(:input_html) || {}
1042
-
1043
- input_name = generate_association_input_name(method)
1044
- value_as_class = options.delete(:value_as_class)
1045
- unchecked_value = options.delete(:unchecked_value) || ''
1046
- html_options = { :name => "#{@object_name}[#{input_name}][]" }.merge(html_options)
1047
-
1048
- list_item_content = collection.map do |c|
1049
- label = c.is_a?(Array) ? c.first : c
1050
- value = c.is_a?(Array) ? c.last : c
1051
-
1052
- html_options.merge!(:id => generate_html_id(input_name, value.to_s.downcase))
1053
-
1054
- li_content = template.content_tag(:label,
1055
- "#{self.check_box(input_name, html_options, value, unchecked_value)} #{label}",
1056
- :for => html_options[:id]
1057
- )
1029
+ li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {}
1030
+ template.content_tag(:li, li_content, li_options)
1031
+ end
1058
1032
 
1059
- li_options = value_as_class ? { :class => value.to_s.downcase } : {}
1060
- template.content_tag(:li, li_content, li_options)
1033
+ field_set_and_list_wrapping_for_method(method, options.merge(:label_for => input_ids.first), list_item_content)
1034
+ end
1035
+ alias :boolean_radio_input :radio_input
1036
+
1037
+ # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
1038
+ # items (li), one for each fragment for the date (year, month, day). Each li contains a label
1039
+ # (eg "Year") and a select box. See date_or_datetime_input for a more detailed output example.
1040
+ #
1041
+ # You can pre-select a specific option value by passing in the :selected option.
1042
+ #
1043
+ # Examples:
1044
+ #
1045
+ # f.input :created_at, :as => :date, :selected => 1.day.ago
1046
+ # f.input :created_at, :as => :date, :selected => nil # override any defaults: select none
1047
+ #
1048
+ # Some of Rails' options for select_date are supported, but not everything yet.
1049
+ #
1050
+ def date_input(method, options)
1051
+ options = set_include_blank(options)
1052
+ date_or_datetime_input(method, options.merge(:discard_hour => true))
1061
1053
  end
1062
1054
 
1063
- field_set_and_list_wrapping_for_method(method, options, list_item_content)
1064
- end
1065
-
1066
-
1067
- # Outputs a country select input, wrapping around a regular country_select helper.
1068
- # Rails doesn't come with a country_select helper by default any more, so you'll need to install
1069
- # the "official" plugin, or, if you wish, any other country_select plugin that behaves in the
1070
- # same way.
1071
- #
1072
- # The Rails plugin iso-3166-country-select plugin can be found "here":http://github.com/rails/iso-3166-country-select.
1073
- #
1074
- # By default, Formtastic includes a handfull of english-speaking countries as "priority counties",
1075
- # which you can change to suit your market and user base (see README for more info on config).
1076
- #
1077
- # Examples:
1078
- # f.input :location, :as => :country # use Formtastic::SemanticFormBuilder.priority_countries array for the priority countries
1079
- # f.input :location, :as => :country, :priority_countries => /Australia/ # set your own
1080
- #
1081
- def country_input(method, options)
1082
- raise "To use the :country input, please install a country_select plugin, like this one: http://github.com/rails/iso-3166-country-select" unless self.respond_to?(:country_select)
1083
-
1084
- html_options = options.delete(:input_html) || {}
1085
- priority_countries = options.delete(:priority_countries) || @@priority_countries
1055
+ # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
1056
+ # items (li), one for each fragment for the date (year, month, day, hour, min, sec). Each li
1057
+ # contains a label (eg "Year") and a select box. See date_or_datetime_input for a more
1058
+ # detailed output example.
1059
+ #
1060
+ # You can pre-select a specific option value by passing in the :selected option.
1061
+ #
1062
+ # Examples:
1063
+ #
1064
+ # f.input :created_at, :as => :datetime, :selected => 1.day.ago
1065
+ # f.input :created_at, :as => :datetime, :selected => nil # override any defaults: select none
1066
+ #
1067
+ # Some of Rails' options for select_date are supported, but not everything yet.
1068
+ #
1069
+ def datetime_input(method, options)
1070
+ options = set_include_blank(options)
1071
+ date_or_datetime_input(method, options)
1072
+ end
1086
1073
 
1087
- self.label(method, options_for_label(options)) +
1088
- self.country_select(method, priority_countries, set_options(options), html_options)
1089
- end
1090
-
1074
+ # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
1075
+ # items (li), one for each fragment for the time (hour, minute, second). Each li contains a label
1076
+ # (eg "Hour") and a select box. See date_or_datetime_input for a more detailed output example.
1077
+ #
1078
+ # You can pre-select a specific option value by passing in the :selected option.
1079
+ #
1080
+ # Examples:
1081
+ #
1082
+ # f.input :created_at, :as => :time, :selected => 1.hour.ago
1083
+ # f.input :created_at, :as => :time, :selected => nil # override any defaults: select none
1084
+ #
1085
+ # Some of Rails' options for select_time are supported, but not everything yet.
1086
+ #
1087
+ def time_input(method, options)
1088
+ options = set_include_blank(options)
1089
+ date_or_datetime_input(method, options.merge(:discard_year => true, :discard_month => true, :discard_day => true))
1090
+ end
1091
1091
 
1092
- # Outputs a label containing a checkbox and the label text. The label defaults
1093
- # to the column name (method name) and can be altered with the :label option.
1094
- # :checked_value and :unchecked_value options are also available.
1095
- #
1096
- def boolean_input(method, options)
1097
- html_options = options.delete(:input_html) || {}
1092
+ # <fieldset>
1093
+ # <legend>Created At</legend>
1094
+ # <ol>
1095
+ # <li>
1096
+ # <label for="user_created_at_1i">Year</label>
1097
+ # <select id="user_created_at_1i" name="user[created_at(1i)]">
1098
+ # <option value="2003">2003</option>
1099
+ # ...
1100
+ # <option value="2013">2013</option>
1101
+ # </select>
1102
+ # </li>
1103
+ # <li>
1104
+ # <label for="user_created_at_2i">Month</label>
1105
+ # <select id="user_created_at_2i" name="user[created_at(2i)]">
1106
+ # <option value="1">January</option>
1107
+ # ...
1108
+ # <option value="12">December</option>
1109
+ # </select>
1110
+ # </li>
1111
+ # <li>
1112
+ # <label for="user_created_at_3i">Day</label>
1113
+ # <select id="user_created_at_3i" name="user[created_at(3i)]">
1114
+ # <option value="1">1</option>
1115
+ # ...
1116
+ # <option value="31">31</option>
1117
+ # </select>
1118
+ # </li>
1119
+ # </ol>
1120
+ # </fieldset>
1121
+ #
1122
+ # This is an absolute abomination, but so is the official Rails select_date().
1123
+ #
1124
+ def date_or_datetime_input(method, options)
1125
+ position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
1126
+ i18n_date_order = ::I18n.t(:order, :scope => [:date])
1127
+ i18n_date_order = nil unless i18n_date_order.is_a?(Array)
1128
+ inputs = options.delete(:order) || i18n_date_order || [:year, :month, :day]
1129
+
1130
+ time_inputs = [:hour, :minute]
1131
+ time_inputs << [:second] if options[:include_seconds]
1132
+
1133
+ list_items_capture = ""
1134
+ hidden_fields_capture = ""
1135
+
1136
+ default_time = ::Time.now
1137
+
1138
+ # Gets the datetime object. It can be a Fixnum, Date or Time, or nil.
1139
+ datetime = options[:selected] || (@object ? @object.send(method) : default_time) || default_time
1140
+
1141
+ html_options = options.delete(:input_html) || {}
1142
+ input_ids = []
1143
+
1144
+ (inputs + time_inputs).each do |input|
1145
+ input_ids << input_id = generate_html_id(method, "#{position[input]}i")
1146
+
1147
+ field_name = "#{method}(#{position[input]}i)"
1148
+ if options[:"discard_#{input}"]
1149
+ break if time_inputs.include?(input)
1150
+
1151
+ hidden_value = datetime.respond_to?(input) ? datetime.send(input.to_sym) : datetime
1152
+ hidden_fields_capture << template.hidden_field_tag("#{@object_name}[#{field_name}]", (hidden_value || 1), :id => input_id)
1153
+ else
1154
+ opts = strip_formtastic_options(options).merge(:prefix => @object_name, :field_name => field_name, :default => datetime)
1155
+ item_label_text = ::I18n.t(input.to_s, :default => input.to_s.humanize, :scope => [:datetime, :prompts])
1156
+
1157
+ list_items_capture << template.content_tag(:li,
1158
+ template.content_tag(:label, item_label_text, :for => input_id) <<
1159
+ template.send(:"select_#{input}", datetime, opts, html_options.merge(:id => input_id))
1160
+ )
1161
+ end
1162
+ end
1098
1163
 
1099
- input = self.check_box(method, set_options(options).merge(html_options),
1100
- options.delete(:checked_value) || '1', options.delete(:unchecked_value) || '0')
1164
+ hidden_fields_capture << field_set_and_list_wrapping_for_method(method, options.merge(:label_for => input_ids.first), list_items_capture)
1165
+ end
1101
1166
 
1102
- label = options.delete(:label) || humanized_attribute_name(method)
1103
- self.label(method, input + label, options_for_label(options))
1104
- end
1167
+ # Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list
1168
+ # items, one for each possible choice in the belongs_to association. Each li contains a
1169
+ # label and a check_box input.
1170
+ #
1171
+ # This is an alternative for has many and has and belongs to many associations.
1172
+ #
1173
+ # Example:
1174
+ #
1175
+ # f.input :author, :as => :check_boxes
1176
+ #
1177
+ # Output:
1178
+ #
1179
+ # <fieldset>
1180
+ # <legend class="label"><label>Authors</label></legend>
1181
+ # <ol>
1182
+ # <li>
1183
+ # <input type="hidden" name="book[author_id][1]" value="">
1184
+ # <label for="book_author_id_1"><input id="book_author_id_1" name="book[author_id][1]" type="checkbox" value="1" /> Justin French</label>
1185
+ # </li>
1186
+ # <li>
1187
+ # <input type="hidden" name="book[author_id][2]" value="">
1188
+ # <label for="book_author_id_2"><input id="book_author_id_2" name="book[owner_id][2]" type="checkbox" value="2" /> Kate French</label>
1189
+ # </li>
1190
+ # </ol>
1191
+ # </fieldset>
1192
+ #
1193
+ # Notice that the value of the checkbox is the same as the id and the hidden
1194
+ # field has empty value. You can override the hidden field value using the
1195
+ # unchecked_value option.
1196
+ #
1197
+ # You can customize the options available in the set by passing in a collection (Array) of
1198
+ # ActiveRecord objects through the :collection option. If not provided, the choices are found
1199
+ # by inferring the parent's class name from the method name and simply calling find(:all) on
1200
+ # it (Author.find(:all) in the example above).
1201
+ #
1202
+ # Examples:
1203
+ #
1204
+ # f.input :author, :as => :check_boxes, :collection => @authors
1205
+ # f.input :author, :as => :check_boxes, :collection => Author.find(:all)
1206
+ # f.input :author, :as => :check_boxes, :collection => [@justin, @kate]
1207
+ #
1208
+ # The :label_method option allows you to customize the label for each checkbox two ways:
1209
+ #
1210
+ # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc)
1211
+ # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together
1212
+ #
1213
+ # Examples:
1214
+ #
1215
+ # f.input :author, :as => :check_boxes, :label_method => :full_name
1216
+ # f.input :author, :as => :check_boxes, :label_method => :login
1217
+ # f.input :author, :as => :check_boxes, :label_method => :full_name_with_post_count
1218
+ # f.input :author, :as => :check_boxes, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" }
1219
+ #
1220
+ # The :value_method option provides the same customization of the value attribute of each checkbox input tag.
1221
+ #
1222
+ # Examples:
1223
+ #
1224
+ # f.input :author, :as => :check_boxes, :value_method => :full_name
1225
+ # f.input :author, :as => :check_boxes, :value_method => :login
1226
+ # f.input :author, :as => :check_boxes, :value_method => Proc.new { |a| "author_#{a.login}" }
1227
+ #
1228
+ # You can pre-select/check a specific checkbox value by passing in the :selected option (alias :checked works as well).
1229
+ #
1230
+ # Examples:
1231
+ #
1232
+ # f.input :authors, :as => :check_boxes, :selected => @justin
1233
+ # f.input :authors, :as => :check_boxes, :selected => Author.most_popular.collect(&:id)
1234
+ # f.input :authors, :as => :check_boxes, :selected => nil # override any defaults: select none
1235
+ #
1236
+ # Finally, you can set :value_as_class => true if you want the li wrapper around each checkbox / label
1237
+ # combination to contain a class with the value of the radio button (useful for applying specific
1238
+ # CSS or Javascript to a particular checkbox).
1239
+ #
1240
+ def check_boxes_input(method, options)
1241
+ collection = find_collection_for_column(method, options)
1242
+ html_options = options.delete(:input_html) || {}
1243
+
1244
+ input_name = generate_association_input_name(method)
1245
+ value_as_class = options.delete(:value_as_class)
1246
+ unchecked_value = options.delete(:unchecked_value) || ''
1247
+ html_options = { :name => "#{@object_name}[#{input_name}][]" }.merge(html_options)
1248
+ input_ids = []
1249
+
1250
+ selected_option_is_present = [:selected, :checked].any? { |k| options.key?(k) }
1251
+ selected_values = (options.key?(:checked) ? options[:checked] : options[:selected]) if selected_option_is_present
1252
+ selected_values = [*selected_values].compact
1253
+
1254
+ list_item_content = collection.map do |c|
1255
+ label = c.is_a?(Array) ? c.first : c
1256
+ value = c.is_a?(Array) ? c.last : c
1257
+ input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase)
1258
+ input_ids << input_id
1259
+
1260
+ html_options[:checked] = selected_values.include?(value) if selected_option_is_present
1261
+ html_options[:id] = input_id
1262
+
1263
+ li_content = template.content_tag(:label,
1264
+ "#{self.check_box(input_name, html_options, value, unchecked_value)} #{label}",
1265
+ :for => input_id
1266
+ )
1105
1267
 
1106
- # Generates an input for the given method using the type supplied with :as.
1107
- #
1108
- # If the input is included in INPUT_MAPPINGS, it uses input_simple
1109
- # implementation which maps most of the inputs. All others have specific
1110
- # code and then a proper handler should be called (like radio_input) for
1111
- # :radio types.
1112
- #
1113
- def inline_input_for(method, options)
1114
- input_type = options.delete(:as)
1268
+ li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {}
1269
+ template.content_tag(:li, li_content, li_options)
1270
+ end
1115
1271
 
1116
- if INPUT_MAPPINGS.key?(input_type)
1117
- input_simple(input_type, method, options)
1118
- else
1119
- send("#{input_type}_input", method, options)
1272
+ field_set_and_list_wrapping_for_method(method, options.merge(:label_for => input_ids.first), list_item_content)
1120
1273
  end
1121
- end
1122
1274
 
1123
- # Generates hints for the given method using the text supplied in :hint.
1124
- #
1125
- def inline_hints_for(method, options) #:nodoc:
1126
- options[:hint] = localized_attribute_string(method, options[:hint], :hint)
1127
- return if options[:hint].blank?
1128
- template.content_tag(:p, options[:hint], :class => 'inline-hints')
1129
- end
1275
+ # Outputs a country select input, wrapping around a regular country_select helper.
1276
+ # Rails doesn't come with a country_select helper by default any more, so you'll need to install
1277
+ # the "official" plugin, or, if you wish, any other country_select plugin that behaves in the
1278
+ # same way.
1279
+ #
1280
+ # The Rails plugin iso-3166-country-select plugin can be found "here":http://github.com/rails/iso-3166-country-select.
1281
+ #
1282
+ # By default, Formtastic includes a handfull of english-speaking countries as "priority counties",
1283
+ # which you can change to suit your market and user base (see README for more info on config).
1284
+ #
1285
+ # Examples:
1286
+ # f.input :location, :as => :country # use Formtastic::SemanticFormBuilder.priority_countries array for the priority countries
1287
+ # f.input :location, :as => :country, :priority_countries => /Australia/ # set your own
1288
+ #
1289
+ def country_input(method, options)
1290
+ raise "To use the :country input, please install a country_select plugin, like this one: http://github.com/rails/iso-3166-country-select" unless self.respond_to?(:country_select)
1291
+
1292
+ html_options = options.delete(:input_html) || {}
1293
+ priority_countries = options.delete(:priority_countries) || @@priority_countries
1130
1294
 
1131
- # Creates an error sentence by calling to_sentence on the errors array.
1132
- #
1133
- def error_sentence(errors) #:nodoc:
1134
- template.content_tag(:p, errors.to_sentence.untaint, :class => 'inline-errors')
1135
- end
1295
+ self.label(method, options_for_label(options)) <<
1296
+ self.country_select(method, priority_countries, strip_formtastic_options(options), html_options)
1297
+ end
1136
1298
 
1137
- # Creates an error li list.
1138
- #
1139
- def error_list(errors) #:nodoc:
1140
- list_elements = []
1141
- errors.each do |error|
1142
- list_elements << template.content_tag(:li, error.untaint)
1299
+ # Outputs a label containing a checkbox and the label text. The label defaults
1300
+ # to the column name (method name) and can be altered with the :label option.
1301
+ # :checked_value and :unchecked_value options are also available.
1302
+ #
1303
+ # You can pre-select/check the boolean checkbox by passing in the :selected option (alias :checked works as well).
1304
+ #
1305
+ # Examples:
1306
+ #
1307
+ # f.input :allow_comments, :as => :boolean, :selected => true # override any default value: selected/checked
1308
+ #
1309
+ def boolean_input(method, options)
1310
+ html_options = options.delete(:input_html) || {}
1311
+ checked = options.key?(:checked) ? options[:checked] : options[:selected]
1312
+ html_options[:checked] = checked == true if [:selected, :checked].any? { |k| options.key?(k) }
1313
+
1314
+ input = self.check_box(method, strip_formtastic_options(options).merge(html_options),
1315
+ options.delete(:checked_value) || '1', options.delete(:unchecked_value) || '0')
1316
+ options = options_for_label(options)
1317
+
1318
+ # the label() method will insert this nested input into the label at the last minute
1319
+ options[:label_prefix_for_nested_input] = input
1320
+
1321
+ self.label(method, options)
1143
1322
  end
1144
- template.content_tag(:ul, list_elements.join("\n"), :class => 'errors')
1145
- end
1146
1323
 
1147
- # Generates the required or optional string. If the value set is a proc,
1148
- # it evaluates the proc first.
1149
- #
1150
- def required_or_optional_string(required) #:nodoc:
1151
- string_or_proc = case required
1152
- when true
1153
- @@required_string
1154
- when false
1155
- @@optional_string
1156
- else
1157
- required
1324
+ # Generates an input for the given method using the type supplied with :as.
1325
+ def inline_input_for(method, options)
1326
+ send(:"#{options.delete(:as)}_input", method, options)
1158
1327
  end
1159
1328
 
1160
- if string_or_proc.is_a?(Proc)
1161
- string_or_proc.call
1162
- else
1163
- string_or_proc.to_s
1329
+ # Generates hints for the given method using the text supplied in :hint.
1330
+ #
1331
+ def inline_hints_for(method, options) #:nodoc:
1332
+ options[:hint] = localized_string(method, options[:hint], :hint)
1333
+ return if options[:hint].blank?
1334
+ template.content_tag(:p, options[:hint], :class => 'inline-hints')
1164
1335
  end
1165
- end
1166
1336
 
1167
- # Generates a fieldset and wraps the content in an ordered list. When working
1168
- # with nested attributes (in Rails 2.3), it allows %i as interpolation option
1169
- # in :name. So you can do:
1170
- #
1171
- # f.inputs :name => 'Task #%i', :for => :tasks
1172
- #
1173
- # And it will generate a fieldset for each task with legend 'Task #1', 'Task #2',
1174
- # 'Task #3' and so on.
1175
- #
1176
- def field_set_and_list_wrapping(html_options, contents='', &block) #:nodoc:
1177
- legend = html_options.delete(:name).to_s
1178
- legend %= parent_child_index(html_options[:parent]) if html_options[:parent]
1179
- legend = template.content_tag(:legend, template.content_tag(:span, legend)) unless legend.blank?
1337
+ # Creates an error sentence by calling to_sentence on the errors array.
1338
+ #
1339
+ def error_sentence(errors) #:nodoc:
1340
+ template.content_tag(:p, errors.to_sentence.untaint, :class => 'inline-errors')
1341
+ end
1180
1342
 
1181
- contents = template.capture(&block) if block_given?
1343
+ # Creates an error li list.
1344
+ #
1345
+ def error_list(errors) #:nodoc:
1346
+ list_elements = []
1347
+ errors.each do |error|
1348
+ list_elements << template.content_tag(:li, error.untaint)
1349
+ end
1350
+ template.content_tag(:ul, list_elements.join("\n"), :class => 'errors')
1351
+ end
1182
1352
 
1183
- # Ruby 1.9: String#to_s behavior changed, need to make an explicit join.
1184
- contents = contents.join if contents.respond_to?(:join)
1185
- fieldset = template.content_tag(:fieldset,
1186
- legend + template.content_tag(:ol, contents),
1187
- html_options.except(:builder, :parent)
1188
- )
1353
+ # Creates an error sentence containing only the first error
1354
+ #
1355
+ def error_first(errors) #:nodoc:
1356
+ template.content_tag(:p, errors.first.untaint, :class => 'inline-errors')
1357
+ end
1189
1358
 
1190
- template.concat(fieldset) if block_given?
1191
- fieldset
1192
- end
1359
+ # Generates the required or optional string. If the value set is a proc,
1360
+ # it evaluates the proc first.
1361
+ #
1362
+ def required_or_optional_string(required) #:nodoc:
1363
+ string_or_proc = case required
1364
+ when true
1365
+ @@required_string
1366
+ when false
1367
+ @@optional_string
1368
+ else
1369
+ required
1370
+ end
1193
1371
 
1194
- # Also generates a fieldset and an ordered list but with label based in
1195
- # method. This methods is currently used by radio and datetime inputs.
1196
- #
1197
- def field_set_and_list_wrapping_for_method(method, options, contents)
1198
- contents = contents.join if contents.respond_to?(:join)
1372
+ if string_or_proc.is_a?(Proc)
1373
+ string_or_proc.call
1374
+ else
1375
+ string_or_proc.to_s
1376
+ end
1377
+ end
1199
1378
 
1200
- template.content_tag(:fieldset,
1201
- %{<legend>#{self.label(method, options_for_label(options).merge!(:as_span => true))}</legend>} +
1202
- template.content_tag(:ol, contents)
1203
- )
1204
- end
1379
+ # Generates a fieldset and wraps the content in an ordered list. When working
1380
+ # with nested attributes (in Rails 2.3), it allows %i as interpolation option
1381
+ # in :name. So you can do:
1382
+ #
1383
+ # f.inputs :name => 'Task #%i', :for => :tasks
1384
+ #
1385
+ # or the shorter equivalent:
1386
+ #
1387
+ # f.inputs 'Task #%i', :for => :tasks
1388
+ #
1389
+ # And it will generate a fieldset for each task with legend 'Task #1', 'Task #2',
1390
+ # 'Task #3' and so on.
1391
+ #
1392
+ # Note: Special case for the inline inputs (non-block):
1393
+ # f.inputs "My little legend", :title, :body, :author # Explicit legend string => "My little legend"
1394
+ # f.inputs :my_little_legend, :title, :body, :author # Localized (118n) legend with I18n key => I18n.t(:my_little_legend, ...)
1395
+ # f.inputs :title, :body, :author # First argument is a column => (no legend)
1396
+ #
1397
+ def field_set_and_list_wrapping(*args, &block) #:nodoc:
1398
+ contents = args.last.is_a?(::Hash) ? '' : args.pop.flatten
1399
+ html_options = args.extract_options!
1400
+
1401
+ legend = html_options.delete(:name).to_s
1402
+ legend %= parent_child_index(html_options[:parent]) if html_options[:parent]
1403
+ legend = template.content_tag(:legend, template.content_tag(:span, legend)) unless legend.blank?
1404
+
1405
+ if block_given?
1406
+ contents = if template.respond_to?(:is_haml?) && template.is_haml?
1407
+ template.capture_haml(&block)
1408
+ else
1409
+ template.capture(&block)
1410
+ end
1411
+ end
1205
1412
 
1206
- # For methods that have a database column, take a best guess as to what the input method
1207
- # should be. In most cases, it will just return the column type (eg :string), but for special
1208
- # cases it will simplify (like the case of :integer, :float & :decimal to :numeric), or do
1209
- # something different (like :password and :select).
1210
- #
1211
- # If there is no column for the method (eg "virtual columns" with an attr_accessor), the
1212
- # default is a :string, a similar behaviour to Rails' scaffolding.
1213
- #
1214
- def default_input_type(method) #:nodoc:
1215
- column = @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute)
1413
+ # Ruby 1.9: String#to_s behavior changed, need to make an explicit join.
1414
+ contents = contents.join if contents.respond_to?(:join)
1415
+ fieldset = template.content_tag(:fieldset,
1416
+ legend << template.content_tag(:ol, contents),
1417
+ html_options.except(:builder, :parent)
1418
+ )
1216
1419
 
1217
- if column
1218
- # handle the special cases where the column type doesn't map to an input method
1219
- return :time_zone if column.type == :string && method.to_s =~ /time_zone/
1220
- return :select if column.type == :integer && method.to_s =~ /_id$/
1221
- return :datetime if column.type == :timestamp
1222
- return :numeric if [:integer, :float, :decimal].include?(column.type)
1223
- return :password if column.type == :string && method.to_s =~ /password/
1224
- return :country if column.type == :string && method.to_s =~ /country/
1420
+ template.concat(fieldset) if block_given?
1421
+ fieldset
1422
+ end
1225
1423
 
1226
- # otherwise assume the input name will be the same as the column type (eg string_input)
1227
- return column.type
1228
- else
1229
- if @object
1230
- return :select if find_reflection(method)
1424
+ def field_set_title_from_args(*args) #:nodoc:
1425
+ options = args.extract_options!
1426
+ options[:name] ||= options.delete(:title)
1427
+ title = options[:name]
1231
1428
 
1232
- file = @object.send(method) if @object.respond_to?(method)
1233
- return :file if file && @@file_methods.any? { |m| file.respond_to?(m) }
1429
+ if title.blank?
1430
+ valid_name_classes = [::String, ::Symbol]
1431
+ valid_name_classes.delete(::Symbol) if !block_given? && (args.first.is_a?(::Symbol) && self.content_columns.include?(args.first))
1432
+ title = args.shift if valid_name_classes.any? { |valid_name_class| args.first.is_a?(valid_name_class) }
1234
1433
  end
1434
+ title = localized_string(title, title, :title) if title.is_a?(::Symbol)
1435
+ title
1436
+ end
1235
1437
 
1236
- return :password if method.to_s =~ /password/
1237
- return :string
1438
+ # Also generates a fieldset and an ordered list but with label based in
1439
+ # method. This methods is currently used by radio and datetime inputs.
1440
+ #
1441
+ def field_set_and_list_wrapping_for_method(method, options, contents) #:nodoc:
1442
+ contents = contents.join if contents.respond_to?(:join)
1443
+
1444
+ template.content_tag(:fieldset,
1445
+ template.content_tag(:legend,
1446
+ self.label(method, options_for_label(options).merge(:for => options.delete(:label_for))), :class => 'label'
1447
+ ) <<
1448
+ template.content_tag(:ol, contents)
1449
+ )
1238
1450
  end
1239
- end
1240
1451
 
1241
- # Used by select and radio inputs. The collection can be retrieved by
1242
- # three ways:
1243
- #
1244
- # * Explicitly provided through :collection
1245
- # * Retrivied through an association
1246
- # * Or a boolean column, which will generate a localized { "Yes" => true, "No" => false } hash.
1247
- #
1248
- # If the collection is not a hash or an array of strings, fixnums or arrays,
1249
- # we use label_method and value_method to retreive an array with the
1250
- # appropriate label and value.
1251
- #
1252
- def find_collection_for_column(column, options)
1253
- reflection = find_reflection(column)
1452
+ # For methods that have a database column, take a best guess as to what the input method
1453
+ # should be. In most cases, it will just return the column type (eg :string), but for special
1454
+ # cases it will simplify (like the case of :integer, :float & :decimal to :numeric), or do
1455
+ # something different (like :password and :select).
1456
+ #
1457
+ # If there is no column for the method (eg "virtual columns" with an attr_accessor), the
1458
+ # default is a :string, a similar behaviour to Rails' scaffolding.
1459
+ #
1460
+ def default_input_type(method, options = {}) #:nodoc:
1461
+ if column = self.column_for(method)
1462
+ # Special cases where the column type doesn't map to an input method.
1463
+ case column.type
1464
+ when :string
1465
+ return :password if method.to_s =~ /password/
1466
+ return :country if method.to_s =~ /country/
1467
+ return :time_zone if method.to_s =~ /time_zone/
1468
+ when :integer
1469
+ return :select if method.to_s =~ /_id$/
1470
+ return :numeric
1471
+ when :float, :decimal
1472
+ return :numeric
1473
+ when :timestamp
1474
+ return :datetime
1475
+ end
1476
+
1477
+ # Try look for hints in options hash. Quite common senario: Enum keys stored as string in the database.
1478
+ return :select if column.type == :string && options.key?(:collection)
1479
+ # Try 3: Assume the input name will be the same as the column type (e.g. string_input).
1480
+ return column.type
1481
+ else
1482
+ if @object
1483
+ return :select if self.reflection_for(method)
1484
+
1485
+ file = @object.send(method) if @object.respond_to?(method)
1486
+ return :file if file && @@file_methods.any? { |m| file.respond_to?(m) }
1487
+ end
1488
+
1489
+ return :select if options.key?(:collection)
1490
+ return :password if method.to_s =~ /password/
1491
+ return :string
1492
+ end
1493
+ end
1494
+
1495
+ # Used by select and radio inputs. The collection can be retrieved by
1496
+ # three ways:
1497
+ #
1498
+ # * Explicitly provided through :collection
1499
+ # * Retrivied through an association
1500
+ # * Or a boolean column, which will generate a localized { "Yes" => true, "No" => false } hash.
1501
+ #
1502
+ # If the collection is not a hash or an array of strings, fixnums or arrays,
1503
+ # we use label_method and value_method to retreive an array with the
1504
+ # appropriate label and value.
1505
+ #
1506
+ def find_collection_for_column(column, options) #:nodoc:
1507
+ collection = find_raw_collection_for_column(column, options)
1508
+
1509
+ # Return if we have an Array of strings, fixnums or arrays
1510
+ return collection if (collection.instance_of?(Array) || collection.instance_of?(Range)) &&
1511
+ [Array, Fixnum, String, Symbol].include?(collection.first.class)
1512
+
1513
+ label, value = detect_label_and_value_method!(collection, options)
1514
+ collection.map { |o| [send_or_call(label, o), send_or_call(value, o)] }
1515
+ end
1254
1516
 
1255
- collection = if options[:collection]
1256
- options.delete(:collection)
1257
- elsif reflection || column.to_s =~ /_id$/
1258
- parent_class = if reflection
1259
- reflection.klass
1517
+ # As #find_collection_for_column but returns the collection without mapping the label and value
1518
+ #
1519
+ def find_raw_collection_for_column(column, options) #:nodoc:
1520
+ collection = if options[:collection]
1521
+ options.delete(:collection)
1522
+ elsif reflection = self.reflection_for(column)
1523
+ reflection.klass.find(:all, options[:find_options] || {})
1260
1524
  else
1261
- ::ActiveSupport::Deprecation.warn("The _id way of doing things is deprecated. Please use the association method (#{column.to_s.sub(/_id$/,'')})", caller[3..-1])
1262
- column.to_s.sub(/_id$/,'').camelize.constantize
1525
+ create_boolean_collection(options)
1263
1526
  end
1264
1527
 
1265
- parent_class.find(:all)
1266
- else
1267
- create_boolean_collection(options)
1528
+ collection = collection.to_a if collection.is_a?(Hash)
1529
+ collection
1530
+ end
1531
+
1532
+ # Detects the label and value methods from a collection values set in
1533
+ # @@collection_label_methods. It will use and delete
1534
+ # the options :label_method and :value_methods when present
1535
+ #
1536
+ def detect_label_and_value_method!(collection_or_instance, options = {}) #:nodoc
1537
+ label = options.delete(:label_method) || detect_label_method(collection_or_instance)
1538
+ value = options.delete(:value_method) || :id
1539
+ [label, value]
1268
1540
  end
1269
1541
 
1270
- collection = collection.to_a if collection.instance_of?(Hash)
1542
+ # Detected the label collection method when none is supplied using the
1543
+ # values set in @@collection_label_methods.
1544
+ #
1545
+ def detect_label_method(collection) #:nodoc:
1546
+ @@collection_label_methods.detect { |m| collection.first.respond_to?(m) }
1547
+ end
1271
1548
 
1272
- # Return if we have an Array of strings, fixnums or arrays
1273
- return collection if collection.instance_of?(Array) &&
1274
- [Array, Fixnum, String, Symbol].include?(collection.first.class)
1549
+ # Detects the method to call for fetching group members from the groups when grouping select options
1550
+ #
1551
+ def detect_group_association(method, group_by)
1552
+ object_to_method_reflection = self.reflection_for(method)
1553
+ method_class = object_to_method_reflection.klass
1554
+
1555
+ method_to_group_association = method_class.reflect_on_association(group_by)
1556
+ group_class = method_to_group_association.klass
1557
+
1558
+ # This will return in the normal case
1559
+ return method.to_s.pluralize.to_sym if group_class.reflect_on_association(method.to_s.pluralize)
1560
+
1561
+ # This is for belongs_to associations named differently than their class
1562
+ # form.input :parent, :group_by => :customer
1563
+ # eg.
1564
+ # class Project
1565
+ # belongs_to :parent, :class_name => 'Project', :foreign_key => 'parent_id'
1566
+ # belongs_to :customer
1567
+ # end
1568
+ # class Customer
1569
+ # has_many :projects
1570
+ # end
1571
+ group_method = method_class.to_s.underscore.pluralize.to_sym
1572
+ return group_method if group_class.reflect_on_association(group_method) # :projects
1573
+
1574
+ # This is for has_many associations named differently than their class
1575
+ # eg.
1576
+ # class Project
1577
+ # belongs_to :parent, :class_name => 'Project', :foreign_key => 'parent_id'
1578
+ # belongs_to :customer
1579
+ # end
1580
+ # class Customer
1581
+ # has_many :tasks, :class_name => 'Project', :foreign_key => 'customer_id'
1582
+ # end
1583
+ possible_associations = group_class.reflect_on_all_associations(:has_many).find_all{|assoc| assoc.klass == object_class}
1584
+ return possible_associations.first.name.to_sym if possible_associations.count == 1
1585
+
1586
+ raise "Cannot infer group association for #{method} grouped by #{group_by}, there were #{possible_associations.empty? ? 'no' : possible_associations.size} possible associations. Please specify using :group_association"
1587
+
1588
+ end
1275
1589
 
1276
- label = options.delete(:label_method) || detect_label_method(collection)
1277
- value = options.delete(:value_method) || :id
1590
+ # Returns a hash to be used by radio and select inputs when a boolean field
1591
+ # is provided.
1592
+ #
1593
+ def create_boolean_collection(options) #:nodoc:
1594
+ options[:true] ||= ::Formtastic::I18n.t(:yes)
1595
+ options[:false] ||= ::Formtastic::I18n.t(:no)
1596
+ options[:value_as_class] = true unless options.key?(:value_as_class)
1278
1597
 
1279
- collection.map { |o| [o.send(label), o.send(value)] }
1280
- end
1598
+ [ [ options.delete(:true), true], [ options.delete(:false), false ] ]
1599
+ end
1281
1600
 
1282
- # Detected the label collection method when none is supplied using the
1283
- # values set in @@collection_label_methods.
1284
- #
1285
- def detect_label_method(collection) #:nodoc:
1286
- @@collection_label_methods.detect { |m| collection.first.respond_to?(m) }
1287
- end
1601
+ # Used by association inputs (select, radio) to generate the name that should
1602
+ # be used for the input
1603
+ #
1604
+ # belongs_to :author; f.input :author; will generate 'author_id'
1605
+ # belongs_to :entity, :foreign_key = :owner_id; f.input :author; will generate 'owner_id'
1606
+ # has_many :authors; f.input :authors; will generate 'author_ids'
1607
+ # has_and_belongs_to_many will act like has_many
1608
+ #
1609
+ def generate_association_input_name(method) #:nodoc:
1610
+ if reflection = self.reflection_for(method)
1611
+ if [:has_and_belongs_to_many, :has_many].include?(reflection.macro)
1612
+ "#{method.to_s.singularize}_ids"
1613
+ else
1614
+ reflection.options[:foreign_key] || "#{method}_id"
1615
+ end
1616
+ else
1617
+ method
1618
+ end.to_sym
1619
+ end
1288
1620
 
1289
- # Returns a hash to be used by radio and select inputs when a boolean field
1290
- # is provided.
1291
- #
1292
- def create_boolean_collection(options)
1293
- options[:true] ||= I18n.t('yes', :default => 'Yes', :scope => [:formtastic])
1294
- options[:false] ||= I18n.t('no', :default => 'No', :scope => [:formtastic])
1295
- options[:value_as_class] = true unless options.key?(:value_as_class)
1621
+ # If an association method is passed in (f.input :author) try to find the
1622
+ # reflection object.
1623
+ #
1624
+ def reflection_for(method) #:nodoc:
1625
+ @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association)
1626
+ end
1296
1627
 
1297
- [ [ options.delete(:true), true], [ options.delete(:false), false ] ]
1298
- end
1628
+ # Get a column object for a specified attribute method - if possible.
1629
+ #
1630
+ def column_for(method) #:nodoc:
1631
+ @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute)
1632
+ end
1299
1633
 
1300
- # Used by association inputs (select, radio) to generate the name that should
1301
- # be used for the input
1302
- #
1303
- # belongs_to :author; f.input :author; will generate 'author_id'
1304
- # belongs_to :entity, :foreign_key = :owner_id; f.input :author; will generate 'owner_id'
1305
- # has_many :authors; f.input :authors; will generate 'author_ids'
1306
- # has_and_belongs_to_many will act like has_many
1307
- #
1308
- def generate_association_input_name(method)
1309
- if reflection = find_reflection(method)
1310
- if [:has_and_belongs_to_many, :has_many].include?(reflection.macro)
1311
- "#{method.to_s.singularize}_ids"
1634
+ # Generates default_string_options by retrieving column information from
1635
+ # the database.
1636
+ #
1637
+ def default_string_options(method, type) #:nodoc:
1638
+ column = self.column_for(method)
1639
+
1640
+ if type == :numeric || column.nil? || column.limit.nil?
1641
+ { :size => @@default_text_field_size }
1312
1642
  else
1313
- reflection.options[:foreign_key] || "#{method}_id"
1643
+ { :maxlength => column.limit, :size => [column.limit, @@default_text_field_size].min }
1314
1644
  end
1315
- else
1316
- method
1317
1645
  end
1318
- end
1319
-
1320
- # If an association method is passed in (f.input :author) try to find the
1321
- # reflection object.
1322
- #
1323
- def find_reflection(method)
1324
- @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association)
1325
- end
1326
1646
 
1327
- # Generates default_string_options by retrieving column information from
1328
- # the database.
1329
- #
1330
- def default_string_options(method, type) #:nodoc:
1331
- column = @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute)
1647
+ # Generate the html id for the li tag.
1648
+ # It takes into account options[:index] and @auto_index to generate li
1649
+ # elements with appropriate index scope. It also sanitizes the object
1650
+ # and method names.
1651
+ #
1652
+ def generate_html_id(method_name, value='input') #:nodoc:
1653
+ if options.has_key?(:index)
1654
+ index = "_#{options[:index]}"
1655
+ elsif defined?(@auto_index)
1656
+ index = "_#{@auto_index}"
1657
+ else
1658
+ index = ""
1659
+ end
1660
+ sanitized_method_name = method_name.to_s.gsub(/[\?\/\-]$/, '')
1332
1661
 
1333
- if type == :numeric || column.nil? || column.limit.nil?
1334
- { :size => @@default_text_field_size }
1335
- else
1336
- { :maxlength => column.limit, :size => [column.limit, @@default_text_field_size].min }
1662
+ "#{sanitized_object_name}#{index}_#{sanitized_method_name}_#{value}"
1337
1663
  end
1338
- end
1339
1664
 
1340
- # Generate the html id for the li tag.
1341
- # It takes into account options[:index] and @auto_index to generate li
1342
- # elements with appropriate index scope. It also sanitizes the object
1343
- # and method names.
1344
- #
1345
- def generate_html_id(method_name, value='input')
1346
- if options.has_key?(:index)
1347
- index = "_#{options[:index]}"
1348
- elsif defined?(@auto_index)
1349
- index = "_#{@auto_index}"
1350
- else
1351
- index = ""
1665
+ # Gets the nested_child_index value from the parent builder. In Rails 2.3
1666
+ # it always returns a fixnum. In next versions it returns a hash with each
1667
+ # association that the parent builds.
1668
+ #
1669
+ def parent_child_index(parent) #:nodoc:
1670
+ duck = parent[:builder].instance_variable_get('@nested_child_index')
1671
+
1672
+ if duck.is_a?(Hash)
1673
+ child = parent[:for]
1674
+ child = child.first if child.respond_to?(:first)
1675
+ duck[child].to_i + 1
1676
+ else
1677
+ duck.to_i + 1
1678
+ end
1352
1679
  end
1353
- sanitized_method_name = method_name.to_s.gsub(/[\?\/\-]$/, '')
1354
-
1355
- "#{sanitized_object_name}#{index}_#{sanitized_method_name}_#{value}"
1356
- end
1357
1680
 
1358
- # Gets the nested_child_index value from the parent builder. In Rails 2.3
1359
- # it always returns a fixnum. In next versions it returns a hash with each
1360
- # association that the parent builds.
1361
- #
1362
- def parent_child_index(parent)
1363
- duck = parent[:builder].instance_variable_get('@nested_child_index')
1681
+ def sanitized_object_name #:nodoc:
1682
+ @sanitized_object_name ||= @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
1683
+ end
1364
1684
 
1365
- if duck.is_a?(Hash)
1366
- child = parent[:for]
1367
- child = child.first if child.respond_to?(:first)
1368
- duck[child].to_i + 1
1369
- else
1370
- duck.to_i + 1
1685
+ def humanized_attribute_name(method) #:nodoc:
1686
+ if @object && @object.class.respond_to?(:human_attribute_name) && @@label_str_method == :humanize
1687
+ @object.class.human_attribute_name(method.to_s)
1688
+ else
1689
+ method.to_s.send(@@label_str_method)
1690
+ end
1371
1691
  end
1372
- end
1373
1692
 
1374
- def sanitized_object_name
1375
- @sanitized_object_name ||= @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
1376
- end
1693
+ # Internal generic method for looking up localized values within Formtastic
1694
+ # using I18n, if no explicit value is set and I18n-lookups are enabled.
1695
+ #
1696
+ # Enabled/Disable this by setting:
1697
+ #
1698
+ # Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true/false
1699
+ #
1700
+ # Lookup priority:
1701
+ #
1702
+ # 'formtastic.{{type}}.{{model}}.{{action}}.{{attribute}}'
1703
+ # 'formtastic.{{type}}.{{model}}.{{attribute}}'
1704
+ # 'formtastic.{{type}}.{{attribute}}'
1705
+ #
1706
+ # Example:
1707
+ #
1708
+ # 'formtastic.labels.post.edit.title'
1709
+ # 'formtastic.labels.post.title'
1710
+ # 'formtastic.labels.title'
1711
+ #
1712
+ # NOTE: Generic, but only used for form input titles/labels/hints/actions (titles = legends, actions = buttons).
1713
+ #
1714
+ def localized_string(key, value, type, options = {}) #:nodoc:
1715
+ key = value if value.is_a?(::Symbol)
1716
+
1717
+ if value.is_a?(::String)
1718
+ value
1719
+ else
1720
+ use_i18n = value.nil? ? @@i18n_lookups_by_default : (value != false)
1721
+
1722
+ if use_i18n
1723
+ model_name = self.model_name.underscore
1724
+ action_name = template.params[:action].to_s rescue ''
1725
+ attribute_name = key.to_s
1726
+
1727
+ defaults = ::Formtastic::I18n::SCOPES.collect do |i18n_scope|
1728
+ i18n_path = i18n_scope.dup
1729
+ i18n_path.gsub!('{{action}}', action_name)
1730
+ i18n_path.gsub!('{{model}}', model_name)
1731
+ i18n_path.gsub!('{{attribute}}', attribute_name)
1732
+ i18n_path.gsub!('..', '.')
1733
+ i18n_path.to_sym
1734
+ end
1735
+ defaults << ''
1736
+
1737
+ i18n_value = ::Formtastic::I18n.t(defaults.shift,
1738
+ options.merge(:default => defaults, :scope => type.to_s.pluralize.to_sym))
1739
+ i18n_value.blank? ? nil : i18n_value
1740
+ end
1741
+ end
1742
+ end
1377
1743
 
1378
- def humanized_attribute_name(method)
1379
- if @object && @object.class.respond_to?(:human_attribute_name)
1380
- @object.class.human_attribute_name(method.to_s)
1381
- else
1382
- method.to_s.send(@@label_str_method)
1744
+ def model_name
1745
+ @object.present? ? @object.class.name : @object_name.to_s.classify
1383
1746
  end
1384
- end
1385
1747
 
1386
- # Internal generic method for looking up localized values within Formtastic
1387
- # using I18n, if no explicit value is set and I18n-lookups are enabled.
1388
- #
1389
- # Enabled/Disable this by setting:
1390
- #
1391
- # Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true/false
1392
- #
1393
- # Lookup priority:
1394
- #
1395
- # 'formtastic.{{type}}.{{model}}.{{action}}.{{attribute}}'
1396
- # 'formtastic.{{type}}.{{model}}.{{attribute}}'
1397
- # 'formtastic.{{type}}.{{attribute}}'
1398
- #
1399
- # Example:
1400
- #
1401
- # 'formtastic.labels.post.edit.title'
1402
- # 'formtastic.labels.post.title'
1403
- # 'formtastic.labels.title'
1404
- #
1405
- # NOTE: Generic, but only used for form input labels/hints.
1406
- #
1407
- def localized_attribute_string(attr_name, attr_value, i18n_key)
1408
- if attr_value.is_a?(String)
1409
- attr_value
1410
- else
1411
- use_i18n = attr_value.nil? ? @@i18n_lookups_by_default : attr_value
1412
- if use_i18n
1413
- model_name = @object.class.name.underscore
1414
- action_name = template.params[:action].to_s rescue ''
1415
- attribute_name = attr_name.to_s
1416
-
1417
- defaults = I18N_SCOPES.collect do |i18n_scope|
1418
- i18n_path = i18n_scope.dup
1419
- i18n_path.gsub!('{{action}}', action_name)
1420
- i18n_path.gsub!('{{model}}', model_name)
1421
- i18n_path.gsub!('{{attribute}}', attribute_name)
1422
- i18n_path.gsub!('..', '.')
1423
- i18n_path.to_sym
1424
- end
1425
- defaults << ''
1748
+ def send_or_call(duck, object)
1749
+ if duck.is_a?(Proc)
1750
+ duck.call(object)
1751
+ else
1752
+ object.send(duck)
1753
+ end
1754
+ end
1426
1755
 
1427
- i18n_value = ::I18n.t(defaults.shift, :default => defaults,
1428
- :scope => "formtastic.#{i18n_key.to_s.pluralize}")
1429
- i18n_value.blank? ? nil : i18n_value
1756
+ def set_include_blank(options)
1757
+ unless options.key?(:include_blank) || options.key?(:prompt)
1758
+ options[:include_blank] = @@include_blank_for_select_by_default
1430
1759
  end
1760
+ options
1431
1761
  end
1432
- end
1433
1762
 
1434
1763
  end
1435
1764
 
@@ -1455,8 +1784,8 @@ module Formtastic #:nodoc:
1455
1784
  # <% end %>
1456
1785
  #
1457
1786
  # The above examples use a resource-oriented style of form_for() helper where only the @post
1458
- # object is given as an argument, but the generic style is also supported if you really want it,
1459
- # as is forms with inline objects (Post.new) rather than objects with instance variables (@post):
1787
+ # object is given as an argument, but the generic style is also supported, as are forms with
1788
+ # inline objects (Post.new) rather than objects with instance variables (@post):
1460
1789
  #
1461
1790
  # <% semantic_form_for :post, @post, :url => posts_path do |f| %>
1462
1791
  # ...
@@ -1465,29 +1794,35 @@ module Formtastic #:nodoc:
1465
1794
  # <% semantic_form_for :post, Post.new, :url => posts_path do |f| %>
1466
1795
  # ...
1467
1796
  # <% end %>
1468
- #
1469
- # The shorter, resource-oriented style is most definitely preferred, and has recieved the most
1470
- # testing to date.
1471
- #
1472
- # Please note: Although it's possible to call Rails' built-in form_for() helper without an
1473
- # object, all semantic forms *must* have an object (either Post.new or @post), as Formtastic
1474
- # has too many dependencies on an ActiveRecord object being present.
1475
- #
1476
1797
  module SemanticFormHelper
1477
- @@builder = Formtastic::SemanticFormBuilder
1478
-
1479
- # cattr_accessor :builder
1480
- def self.builder=(val)
1481
- @@builder = val
1798
+ @@builder = ::Formtastic::SemanticFormBuilder
1799
+ mattr_accessor :builder
1800
+
1801
+ @@default_field_error_proc = nil
1802
+
1803
+ # Override the default ActiveRecordHelper behaviour of wrapping the input.
1804
+ # This gets taken care of semantically by adding an error class to the LI tag
1805
+ # containing the input.
1806
+ #
1807
+ FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
1808
+ html_tag
1482
1809
  end
1483
-
1484
- [:form_for, :fields_for, :form_remote_for, :remote_form_for].each do |meth|
1810
+
1811
+ def with_custom_field_error_proc(&block)
1812
+ @@default_field_error_proc = ::ActionView::Base.field_error_proc
1813
+ ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
1814
+ result = yield
1815
+ ::ActionView::Base.field_error_proc = @@default_field_error_proc
1816
+ result
1817
+ end
1818
+
1819
+ [:form_for, :fields_for, :remote_form_for].each do |meth|
1485
1820
  src = <<-END_SRC
1486
1821
  def semantic_#{meth}(record_or_name_or_array, *args, &proc)
1487
1822
  options = args.extract_options!
1488
- options[:builder] = @@builder
1823
+ options[:builder] ||= @@builder
1489
1824
  options[:html] ||= {}
1490
-
1825
+
1491
1826
  class_names = options[:html][:class] ? options[:html][:class].split(" ") : []
1492
1827
  class_names << "formtastic"
1493
1828
  class_names << case record_or_name_or_array
@@ -1496,11 +1831,15 @@ module Formtastic #:nodoc:
1496
1831
  else record_or_name_or_array.class.to_s.underscore # @post => "post"
1497
1832
  end
1498
1833
  options[:html][:class] = class_names.join(" ")
1499
-
1500
- #{meth}(record_or_name_or_array, *(args << options), &proc)
1834
+
1835
+ with_custom_field_error_proc do
1836
+ #{meth}(record_or_name_or_array, *(args << options), &proc)
1837
+ end
1501
1838
  end
1502
1839
  END_SRC
1503
1840
  module_eval src, __FILE__, __LINE__
1504
1841
  end
1842
+ alias :semantic_form_remote_for :semantic_remote_form_for
1843
+
1505
1844
  end
1506
1845
  end