Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Revert "This reverts commit 440bd2d."

This reverts commit e4fc2be.
  • Loading branch information...
commit 17f6668038a323ee04c39f128b1cc2b8d70391cc 1 parent e4fc2be
@justinfrench authored
View
6 README.textile
@@ -148,7 +148,7 @@ If you want to customize the label text, or render some hint text below the fiel
<pre>
<% semantic_form_for @post do |form| %>
- <% form.inputs :name => "Basic", :id => "basic" do %>
+ <% form.inputs "Basic", :id => "basic" do %>
<%= form.input :title %>
<%= form.input :body %>
<% end %>
@@ -324,11 +324,11 @@ Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the
*4. Localized titles (a.k.a. legends):*
-_Note: Slightly different because Formtastic can't guess how you group fields in a form._
+_Note: Slightly different because Formtastic can't guess how you group fields in a form. Legend text can be set with first (as in the sample below) specified value, or :name/:title options - depending on what flavor is preferred._
<pre>
<% semantic_form_for @post do |form| %>
- <% form.inputs :title => :post_details do %> # => :title => "Post details"
+ <% form.inputs :post_details do %> # => :title => "Post details"
# ...
<% end %>
# ...
View
118 lib/formtastic.rb
@@ -7,7 +7,7 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
@@default_text_field_size = 50
@@all_fields_required_by_default = true
@@include_blank_for_select_by_default = true
- @@required_string = proc { %{<abbr title="#{I18n.t 'formtastic.required', :default => 'required'}">*</abbr>} }
+ @@required_string = proc { %{<abbr title="#{::I18n.t 'formtastic.required', :default => 'required'}">*</abbr>} }
@@optional_string = ''
@@inline_errors = :sentence
@@label_str_method = :humanize
@@ -22,6 +22,8 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
:required_string, :optional_string, :inline_errors, :label_str_method, :collection_label_methods,
:inline_order, :file_methods, :priority_countries, :i18n_lookups_by_default, :default_commit_button_accesskey
+ RESERVED_COLUMNS = [:created_at, :updated_at, :created_on, :updated_on, :lock_version, :version]
+
I18N_SCOPES = [ '{{model}}.{{action}}.{{attribute}}',
'{{model}}.{{attribute}}',
'{{attribute}}']
@@ -136,11 +138,16 @@ def input(method, options = {})
# <%= form.inputs %>
# <% end %>
#
+ # With a few arguments:
+ # <% semantic_form_for @post do |form| %>
+ # <%= form.inputs "Post details", :title, :body %>
+ # <% end %>
+ #
# === Options
#
- # All options (with the exception of :name) are passed down to the fieldset as HTML
- # attributes (id, class, style, etc). If provided, the :name option is passed into a
- # legend tag inside the fieldset (otherwise a legend is not generated).
+ # All options (with the exception of :name/:title) are passed down to the fieldset as HTML
+ # attributes (id, class, style, etc). If provided, the :name/:title option is passed into a
+ # legend tag inside the fieldset.
#
# # With a block:
# <% semantic_form_for @post do |form| %>
@@ -154,6 +161,11 @@ def input(method, options = {})
# <%= form.inputs :title, :body, :name => "Create a new post", :style => "border:1px;" %>
# <% end %>
#
+ # # ...or the equivalent:
+ # <% semantic_form_for @post do |form| %>
+ # <%= form.inputs "Create a new post", :title, :body, :style => "border:1px;" %>
+ # <% end %>
+ #
# === It's basically a fieldset!
#
# Instead of hard-coding fieldsets & legends into your form to logically group related fields,
@@ -168,6 +180,9 @@ def input(method, options = {})
# <%= f.input :created_at %>
# <%= f.input :user_id, :label => "Author" %>
# <% end %>
+ # <% f.inputs "Extra" do %>
+ # <%= f.input :update_at %>
+ # <% end %>
# <% end %>
#
# # Output:
@@ -185,6 +200,12 @@ def input(method, options = {})
# <li class="select">...</li>
# </ol>
# </fieldset>
+ # <fieldset class="inputs">
+ # <legend><span>Extra</span></legend>
+ # <ol>
+ # <li class="datetime">...</li>
+ # </ol>
+ # </fieldset>
# </form>
#
# === Nested attributes
@@ -232,17 +253,19 @@ def inputs(*args, &block)
if html_options[:for]
inputs_for_nested_attributes(args, html_options, &block)
elsif block_given?
- field_set_and_list_wrapping(html_options, &block)
+ field_set_and_list_wrapping(*(args << html_options), &block)
else
if @object && args.empty?
- args = @object.class.reflections.map { |n,_| n if _.macro == :belongs_to }
- args += @object.class.content_columns.map(&:name)
- args -= %w[created_at updated_at created_on updated_on lock_version version]
+ args = self.association_columns(:belongs_to)
+ args += self.content_columns
+ args -= RESERVED_COLUMNS
args.compact!
end
- contents = args.map { |method| input(method.to_sym) }
-
- field_set_and_list_wrapping(html_options, contents)
+ legend = args.shift if args.first.is_a?(::String)
+ contents = args.collect { |method| input(method.to_sym) }
+ args.unshift(legend) if legend.present?
+
+ field_set_and_list_wrapping(*(args << html_options), contents)
end
end
alias :input_field_set :inputs
@@ -326,7 +349,7 @@ def commit_button(*args)
#
def semantic_fields_for(record_or_name_or_array, *args, &block)
opts = args.extract_options!
- opts.merge!(:builder => Formtastic::SemanticFormHelper.builder)
+ opts.merge!(:builder => ::Formtastic::SemanticFormHelper.builder)
args.push(opts)
fields_for(record_or_name_or_array, *args, &block)
end
@@ -396,6 +419,32 @@ def inline_errors_for(method, options=nil) #:nodoc:
protected
+ # Collects content columns (non-relation columns) for the current form object class.
+ #
+ def content_columns
+ if @object.present?
+ @object.class.name.constantize.content_columns.collect { |c| c.name.to_sym }.compact
+ else
+ @object_name.to_s.classify.constantize.content_columns.collect { |c| c.name.to_sym }.compact rescue []
+ end
+ end
+
+ # Collects association columns (relation columns) for the current form object class.
+ #
+ def association_columns(*by_associations)
+ if @object.present?
+ @object.class.reflections.collect do |name, _|
+ if by_associations.present?
+ name if by_associations.include?(_.macro)
+ else
+ name
+ end
+ end.compact
+ else
+ []
+ end
+ end
+
# Prepare options to be sent to label
#
def options_for_label(options)
@@ -819,7 +868,7 @@ def time_input(method, options)
#
def date_or_datetime_input(method, options)
position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 }
- i18n_date_order = I18n.translate(:'date.order').is_a?(Array) ? I18n.translate(:'date.order') : nil
+ i18n_date_order = ::I18n.translate(:'date.order').is_a?(Array) ? ::I18n.translate(:'date.order') : nil
inputs = options.delete(:order) || i18n_date_order || [:year, :month, :day]
time_inputs = [:hour, :minute]
@@ -842,7 +891,7 @@ def date_or_datetime_input(method, options)
hidden_fields_capture << template.hidden_field_tag("#{@object_name}[#{field_name}]", (hidden_value || 1), :id => html_id)
else
opts = set_options(options).merge(:prefix => @object_name, :field_name => field_name)
- item_label_text = I18n.t(input.to_s, :default => input.to_s.humanize, :scope => [:datetime, :prompts])
+ item_label_text = ::I18n.t(input.to_s, :default => input.to_s.humanize, :scope => [:datetime, :prompts])
list_items_capture << template.content_tag(:li,
template.content_tag(:label, item_label_text, :for => html_id) +
@@ -1050,12 +1099,29 @@ def required_or_optional_string(required) #:nodoc:
#
# f.inputs :name => 'Task #%i', :for => :tasks
#
+ # or the shorter equivalent:
+ #
+ # f.inputs 'Task #%i', :for => :tasks
+ #
# And it will generate a fieldset for each task with legend 'Task #1', 'Task #2',
# 'Task #3' and so on.
#
- def field_set_and_list_wrapping(html_options, contents='', &block) #:nodoc:
+ # Note: Special case for the inline inputs (non-block):
+ # f.inputs "My little legend", :title, :body, :author # Explicit legend string => "My little legend"
+ # f.inputs :my_little_legend, :title, :body, :author # Localized (118n) legend with I18n key => I18n.t(:my_little_legend, ...)
+ # f.inputs :title, :body, :author # First argument is a column => (no legend)
+ #
+ def field_set_and_list_wrapping(*args, &block) #:nodoc:
+ contents = args.last.is_a?(::Hash) ? '' : args.pop.flatten
+ html_options = args.extract_options!
+
html_options[:name] ||= html_options.delete(:title)
- html_options[:name] = localized_string(html_options[:name], html_options[:name], :title) if html_options[:name].is_a?(Symbol)
+ if html_options[:name].blank?
+ valid_name_classes = [::String, ::Symbol]
+ valid_name_classes.delete(::Symbol) if !block_given? && (args.first.is_a?(::Symbol) && self.content_columns.include?(args.first))
+ html_options[:name] = args.shift if valid_name_classes.any? { |valid_name_class| args.first.is_a?(valid_name_class) }
+ end
+ html_options[:name] = localized_string(html_options[:name], html_options[:name], :title) if html_options[:name].is_a?(::Symbol)
legend = html_options.delete(:name).to_s
legend %= parent_child_index(html_options[:parent]) if html_options[:parent]
@@ -1142,7 +1208,7 @@ def find_collection_for_column(column, options)
collection = find_raw_collection_for_column(column, options)
# Return if we have an Array of strings, fixnums or arrays
- return collection if collection.instance_of?(Array) &&
+ return collection if (collection.instance_of?(Array) || collection.instance_of?(Range)) &&
[Array, Fixnum, String, Symbol].include?(collection.first.class)
label, value = detect_label_and_value_method!(collection, options)
@@ -1189,8 +1255,8 @@ def detect_label_method(collection) #:nodoc:
# is provided.
#
def create_boolean_collection(options)
- options[:true] ||= I18n.t('yes', :default => 'Yes', :scope => [:formtastic])
- options[:false] ||= I18n.t('no', :default => 'No', :scope => [:formtastic])
+ options[:true] ||= ::I18n.t(:yes, :default => 'Yes', :scope => [:formtastic])
+ options[:false] ||= ::I18n.t(:no, :default => 'No', :scope => [:formtastic])
options[:value_as_class] = true unless options.key?(:value_as_class)
[ [ options.delete(:true), true], [ options.delete(:false), false ] ]
@@ -1341,14 +1407,12 @@ def send_or_call(duck, object)
end
end
- private
-
- def set_include_blank(options)
- unless options.key?(:include_blank) || options.key?(:prompt)
- options[:include_blank] = @@include_blank_for_select_by_default
- end
- options
+ def set_include_blank(options)
+ unless options.key?(:include_blank) || options.key?(:prompt)
+ options[:include_blank] = @@include_blank_for_select_by_default
end
+ options
+ end
end
@@ -1385,7 +1449,7 @@ def set_include_blank(options)
# ...
# <% end %>
module SemanticFormHelper
- @@builder = Formtastic::SemanticFormBuilder
+ @@builder = ::Formtastic::SemanticFormBuilder
mattr_accessor :builder
@@default_field_error_proc = nil
View
4 lib/locale/en.yml
@@ -1,7 +1,7 @@
en:
formtastic:
- "yes": 'Yes'
- "no": 'No'
+ :yes: 'Yes'
+ :no: 'No'
create: 'Create'
save: 'Save'
submit: 'Submit'
View
10 spec/commit_button_spec.rb
@@ -46,12 +46,12 @@
describe "its accesskey" do
it 'should allow nil default' do
- Formtastic::SemanticFormBuilder.default_commit_button_accesskey.should == nil
+ ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey.should == nil
output_buffer.should_not have_tag('li.commit input[@accesskey]')
end
it 'should use the default if set' do
- Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
+ ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
@new_post.stub!(:new_record?).and_return(false)
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button('text', :button_html => {}))
@@ -60,7 +60,7 @@
end
it 'should use the value set in options over the default' do
- Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
+ ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
@new_post.stub!(:new_record?).and_return(false)
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button('text', :accesskey => 'o'))
@@ -70,7 +70,7 @@
end
it 'should use the value set in button_html over options' do
- Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
+ ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
@new_post.stub!(:new_record?).and_return(false)
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button('text', :accesskey => 'o', :button_html => {:accesskey => 't'}))
@@ -81,7 +81,7 @@
end
after do
- Formtastic::SemanticFormBuilder.default_commit_button_accesskey = nil
+ ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey = nil
end
end
View
16 spec/custom_builder_spec.rb
@@ -5,7 +5,7 @@
include FormtasticSpecHelper
- class MyCustomFormBuilder < Formtastic::SemanticFormBuilder
+ class MyCustomFormBuilder < ::Formtastic::SemanticFormBuilder
def awesome_input(method, options)
self.text_field(method)
end
@@ -17,28 +17,28 @@ def awesome_input(method, options)
end
it 'is the Formtastic::SemanticFormBuilder by default' do
- Formtastic::SemanticFormHelper.builder.should == Formtastic::SemanticFormBuilder
+ ::Formtastic::SemanticFormHelper.builder.should == ::Formtastic::SemanticFormBuilder
end
it 'can be configured to use your own custom form builder' do
# Set it to a custom builder class
- Formtastic::SemanticFormHelper.builder = MyCustomFormBuilder
- Formtastic::SemanticFormHelper.builder.should == MyCustomFormBuilder
+ ::Formtastic::SemanticFormHelper.builder = MyCustomFormBuilder
+ ::Formtastic::SemanticFormHelper.builder.should == MyCustomFormBuilder
# Reset it to the default
- Formtastic::SemanticFormHelper.builder = Formtastic::SemanticFormBuilder
- Formtastic::SemanticFormHelper.builder.should == Formtastic::SemanticFormBuilder
+ ::Formtastic::SemanticFormHelper.builder = ::Formtastic::SemanticFormBuilder
+ ::Formtastic::SemanticFormHelper.builder.should == ::Formtastic::SemanticFormBuilder
end
describe "when using a custom builder" do
before do
@new_post.stub!(:title)
- Formtastic::SemanticFormHelper.builder = MyCustomFormBuilder
+ ::Formtastic::SemanticFormHelper.builder = MyCustomFormBuilder
end
after do
- Formtastic::SemanticFormHelper.builder = Formtastic::SemanticFormBuilder
+ ::Formtastic::SemanticFormHelper.builder = ::Formtastic::SemanticFormBuilder
end
describe "semantic_form_for" do
View
6 spec/custom_macros.rb
@@ -157,7 +157,7 @@ def it_should_apply_error_logic_for_input_type(type)
end
it 'should render a paragraph for the errors' do
- Formtastic::SemanticFormBuilder.inline_errors = :sentence
+ ::Formtastic::SemanticFormBuilder.inline_errors = :sentence
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :as => type))
end
@@ -165,7 +165,7 @@ def it_should_apply_error_logic_for_input_type(type)
end
it 'should not display an error list' do
- Formtastic::SemanticFormBuilder.inline_errors = :list
+ ::Formtastic::SemanticFormBuilder.inline_errors = :list
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :as => type))
end
@@ -399,7 +399,7 @@ def it_should_use_the_collection_when_provided(as, countable)
end
describe 'when the :label_method option is not provided' do
- Formtastic::SemanticFormBuilder.collection_label_methods.each do |label_method|
+ ::Formtastic::SemanticFormBuilder.collection_label_methods.each do |label_method|
describe "when the collection objects respond to #{label_method}" do
before do
View
12 spec/errors_spec.rb
@@ -19,14 +19,14 @@
end
it 'should render a paragraph with the errors joined into a sentence when inline_errors config is :sentence' do
- Formtastic::SemanticFormBuilder.inline_errors = :sentence
+ ::Formtastic::SemanticFormBuilder.inline_errors = :sentence
semantic_form_for(@new_post) do |builder|
builder.errors_on(:title).should have_tag('p.inline-errors', @title_errors.to_sentence)
end
end
it 'should render an unordered list with the class errors when inline_errors config is :list' do
- Formtastic::SemanticFormBuilder.inline_errors = :list
+ ::Formtastic::SemanticFormBuilder.inline_errors = :list
semantic_form_for(@new_post) do |builder|
builder.errors_on(:title).should have_tag('ul.errors')
@title_errors.each do |error|
@@ -36,14 +36,14 @@
end
it 'should render a paragraph with the first error when inline_errors config is :first' do
- Formtastic::SemanticFormBuilder.inline_errors = :first
+ ::Formtastic::SemanticFormBuilder.inline_errors = :first
semantic_form_for(@new_post) do |builder|
builder.errors_on(:title).should have_tag('p.inline-errors', @title_errors.first)
end
end
it 'should return nil when inline_errors config is :none' do
- Formtastic::SemanticFormBuilder.inline_errors = :none
+ ::Formtastic::SemanticFormBuilder.inline_errors = :none
semantic_form_for(@new_post) do |builder|
builder.errors_on(:title).should be_nil
end
@@ -58,7 +58,7 @@
it 'should return nil when inline_errors config is :sentence, :list or :none' do
[:sentence, :list, :none].each do |config|
- Formtastic::SemanticFormBuilder.inline_errors = config
+ ::Formtastic::SemanticFormBuilder.inline_errors = config
semantic_form_for(@new_post) do |builder|
builder.errors_on(:title).should be_nil
end
@@ -73,7 +73,7 @@
it 'should return nil when inline_errors config is :sentence, :list or :none' do
[:sentence, :list, :none].each do |config|
- Formtastic::SemanticFormBuilder.inline_errors = config
+ ::Formtastic::SemanticFormBuilder.inline_errors = config
semantic_form_for(@new_post) do |builder|
builder.errors_on(:title).should be_nil
end
View
8 spec/form_helper_spec.rb
@@ -14,7 +14,7 @@
it 'yields an instance of SemanticFormBuilder' do
semantic_form_for(:post, ::Post.new, :url => '/hello') do |builder|
- builder.class.should == Formtastic::SemanticFormBuilder
+ builder.class.should == ::Formtastic::SemanticFormBuilder
end
end
@@ -85,7 +85,7 @@
describe '#semantic_fields_for' do
it 'yields an instance of SemanticFormBuilder' do
semantic_fields_for(:post, ::Post.new, :url => '/hello') do |builder|
- builder.class.should == Formtastic::SemanticFormBuilder
+ builder.class.should == ::Formtastic::SemanticFormBuilder
end
end
end
@@ -93,7 +93,7 @@
describe '#semantic_form_remote_for' do
it 'yields an instance of SemanticFormBuilder' do
semantic_form_remote_for(:post, ::Post.new, :url => '/hello') do |builder|
- builder.class.should == Formtastic::SemanticFormBuilder
+ builder.class.should == ::Formtastic::SemanticFormBuilder
end
end
end
@@ -101,7 +101,7 @@
describe '#semantic_form_for_remote' do
it 'yields an instance of SemanticFormBuilder' do
semantic_remote_form_for(:post, ::Post.new, :url => '/hello') do |builder|
- builder.class.should == Formtastic::SemanticFormBuilder
+ builder.class.should == ::Formtastic::SemanticFormBuilder
end
end
end
View
40 spec/input_spec.rb
@@ -12,7 +12,7 @@
describe 'with inline order customization' do
it 'should allow input, hints, errors as order' do
- Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
+ ::Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
semantic_form_for(@new_post) do |builder|
builder.should_receive(:inline_input_for).once.ordered
@@ -23,7 +23,7 @@
end
it 'should allow hints, input, errors as order' do
- Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors]
+ ::Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors]
semantic_form_for(@new_post) do |builder|
builder.should_receive(:inline_hints_for).once.ordered
@@ -49,12 +49,12 @@
describe 'when true' do
before do
- @string = Formtastic::SemanticFormBuilder.required_string = " required yo!" # ensure there's something in the string
+ @string = ::Formtastic::SemanticFormBuilder.required_string = " required yo!" # ensure there's something in the string
@new_post.class.should_not_receive(:reflect_on_all_validations)
end
after do
- Formtastic::SemanticFormBuilder.required_string = %{<abbr title="required">*</abbr>}
+ ::Formtastic::SemanticFormBuilder.required_string = %{<abbr title="required">*</abbr>}
end
it 'should set a "required" class' do
@@ -77,12 +77,12 @@
describe 'when false' do
before do
- @string = Formtastic::SemanticFormBuilder.optional_string = " optional yo!" # ensure there's something in the string
+ @string = ::Formtastic::SemanticFormBuilder.optional_string = " optional yo!" # ensure there's something in the string
@new_post.class.should_not_receive(:reflect_on_all_validations)
end
after do
- Formtastic::SemanticFormBuilder.optional_string = ''
+ ::Formtastic::SemanticFormBuilder.optional_string = ''
end
it 'should set an "optional" class' do
@@ -107,8 +107,8 @@
describe 'and an object was not given' do
it 'should use the default value' do
- Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
- Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
+ ::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
+ ::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
semantic_form_for(:project, :url => 'http://test.host/') do |builder|
concat(builder.input(:title))
@@ -116,7 +116,7 @@
output_buffer.should_not have_tag('form li.required')
output_buffer.should have_tag('form li.optional')
- Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
+ ::Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
end
end
@@ -225,8 +225,8 @@ def should_be_required(options)
describe 'and the validation reflection plugin is not available' do
it 'should use the default value' do
- Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
- Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
+ ::Formtastic::SemanticFormBuilder.all_fields_required_by_default.should == true
+ ::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
@@ -234,7 +234,7 @@ def should_be_required(options)
output_buffer.should_not have_tag('form li.required')
output_buffer.should have_tag('form li.optional')
- Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
+ ::Formtastic::SemanticFormBuilder.all_fields_required_by_default = true
end
end
@@ -330,12 +330,12 @@ def should_be_required(options)
end
describe 'defaulting to file column' do
- Formtastic::SemanticFormBuilder.file_methods.each do |method|
+ ::Formtastic::SemanticFormBuilder.file_methods.each do |method|
it "should default to :file for attributes that respond to ##{method}" do
@new_post.stub!(:column_for_attribute).and_return(nil)
column = mock('column')
- Formtastic::SemanticFormBuilder.file_methods.each do |test|
+ ::Formtastic::SemanticFormBuilder.file_methods.each do |test|
column.stub!(:respond_to?).with(test).and_return(method == test)
end
@@ -401,25 +401,25 @@ def should_be_required(options)
describe 'when localized label is NOT provided' do
describe 'and object is not given' do
it 'should default the humanized method name, passing it down to the label tag' do
- Formtastic::SemanticFormBuilder.label_str_method = :humanize
-
+ ::Formtastic::SemanticFormBuilder.label_str_method = :humanize
+
semantic_form_for(:project, :url => 'http://test.host') do |builder|
concat(builder.input(:meta_description))
end
-
+
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
end
end
-
+
describe 'and object is given' do
it 'should delegate the label logic to class human attribute name and pass it down to the label tag' do
@new_post.stub!(:meta_description) # a two word method name
@new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return('meta_description'.humanize)
-
+
semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description))
end
-
+
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
end
end
View
2  spec/inputs/country_input_spec.rb
@@ -62,7 +62,7 @@
end
it "should default to the @@priority_countries config when absent" do
- priority_countries = Formtastic::SemanticFormBuilder.priority_countries
+ priority_countries = ::Formtastic::SemanticFormBuilder.priority_countries
priority_countries.should_not be_empty
priority_countries.should_not be_nil
View
6 spec/inputs/datetime_input_spec.rb
@@ -84,7 +84,7 @@
end
it 'should be specified with :order option' do
- I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] }
+ ::I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] }
semantic_form_for(@new_post) do |builder|
self.should_receive(:select_month).once.ordered.and_return('')
self.should_receive(:select_year).once.ordered.and_return('')
@@ -105,7 +105,7 @@
describe 'when the locale changes the label text' do
before do
- I18n.backend.store_translations 'en', :datetime => {:prompts => {
+ ::I18n.backend.store_translations 'en', :datetime => {:prompts => {
:year => 'The Year', :month => 'The Month', :day => 'The Day',
:hour => 'The Hour', :minute => 'The Minute'
}}
@@ -115,7 +115,7 @@
end
after do
- I18n.backend.store_translations 'en', :formtastic => {
+ ::I18n.backend.store_translations 'en', :formtastic => {
:year => nil, :month => nil, :day => nil,
:hour => nil, :minute => nil
}
View
87 spec/inputs/select_input_spec.rb
@@ -2,14 +2,51 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe 'select input' do
-
+
include FormtasticSpecHelper
-
+
before do
@output_buffer = ''
mock_everything
end
-
+
+ describe 'explicit values' do
+ describe 'using an array of values' do
+ before do
+ @array_with_values = ["Title A", "Title B", "Title C"]
+ @array_with_keys_and_values = [["Title D", 1], ["Title E", 2], ["Title F", 3]]
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :as => :select, :collection => @array_with_values))
+ concat(builder.input(:title, :as => :select, :collection => @array_with_keys_and_values))
+ end
+ end
+
+ it 'should have a option for each key and/or value' do
+ @array_with_values.each do |v|
+ output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
+ end
+ @array_with_keys_and_values.each do |v|
+ output_buffer.should have_tag("form li select option[@value='#{v.second}']", /^#{v.first}$/)
+ end
+ end
+ end
+
+ describe 'using a range' do
+ before do
+ @range_with_values = 1..5
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :as => :select, :collection => @range_with_values))
+ end
+ end
+
+ it 'should have an option for each value' do
+ @range_with_values.each do |v|
+ output_buffer.should have_tag("form li select option[@value='#{v}']", /^#{v}$/)
+ end
+ end
+ end
+ end
+
describe 'for a belongs_to association' do
before do
semantic_form_for(@new_post) do |builder|
@@ -259,5 +296,47 @@
end
-end
+ describe 'boolean select' do
+ describe 'default formtastic locale' do
+ before do
+ # Note: Works, but something like Formtastic.root.join(...) would probably be "safer".
+ ::I18n.load_path = [File.join(File.dirname(__FILE__), *%w[.. .. lib locale en.yml])]
+ ::I18n.backend.send(:init_translations)
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:published, :as => :select))
+ end
+ end
+
+ after do
+ ::I18n.backend.store_translations :en, {}
+ end
+ it 'should render a select with at least options: true/false' do
+ output_buffer.should have_tag("form li select option[@value='true']", /^Yes$/)
+ output_buffer.should have_tag("form li select option[@value='false']", /^No$/)
+ end
+ end
+
+ describe 'custom locale' do
+ before do
+ @boolean_select_labels = {:yes => 'Yep', :no => 'Nope'}
+ ::I18n.backend.store_translations :en, :formtastic => @boolean_select_labels
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:published, :as => :select))
+ end
+ end
+
+ after do
+ ::I18n.backend.store_translations :en, {}
+ end
+
+ it 'should render a select with at least options: true/false' do
+ output_buffer.should have_tag("form li select option[@value='true']", /#{@boolean_select_labels[:yes]}/)
+ output_buffer.should have_tag("form li select option[@value='false']", /#{@boolean_select_labels[:no]}/)
+ end
+ end
+ end
+
+end
View
44 spec/inputs_spec.rb
@@ -174,9 +174,12 @@
describe 'and is a string' do
before do
@legend_text = "Advanced options"
- @legend_text_using_title = "Advanced options 2"
+ @legend_text_using_name = "Advanced options 2"
+ @legend_text_using_title = "Advanced options 3"
semantic_form_for(@new_post) do |builder|
- builder.inputs :name => @legend_text do
+ builder.inputs @legend_text do
+ end
+ builder.inputs :name => @legend_text_using_name do
end
builder.inputs :title => @legend_text_using_title do
end
@@ -184,34 +187,40 @@
end
it 'should render a fieldset with a legend inside the form' do
- output_buffer.should have_tag("form fieldset legend", /#{@legend_text}/)
- output_buffer.should have_tag("form fieldset legend", /#{@legend_text_using_title}/)
+ output_buffer.should have_tag("form fieldset legend", /^#{@legend_text}$/)
+ output_buffer.should have_tag("form fieldset legend", /^#{@legend_text_using_name}$/)
+ output_buffer.should have_tag("form fieldset legend", /^#{@legend_text_using_title}$/)
end
end
describe 'and is a symbol' do
before do
@localized_legend_text = "Localized advanced options"
- @localized_legend_text_using_title = "Localized advanced options 2"
- I18n.backend.store_translations :en, :formtastic => {
+ @localized_legend_text_using_name = "Localized advanced options 2"
+ @localized_legend_text_using_title = "Localized advanced options 3"
+ ::I18n.backend.store_translations :en, :formtastic => {
:titles => {
:post => {
:advanced_options => @localized_legend_text,
- :advanced_options_2 => @localized_legend_text_using_title
+ :advanced_options_using_name => @localized_legend_text_using_name,
+ :advanced_options_using_title => @localized_legend_text_using_title
}
}
}
semantic_form_for(@new_post) do |builder|
- builder.inputs :name => :advanced_options do
+ builder.inputs :advanced_options do
+ end
+ builder.inputs :name => :advanced_options_using_name do
end
- builder.inputs :title => :advanced_options_2 do
+ builder.inputs :title => :advanced_options_using_title do
end
end
end
it 'should render a fieldset with a localized legend inside the form' do
- output_buffer.should have_tag("form fieldset legend", /#{@localized_legend_text}/)
- output_buffer.should have_tag("form fieldset legend", /#{@localized_legend_text_using_title}/)
+ output_buffer.should have_tag("form fieldset legend", /^#{@localized_legend_text}$/)
+ output_buffer.should have_tag("form fieldset legend", /^#{@localized_legend_text_using_name}$/)
+ output_buffer.should have_tag("form fieldset legend", /^#{@localized_legend_text_using_title}$/)
end
end
end
@@ -238,9 +247,8 @@
describe 'without a block' do
before do
- ::Post.stub!(:reflections).and_return({:author => mock('reflection', :options => {}, :macro => :belongs_to),
+ ::Post.stub!(:reflections).and_return({:author => mock('reflection', :options => {}, :macro => :belongs_to),
:comments => mock('reflection', :options => {}, :macro => :has_many) })
- ::Post.stub!(:content_columns).and_return([mock('column', :name => 'title'), mock('column', :name => 'body'), mock('column', :name => 'created_at')])
::Author.stub!(:find).and_return([@fred, @bob])
@new_post.stub!(:title)
@@ -352,12 +360,15 @@
describe 'with column names and an options hash as args' do
before do
semantic_form_for(@new_post) do |builder|
- concat(builder.inputs(:title, :body, :name => "Legendary Legend Text", :id => "my-id"))
+ @legend_text_using_option = "Legendary Legend Text"
+ @legend_text_using_arg = "Legendary Legend Text 2"
+ concat(builder.inputs(:title, :body, :name => @legend_text_using_option, :id => "my-id"))
+ concat(builder.inputs(@legend_text_using_arg, :title, :body, :id => "my-id-2"))
end
end
it 'should render a form with a fieldset containing two list items' do
- output_buffer.should have_tag('form > fieldset.inputs > ol > li', :count => 2)
+ output_buffer.should have_tag('form > fieldset.inputs > ol > li', :count => 4)
end
it 'should pass the options down to the fieldset' do
@@ -365,7 +376,8 @@
end
it 'should use the special :name option as a text for the legend tag' do
- output_buffer.should have_tag('form > fieldset#my-id.inputs > legend', /Legendary Legend Text/)
+ output_buffer.should have_tag('form > fieldset#my-id.inputs > legend', /^#{@legend_text_using_option}$/)
+ output_buffer.should have_tag('form > fieldset#my-id-2.inputs > legend', /^#{@legend_text_using_arg}$/)
end
end
View
2  spec/semantic_fields_for_spec.rb
@@ -14,7 +14,7 @@
it 'yields an instance of SemanticFormHelper.builder' do
semantic_form_for(@new_post) do |builder|
builder.semantic_fields_for(:author) do |nested_builder|
- nested_builder.class.should == Formtastic::SemanticFormHelper.builder
+ nested_builder.class.should == ::Formtastic::SemanticFormHelper.builder
end
end
end
View
3  spec/spec_helper.rb
@@ -116,6 +116,7 @@ def new_author_path; "/authors/new"; end
::Author.stub!(:human_name).and_return('::Author')
::Author.stub!(:reflect_on_validations_for).and_return([])
::Author.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => Post, :macro => :has_many) if column_name == :posts }
+ ::Author.stub!(:content_columns).and_return([mock('column', :name => 'login'), mock('column', :name => 'created_at')])
# Sometimes we need a mock @post object and some Authors for belongs_to
@new_post = mock('post')
@@ -153,11 +154,13 @@ def new_author_path; "/authors/new"; end
end
end
::Post.stub!(:find).and_return([@freds_post])
+ ::Post.stub!(:content_columns).and_return([mock('column', :name => 'title'), mock('column', :name => 'body'), mock('column', :name => 'created_at')])
@new_post.stub!(:title)
@new_post.stub!(:body)
@new_post.stub!(:published)
@new_post.stub!(:publish_at)
+ @new_post.stub!(:created_at)
@new_post.stub!(:secret)
@new_post.stub!(:time_zone)
@new_post.stub!(:category_name)
Please sign in to comment.
Something went wrong with that request. Please try again.