Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #55 from rvanlieshout/master

Added support for date_picker and datetime_picker
  • Loading branch information...
commit ce27c4ed13684e76f7d021ff232a6208e1297356 2 parents bff392f + 0abeba2
@sodabrew sodabrew authored
View
4 lib/formtastic-bootstrap/inputs.rb
@@ -7,8 +7,8 @@ module Inputs
autoload :CheckBoxesInput, "formtastic-bootstrap/inputs/check_boxes_input"
autoload :CountryInput, "formtastic-bootstrap/inputs/country_input"
autoload :DateInput, "formtastic-bootstrap/inputs/date_input"
- # autoload :DatePickerInput # TODO Come back to this.
- # autoload :DatetimePickerInput # TODO Come back to this.
+ autoload :DatePickerInput, "formtastic-bootstrap/inputs/date_picker_input"
+ autoload :DatetimePickerInput, "formtastic-bootstrap/inputs/datetime_picker_input"
autoload :DateSelectInput, "formtastic-bootstrap/inputs/date_select_input"
autoload :DatetimeInput, "formtastic-bootstrap/inputs/datetime_input"
autoload :DatetimeSelectInput, "formtastic-bootstrap/inputs/datetime_select_input"
View
4 lib/formtastic-bootstrap/inputs/base.rb
@@ -7,7 +7,7 @@ module FormtasticBootstrap
module Inputs
module Base
- # autoload :DatetimePickerish
+ autoload :DatetimePickerish, "formtastic-bootstrap/inputs/base/datetime_pickerish"
# autoload :Associations
autoload :Collections, "formtastic-bootstrap/inputs/base/collections"
autoload :Choices, "formtastic-bootstrap/inputs/base/choices"
@@ -21,7 +21,7 @@ module Base
# autoload :Naming
autoload :Numeric, "formtastic-bootstrap/inputs/base/numeric"
# autoload :Options
- # autoload :Placeholder
+ autoload :Placeholder, "formtastic-bootstrap/inputs/base/placeholder"
autoload :Stringish, "formtastic-bootstrap/inputs/base/stringish"
autoload :Timeish, "formtastic-bootstrap/inputs/base/timeish"
# autoload :Validations
View
10 lib/formtastic-bootstrap/inputs/base/datetime_pickerish.rb
@@ -0,0 +1,10 @@
+module FormtasticBootstrap
+ module Inputs
+ module Base
+ module DatetimePickerish
+ include Base::Placeholder
+ include Formtastic::Inputs::Base::DatetimePickerish
+ end
+ end
+ end
+end
View
9 lib/formtastic-bootstrap/inputs/base/placeholder.rb
@@ -0,0 +1,9 @@
+module FormtasticBootstrap
+ module Inputs
+ module Base
+ module Placeholder
+ include Formtastic::Inputs::Base::Placeholder
+ end
+ end
+ end
+end
View
9 lib/formtastic-bootstrap/inputs/date_picker_input.rb
@@ -0,0 +1,9 @@
+module FormtasticBootstrap
+ module Inputs
+ class DatePickerInput < Formtastic::Inputs::DatePickerInput
+ include Base
+ include Base::Stringish
+ include Base::DatetimePickerish
+ end
+ end
+end
View
9 lib/formtastic-bootstrap/inputs/datetime_picker_input.rb
@@ -0,0 +1,9 @@
+module FormtasticBootstrap
+ module Inputs
+ class DatetimePickerInput < Formtastic::Inputs::DatetimePickerInput
+ include Base
+ include Base::Stringish
+ include Base::DatetimePickerish
+ end
+ end
+end
View
450 spec/inputs/date_picker_input_spec.rb
@@ -0,0 +1,450 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe 'date_picker input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ context "with an object" do
+ before do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :date_picker))
+ end)
+ end
+
+ it_should_have_bootstrap_horizontal_wrapping
+ it_should_have_input_wrapper_with_class(:date_picker)
+ it_should_have_input_wrapper_with_class(:input)
+ it_should_have_input_wrapper_with_class(:stringish)
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
+ it_should_have_label_with_text(/Publish at/)
+ it_should_have_label_for("post_publish_at")
+ it_should_have_input_with_id("post_publish_at")
+ it_should_have_input_with_type(:date)
+ it_should_have_input_with_name("post[publish_at]")
+ it_should_apply_custom_input_attributes_when_input_html_provided(:date_picker)
+ it_should_apply_custom_for_to_label_when_input_html_id_provided(:date_picker)
+ # TODO why does this blow-up it_should_apply_error_logic_for_input_type(:date_picker)
+
+ end
+
+ describe "size attribute" do
+
+ it "defaults to 10 chars (YYYY-YY-YY)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker))
+ end
+ )
+ output_buffer.should have_tag "input[size='10']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :size => "11" }))
+ end
+ )
+ output_buffer.should have_tag "input[size='11']"
+ end
+
+ it "can be set from options (ignoring input_html)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :size => '12', :input_html => { :size => "11" }))
+ end
+ )
+ output_buffer.should have_tag "input[size='12']"
+ end
+
+ end
+
+ describe "maxlength attribute" do
+
+ it "defaults to 10 chars (YYYY-YY-YY)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker))
+ end
+ )
+ output_buffer.should have_tag "input[maxlength='10']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :maxlength => "11" }))
+ end
+ )
+ output_buffer.should have_tag "input[maxlength='11']"
+ end
+
+ it "can be set from options (ignoring input_html)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :maxlength => 12, :input_html => { :maxlength => "11" }))
+ end
+ )
+ output_buffer.should have_tag "input[maxlength='12']"
+ end
+
+ end
+
+ describe "value attribute" do
+
+ context "when method returns nil" do
+
+ it "has no value" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker ))
+ end
+ )
+ output_buffer.should_not have_tag "li input[value]"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11']"
+ end
+
+ end
+
+ context "when method returns a Date" do
+
+ before do
+ @date = Date.new(2000, 11, 11)
+ @new_post.stub!(:publish_at).and_return(@date)
+ end
+
+ it "renders the date as YYYY-MM-DD" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker ))
+ end
+ )
+ output_buffer.should have_tag "input[value='#{@date.to_s}']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11']"
+ end
+
+ end
+
+ context "when method returns a Time" do
+
+ before do
+ @time = Time.utc(2000,11,11,11,11,11)
+ @new_post.stub!(:publish_at).and_return(@time)
+ end
+
+ it "renders the time as a YYYY-MM-DD" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker ))
+ end
+ )
+ output_buffer.should have_tag "input[value='2000-11-11']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11']"
+ end
+
+ end
+
+ context "when method returns an empty String" do
+
+ before do
+ @new_post.stub!(:publish_at).and_return("")
+ end
+
+ it "will be empty" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker ))
+ end
+ )
+ output_buffer.should have_tag "input[value='']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11']"
+ end
+
+ end
+
+ context "when method returns a String" do
+
+ before do
+ @new_post.stub!(:publish_at).and_return("yeah")
+ end
+
+ it "will be the string" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker ))
+ end
+ )
+ output_buffer.should have_tag "input[value='yeah']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11']"
+ end
+
+ end
+
+ end
+
+ describe "min attribute" do
+
+ it "will be omitted by default" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker))
+ end
+ )
+ output_buffer.should_not have_tag "input[min]"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :min => "1970-01-01" }))
+ end
+ )
+ output_buffer.should have_tag "input[min='1970-01-01']"
+ end
+
+ end
+
+ describe "max attribute" do
+
+ it "will be omitted by default" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker))
+ end
+ )
+ output_buffer.should_not have_tag "input[max]"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :max => "1970-01-01" }))
+ end
+ )
+ output_buffer.should have_tag "input[max='1970-01-01']"
+ end
+
+ end
+
+ describe "step attribute" do
+
+ it "defaults to 1" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker))
+ end
+ )
+ output_buffer.should have_tag "input[step='1']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :step => "5" }))
+ end
+ )
+ output_buffer.should have_tag "input[step='5']"
+ end
+
+ describe "macros" do
+
+ before do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :step => step }))
+ end
+ )
+ end
+
+ context ":day" do
+ let(:step) { :day }
+ it "uses 1" do
+ output_buffer.should have_tag "input[step='1']"
+ end
+ end
+
+ context ":seven_days" do
+ let(:step) { :seven_days }
+ it "uses 7" do
+ output_buffer.should have_tag "input[step='7']"
+ end
+ end
+
+ context ":week" do
+ let(:step) { :week }
+ it "uses 7" do
+ output_buffer.should have_tag "input[step='7']"
+ end
+ end
+
+ context ":fortnight" do
+ let(:step) { :fortnight }
+ it "uses 14" do
+ output_buffer.should have_tag "input[step='14']"
+ end
+ end
+
+ context ":two_weeks" do
+ let(:step) { :two_weeks }
+ it "uses 14" do
+ output_buffer.should have_tag "input[step='14']"
+ end
+ end
+
+ context ":four_weeks" do
+ let(:step) { :four_weeks }
+ it "uses 28" do
+ output_buffer.should have_tag "input[step='28']"
+ end
+ end
+
+ context ":thirty_days" do
+ let(:step) { :thirty_days }
+ it "uses 30" do
+ output_buffer.should have_tag "input[step='30']"
+ end
+ end
+
+ end
+
+ end
+
+ describe "placeholder attribute" do
+
+ it "will be omitted" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker))
+ end
+ )
+ output_buffer.should_not have_tag "input[placeholder]"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :placeholder => "1970-01-01" }))
+ end
+ )
+ output_buffer.should have_tag "input[placeholder='1970-01-01']"
+ end
+
+ context "with i18n set" do
+ before do
+ ::I18n.backend.store_translations :en, :formtastic => { :placeholders => { :publish_at => 'YYYY-MM-DD' }}
+ end
+
+ it "can be set with i18n" do
+ with_config :i18n_lookups_by_default, true do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :date_picker))
+ end)
+ output_buffer.should have_tag('input[@placeholder="YYYY-MM-DD"]')
+ end
+ end
+
+ it "can be set with input_html, trumping i18n" do
+ with_config :i18n_lookups_by_default, true do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :date_picker, :input_html => { :placeholder => "Something" }))
+ end)
+ output_buffer.should have_tag('input[@placeholder="Something"]')
+ end
+ end
+ end
+
+ end
+
+ describe "when namespace is provided" do
+ before do
+ concat(semantic_form_for(@new_post, :namespace => "context2") do |builder|
+ concat(builder.input(:publish_at, :as => :date_picker))
+ end)
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_publish_at_input")
+ it_should_have_label_and_input_with_id("context2_post_publish_at")
+ end
+
+ describe "when index is provided" do
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.fields_for(:author, :index => 3) do |author|
+ concat(author.input(:created_at, :as => :date_picker))
+ end)
+ end)
+ end
+
+ it 'should index the id of the wrapper' do
+ output_buffer.should have_tag("div#post_author_attributes_3_created_at_input")
+ end
+
+ it 'should index the id of the select tag' do
+ output_buffer.should have_tag("input#post_author_attributes_3_created_at")
+ end
+
+ it 'should index the name of the select tag' do
+ output_buffer.should have_tag("input[@name='post[author_attributes][3][created_at]']")
+ end
+ end
+
+ describe "when required" do
+ it "should add the required attribute to the input's html options" do
+ with_config :use_required_attribute, true do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :date_picker, :required => true))
+ end)
+ output_buffer.should have_tag("input[@required]")
+ end
+ end
+ end
+
+end
View
491 spec/inputs/datetime_picker_input_spec.rb
@@ -0,0 +1,491 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe 'datetime_picker input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ after do
+ ::I18n.backend.reload!
+ end
+
+ context "with an object" do
+ before do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime_picker))
+ end)
+ end
+
+ it_should_have_bootstrap_horizontal_wrapping
+ it_should_have_input_wrapper_with_class(:datetime_picker)
+ it_should_have_input_wrapper_with_class(:input)
+ it_should_have_input_wrapper_with_class(:stringish)
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
+ it_should_have_label_with_text(/Publish at/)
+ it_should_have_label_for("post_publish_at")
+ it_should_have_input_with_id("post_publish_at")
+ it_should_have_input_with_type("datetime-local")
+ it_should_have_input_with_name("post[publish_at]")
+ it_should_apply_custom_input_attributes_when_input_html_provided(:datetime_picker)
+ it_should_apply_custom_for_to_label_when_input_html_id_provided(:datetime_picker)
+ # TODO why does this blow-up it_should_apply_error_logic_for_input_type(:datetime_picker)
+
+ end
+
+ describe ":local option for UTC or local time" do
+
+ it "should default to a datetime-local input (true)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker))
+ end
+ )
+ output_buffer.should have_tag "input[type='datetime-local']"
+ end
+
+ it "can be set to true for a datetime-local" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :local => true))
+ end
+ )
+ output_buffer.should have_tag "input[type='datetime-local']"
+ end
+
+ it "can be set to false for a datetime" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :local => false))
+ end
+ )
+ output_buffer.should have_tag "input[type='datetime']"
+ end
+
+ end
+
+ describe "size attribute" do
+
+ it "defaults to 10 chars (YYYY-YY-YY HH:MM)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker))
+ end
+ )
+ output_buffer.should have_tag "input[size='16']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :size => "11" }))
+ end
+ )
+ output_buffer.should have_tag "input[size='11']"
+ end
+
+ it "can be set from options (ignoring input_html)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :size => '12', :input_html => { :size => "11" }))
+ end
+ )
+ output_buffer.should have_tag "input[size='12']"
+ end
+
+ end
+
+ describe "maxlength attribute" do
+
+ it "defaults to 10 chars (YYYY-YY-YY HH:MM)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker))
+ end
+ )
+ output_buffer.should have_tag "input[maxlength='16']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :maxlength => "11" }))
+ end
+ )
+ output_buffer.should have_tag "input[maxlength='11']"
+ end
+
+ it "can be set from options (ignoring input_html)" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :maxlength => 12, :input_html => { :maxlength => "11" }))
+ end
+ )
+ output_buffer.should have_tag "input[maxlength='12']"
+ end
+
+ end
+
+ describe "value attribute" do
+
+ context "when method returns nil" do
+
+ it "has no value" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker ))
+ end
+ )
+ output_buffer.should_not have_tag "li input[value]"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :value => "1111-11-11 23:00" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11 23:00']"
+ end
+
+ end
+
+ context "when method returns a Date" do
+
+ before do
+ @date = Date.new(2000, 11, 11)
+ @new_post.stub!(:publish_at).and_return(@date)
+ end
+
+ it "renders the date as YYYY-MM-DD 00:00" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker ))
+ end
+ )
+ output_buffer.should have_tag "input[value='#{@date.to_s} 00:00']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :value => "1111-11-11 00:00" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11 00:00']"
+ end
+
+ end
+
+ context "when method returns a Time" do
+
+ before do
+ @time = Time.utc(2000,11,11,11,11,11)
+ @new_post.stub!(:publish_at).and_return(@time)
+ end
+
+ it "renders the time as a YYYY-MM-DD HH:MM" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker ))
+ end
+ )
+ output_buffer.should have_tag "input[value='2000-11-11 11:11']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :value => "1111-11-11 11:11" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11 11:11']"
+ end
+
+ end
+
+ context "when method returns an empty String" do
+
+ before do
+ @new_post.stub!(:publish_at).and_return("")
+ end
+
+ it "will be empty" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker ))
+ end
+ )
+ output_buffer.should have_tag "input[value='']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :value => "1111-11-11 11:11" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11 11:11']"
+ end
+
+ end
+
+ context "when method returns a String" do
+
+ before do
+ @new_post.stub!(:publish_at).and_return("yeah")
+ end
+
+ it "will be the string" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker ))
+ end
+ )
+ output_buffer.should have_tag "input[value='yeah']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :value => "1111-11-11 11:11" }))
+ end
+ )
+ output_buffer.should have_tag "input[value='1111-11-11 11:11']"
+ end
+
+ end
+
+ end
+
+ describe "min attribute" do
+
+ it "will be omitted by default" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker))
+ end
+ )
+ output_buffer.should_not have_tag "input[min]"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :min => "1970-01-01 12:00" }))
+ end
+ )
+ output_buffer.should have_tag "input[min='1970-01-01 12:00']"
+ end
+
+ end
+
+ describe "max attribute" do
+
+ it "will be omitted by default" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker))
+ end
+ )
+ output_buffer.should_not have_tag "input[max]"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :max => "1970-01-01 12:00" }))
+ end
+ )
+ output_buffer.should have_tag "input[max='1970-01-01 12:00']"
+ end
+
+ end
+
+ describe "step attribute" do
+
+ it "defaults to 1" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker))
+ end
+ )
+ output_buffer.should have_tag "input[step='1']"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :step => "5" }))
+ end
+ )
+ output_buffer.should have_tag "input[step='5']"
+ end
+
+ describe "macros" do
+ before do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :step => step }))
+ end
+ )
+ end
+
+ context ":second" do
+ let(:step) { :second }
+ it "uses 1" do
+ output_buffer.should have_tag "input[step='1']"
+ end
+ end
+
+ context ":minute" do
+ let(:step) { :minute }
+ it "uses 60" do
+ output_buffer.should have_tag "input[step='60']"
+ end
+ end
+
+ context ":fifteen_minutes" do
+ let(:step) { :fifteen_minutes }
+ it "uses 900" do
+ output_buffer.should have_tag "input[step='900']"
+ end
+ end
+
+ context ":quarter_hour" do
+ let(:step) { :quarter_hour }
+ it "uses 900" do
+ output_buffer.should have_tag "input[step='900']"
+ end
+ end
+
+ context ":thirty_minutes" do
+ let(:step) { :thirty_minutes }
+ it "uses 1800" do
+ output_buffer.should have_tag "input[step='1800']"
+ end
+ end
+
+ context ":half_hour" do
+ let(:step) { :half_hour }
+ it "uses 1800" do
+ output_buffer.should have_tag "input[step='1800']"
+ end
+ end
+
+ context ":hour" do
+ let(:step) { :hour }
+ it "uses 3600" do
+ output_buffer.should have_tag "input[step='3600']"
+ end
+ end
+
+ context ":sixty_minutes" do
+ let(:step) { :sixty_minutes }
+ it "uses 3600" do
+ output_buffer.should have_tag "input[step='3600']"
+ end
+ end
+
+ end
+
+ end
+
+ describe "placeholder attribute" do
+
+ it "will be omitted" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker))
+ end
+ )
+ output_buffer.should_not have_tag "input[placeholder]"
+ end
+
+ it "can be set from :input_html options" do
+ concat(
+ semantic_form_for(@new_post) do |f|
+ concat(f.input(:publish_at, :as => :datetime_picker, :input_html => { :placeholder => "1970-01-01 00:00" }))
+ end
+ )
+ output_buffer.should have_tag "input[placeholder='1970-01-01 00:00']"
+ end
+
+ context "with i18n set" do
+ before do
+ ::I18n.backend.store_translations :en, :formtastic => { :placeholders => { :publish_at => 'YYYY-MM-DD HH:MM' }}
+ end
+
+ it "can be set with i18n" do
+ with_config :i18n_lookups_by_default, true do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime_picker))
+ end)
+ output_buffer.should have_tag('input[@placeholder="YYYY-MM-DD HH:MM"]')
+ end
+ end
+
+ it "can be set with input_html, trumping i18n" do
+ with_config :i18n_lookups_by_default, true do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime_picker, :input_html => { :placeholder => "Something" }))
+ end)
+ output_buffer.should have_tag('input[@placeholder="Something"]')
+ end
+ end
+ end
+
+ end
+
+ describe "when namespace is provided" do
+ before do
+ concat(semantic_form_for(@new_post, :namespace => "context2") do |builder|
+ concat(builder.input(:publish_at, :as => :datetime_picker))
+ end)
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_publish_at_input")
+ it_should_have_label_and_input_with_id("context2_post_publish_at")
+ end
+
+ describe "when index is provided" do
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.fields_for(:author, :index => 3) do |author|
+ concat(author.input(:created_at, :as => :datetime_picker))
+ end)
+ end)
+ end
+
+ it 'should index the id of the wrapper' do
+ output_buffer.should have_tag("div#post_author_attributes_3_created_at_input")
+ end
+
+ it 'should index the id of the select tag' do
+ output_buffer.should have_tag("input#post_author_attributes_3_created_at")
+ end
+
+ it 'should index the name of the select tag' do
+ output_buffer.should have_tag("input[@name='post[author_attributes][3][created_at]']")
+ end
+ end
+
+ describe "when required" do
+ it "should add the required attribute to the input's html options" do
+ with_config :use_required_attribute, true do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime_picker, :required => true))
+ end)
+ output_buffer.should have_tag("input[@required]")
+ end
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.