diff --git a/vendor/gems/formtastic-0.9.10/.specification b/vendor/gems/formtastic-0.9.10/.specification new file mode 100644 index 0000000..7bf9f96 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/.specification @@ -0,0 +1,211 @@ +--- !ruby/object:Gem::Specification +name: formtastic +version: !ruby/object:Gem::Version + hash: 47 + prerelease: false + segments: + - 0 + - 9 + - 10 + version: 0.9.10 +platform: ruby +authors: +- Justin French +autorequire: +bindir: bin +cert_chain: [] + +date: 2010-05-25 00:00:00 -05:00 +default_executable: +dependencies: +- !ruby/object:Gem::Dependency + name: activesupport + prerelease: false + requirement: &id001 !ruby/object:Gem::Requirement + none: false + requirements: + - - ">=" + - !ruby/object:Gem::Version + hash: 3 + segments: + - 2 + - 3 + - 0 + version: 2.3.0 + type: :runtime + version_requirements: *id001 +- !ruby/object:Gem::Dependency + name: actionpack + prerelease: false + requirement: &id002 !ruby/object:Gem::Requirement + none: false + requirements: + - - ">=" + - !ruby/object:Gem::Version + hash: 3 + segments: + - 2 + - 3 + - 0 + version: 2.3.0 + type: :runtime + version_requirements: *id002 +- !ruby/object:Gem::Dependency + name: rspec-rails + prerelease: false + requirement: &id003 !ruby/object:Gem::Requirement + none: false + requirements: + - - ">=" + - !ruby/object:Gem::Version + hash: 19 + segments: + - 1 + - 2 + - 6 + version: 1.2.6 + type: :development + version_requirements: *id003 +- !ruby/object:Gem::Dependency + name: rspec_tag_matchers + prerelease: false + requirement: &id004 !ruby/object:Gem::Requirement + none: false + requirements: + - - ">=" + - !ruby/object:Gem::Version + hash: 23 + segments: + - 1 + - 0 + - 0 + version: 1.0.0 + type: :development + version_requirements: *id004 +description: A Rails form builder plugin/gem with semantically rich and accessible markup +email: justin@indent.com.au +executables: [] + +extensions: [] + +extra_rdoc_files: +- README.textile +files: +- MIT-LICENSE +- README.textile +- Rakefile +- generators/form/USAGE +- generators/form/form_generator.rb +- generators/form/templates/view__form.html.erb +- generators/form/templates/view__form.html.haml +- generators/formtastic/formtastic_generator.rb +- generators/formtastic/templates/formtastic.css +- generators/formtastic/templates/formtastic.rb +- generators/formtastic/templates/formtastic_changes.css +- generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb +- lib/formtastic.rb +- lib/formtastic/i18n.rb +- lib/formtastic/layout_helper.rb +- lib/formtastic/util.rb +- lib/locale/en.yml +- rails/init.rb +- spec/buttons_spec.rb +- spec/commit_button_spec.rb +- spec/custom_builder_spec.rb +- spec/custom_macros.rb +- spec/defaults_spec.rb +- spec/error_proc_spec.rb +- spec/errors_spec.rb +- spec/form_helper_spec.rb +- spec/i18n_spec.rb +- spec/include_blank_spec.rb +- spec/input_spec.rb +- spec/inputs/boolean_input_spec.rb +- spec/inputs/check_boxes_input_spec.rb +- spec/inputs/country_input_spec.rb +- spec/inputs/date_input_spec.rb +- spec/inputs/datetime_input_spec.rb +- spec/inputs/file_input_spec.rb +- spec/inputs/hidden_input_spec.rb +- spec/inputs/numeric_input_spec.rb +- spec/inputs/password_input_spec.rb +- spec/inputs/radio_input_spec.rb +- spec/inputs/select_input_spec.rb +- spec/inputs/string_input_spec.rb +- spec/inputs/text_input_spec.rb +- spec/inputs/time_input_spec.rb +- spec/inputs/time_zone_input_spec.rb +- spec/inputs_spec.rb +- spec/label_spec.rb +- spec/layout_helper_spec.rb +- spec/semantic_errors_spec.rb +- spec/semantic_fields_for_spec.rb +- spec/spec.opts +- spec/spec_helper.rb +has_rdoc: true +homepage: http://github.com/justinfrench/formtastic/tree/master +licenses: [] + +post_install_message: "\n ========================================================================\n Thanks for installing Formtastic!\n ------------------------------------------------------------------------\n You can now (optionally) run the generator to copy some stylesheets and\n a config initializer into your application:\n ./script/generate formtastic\n\n To generate some semantic form markup for your existing models, just run:\n ./script/generate form MODEL_NAME\n\n Find out more and get involved:\n http://github.com/justinfrench/formtastic\n http://groups.google.com.au/group/formtastic\n ========================================================================\n " +rdoc_options: +- --charset=UTF-8 +require_paths: +- lib +required_ruby_version: !ruby/object:Gem::Requirement + none: false + requirements: + - - ">=" + - !ruby/object:Gem::Version + hash: 3 + segments: + - 0 + version: "0" +required_rubygems_version: !ruby/object:Gem::Requirement + none: false + requirements: + - - ">=" + - !ruby/object:Gem::Version + hash: 3 + segments: + - 0 + version: "0" +requirements: [] + +rubyforge_project: +rubygems_version: 1.3.7 +signing_key: +specification_version: 3 +summary: A Rails form builder plugin/gem with semantically rich and accessible markup +test_files: +- spec/buttons_spec.rb +- spec/commit_button_spec.rb +- spec/custom_builder_spec.rb +- spec/custom_macros.rb +- spec/defaults_spec.rb +- spec/error_proc_spec.rb +- spec/errors_spec.rb +- spec/form_helper_spec.rb +- spec/i18n_spec.rb +- spec/include_blank_spec.rb +- spec/input_spec.rb +- spec/inputs/boolean_input_spec.rb +- spec/inputs/check_boxes_input_spec.rb +- spec/inputs/country_input_spec.rb +- spec/inputs/date_input_spec.rb +- spec/inputs/datetime_input_spec.rb +- spec/inputs/file_input_spec.rb +- spec/inputs/hidden_input_spec.rb +- spec/inputs/numeric_input_spec.rb +- spec/inputs/password_input_spec.rb +- spec/inputs/radio_input_spec.rb +- spec/inputs/select_input_spec.rb +- spec/inputs/string_input_spec.rb +- spec/inputs/text_input_spec.rb +- spec/inputs/time_input_spec.rb +- spec/inputs/time_zone_input_spec.rb +- spec/inputs_spec.rb +- spec/label_spec.rb +- spec/layout_helper_spec.rb +- spec/semantic_errors_spec.rb +- spec/semantic_fields_for_spec.rb +- spec/spec_helper.rb diff --git a/vendor/plugins/formtastic/MIT-LICENSE b/vendor/gems/formtastic-0.9.10/MIT-LICENSE similarity index 100% rename from vendor/plugins/formtastic/MIT-LICENSE rename to vendor/gems/formtastic-0.9.10/MIT-LICENSE diff --git a/vendor/gems/formtastic-0.9.10/README.textile b/vendor/gems/formtastic-0.9.10/README.textile new file mode 100644 index 0000000..a620af9 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/README.textile @@ -0,0 +1,574 @@ +h1. Formtastic + +Formtastic is a Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications. + +h2. The Story + +One day, I finally had enough, so I opened up my text editor, and wrote a DSL for how I'd like to author forms: + +
+  <% semantic_form_for @article do |form| %>
+
+    <% form.inputs :name => "Basic" do %>
+      <%= form.input :title %>
+      <%= form.input :body %>
+      <%= form.input :section %>
+      <%= form.input :publication_state, :as => :radio %>
+      <%= form.input :category %>
+      <%= form.input :allow_comments, :label => "Allow commenting on this article" %>
+    <% end %>
+
+    <% form.inputs :name => "Advanced" do %>
+      <%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
+      <%= form.input :extract, :required => false %>
+      <%= form.input :description, :required => false %>
+      <%= form.input :url_title, :required => false %>
+    <% end %>
+
+    <% form.inputs :name => "Author", :for => :author do |author_form| %>
+      <%= author_form.input :first_name %>
+      <%= author_form.input :last_name %>
+    <% end %>
+
+    <% form.buttons do %>
+      <%= form.commit_button %>
+    <% end %>
+
+  <% end %>
+
+ +I also wrote the accompanying HTML output I expected, favoring something very similar to the fieldsets, lists and other semantic elements Aaron Gustafson presented in "Learning to Love Forms":http://www.slideshare.net/AaronGustafson/learning-to-love-forms-web-directions-south-07, hacking together enough Ruby to prove it could be done. + + +h2. It's better than _SomeOtherFormBuilder_ because... + +* it can handle @belongs_to@ associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model. +* it can handle @has_many@ and @has_and_belongs_to_many@ associations (like: Post has_many :tags), rendering a multi-select with choices from the child models. +* it's Rails 2.3-ready (including nested forms). +* it has internationalization (I18n)! +* it's _really_ quick to get started with a basic form in place (4 lines), then go back to add in more detail if you need it. +* there's heaps of elements, id and class attributes for you to hook in your CSS and JS. +* it handles real world stuff like inline hints, inline error messages & help text. +* it doesn't hijack or change any of the standard Rails form inputs, so you can still use them as expected (even mix and match). +* it's got absolutely awesome spec coverage. +* there's a bunch of people using and working on it (it's not just one developer building half a solution). + + +h2. Why? + +* web apps = lots of forms. +* forms are so friggin' boring to code. +* semantically rich & accessible forms really are possible. +* the "V" is way behind the "M" and "C" in Rails' MVC – it's the ugly sibling. +* best practices and common patterns have to start somewhere. +* i need a challenge. + + +h2. Opinions + +* it should be easier to do things the right way than the wrong way. +* sometimes _more mark-up_ is better. +* elements and attribute hooks are _gold_ for stylesheet authors. +* make the common things we do easy, yet still ensure uncommon things are still possible. + + +h2. Documentation + +RDoc documentation _should_ be automatically generated after each commit and made available on the "rdoc.info website":http://rdoc.info/projects/justinfrench/formtastic. + + +h2. Installation + +The gem is hosted on gemcutter, so *if you haven't already*, add it as a gem source: + +
+  sudo gem sources -a http://gemcutter.org/
+
+ +Then install the Formtastic gem: + +
+  sudo gem install formtastic
+
+ +And add it to your environment.rb configuration as a gem dependency: + +
+  config.gem 'formtastic'
+
+ +Optionally, run @./script/generate formtastic@ to copy the following files into your app: + +* @config/initializers/formtastic.rb@ - a commented out Formtastic config initializer +* @public/stylesheets/formtastic.css@ +* @public/stylesheets/formtastic_changes.css@ + +A proof-of-concept stylesheet is provided which you can include in your layout. Customization is best achieved by overriding these styles in an additional stylesheet so that the Formtastic styles can be updated without clobbering your changes. If you want to use these stylesheets, add both to your layout with this helper: + +
+  
+    ...
+    <%= formtastic_stylesheet_link_tag %>
+    ...
+  
+
+ +h2. Usage + +Forms are really boring to code... you want to get onto the good stuff as fast as possible. + +This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord @belongs_to@-association), followed by a submit button: + +
+  <% semantic_form_for @user do |form| %>
+    <%= form.inputs %>
+    <%= form.buttons %>
+  <% end %>
+
+ +If you want to specify the order of the fields, skip some of the fields or even add in fields that Formtastic couldn't detect, you can pass in a list of field names to @inputs@ and list of button names to @buttons@: + +
+  <% semantic_form_for @user do |form| %>
+    <%= form.inputs :title, :body, :section, :categories, :created_at %>
+    <%= form.buttons :commit %>
+  <% end %>
+
+ +If you want control over the input type Formtastic uses for each field, you can expand the @inputs@ and @buttons@ blocks. This specifies the @:section@ input should be a set of radio buttons (rather than the default select box), and that the @:created_at@ field should be a string (rather than the default datetime selects): + +
+  <% semantic_form_for @post do |form| %>
+    <% form.inputs do %>
+      <%= form.input :title %>
+      <%= form.input :body %>
+      <%= form.input :section, :as => :radio %>
+      <%= form.input :categories %>
+      <%= form.input :created_at, :as => :string %>
+    <% end %>
+    <% form.buttons do %>
+      <%= form.commit_button %>
+    <% end %>
+  <% end %>
+
+ +If you want to customize the label text, or render some hint text below the field, specify which fields are required/optional, or break the form into two fieldsets, the DSL is pretty comprehensive: + +
+  <% semantic_form_for @post do |form| %>
+    <% form.inputs "Basic", :id => "basic" do %>
+      <%= form.input :title %>
+      <%= form.input :body %>
+    <% end %>
+    <% form.inputs :name => "Advanced Options", :id => "advanced" do %>
+      <%= form.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
+      <%= form.input :section, :as => :radio %>
+      <%= form.input :user, :label => "Author", :label_method => :full_name,  %>
+      <%= form.input :categories, :required => false %>
+      <%= form.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
+    <% end %>
+    <% form.buttons do %>
+      <%= form.commit_button %>
+    <% end %>
+  <% end %>
+
+ +Nested forms (Rails 2.3) are also supported (don't forget your models need to be setup correctly with accepts_nested_attributes_for – search the Rails docs). You can do it in the Rails way: + +
+  <% semantic_form_for @post do |form| %>
+    <%= form.inputs :title, :body, :created_at %>
+    <% form.semantic_fields_for :author do |author| %>
+      <%= author.inputs :first_name, :last_name, :name => "Author" %>
+    <% end %>
+    <%= form.buttons %>
+  <% end %>
+
+ +Or the Formtastic way with the @:for@ option: + +
+  <% semantic_form_for @post do |form| %>
+    <%= form.inputs :title, :body, :created_at %>
+    <%= form.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
+    <%= form.buttons %>
+  <% end %>
+
+ +When working in has many association, you can even supply @"%i"@ in your fieldset name that it will be properly interpolated with the child index. For example: + +
+  <% semantic_form_for @post do |form| %>
+    <%= form.inputs %>
+    <%= form.inputs :name => 'Category #%i', :for => :categories %>
+    <%= form.buttons %>
+  <% end %>
+
+ + +Customize HTML attributes for any input using the @:input_html@ option. Typically this is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like "autogrow":http://plugins.jquery.com/project/autogrow textareas: + +
+  <% semantic_form_for @post do |form| %>
+    <%= form.input :title,      :input_html => { :size => 60 } %>
+    <%= form.input :body,       :input_html => { :class => 'autogrow' } %>
+    <%= form.input :created_at, :input_html => { :disabled => true } %>
+    <%= form.buttons %>
+  <% end %>
+
+ +The same can be done for buttons with the @:button_html@ option: + +
+  <% semantic_form_for @post do |form| %>
+    ...
+    <% form.buttons do %>
+      <%= form.commit_button :button_html => { :class => "primary" } %>
+    <% end %>
+  <% end %>
+
+ +Customize the HTML attributes for the @
  • @ wrapper around every input with the @:wrapper_html@ option hash. There's one special key in the hash (@:class@), which will actually _append_ your string of classes to the existing classes provided by Formtastic (like @"required string error"@) + +
    +  <% semantic_form_for @post do |form| %>
    +    <%= form.input :title, :wrapper_html => { :class => "important" } %>
    +    <%= form.input :body %>
    +    <%= form.input :description, :wrapper_html => { :style => "display:none;" } %>
    +    ...
    +  <% end %>
    +
    + +Many inputs provide a collection of options to choose from (like @:select@, @:radio@, @:check_boxes@, @:boolean@). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the @:collection@ option is what you want. You can pass in an Array of objects, an array of Strings, a Hash... Throw almost anything at it! Examples: + +
    +  f.input :authors, :as => :check_boxes, :collection => User.find(:all, :order => "last_name ASC")
    +  f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
    +  f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
    +  f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
    +  f.input :author,  :as => :select,      :collection => Author.find(:all)
    +  f.input :author,  :as => :select,      :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
    +  f.input :author,  :as => :select,      :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
    +  f.input :author,  :as => :radio,       :collection => User.find(:all)
    +  f.input :author,  :as => :radio,       :collection => [@justin, @kate]
    +  f.input :author,  :as => :radio,       :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
    +  f.input :author,  :as => :radio,       :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
    +  f.input :admin,   :as => :radio,       :collection => ["Yes!", "No"]
    +
    + + +h2. The Available Inputs + +The Formtastic input types: + +* @:select@ - a select menu. Default for ActiveRecord associations: @belongs_to@, @has_many@, and @has_and_belongs_to_many@. +* @:check_boxes@ - a set of check_box inputs. Alternative to @:select@ for ActiveRecord-associations: @has_many@, and @has_and_belongs_to_many@. +* @:radio@ - a set of radio inputs. Alternative to @:select@ for ActiveRecord-associations: @belongs_to@. +* @:time_zone@ - a select input. Default for column types: @:string@ with name matching @"time_zone"@. +* @:password@ - a password input. Default for column types: @:string@ with name matching @"password"@. +* @:text@ - a textarea. Default for column types: @:text@. +* @:date@ - a date select. Default for column types: @:date@. +* @:datetime@ - a date and time select. Default for column types: @:datetime@ and @:timestamp@. +* @:time@ - a time select. Default for column types: @:time@. +* @:boolean@ - a checkbox. Default for column types: @:boolean@. +* @:string@ - a text field. Default for column types: @:string@. +* @:numeric@ - a text field (just like string). Default for column types: @:integer@, @:float@, and @:decimal@. +* @:file@ - a file field. Default for file-attachment attributes matching: "paperclip":http://github.com/thoughtbot/paperclip or "attachment_fu":http://github.com/technoweenie/attachment_fu. +* @:country@ - a select menu of country names. Default for column types: :string with name @"country"@ - requires a *country_select* plugin to be installed. +* @:hidden@ - a hidden field. Creates a hidden field (added for compatibility). + +The comments in the code are pretty good for each of these (what it does, what the output is, what the options are, etc.) so go check it out. + + +h2. Delegation for label lookups + +Formtastic decides which label to use in the following order: + +
    +  1. :label             # :label => "Choose Title"
    +  2. Formtastic i18n    # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
    +  3. Activerecord i18n  # if localization file found for the given attribute
    +  4. label_str_method   # if nothing provided this defaults to :humanize but can be set to a custom method 
    +
    + +h2. Internationalization (I18n) + +h3. Basic Localization + +Formtastic got some neat I18n-features. ActiveRecord object names and attributes are, by default, taken from calling @@object.human_name@ and @@object.human_attribute_name(attr)@ respectively. There are a few words specific to Formtastic that can be translated. See @lib/locale/en.yml@ for more information. + +Basic localization (labels only, with ActiveRecord): + +
    +  <% semantic_form_for @post do |form| %>
    +    <%= form.input :title %>        # => :label => I18n.t('activerecord.attributes.user.title')    or 'Title'
    +    <%= form.input :body %>         # => :label => I18n.t('activerecord.attributes.user.body')     or 'Body'
    +    <%= form.input :section %>      # => :label => I18n.t('activerecord.attributes.user.section')  or 'Section'
    +  <% end %>
    +
    + +*Note:* This is perfectly fine if you just want your labels/attributes and/or models to be translated using *ActiveRecord I18n attribute translations*, and you don't use input hints and legends. But what if you do? And what if you don't want same labels in all forms? + +h3. Enhanced Localization (Formtastic I18n API) + +Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the I18n API for more advanced usage. Your forms can now be DRYer and more flexible than ever, and still fully localized. This is how: + +*1. Enable I18n lookups by default (@config/initializers/formtastic.rb@):* + +
    +  Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
    +
    + +*2. Add some cool label-translations/variants (@config/locale/en.yml@):* + +
    +  en:
    +    formtastic:
    +      titles:
    +        post_details: "Post details"
    +      labels:
    +        post:
    +          title: "Choose a title..."
    +          body: "Write something..."
    +          edit:
    +            title: "Edit title"
    +      hints:
    +        post:
    +          title: "Choose a good title for you post."
    +          body: "Write something inspiring here."
    +      actions:
    +        create: "Create my {{model}}"
    +        update: "Save changes"
    +        dummie: "Launch!"
    +
    + +*Note:* We are using English here still, but you get the point. + +*3. ...and now you'll get:* + +
    +  <% semantic_form_for Post.new do |form| %>
    +    <% form.inputs do %>
    +      <%= form.input :title %>      # => :label => "Choose a title...", :hint => "Choose a good title for you post."
    +      <%= form.input :body %>       # => :label => "Write something...", :hint => "Write something inspiring here."
    +      <%= form.input :section %>    # => :label => I18n.t('activerecord.attributes.user.section')  or 'Section'
    +    <% end %>
    +    <% form.buttons do %>
    +      <%= form.commit_button %>     # => "Create my {{model}}"
    +    <% end %>
    +  <% end %>
    +
    + +*4. Localized titles (a.k.a. legends):* + +_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._ + +
    +  <% semantic_form_for @post do |form| %>
    +    <% form.inputs :post_details do %>   # => :title => "Post details"
    +      # ...
    +    <% end %>
    +    # ...
    +<% end %>
    +
    + +*5. Override I18n settings:* + +
    +  <% semantic_form_for @post do |form| %>
    +    <% form.inputs do %>
    +      <%= form.input :title %>      # => :label => "Choose a title...", :hint => "Choose a good title for you post."
    +      <%= form.input :body, :hint => false %>                 # => :label => "Write something..."
    +      <%= form.input :section, :label => 'Some section' %>    # => :label => 'Some section'
    +    <% end %>
    +    <% form.buttons do %>
    +      <%= form.commit_button :dummie %>     # => "Launch!"
    +    <% end %>
    +  <% end %>
    +
    + +If I18n-lookups is disabled, i.e.: + +
    +  Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
    +
    + +...then you can enable I18n within the forms instead: + +
    +  <% semantic_form_for @post do |form| %>
    +    <% form.inputs do %>
    +      <%= form.input :title, :label => true %>      # => :label => "Choose a title..."
    +      <%= form.input :body, :label => true %>       # => :label => "Write something..."
    +      <%= form.input :section, :label => true %>    # => :label => I18n.t('activerecord.attributes.user.section')  or 'Section'
    +    <% end %>
    +    <% form.buttons do %>
    +      <%= form.commit_button true %>                # => "Save changes" (if we are in edit that is...)
    +    <% end %>
    +  <% end %>
    +
    + +*6. Advanced I18n lookups* + +For more flexible forms; Formtastic find translations using a bottom-up approach taking the following variables in account: + +* @MODEL@, e.g. "post" +* @ACTION@, e.g. "edit" +* @KEY/ATTRIBUTE@, e.g. "title", :my_custom_key, ... + +...in the following order: + +1. @formtastic.{titles,labels,hints,actions}.MODEL.ACTION.ATTRIBUTE@ - by model and action +2. @formtastic.{titles,labels,hints,actions}.MODEL.ATTRIBUTE@ - by model +3. @formtastic.{titles,labels,hints,actions}.ATTRIBUTE@ - global default + +...which means that you can define translations like this: + +
    +  en:
    +    formtastic:
    +      labels:
    +        title: "Title"  # Default global value
    +        article:
    +          body: "Article content"
    +        post:
    +          new:
    +            title: "Choose a title..."
    +            body: "Write something..."
    +          edit:
    +            title: "Edit title"
    +            body: "Edit body"
    +
    + +Values for @labels@/@hints@/@actions@ are can take values: @String@ (explicit value), @Symbol@ (i18n-lookup-key relative to the current "type", e.g. actions:), @true@ (force I18n lookup), @false@ (force no I18n lookup). Titles (legends) can only take: @String@ and @Symbol@ - true/false have no meaning. + + +h2. Semantic errors + +You can show errors on base (by default) and any other attribute just passing it name to semantic_errors method: + +
    +  <% semantic_form_for @post do |form| %>
    +    <%= form.semantic_errors :state %>
    +  <% end %>
    +
    + + +h2. ValidationReflection plugin + +If you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin installed, you won't have to specify the @:required@ option (it checks the validations on the model instead). + + +h2. Configuration + +Run @./script/generate formtastic@ to copy a commented out config file into @config/initializers/formtastic.rb@. You can "view the configuration file on GitHub":http://github.com/justinfrench/formtastic/blob/master/generators/formtastic/templates/formtastic.rb + + +h2. Form Generator + +There's a Formtastic form code generator to make your transition to Formtastic easier. All you have to do is to *specify an existing model name*, and optionally specify view template framework (ERB/HAML), and you are good to go. *Note:* This won't overwrite any of you stuff. This is how you use it: + +*Alt. 1: Generate in terminal:* + +
    +$ ./script/generate form Post
    +# ---------------------------------------------------------
    +#  GENERATED FORMTASTIC CODE
    +# ---------------------------------------------------------
    +
    +<% f.inputs do %>
    +  <%= f.input :title, :label => 'Title' %>
    +  <%= f.input :body, :label => 'Body' %>
    +  <%= f.input :published, :label => 'Published' %>
    +<% end %>
    +
    +# ---------------------------------------------------------
    + Copied to clipboard - just paste it!
    +
    + +*Alt. 2: Generate partial:* + +
    +$ ./script/generate form Post --partial
    +      exists  app/views/posts
    +      create  app/views/posts/_form.html.erb
    +
    + +To generate *HAML* markup, just add the @--haml@ as argument: + +
    +$ ./script/generate form Post --haml
    +      exists  app/views/admin/posts
    +      create  app/views/admin/posts/_form.html.haml
    +
    + +To specify the controller in a namespace (eg admin/posts instead of posts), use the --controller argument: + +
    +$ ./script/generate form Post --partial --controller admin/posts
    +      exists  app/views/admin/posts
    +      create  app/views/admin/posts/_form.html.erb
    +
    + + +h2. Custom Inputs + +If you want to add your own input types to encapsulate your own logic or interface patterns, you can do so by subclassing SemanticFormBuilder and configuring Formtastic to use your custom builder class. + +@Formtastic::SemanticFormHelper.builder = MyCustomBuilder@ + + + + +h2. Status + +Formtastic has been in active development for about a year. We've just recently jumped to an 0.9 version number, signaling that we consider this a 1.0 release candidate, and that the API won't change significantly for the 1.x series. + + +h2. Dependencies + +There are none, but... + +* if you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin is installed, you won't have to specify the @:required@ option (it checks the validations on the model instead). +* if you want to use the @:country@ input, you'll need to install the "iso-3166-country-select plugin":http://github.com/rails/iso-3166-country-select (or any other country_select plugin with the same API). +* "rspec":http://github.com/dchelimsky/rspec/, "rspec_hpricot_matchers":http://rubyforge.org/projects/rspec-hpricot/ and "rcov":http://github.com/relevance/rcov gems (plus any of their own dependencies) are required for the test suite. + + +h2. Compatibility + +I'm only testing Formtastic with the latest Rails 2.4.x stable release, and it should be fine under Rails 2.3.x as well (including nested forms). Patches are welcome to allow backwards compatibility, but I don't have the energy! + +h2. Got TextMate? + +Well...there's a TextMate-bundle in town, dedicated to make usage of Formtastic in the "TextMate":http://macromates.com/ editor even more of a breeze: + +"Formtastic.tmbundle":http://github.com/grimen/formtastic_tmbundle + + +h2. How to contribute + +*Before you send a pull request*, please ensure that you provide appropriate spec/test coverage and ensure the documentation is up-to-date. Bonus points if you perform your changes in a clean topic branch rather than master. + +Please also keep your commits *atomic* so that they are more likely to apply cleanly. That means that each commit should contain the smallest possible logical change. Don't commit two features at once, don't update the gemspec at the same time you add a feature, don't fix a whole bunch of whitespace in a file at the same time you change a few lines, etc, etc. + +For significant changes, you may wish to discuss your idea on the Formtastic Google group before coding to ensure that your change is likely to be accepted. Formtastic relies heavily on i18n, so if you're unsure of the impact this has on your changes, please discuss them with the group. + + +h2. Maintainers & Contributors + +Formtastic is maintained by "Justin French":http://justinfrench.com, "José Valim":http://github.com/josevalim and "Jonas Grimfelt":http://github.com/grimen, but it wouldn't be as awesome as it is today without help from over 40 contributors. + +@git shortlog -n -s --no-merges@ + + +h2. Google Group + +Please join the "Formtastic Google Group":http://groups.google.com.au/group/formtastic, especially if you'd like to talk about a new feature, or report a bug. + + +h2. Project Info + +Formtastic is hosted on Github: "http://github.com/justinfrench/formtastic":http://github.com/justinfrench/formtastic, where your contributions, forkings, comments and feedback are greatly welcomed. + + +Copyright (c) 2007-2008 Justin French, released under the MIT license. diff --git a/vendor/gems/formtastic-0.9.10/Rakefile b/vendor/gems/formtastic-0.9.10/Rakefile new file mode 100644 index 0000000..6a57cbc --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/Rakefile @@ -0,0 +1,101 @@ +# coding: utf-8 +require 'rake' +require 'rake/rdoctask' + +begin + require 'spec/rake/spectask' +rescue LoadError + begin + gem 'rspec-rails', '>= 1.0.0' + require 'spec/rake/spectask' + rescue LoadError + puts "[formtastic:] RSpec - or one of it's dependencies - is not available. Install it with: sudo gem install rspec-rails" + end +end + +begin + GEM = "formtastic" + AUTHOR = "Justin French" + EMAIL = "justin@indent.com.au" + SUMMARY = "A Rails form builder plugin/gem with semantically rich and accessible markup" + HOMEPAGE = "http://github.com/justinfrench/formtastic/tree/master" + INSTALL_MESSAGE = %q{ + ======================================================================== + Thanks for installing Formtastic! + ------------------------------------------------------------------------ + You can now (optionally) run the generator to copy some stylesheets and + a config initializer into your application: + ./script/generate formtastic + + To generate some semantic form markup for your existing models, just run: + ./script/generate form MODEL_NAME + + Find out more and get involved: + http://github.com/justinfrench/formtastic + http://groups.google.com.au/group/formtastic + ======================================================================== + } + + gem 'jeweler', '>= 1.0.0' + require 'jeweler' + + Jeweler::Tasks.new do |s| + s.name = GEM + s.summary = SUMMARY + s.email = EMAIL + s.homepage = HOMEPAGE + s.description = SUMMARY + s.author = AUTHOR + s.post_install_message = INSTALL_MESSAGE + + s.require_path = 'lib' + s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,generators,spec}/**/*") + + # Runtime dependencies: When installing Formtastic these will be checked if they are installed. + # Will be offered to install these if they are not already installed. + s.add_dependency 'activesupport', '>= 2.3.0' + s.add_dependency 'actionpack', '>= 2.3.0' + + # Development dependencies. Not installed by default. + # Install with: sudo gem install formtastic --development + s.add_development_dependency 'rspec-rails', '>= 1.2.6' + s.add_development_dependency 'rspec_tag_matchers', '>= 1.0.0' + end + + Jeweler::GemcutterTasks.new +rescue LoadError + puts "[formtastic:] Jeweler - or one of its dependencies - is not available. Install it with: sudo gem install jeweler -s http://gemcutter.org" +end + +desc 'Default: run unit specs.' +task :default => :spec + +desc 'Generate documentation for the formtastic plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'Formtastic' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README.textile') + rdoc.rdoc_files.include('lib/**/*.rb') +end + +if defined?(Spec) + desc 'Test the formtastic plugin.' + Spec::Rake::SpecTask.new('spec') do |t| + t.spec_files = FileList['spec/**/*_spec.rb'] + t.spec_opts = ["-c"] + end + + desc 'Test the formtastic plugin with specdoc formatting and colors' + Spec::Rake::SpecTask.new('specdoc') do |t| + t.spec_files = FileList['spec/**/*_spec.rb'] + t.spec_opts = ["--format specdoc", "-c"] + end + + desc "Run all examples with RCov" + Spec::Rake::SpecTask.new('examples_with_rcov') do |t| + t.spec_files = FileList['spec/**/*_spec.rb'] + t.rcov = true + t.rcov_opts = ['--exclude', 'spec,Library'] + end +end diff --git a/vendor/plugins/formtastic/generators/form/USAGE b/vendor/gems/formtastic-0.9.10/generators/form/USAGE similarity index 52% rename from vendor/plugins/formtastic/generators/form/USAGE rename to vendor/gems/formtastic-0.9.10/generators/form/USAGE index 03fc8b2..09e6bb4 100644 --- a/vendor/plugins/formtastic/generators/form/USAGE +++ b/vendor/gems/formtastic-0.9.10/generators/form/USAGE @@ -8,8 +8,9 @@ DESCRIPTION ExistingModelName - The name of an existing model for which the generator should generate form code. Options: - --haml Generate HAML instead of ERB. - --partial Generate a form partial in the model views path, i.e. "_form.html.erb" or _form.html.haml". + --haml Generate HAML instead of ERB. + --partial Generate a form partial in the model views path, i.e. "_form.html.erb" or _form.html.haml". + --controller PATH Generate for custom controller/view path - in case model and controller namespace is different, i.e. "admin/posts". EXAMPLE ./script/generate form ExistingModelName [--haml] [--partial] \ No newline at end of file diff --git a/vendor/plugins/formtastic/generators/form/form_generator.rb b/vendor/gems/formtastic-0.9.10/generators/form/form_generator.rb similarity index 87% rename from vendor/plugins/formtastic/generators/form/form_generator.rb rename to vendor/gems/formtastic-0.9.10/generators/form/form_generator.rb index 84456c2..96ba3ce 100644 --- a/vendor/plugins/formtastic/generators/form/form_generator.rb +++ b/vendor/gems/formtastic-0.9.10/generators/form/form_generator.rb @@ -39,10 +39,11 @@ def initialize(runtime_args, runtime_options = {}) def manifest record do |m| if options[:partial] + controller_and_view_path = options[:controller] || File.join(controller_class_path, controller_file_name) # Ensure directory exists. - m.directory File.join(VIEWS_PATH, controller_class_path, controller_file_name) + m.directory File.join(VIEWS_PATH, controller_and_view_path) # Create a form partial for the model as "_form" in it's views path. - m.template "view__form.html.#{template_type}", File.join(VIEWS_PATH, controller_file_name, "_form.html.#{template_type}") + m.template "view__form.html.#{template_type}", File.join(VIEWS_PATH, controller_and_view_path, "_form.html.#{template_type}") else # Load template file, and render without saving to file template = File.read(File.join(source_root, "view__form.html.#{template_type}")) @@ -85,7 +86,7 @@ def save_to_clipboard(data) # Add additional model attributes if specified in args - probably not that common scenario. def attributes - # Get columns for the requested model + # Get columns for the requested model. existing_attributes = @class_name.constantize.content_columns.reject { |column| IGNORED_COLUMNS.include?(column.name.to_sym) } @args = super + existing_attributes end @@ -105,6 +106,11 @@ def add_options!(opt) "Save generated output directly to a form partial (app/views/{resource}/_form.html.*).") do |v| options[:partial] = v end + + opt.on('--controller CONTROLLER_PATH', + "Specify a non-standard controller for the specified model (e.g. admin/posts).") do |v| + options[:controller] = v if v.present? + end end def banner diff --git a/vendor/gems/formtastic-0.9.10/generators/form/templates/view__form.html.erb b/vendor/gems/formtastic-0.9.10/generators/form/templates/view__form.html.erb new file mode 100644 index 0000000..21daaab --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/generators/form/templates/view__form.html.erb @@ -0,0 +1,5 @@ +<%% f.inputs do %> +<% attributes.each do |attribute| -%> + <%%= f.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>' %> +<% end -%> +<%% end %> \ No newline at end of file diff --git a/vendor/gems/formtastic-0.9.10/generators/form/templates/view__form.html.haml b/vendor/gems/formtastic-0.9.10/generators/form/templates/view__form.html.haml new file mode 100644 index 0000000..5e06093 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/generators/form/templates/view__form.html.haml @@ -0,0 +1,4 @@ +- f.inputs do +<% attributes.each do |attribute| -%> + = f.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>' +<% end -%> \ No newline at end of file diff --git a/vendor/plugins/formtastic/generators/formtastic/formtastic_generator.rb b/vendor/gems/formtastic-0.9.10/generators/formtastic/formtastic_generator.rb similarity index 100% rename from vendor/plugins/formtastic/generators/formtastic/formtastic_generator.rb rename to vendor/gems/formtastic-0.9.10/generators/formtastic/formtastic_generator.rb diff --git a/vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic.css b/vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic.css new file mode 100644 index 0000000..a64a164 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic.css @@ -0,0 +1,146 @@ +/* ------------------------------------------------------------------------------------------------- + +It's *strongly* suggested that you don't modify this file. Instead, load a new stylesheet after +this one in your layouts (eg formtastic_changes.css) and override the styles to suit your needs. +This will allow you to update formtastic.css with new releases without clobbering your own changes. + +This stylesheet forms part of the Formtastic Rails Plugin +(c) 2008 Justin French + +--------------------------------------------------------------------------------------------------*/ + + +/* NORMALIZE AND RESET - obviously inspired by Yahoo's reset.css, but scoped to just form.formtastic +--------------------------------------------------------------------------------------------------*/ +form.formtastic, form.formtastic ul, form.formtastic ol, form.formtastic li, form.formtastic fieldset, form.formtastic legend, form.formtastic input, form.formtastic textarea, form.formtastic select, form.formtastic p { margin:0; padding:0; } +form.formtastic fieldset { border:0; } +form.formtastic em, form.formtastic strong { font-style:normal; font-weight:normal; } +form.formtastic ol, form.formtastic ul { list-style:none; } +form.formtastic abbr, form.formtastic acronym { border:0; font-variant:normal; } +form.formtastic input, form.formtastic textarea, form.formtastic select { font-family:inherit; font-size:inherit; font-weight:inherit; } +form.formtastic input, form.formtastic textarea, form.formtastic select { font-size:100%; } +form.formtastic legend { white-space:normal; color:#000; } + + +/* SEMANTIC ERRORS +--------------------------------------------------------------------------------------------------*/ +form.formtastic ul.errors { color:#cc0000; margin:0.5em 0 1.5em 25%; list-style:square; } +form.formtastic ul.errors li { padding:0; border:none; display:list-item; } + + +/* FIELDSETS & LISTS +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset { } +form.formtastic fieldset.inputs { } +form.formtastic fieldset.buttons { padding-left:25%; } +form.formtastic fieldset ol { } +form.formtastic fieldset.buttons li { float:left; padding-right:0.5em; } + +/* clearfixing the fieldsets */ +form.formtastic fieldset { display: inline-block; } +form.formtastic fieldset:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } +html[xmlns] form.formtastic fieldset { display: block; } +* html form.formtastic fieldset { height: 1%; } + + +/* INPUT LIs +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li { margin-bottom:1.5em; } + +/* clearfixing the li's */ +form.formtastic fieldset > ol > li { display: inline-block; } +form.formtastic fieldset > ol > li:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } +html[xmlns] form.formtastic fieldset > ol > li { display: block; } +* html form.formtastic fieldset > ol > li { height: 1%; } + +form.formtastic fieldset > ol > li.required { } +form.formtastic fieldset > ol > li.optional { } +form.formtastic fieldset > ol > li.error { } + + +/* LABELS +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li label { display:block; width:25%; float:left; padding-top:.2em; } +form.formtastic fieldset > ol > li > li label { line-height:100%; padding-top:0; } +form.formtastic fieldset > ol > li > li label input { line-height:100%; vertical-align:middle; margin-top:-0.1em;} + + +/* NESTED FIELDSETS AND LEGENDS (radio, check boxes and date/time inputs use nested fieldsets) +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li fieldset { position:relative; } +form.formtastic fieldset > ol > li fieldset legend { position:absolute; width:95%; padding-top:0.1em; left: 0px; } +form.formtastic fieldset > ol > li fieldset legend span { position:absolute; } +form.formtastic fieldset > ol > li fieldset legend.label label { position:absolute; } +form.formtastic fieldset > ol > li fieldset ol { float:left; width:74%; margin:0; padding:0 0 0 25%; } +form.formtastic fieldset > ol > li fieldset ol li { padding:0; border:0; } + + +/* INLINE HINTS +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li p.inline-hints { color:#666; margin:0.5em 0 0 25%; } + + +/* INLINE ERRORS +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li p.inline-errors { color:#cc0000; margin:0.5em 0 0 25%; } +form.formtastic fieldset > ol > li ul.errors { color:#cc0000; margin:0.5em 0 0 25%; list-style:square; } +form.formtastic fieldset > ol > li ul.errors li { padding:0; border:none; display:list-item; } + + +/* STRING & NUMERIC OVERRIDES +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li.string input { max-width:74%; } +form.formtastic fieldset > ol > li.password input { max-width: 13em; } +form.formtastic fieldset > ol > li.numeric input { max-width:74%; } + + +/* TEXTAREA OVERRIDES +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li.text textarea { width:74%; } + + +/* HIDDEN OVERRIDES +The dual declarations are required because of our clearfix display hack on the LIs, which is more +specific than the more general rule below. TODO: Revist the clearing hack and this rule. +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset ol li.hidden, +html[xmlns] form.formtastic fieldset ol li.hidden { display:none; } + +/* BOOLEAN OVERRIDES +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li.boolean label { padding-left:25%; width:auto; } +form.formtastic fieldset > ol > li.boolean label input { margin:0 0.5em 0 0.2em; } + + +/* RADIO OVERRIDES +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li.radio { } +form.formtastic fieldset > ol > li.radio fieldset ol { margin-bottom:-0.6em; } +form.formtastic fieldset > ol > li.radio fieldset ol li { margin:0.1em 0 0.5em 0; } +form.formtastic fieldset > ol > li.radio fieldset ol li label { float:none; width:100%; } +form.formtastic fieldset > ol > li.radio fieldset ol li label input { margin-right:0.2em; } + + +/* CHECK BOXES (COLLECTION) OVERRIDES +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li.check_boxes { } +form.formtastic fieldset > ol > li.check_boxes fieldset ol { margin-bottom:-0.6em; } +form.formtastic fieldset > ol > li.check_boxes fieldset ol li { margin:0.1em 0 0.5em 0; } +form.formtastic fieldset > ol > li.check_boxes fieldset ol li label { float:none; width:100%; } +form.formtastic fieldset > ol > li.check_boxes fieldset ol li label input { margin-right:0.2em; } + + + +/* DATE & TIME OVERRIDES +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset > ol > li.date fieldset ol li, +form.formtastic fieldset > ol > li.time fieldset ol li, +form.formtastic fieldset > ol > li.datetime fieldset ol li { float:left; width:auto; margin:0 .3em 0 0; } + +form.formtastic fieldset > ol > li.date fieldset ol li label, +form.formtastic fieldset > ol > li.time fieldset ol li label, +form.formtastic fieldset > ol > li.datetime fieldset ol li label { display:none; } + +form.formtastic fieldset > ol > li.date fieldset ol li label input, +form.formtastic fieldset > ol > li.time fieldset ol li label input, +form.formtastic fieldset > ol > li.datetime fieldset ol li label input { display:inline; margin:0; padding:0; } diff --git a/vendor/plugins/formtastic/generators/formtastic/templates/formtastic.rb b/vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic.rb similarity index 86% rename from vendor/plugins/formtastic/generators/formtastic/templates/formtastic.rb rename to vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic.rb index 829b255..bb710ef 100644 --- a/vendor/plugins/formtastic/generators/formtastic/templates/formtastic.rb +++ b/vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic.rb @@ -1,6 +1,9 @@ # Set the default text field size when input is a string. Default is 50. # Formtastic::SemanticFormBuilder.default_text_field_size = 50 +# Set the default text area height when input is a text. Default is 20. +# Formtastic::SemanticFormBuilder.default_text_area_height = 5 + # Should all fields be considered "required" by default? # Defaults to true, see ValidationReflection notes below. # Formtastic::SemanticFormBuilder.all_fields_required_by_default = true @@ -25,7 +28,7 @@ # Formtastic::SemanticFormBuilder.inline_errors = :sentence # Set the method to call on label text to transform or format it for human-friendly -# reading when formtastic is user without object. Defaults to :humanize. +# reading when formtastic is used without object. Defaults to :humanize. # Formtastic::SemanticFormBuilder.label_str_method = :humanize # Set the array of methods to try calling on parent objects in :select and :radio inputs @@ -45,3 +48,7 @@ # Default value: false. Overridden for specific fields by setting value to true, # i.e. :label => true, or :hint => true (or opposite depending on initialized value) # Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false + +# You can add custom inputs or override parts of Formtastic by subclassing SemanticFormBuilder and +# specifying that class here. Defaults to SemanticFormBuilder. +# Formtastic::SemanticFormHelper.builder = MyCustomBuilder diff --git a/vendor/plugins/formtastic/generators/formtastic/templates/formtastic_changes.css b/vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic_changes.css similarity index 67% rename from vendor/plugins/formtastic/generators/formtastic/templates/formtastic_changes.css rename to vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic_changes.css index 608d428..8a044fa 100644 --- a/vendor/plugins/formtastic/generators/formtastic/templates/formtastic_changes.css +++ b/vendor/gems/formtastic-0.9.10/generators/formtastic/templates/formtastic_changes.css @@ -5,6 +5,10 @@ This will allow you to update formtastic.css with new releases without clobberin For example, to make the inline hint paragraphs a little darker in color than the standard #666: -form.formtastic fieldset ol li p.inline-hints { color:#333; } +form.formtastic fieldset > ol > li p.inline-hints { color:#333; } + +HINT: +The following style may be *conditionally* included for improved support on older versions of IE(<8) +form.formtastic fieldset ol li fieldset legend { margin-left: -6px;} --------------------------------------------------------------------------------------------------*/ diff --git a/vendor/plugins/formtastic/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb b/vendor/gems/formtastic-0.9.10/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb similarity index 100% rename from vendor/plugins/formtastic/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb rename to vendor/gems/formtastic-0.9.10/generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb diff --git a/vendor/gems/formtastic-0.9.10/lib/formtastic.rb b/vendor/gems/formtastic-0.9.10/lib/formtastic.rb new file mode 100644 index 0000000..cffbfc6 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/lib/formtastic.rb @@ -0,0 +1,1749 @@ +# coding: utf-8 +require File.join(File.dirname(__FILE__), *%w[formtastic i18n]) +require File.join(File.dirname(__FILE__), *%w[formtastic util]) + +module Formtastic #:nodoc: + + class SemanticFormBuilder < ActionView::Helpers::FormBuilder + + @@default_text_field_size = 50 + @@default_text_area_height = 20 + @@all_fields_required_by_default = true + @@include_blank_for_select_by_default = true + @@required_string = proc { ::Formtastic::Util.html_safe(%{*}) } + @@optional_string = '' + @@inline_errors = :sentence + @@label_str_method = :humanize + @@collection_label_methods = %w[to_label display_name full_name name title username login value to_s] + @@inline_order = [ :input, :hints, :errors ] + @@file_methods = [ :file?, :public_filename, :filename ] + @@priority_countries = ["Australia", "Canada", "United Kingdom", "United States"] + @@i18n_lookups_by_default = false + @@default_commit_button_accesskey = nil + + cattr_accessor :default_text_field_size, :default_text_area_height, :all_fields_required_by_default, :include_blank_for_select_by_default, + :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] + + INLINE_ERROR_TYPES = [:sentence, :list, :first] + + attr_accessor :template + + # Returns a suitable form input for the given +method+, using the database column information + # and other factors (like the method name) to figure out what you probably want. + # + # Options: + # + # * :as - override the input type (eg force a :string to render as a :password field) + # * :label - use something other than the method name as the label text, when false no label is printed + # * :required - specify if the column is required (true) or not (false) + # * :hint - provide some text to hint or help the user provide the correct information for a field + # * :input_html - provide options that will be passed down to the generated input + # * :wrapper_html - provide options that will be passed down to the li wrapper + # + # Input Types: + # + # Most inputs map directly to one of ActiveRecord's column types by default (eg string_input), + # but there are a few special cases and some simplification (:integer, :float and :decimal + # columns all map to a single numeric_input, for example). + # + # * :select (a select menu for associations) - default to association names + # * :check_boxes (a set of check_box inputs for associations) - alternative to :select has_many and has_and_belongs_to_many associations + # * :radio (a set of radio inputs for associations) - alternative to :select belongs_to associations + # * :time_zone (a select menu with time zones) + # * :password (a password input) - default for :string column types with 'password' in the method name + # * :text (a textarea) - default for :text column types + # * :date (a date select) - default for :date column types + # * :datetime (a date and time select) - default for :datetime and :timestamp column types + # * :time (a time select) - default for :time column types + # * :boolean (a checkbox) - default for :boolean column types (you can also have booleans as :select and :radio) + # * :string (a text field) - default for :string column types + # * :numeric (a text field, like string) - default for :integer, :float and :decimal column types + # * :country (a select menu of country names) - requires a country_select plugin to be installed + # * :hidden (a hidden field) - creates a hidden field (added for compatibility) + # + # Example: + # + # <% semantic_form_for @employee do |form| %> + # <% form.inputs do -%> + # <%= form.input :secret, :value => "Hello" %> + # <%= form.input :name, :label => "Full Name" %> + # <%= form.input :manager_id, :as => :radio %> + # <%= form.input :hired_at, :as => :date, :label => "Date Hired" %> + # <%= form.input :phone, :required => false, :hint => "Eg: +1 555 1234" %> + # <% end %> + # <% end %> + # + def input(method, options = {}) + if options.key?(:selected) || options.key?(:checked) || options.key?(:default) + ::ActiveSupport::Deprecation.warn( + "The :selected, :checked (and :default) options are deprecated in Formtastic and will be removed from 1.0. " << + "Please set default values in your models (using an after_initialize callback) or in your controller set-up. " << + "See http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html for more information.", caller) + end + + options[:required] = method_required?(method) unless options.key?(:required) + options[:as] ||= default_input_type(method, options) + + html_class = [ options[:as], (options[:required] ? :required : :optional) ] + html_class << 'error' if @object && @object.respond_to?(:errors) && !@object.errors[method.to_sym].blank? + + wrapper_html = options.delete(:wrapper_html) || {} + wrapper_html[:id] ||= generate_html_id(method) + wrapper_html[:class] = (html_class << wrapper_html[:class]).flatten.compact.join(' ') + + if options[:input_html] && options[:input_html][:id] + options[:label_html] ||= {} + options[:label_html][:for] ||= options[:input_html][:id] + end + + input_parts = @@inline_order.dup + input_parts = input_parts - [:errors, :hints] if options[:as] == :hidden + + list_item_content = input_parts.map do |type| + send(:"inline_#{type}_for", method, options) + end.compact.join("\n") + + return template.content_tag(:li, Formtastic::Util.html_safe(list_item_content), wrapper_html) + end + + # Creates an input fieldset and ol tag wrapping for use around a set of inputs. It can be + # called either with a block (in which you can do the usual Rails form stuff, HTML, ERB, etc), + # or with a list of fields. These two examples are functionally equivalent: + # + # # With a block: + # <% semantic_form_for @post do |form| %> + # <% form.inputs do %> + # <%= form.input :title %> + # <%= form.input :body %> + # <% end %> + # <% end %> + # + # # With a list of fields: + # <% semantic_form_for @post do |form| %> + # <%= form.inputs :title, :body %> + # <% end %> + # + # # Output: + #
    + #
    + #
      + #
    1. ...
    2. + #
    3. ...
    4. + #
    + #
    + #
    + # + # === Quick Forms + # + # When called without a block or a field list, an input is rendered for each column in the + # model's database table, just like Rails' scaffolding. You'll obviously want more control + # than this in a production application, but it's a great way to get started, then come back + # later to customise the form with a field list or a block of inputs. Example: + # + # <% semantic_form_for @post do |form| %> + # <%= 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/: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| %> + # <% form.inputs :name => "Create a new post", :style => "border:1px;" do %> + # ... + # <% end %> + # <% end %> + # + # # With a list (the options must come after the field list): + # <% semantic_form_for @post do |form| %> + # <%= 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, + # use inputs: + # + # <% semantic_form_for @post do |f| %> + # <% f.inputs do %> + # <%= f.input :title %> + # <%= f.input :body %> + # <% end %> + # <% f.inputs :name => "Advanced", :id => "advanced" do %> + # <%= f.input :created_at %> + # <%= f.input :user_id, :label => "Author" %> + # <% end %> + # <% f.inputs "Extra" do %> + # <%= f.input :update_at %> + # <% end %> + # <% end %> + # + # # Output: + #
    + #
    + #
      + #
    1. ...
    2. + #
    3. ...
    4. + #
    + #
    + #
    + # Advanced + #
      + #
    1. ...
    2. + #
    3. ...
    4. + #
    + #
    + #
    + # Extra + #
      + #
    1. ...
    2. + #
    + #
    + #
    + # + # === Nested attributes + # + # As in Rails, you can use semantic_fields_for to nest attributes: + # + # <% semantic_form_for @post do |form| %> + # <%= form.inputs :title, :body %> + # + # <% form.semantic_fields_for :author, @bob do |author_form| %> + # <% author_form.inputs do %> + # <%= author_form.input :first_name, :required => false %> + # <%= author_form.input :last_name %> + # <% end %> + # <% end %> + # <% end %> + # + # But this does not look formtastic! This is equivalent: + # + # <% semantic_form_for @post do |form| %> + # <%= form.inputs :title, :body %> + # <% form.inputs :for => [ :author, @bob ] do |author_form| %> + # <%= author_form.input :first_name, :required => false %> + # <%= author_form.input :last_name %> + # <% end %> + # <% end %> + # + # And if you don't need to give options to your input call, you could do it + # in just one line: + # + # <% semantic_form_for @post do |form| %> + # <%= form.inputs :title, :body %> + # <%= form.inputs :first_name, :last_name, :for => @bob %> + # <% end %> + # + # Just remember that calling inputs generates a new fieldset to wrap your + # inputs. If you have two separate models, but, semantically, on the page + # they are part of the same fieldset, you should use semantic_fields_for + # instead (just as you would do with Rails' form builder). + # + def inputs(*args, &block) + title = field_set_title_from_args(*args) + html_options = args.extract_options! + html_options[:class] ||= "inputs" + html_options[:name] = title + + if html_options[:for] # Nested form + inputs_for_nested_attributes(*(args << html_options), &block) + elsif block_given? + field_set_and_list_wrapping(*(args << html_options), &block) + else + if @object && args.empty? + args = self.association_columns(:belongs_to) + args += self.content_columns + args -= RESERVED_COLUMNS + args.compact! + end + 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 + + # Creates a fieldset and ol tag wrapping for form buttons / actions as list items. + # See inputs documentation for a full example. The fieldset's default class attriute + # is set to "buttons". + # + # See inputs for html attributes and special options. + def buttons(*args, &block) + html_options = args.extract_options! + html_options[:class] ||= "buttons" + + if block_given? + field_set_and_list_wrapping(html_options, &block) + else + args = [:commit] if args.empty? + contents = args.map { |button_name| send(:"#{button_name}_button") } + field_set_and_list_wrapping(html_options, contents) + end + end + alias :button_field_set :buttons + + # Creates a submit input tag with the value "Save [model name]" (for existing records) or + # "Create [model name]" (for new records) by default: + # + # <%= form.commit_button %> => + # + # The value of the button text can be overridden: + # + # <%= form.commit_button "Go" %> => + # <%= form.commit_button :label => "Go" %> => + # + # And you can pass html atributes down to the input, with or without the button text: + # + # <%= form.commit_button "Go" %> => + # <%= form.commit_button :class => "pretty" %> => + # + def commit_button(*args) + options = args.extract_options! + text = options.delete(:label) || args.shift + + if @object && @object.respond_to?(:new_record?) + key = @object.new_record? ? :create : :update + + # Deal with some complications with ActiveRecord::Base.human_name and two name models (eg UserPost) + # ActiveRecord::Base.human_name falls back to ActiveRecord::Base.name.humanize ("Userpost") + # if there's no i18n, which is pretty crappy. In this circumstance we want to detect this + # fall back (human_name == name.humanize) and do our own thing name.underscore.humanize ("User Post") + object_human_name = @object.class.human_name # default is UserPost => "Userpost", but i18n may do better ("User post") + crappy_human_name = @object.class.name.humanize # UserPost => "Userpost" + decent_human_name = @object.class.name.underscore.humanize # UserPost => "User post" + object_name = (object_human_name == crappy_human_name) ? decent_human_name : object_human_name + else + key = :submit + object_name = @object_name.to_s.send(@@label_str_method) + end + + text = (self.localized_string(key, text, :action, :model => object_name) || + ::Formtastic::I18n.t(key, :model => object_name)) unless text.is_a?(::String) + + button_html = options.delete(:button_html) || {} + button_html.merge!(:class => [button_html[:class], key].compact.join(' ')) + element_class = ['commit', options.delete(:class)].compact.join(' ') # TODO: Add class reflecting on form action. + accesskey = (options.delete(:accesskey) || @@default_commit_button_accesskey) unless button_html.has_key?(:accesskey) + button_html = button_html.merge(:accesskey => accesskey) if accesskey + template.content_tag(:li, Formtastic::Util.html_safe(self.submit(text, button_html)), :class => element_class) + end + + # A thin wrapper around #fields_for to set :builder => Formtastic::SemanticFormBuilder + # for nesting forms: + # + # # Example: + # <% semantic_form_for @post do |post| %> + # <% post.semantic_fields_for :author do |author| %> + # <% author.inputs :name %> + # <% end %> + # <% end %> + # + # # Output: + #
    + #
    + #
      + #
    1. + #
    + #
    + #
    + # + def semantic_fields_for(record_or_name_or_array, *args, &block) + opts = args.extract_options! + opts[:builder] ||= Formtastic::SemanticFormHelper.builder + args.push(opts) + fields_for(record_or_name_or_array, *args, &block) + end + + # Generates the label for the input. It also accepts the same arguments as + # Rails label method. It has three options that are not supported by Rails + # label method: + # + # * :required - Appends an abbr tag if :required is true + # * :label - An alternative form to give the label content. Whenever label + # is false, a blank string is returned. + # * :input_name - Gives the input to match for. This is needed when you want to + # to call f.label :authors but it should match :author_ids. + # + # == Examples + # + # f.label :title # like in rails, except that it searches the label on I18n API too + # + # f.label :title, "Your post title" + # f.label :title, :label => "Your post title" # Added for formtastic API + # + # f.label :title, :required => true # Returns + # + def label(method, options_or_text=nil, options=nil) + if options_or_text.is_a?(Hash) + return "" if options_or_text[:label] == false + options = options_or_text + text = options.delete(:label) + else + text = options_or_text + options ||= {} + end + + text = localized_string(method, text, :label) || humanized_attribute_name(method) + text += required_or_optional_string(options.delete(:required)) + text = Formtastic::Util.html_safe(text) + + # special case for boolean (checkbox) labels, which have a nested input + if options.key?(:label_prefix_for_nested_input) + text = options.delete(:label_prefix_for_nested_input) + text + end + + input_name = options.delete(:input_name) || method + super(input_name, text, options) + end + + # Generates error messages for the given method. Errors can be shown as list, + # as sentence or just the first error can be displayed. If :none is set, no error is shown. + # + # This method is also aliased as errors_on, so you can call on your custom + # inputs as well: + # + # semantic_form_for :post do |f| + # f.text_field(:body) + # f.errors_on(:body) + # end + # + def inline_errors_for(method, options = nil) #:nodoc: + if render_inline_errors? + errors = [@object.errors[method.to_sym]] + errors << [@object.errors[association_primary_key(method)]] if association_macro_for_method(method) == :belongs_to + errors = errors.flatten.compact.uniq + send(:"error_#{@@inline_errors}", [*errors]) if errors.any? + else + nil + end + end + alias :errors_on :inline_errors_for + + # Generates error messages for given method names and for base. + # You can pass a hash with html options that will be added to ul tag + # + # == Examples + # + # f.semantic_errors # This will show only errors on base + # f.semantic_errors :state # This will show errors on base and state + # f.semantic_errors :state, :class => "awesome" # errors will be rendered in ul.awesome + # + def semantic_errors(*args) + html_options = args.extract_options! + full_errors = args.inject([]) do |array, method| + attribute = localized_string(method, method.to_sym, :label) || humanized_attribute_name(method) + errors = Array(@object.errors[method.to_sym]).to_sentence + errors.present? ? array << [attribute, errors].join(" ") : array ||= [] + end + full_errors << @object.errors.on_base + full_errors.flatten! + full_errors.compact! + return nil if full_errors.blank? + html_options[:class] ||= "errors" + template.content_tag(:ul, html_options) do + Formtastic::Util.html_safe(full_errors.map { |error| template.content_tag(:li, Formtastic::Util.html_safe(error)) }.join) + end + end + + protected + + def render_inline_errors? + @object && @object.respond_to?(:errors) && INLINE_ERROR_TYPES.include?(@@inline_errors) + end + + # Collects content columns (non-relation columns) for the current form object class. + # + def content_columns #:nodoc: + self.model_name.constantize.content_columns.collect { |c| c.name.to_sym }.compact rescue [] + end + + # Collects association columns (relation columns) for the current form object class. + # + def association_columns(*by_associations) #:nodoc: + 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 + + # Returns nil, or a symbol like :belongs_to or :has_many + def association_macro_for_method(method) #:nodoc: + reflection = self.reflection_for(method) + reflection.macro if reflection + end + + def association_primary_key(method) + reflection = self.reflection_for(method) + reflection.options[:foreign_key] if reflection && !reflection.options[:foreign_key].blank? + :"#{method}_id" + end + + # Prepare options to be sent to label + # + def options_for_label(options) #:nodoc: + options.slice(:label, :required).merge!(options.fetch(:label_html, {})) + end + + # Deals with :for option when it's supplied to inputs methods. Additional + # options to be passed down to :for should be supplied using :for_options + # key. + # + # It should raise an error if a block with arity zero is given. + # + def inputs_for_nested_attributes(*args, &block) #:nodoc: + options = args.extract_options! + args << options.merge!(:parent => { :builder => self, :for => options[:for] }) + + fields_for_block = if block_given? + raise ArgumentError, 'You gave :for option with a block to inputs method, ' << + 'but the block does not accept any argument.' if block.arity <= 0 + + proc { |f| return f.inputs(*args){ block.call(f) } } + else + proc { |f| return f.inputs(*args) } + end + + fields_for_args = [options.delete(:for), options.delete(:for_options) || {}].flatten + semantic_fields_for(*fields_for_args, &fields_for_block) + end + + # Remove any Formtastic-specific options before passing the down options. + # + def strip_formtastic_options(options) #:nodoc: + options.except(:value_method, :label_method, :collection, :required, :label, + :as, :hint, :input_html, :label_html, :value_as_class) + end + + # Determins if the attribute (eg :title) should be considered required or not. + # + # * if the :required option was provided in the options hash, the true/false value will be + # returned immediately, allowing the view to override any guesswork that follows: + # + # * if the :required option isn't provided in the options hash, and the ValidationReflection + # plugin is installed (http://github.com/redinger/validation_reflection), true is returned + # if the validates_presence_of macro has been used in the class for this attribute, or false + # otherwise. + # + # * if the :required option isn't provided, and the plugin isn't available, the value of the + # configuration option @@all_fields_required_by_default is used. + # + def method_required?(attribute) #:nodoc: + if @object && @object.class.respond_to?(:reflect_on_validations_for) + attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym + + @object.class.reflect_on_validations_for(attribute_sym).any? do |validation| + validation.macro == :validates_presence_of && + validation.name == attribute_sym && + (validation.options.present? ? options_require_validation?(validation.options) : true) + end + else + @@all_fields_required_by_default + end + end + + # Determines whether the given options evaluate to true + def options_require_validation?(options) #nodoc + if_condition = !options[:if].nil? + condition = if_condition ? options[:if] : options[:unless] + + condition = if condition.respond_to?(:call) + condition.call(@object) + elsif condition.is_a?(::Symbol) && @object.respond_to?(condition) + @object.send(condition) + else + condition + end + + if_condition ? !!condition : !condition + end + + def basic_input_helper(form_helper_method, type, method, options) #:nodoc: + html_options = options.delete(:input_html) || {} + html_options = default_string_options(method, type).merge(html_options) if [:numeric, :string, :password, :text].include?(type) + + self.label(method, options_for_label(options)) << + self.send(form_helper_method, method, html_options) + end + + # Outputs a label and standard Rails text field inside the wrapper. + def string_input(method, options) + basic_input_helper(:text_field, :string, method, options) + end + + # Outputs a label and standard Rails password field inside the wrapper. + def password_input(method, options) + basic_input_helper(:password_field, :password, method, options) + end + + # Outputs a label and standard Rails text field inside the wrapper. + def numeric_input(method, options) + basic_input_helper(:text_field, :numeric, method, options) + end + + # Ouputs a label and standard Rails text area inside the wrapper. + def text_input(method, options) + basic_input_helper(:text_area, :text, method, options) + end + + # Outputs a label and a standard Rails file field inside the wrapper. + def file_input(method, options) + basic_input_helper(:file_field, :file, method, options) + end + + # Outputs a hidden field inside the wrapper, which should be hidden with CSS. + # Additionals options can be given and will be sent straight to hidden input + # element. + # + def hidden_input(method, options) + options ||= {} + if options[:input_html].present? + options[:value] = options[:input_html][:value] if options[:input_html][:value].present? + end + self.hidden_field(method, strip_formtastic_options(options)) + end + + # Outputs a label and a select box containing options from the parent + # (belongs_to, has_many, has_and_belongs_to_many) association. If an association + # is has_many or has_and_belongs_to_many the select box will be set as multi-select + # and size = 5 + # + # Example (belongs_to): + # + # f.input :author + # + # + # + # + # Example (has_many): + # + # f.input :chapters + # + # + # + # + # Example (has_and_belongs_to_many): + # + # f.input :authors + # + # + # + # + # + # You can customize the options available in the select by passing in a collection (an Array or + # Hash) through the :collection option. If not provided, the choices are found by inferring the + # parent's class name from the method name and simply calling find(:all) on it + # (VehicleOwner.find(:all) in the example above). + # + # Examples: + # + # f.input :author, :collection => @authors + # f.input :author, :collection => Author.find(:all) + # f.input :author, :collection => [@justin, @kate] + # f.input :author, :collection => {@justin.name => @justin.id, @kate.name => @kate.id} + # f.input :author, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"] + # + # The :label_method option allows you to customize the text label inside each option tag two ways: + # + # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc) + # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together + # + # Examples: + # + # f.input :author, :label_method => :full_name + # f.input :author, :label_method => :login + # f.input :author, :label_method => :full_name_with_post_count + # f.input :author, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" } + # + # The :value_method option provides the same customization of the value attribute of each option tag. + # + # Examples: + # + # f.input :author, :value_method => :full_name + # f.input :author, :value_method => :login + # f.input :author, :value_method => Proc.new { |a| "author_#{a.login}" } + # + # You can pre-select a specific option value by passing in the :selected option. + # + # Examples: + # + # f.input :author, :selected => current_user.id + # f.input :author, :value_method => :login, :selected => current_user.login + # f.input :authors, :value_method => :login, :selected => Author.most_popular.collect(&:id) + # f.input :authors, :value_method => :login, :selected => nil # override any defaults: select none + # + # You can pass html_options to the select tag using :input_html => {} + # + # Examples: + # + # f.input :authors, :input_html => {:size => 20, :multiple => true} + # + # By default, all select inputs will have a blank option at the top of the list. You can add + # a prompt with the :prompt option, or disable the blank option with :include_blank => false. + # + # + # You can group the options in optgroup elements by passing the :group_by option + # (Note: only tested for belongs_to relations) + # + # Examples: + # + # f.input :author, :group_by => :continent + # + # All the other options should work as expected. If you want to call a custom method on the + # group item. You can include the option:group_label_method + # Examples: + # + # f.input :author, :group_by => :continents, :group_label_method => :something_different + # + def select_input(method, options) + html_options = options.delete(:input_html) || {} + options = set_include_blank(options) + html_options[:multiple] = html_options[:multiple] || options.delete(:multiple) + html_options.delete(:multiple) if html_options[:multiple].nil? + + reflection = self.reflection_for(method) + if reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro) + options[:include_blank] = false + html_options[:multiple] = true if html_options[:multiple].nil? + html_options[:size] ||= 5 + end + options[:selected] = options[:selected].first if options[:selected].present? && html_options[:multiple] == false + input_name = generate_association_input_name(method) + + select_html = if options[:group_by] + # The grouped_options_select is a bit counter intuitive and not optimised (mostly due to ActiveRecord). + # The formtastic user however shouldn't notice this too much. + raw_collection = find_raw_collection_for_column(method, options.reverse_merge(:find_options => { :include => options[:group_by] })) + label, value = detect_label_and_value_method!(raw_collection) + group_collection = raw_collection.map { |option| option.send(options[:group_by]) }.uniq + group_label_method = options[:group_label_method] || detect_label_method(group_collection) + group_collection = group_collection.sort_by { |group_item| group_item.send(group_label_method) } + group_association = options[:group_association] || detect_group_association(method, options[:group_by]) + + # Here comes the monster with 8 arguments + self.grouped_collection_select(input_name, group_collection, + group_association, group_label_method, + value, label, + strip_formtastic_options(options), html_options) + else + collection = find_collection_for_column(method, options) + + self.select(input_name, collection, strip_formtastic_options(options), html_options) + end + + self.label(method, options_for_label(options).merge(:input_name => input_name)) << select_html + end + alias :boolean_select_input :select_input + + # Outputs a timezone select input as Rails' time_zone_select helper. You + # can give priority zones as option. + # + # Examples: + # + # f.input :time_zone, :as => :time_zone, :priority_zones => /Australia/ + # + # You can pre-select a specific option value by passing in the :selected option. + # Note: Right now only works if the form object attribute value is not set (nil), + # because of how the core helper is implemented. + # + # Examples: + # + # f.input :my_favorite_time_zone, :as => :time_zone, :selected => 'Singapore' + # + def time_zone_input(method, options) + html_options = options.delete(:input_html) || {} + selected_value = options.delete(:selected) + + self.label(method, options_for_label(options)) << + self.time_zone_select(method, options.delete(:priority_zones), + strip_formtastic_options(options).merge(:default => selected_value), html_options) + end + + # Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list + # items, one for each possible choice in the belongs_to association. Each li contains a + # label and a radio input. + # + # Example: + # + # f.input :author, :as => :radio + # + # Output: + # + #
    + # Author + #
      + #
    1. + # + #
    2. + #
    3. + # + #
    4. + #
    + #
    + # + # You can customize the choices available in the radio button set by passing in a collection (an Array or + # Hash) through the :collection option. If not provided, the choices are found by reflecting on the association + # (Author.find(:all) in the example above). + # + # Examples: + # + # f.input :author, :as => :radio, :collection => @authors + # f.input :author, :as => :radio, :collection => Author.find(:all) + # f.input :author, :as => :radio, :collection => [@justin, @kate] + # f.input :author, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"] + # + # The :label_method option allows you to customize the label for each radio button two ways: + # + # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc) + # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together + # + # Examples: + # + # f.input :author, :as => :radio, :label_method => :full_name + # f.input :author, :as => :radio, :label_method => :login + # f.input :author, :as => :radio, :label_method => :full_name_with_post_count + # f.input :author, :as => :radio, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" } + # + # The :value_method option provides the same customization of the value attribute of each option tag. + # + # Examples: + # + # f.input :author, :as => :radio, :value_method => :full_name + # f.input :author, :as => :radio, :value_method => :login + # f.input :author, :as => :radio, :value_method => Proc.new { |a| "author_#{a.login}" } + # + # You can force a particular radio button in the collection to be checked with the :selected option. + # + # Examples: + # + # f.input :subscribe_to_newsletter, :as => :radio, :selected => true + # f.input :subscribe_to_newsletter, :as => :radio, :collection => ["Yeah!", "Nope!"], :selected => "Nope!" + # + # Finally, you can set :value_as_class => true if you want the li wrapper around each radio + # button / label combination to contain a class with the value of the radio button (useful for + # applying specific CSS or Javascript to a particular radio button). + # + def radio_input(method, options) + collection = find_collection_for_column(method, options) + html_options = strip_formtastic_options(options).merge(options.delete(:input_html) || {}) + + input_name = generate_association_input_name(method) + value_as_class = options.delete(:value_as_class) + input_ids = [] + selected_option_is_present = [:selected, :checked].any? { |k| options.key?(k) } + selected_value = (options.key?(:checked) ? options[:checked] : options[:selected]) if selected_option_is_present + + list_item_content = collection.map do |c| + label = c.is_a?(Array) ? c.first : c + value = c.is_a?(Array) ? c.last : c + input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase) + input_ids << input_id + + html_options[:checked] = selected_value == value if selected_option_is_present + + li_content = template.content_tag(:label, + Formtastic::Util.html_safe("#{self.radio_button(input_name, value, html_options)} #{label}"), + :for => input_id + ) + + li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {} + template.content_tag(:li, Formtastic::Util.html_safe(li_content), li_options) + end + + field_set_and_list_wrapping_for_method(method, options, list_item_content) + end + alias :boolean_radio_input :radio_input + + # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list + # items (li), one for each fragment for the date (year, month, day). Each li contains a label + # (eg "Year") and a select box. Overwriting the label is possible by adding the :labels option. + # :labels should be a hash with the field (e.g. day) as key and the label text as value. + # See date_or_datetime_input for a more detailed output example. + # + # You can pre-select a specific option value by passing in the :selected option. + # + # Examples: + # + # f.input :created_at, :as => :date, :selected => 1.day.ago + # f.input :created_at, :as => :date, :selected => nil # override any defaults: select none + # f.input :created_at, :as => :date, :labels => { :year => "Year", :month => "Month", :day => "Day" } + # + # Some of Rails' options for select_date are supported, but not everything yet, see + # documentation of date_or_datetime_input() for more information. + def date_input(method, options) + options = set_include_blank(options) + date_or_datetime_input(method, options.merge(:discard_hour => true)) + end + + # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list + # items (li), one for each fragment for the date (year, month, day, hour, min, sec). Each li + # contains a label (eg "Year") and a select box. Overwriting the label is possible by adding + # the :labels option. :labels should be a hash with the field (e.g. day) as key and the label + # text as value. See date_or_datetime_input for a more detailed output example. + # + # You can pre-select a specific option value by passing in the :selected option. + # + # Examples: + # + # f.input :created_at, :as => :datetime, :selected => 1.day.ago + # f.input :created_at, :as => :datetime, :selected => nil # override any defaults: select none + # f.input :created_at, :as => :date, :labels => { :year => "Year", :month => "Month", :day => "Day", + # :hour => "Hour", :minute => "Minute" } + # + # Some of Rails' options for select_date are supported, but not everything yet, see + # documentation of date_or_datetime_input() for more information. + def datetime_input(method, options) + options = set_include_blank(options) + date_or_datetime_input(method, options) + end + + # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list + # items (li), one for each fragment for the time (hour, minute, second). Each li contains a label + # (eg "Hour") and a select box. Overwriting the label is possible by adding the :labels option. + # :labels should be a hash with the field (e.g. day) as key and the label text as value. + # See date_or_datetime_input for a more detailed output example. + # + # You can pre-select a specific option value by passing in the :selected option. + # + # Examples: + # + # f.input :created_at, :as => :time, :selected => 1.hour.ago + # f.input :created_at, :as => :time, :selected => nil # override any defaults: select none + # f.input :created_at, :as => :date, :labels => { :hour => "Hour", :minute => "Minute" } + # + # Some of Rails' options for select_time are supported, but not everything yet, see + # documentation of date_or_datetime_input() for more information. + def time_input(method, options) + options = set_include_blank(options) + date_or_datetime_input(method, options.merge(:discard_year => true, :discard_month => true, :discard_day => true)) + end + + # Helper method used by :as => (:date|:datetime|:time). Generates a fieldset containing a + # legend (for what would normally be considered the label), and an ordered list of list items + # for year, month, day, hour, etc, each containing a label and a select. Example: + # + #
    + # Created At + #
      + #
    1. + # + # + #
    2. + #
    3. + # + # + #
    4. + #
    5. + # + # + #
    6. + #
    + #
    + # + # This is an absolute abomination, but so is the official Rails select_date(). + # + # Options: + # + # * @:order => [:month, :day, :year]@ + # * @:include_seconds@ => true@ + # * @:selected => Time.mktime(2008)@ + # * @:selected => Date.new(2008)@ + # * @:selected => nil@ + # * @:discard_(year|month|day|hour|minute) => true@ + # * @:include_blank => true@ + # * @:labels => {}@ + def date_or_datetime_input(method, options) + position = { :year => 1, :month => 2, :day => 3, :hour => 4, :minute => 5, :second => 6 } + i18n_date_order = ::I18n.t(:order, :scope => [:date]) + i18n_date_order = nil unless i18n_date_order.is_a?(Array) + inputs = options.delete(:order) || i18n_date_order || [:year, :month, :day] + labels = options.delete(:labels) || {} + + time_inputs = [:hour, :minute] + time_inputs << :second if options[:include_seconds] + + list_items_capture = "" + hidden_fields_capture = "" + + datetime = options[:selected] + datetime = @object.send(method) if @object && @object.send(method) # object value trumps :selected value + + html_options = options.delete(:input_html) || {} + input_ids = [] + + (inputs + time_inputs).each do |input| + input_ids << input_id = generate_html_id(method, "#{position[input]}i") + + field_name = "#{method}(#{position[input]}i)" + if options[:"discard_#{input}"] + break if time_inputs.include?(input) + + hidden_value = datetime.respond_to?(input) ? datetime.send(input) : datetime + hidden_fields_capture << template.hidden_field_tag("#{@object_name}[#{field_name}]", (hidden_value || 1), :id => input_id) + else + opts = strip_formtastic_options(options).merge(:prefix => @object_name, :field_name => field_name, :default => datetime) + item_label_text = labels[input] || ::I18n.t(input.to_s, :default => input.to_s.humanize, :scope => [:datetime, :prompts]) + + list_items_capture << template.content_tag(:li, Formtastic::Util.html_safe([ + !item_label_text.blank? ? template.content_tag(:label, Formtastic::Util.html_safe(item_label_text), :for => input_id) : "", + template.send(:"select_#{input}", datetime, opts, html_options.merge(:id => input_id)) + ].join("")) + ) + end + end + + hidden_fields_capture << field_set_and_list_wrapping_for_method(method, options.merge(:label_for => input_ids.first), list_items_capture) + end + + # Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list + # items, one for each possible choice in the belongs_to association. Each li contains a + # label and a check_box input. + # + # This is an alternative for has many and has and belongs to many associations. + # + # Example: + # + # f.input :author, :as => :check_boxes + # + # Output: + # + #
    + # + #
      + #
    1. + # + # + #
    2. + #
    3. + # + # + #
    4. + #
    + #
    + # + # Notice that the value of the checkbox is the same as the id and the hidden + # field has empty value. You can override the hidden field value using the + # unchecked_value option. + # + # You can customize the options available in the set by passing in a collection (Array) of + # ActiveRecord objects through the :collection option. If not provided, the choices are found + # by inferring the parent's class name from the method name and simply calling find(:all) on + # it (Author.find(:all) in the example above). + # + # Examples: + # + # f.input :author, :as => :check_boxes, :collection => @authors + # f.input :author, :as => :check_boxes, :collection => Author.find(:all) + # f.input :author, :as => :check_boxes, :collection => [@justin, @kate] + # + # The :label_method option allows you to customize the label for each checkbox two ways: + # + # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc) + # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together + # + # Examples: + # + # f.input :author, :as => :check_boxes, :label_method => :full_name + # f.input :author, :as => :check_boxes, :label_method => :login + # f.input :author, :as => :check_boxes, :label_method => :full_name_with_post_count + # f.input :author, :as => :check_boxes, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" } + # + # The :value_method option provides the same customization of the value attribute of each checkbox input tag. + # + # Examples: + # + # f.input :author, :as => :check_boxes, :value_method => :full_name + # f.input :author, :as => :check_boxes, :value_method => :login + # f.input :author, :as => :check_boxes, :value_method => Proc.new { |a| "author_#{a.login}" } + # + # You can pre-select/check a specific checkbox value by passing in the :selected option (alias :checked works as well). + # + # Examples: + # + # f.input :authors, :as => :check_boxes, :selected => @justin + # f.input :authors, :as => :check_boxes, :selected => Author.most_popular.collect(&:id) + # f.input :authors, :as => :check_boxes, :selected => nil # override any defaults: select none + # + # Finally, you can set :value_as_class => true if you want the li wrapper around each checkbox / label + # combination to contain a class with the value of the radio button (useful for applying specific + # CSS or Javascript to a particular checkbox). + # + def check_boxes_input(method, options) + collection = find_collection_for_column(method, options) + html_options = options.delete(:input_html) || {} + + input_name = generate_association_input_name(method) + value_as_class = options.delete(:value_as_class) + unchecked_value = options.delete(:unchecked_value) || '' + html_options = { :name => "#{@object_name}[#{input_name}][]" }.merge(html_options) + input_ids = [] + + selected_option_is_present = [:selected, :checked].any? { |k| options.key?(k) } + selected_values = (options.key?(:checked) ? options[:checked] : options[:selected]) if selected_option_is_present + selected_values = [*selected_values].compact + + disabled_option_is_present = options.key?(:disabled) + disabled_values = [*options[:disabled]] if disabled_option_is_present + + list_item_content = collection.map do |c| + label = c.is_a?(Array) ? c.first : c + value = c.is_a?(Array) ? c.last : c + input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase) + input_ids << input_id + + html_options[:checked] = selected_values.include?(value) if selected_option_is_present + html_options[:disabled] = disabled_values.include?(value) if disabled_option_is_present + html_options[:id] = input_id + + li_content = template.content_tag(:label, + Formtastic::Util.html_safe("#{self.check_box(input_name, html_options, value, unchecked_value)} #{label}"), + :for => input_id + ) + + li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {} + template.content_tag(:li, Formtastic::Util.html_safe(li_content), li_options) + end + + field_set_and_list_wrapping_for_method(method, options, list_item_content) + end + + # Outputs a country select input, wrapping around a regular country_select helper. + # Rails doesn't come with a country_select helper by default any more, so you'll need to install + # the "official" plugin, or, if you wish, any other country_select plugin that behaves in the + # same way. + # + # The Rails plugin iso-3166-country-select plugin can be found "here":http://github.com/rails/iso-3166-country-select. + # + # By default, Formtastic includes a handfull of english-speaking countries as "priority counties", + # which you can change to suit your market and user base (see README for more info on config). + # + # Examples: + # f.input :location, :as => :country # use Formtastic::SemanticFormBuilder.priority_countries array for the priority countries + # f.input :location, :as => :country, :priority_countries => /Australia/ # set your own + # + def country_input(method, options) + 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) + + html_options = options.delete(:input_html) || {} + priority_countries = options.delete(:priority_countries) || @@priority_countries + + self.label(method, options_for_label(options)) << + self.country_select(method, priority_countries, strip_formtastic_options(options), html_options) + end + + # Outputs a label containing a checkbox and the label text. The label defaults + # to the column name (method name) and can be altered with the :label option. + # :checked_value and :unchecked_value options are also available. + # + # You can pre-select/check the boolean checkbox by passing in the :selected option (alias :checked works as well). + # + # Examples: + # + # f.input :allow_comments, :as => :boolean, :selected => true # override any default value: selected/checked + # + def boolean_input(method, options) + html_options = options.delete(:input_html) || {} + checked = options.key?(:checked) ? options[:checked] : options[:selected] + html_options[:checked] = checked == true if [:selected, :checked].any? { |k| options.key?(k) } + + input = self.check_box(method, strip_formtastic_options(options).merge(html_options), + options.delete(:checked_value) || '1', options.delete(:unchecked_value) || '0') + options = options_for_label(options) + + # the label() method will insert this nested input into the label at the last minute + options[:label_prefix_for_nested_input] = input + + self.label(method, options) + end + + # Generates an input for the given method using the type supplied with :as. + def inline_input_for(method, options) + send(:"#{options.delete(:as)}_input", method, options) + end + + # Generates hints for the given method using the text supplied in :hint. + # + def inline_hints_for(method, options) #:nodoc: + options[:hint] = localized_string(method, options[:hint], :hint) + return if options[:hint].blank? + template.content_tag(:p, Formtastic::Util.html_safe(options[:hint]), :class => 'inline-hints') + end + + # Creates an error sentence by calling to_sentence on the errors array. + # + def error_sentence(errors) #:nodoc: + template.content_tag(:p, Formtastic::Util.html_safe(errors.to_sentence.untaint), :class => 'inline-errors') + end + + # Creates an error li list. + # + def error_list(errors) #:nodoc: + list_elements = [] + errors.each do |error| + list_elements << template.content_tag(:li, Formtastic::Util.html_safe(error.untaint)) + end + template.content_tag(:ul, Formtastic::Util.html_safe(list_elements.join("\n")), :class => 'errors') + end + + # Creates an error sentence containing only the first error + # + def error_first(errors) #:nodoc: + template.content_tag(:p, Formtastic::Util.html_safe(errors.first.untaint), :class => 'inline-errors') + end + + # Generates the required or optional string. If the value set is a proc, + # it evaluates the proc first. + # + def required_or_optional_string(required) #:nodoc: + string_or_proc = case required + when true + @@required_string + when false + @@optional_string + else + required + end + + if string_or_proc.is_a?(Proc) + string_or_proc.call + else + string_or_proc.to_s + end + end + + # Generates a fieldset and wraps the content in an ordered list. When working + # with nested attributes (in Rails 2.3), it allows %i as interpolation option + # in :name. So you can do: + # + # 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. + # + # 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! + + legend = html_options.delete(:name).to_s + legend %= parent_child_index(html_options[:parent]) if html_options[:parent] + legend = template.content_tag(:legend, template.content_tag(:span, Formtastic::Util.html_safe(legend))) unless legend.blank? + + if block_given? + contents = if template.respond_to?(:is_haml?) && template.is_haml? + template.capture_haml(&block) + else + template.capture(&block) + end + end + + # Ruby 1.9: String#to_s behavior changed, need to make an explicit join. + contents = contents.join if contents.respond_to?(:join) + fieldset = template.content_tag(:fieldset, + Formtastic::Util.html_safe(legend) << template.content_tag(:ol, Formtastic::Util.html_safe(contents)), + html_options.except(:builder, :parent) + ) + + template.concat(fieldset) if block_given? && (!defined?(Rails::VERSION) || Rails::VERSION::MAJOR == 2) + fieldset + end + + def field_set_title_from_args(*args) #:nodoc: + options = args.extract_options! + options[:name] ||= options.delete(:title) + title = options[:name] + + if title.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)) + title = args.shift if valid_name_classes.any? { |valid_name_class| args.first.is_a?(valid_name_class) } + end + title = localized_string(title, title, :title) if title.is_a?(::Symbol) + title + end + + # Also generates a fieldset and an ordered list but with label based in + # method. This methods is currently used by radio and datetime inputs. + # + def field_set_and_list_wrapping_for_method(method, options, contents) #:nodoc: + contents = contents.join if contents.respond_to?(:join) + + template.content_tag(:fieldset, + template.content_tag(:legend, + self.label(method, options_for_label(options).merge(:for => options.delete(:label_for))), :class => 'label' + ) << + template.content_tag(:ol, Formtastic::Util.html_safe(contents)) + ) + end + + # For methods that have a database column, take a best guess as to what the input method + # should be. In most cases, it will just return the column type (eg :string), but for special + # cases it will simplify (like the case of :integer, :float & :decimal to :numeric), or do + # something different (like :password and :select). + # + # If there is no column for the method (eg "virtual columns" with an attr_accessor), the + # default is a :string, a similar behaviour to Rails' scaffolding. + # + def default_input_type(method, options = {}) #:nodoc: + if column = self.column_for(method) + # Special cases where the column type doesn't map to an input method. + case column.type + when :string + return :password if method.to_s =~ /password/ + return :country if method.to_s =~ /country$/ + return :time_zone if method.to_s =~ /time_zone/ + when :integer + return :select if method.to_s =~ /_id$/ + return :numeric + when :float, :decimal + return :numeric + when :timestamp + return :datetime + end + + # Try look for hints in options hash. Quite common senario: Enum keys stored as string in the database. + return :select if column.type == :string && options.key?(:collection) + # Try 3: Assume the input name will be the same as the column type (e.g. string_input). + return column.type + else + if @object + return :select if self.reflection_for(method) + + file = @object.send(method) if @object.respond_to?(method) + return :file if file && @@file_methods.any? { |m| file.respond_to?(m) } + end + + return :select if options.key?(:collection) + return :password if method.to_s =~ /password/ + return :string + end + end + + # Used by select and radio inputs. The collection can be retrieved by + # three ways: + # + # * Explicitly provided through :collection + # * Retrivied through an association + # * Or a boolean column, which will generate a localized { "Yes" => true, "No" => false } hash. + # + # If the collection is not a hash or an array of strings, fixnums or arrays, + # we use label_method and value_method to retreive an array with the + # appropriate label and value. + # + def find_collection_for_column(column, options) #:nodoc: + 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) || collection.instance_of?(Range)) && + [Array, Fixnum, String, Symbol].include?(collection.first.class) + + label, value = detect_label_and_value_method!(collection, options) + collection.map { |o| [send_or_call(label, o), send_or_call(value, o)] } + end + + # As #find_collection_for_column but returns the collection without mapping the label and value + # + def find_raw_collection_for_column(column, options) #:nodoc: + collection = if options[:collection] + options.delete(:collection) + elsif reflection = self.reflection_for(column) + options[:find_options] ||= {} + + if conditions = reflection.options[:conditions] + options[:find_options][:conditions] = reflection.klass.merge_conditions(conditions, options[:find_options][:conditions]) + end + + reflection.klass.find(:all, options[:find_options]) + else + create_boolean_collection(options) + end + + collection = collection.to_a if collection.is_a?(Hash) + collection + end + + # Detects the label and value methods from a collection values set in + # @@collection_label_methods. It will use and delete + # the options :label_method and :value_methods when present + # + def detect_label_and_value_method!(collection_or_instance, options = {}) #:nodoc + label = options.delete(:label_method) || detect_label_method(collection_or_instance) + value = options.delete(:value_method) || :id + [label, value] + end + + # Detected the label collection method when none is supplied using the + # values set in @@collection_label_methods. + # + def detect_label_method(collection) #:nodoc: + @@collection_label_methods.detect { |m| collection.first.respond_to?(m) } + end + + # Detects the method to call for fetching group members from the groups when grouping select options + # + def detect_group_association(method, group_by) + object_to_method_reflection = self.reflection_for(method) + method_class = object_to_method_reflection.klass + + method_to_group_association = method_class.reflect_on_association(group_by) + group_class = method_to_group_association.klass + + # This will return in the normal case + return method.to_s.pluralize.to_sym if group_class.reflect_on_association(method.to_s.pluralize) + + # This is for belongs_to associations named differently than their class + # form.input :parent, :group_by => :customer + # eg. + # class Project + # belongs_to :parent, :class_name => 'Project', :foreign_key => 'parent_id' + # belongs_to :customer + # end + # class Customer + # has_many :projects + # end + group_method = method_class.to_s.underscore.pluralize.to_sym + return group_method if group_class.reflect_on_association(group_method) # :projects + + # This is for has_many associations named differently than their class + # eg. + # class Project + # belongs_to :parent, :class_name => 'Project', :foreign_key => 'parent_id' + # belongs_to :customer + # end + # class Customer + # has_many :tasks, :class_name => 'Project', :foreign_key => 'customer_id' + # end + possible_associations = group_class.reflect_on_all_associations(:has_many).find_all{|assoc| assoc.klass == object_class} + return possible_associations.first.name.to_sym if possible_associations.count == 1 + + 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" + + end + + # Returns a hash to be used by radio and select inputs when a boolean field + # is provided. + # + def create_boolean_collection(options) #:nodoc: + options[:true] ||= ::Formtastic::I18n.t(:yes) + options[:false] ||= ::Formtastic::I18n.t(:no) + options[:value_as_class] = true unless options.key?(:value_as_class) + + [ [ options.delete(:true), true], [ options.delete(:false), false ] ] + end + + # Used by association inputs (select, radio) to generate the name that should + # be used for the input + # + # belongs_to :author; f.input :author; will generate 'author_id' + # belongs_to :entity, :foreign_key = :owner_id; f.input :author; will generate 'owner_id' + # has_many :authors; f.input :authors; will generate 'author_ids' + # has_and_belongs_to_many will act like has_many + # + def generate_association_input_name(method) #:nodoc: + if reflection = self.reflection_for(method) + if [:has_and_belongs_to_many, :has_many].include?(reflection.macro) + "#{method.to_s.singularize}_ids" + else + reflection.options[:foreign_key] || "#{method}_id" + end + else + method + end.to_sym + end + + # If an association method is passed in (f.input :author) try to find the + # reflection object. + # + def reflection_for(method) #:nodoc: + @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association) + end + + # Get a column object for a specified attribute method - if possible. + # + def column_for(method) #:nodoc: + @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute) + end + + # Generates default_string_options by retrieving column information from + # the database. + # + def default_string_options(method, type) #:nodoc: + column = self.column_for(method) + + if type == :text + { :cols => @@default_text_field_size, :rows => @@default_text_area_height } + elsif type == :numeric || column.nil? || column.limit.nil? + { :size => @@default_text_field_size } + else + { :maxlength => column.limit, :size => [column.limit, @@default_text_field_size].min } + end + end + + # Generate the html id for the li tag. + # It takes into account options[:index] and @auto_index to generate li + # elements with appropriate index scope. It also sanitizes the object + # and method names. + # + def generate_html_id(method_name, value='input') #:nodoc: + if options.has_key?(:index) + index = "_#{options[:index]}" + elsif defined?(@auto_index) + index = "_#{@auto_index}" + else + index = "" + end + sanitized_method_name = method_name.to_s.gsub(/[\?\/\-]$/, '') + + "#{sanitized_object_name}#{index}_#{sanitized_method_name}_#{value}" + end + + # Gets the nested_child_index value from the parent builder. In Rails 2.3 + # it always returns a fixnum. In next versions it returns a hash with each + # association that the parent builds. + # + def parent_child_index(parent) #:nodoc: + duck = parent[:builder].instance_variable_get('@nested_child_index') + + if duck.is_a?(Hash) + child = parent[:for] + child = child.first if child.respond_to?(:first) + duck[child].to_i + 1 + else + duck.to_i + 1 + end + end + + def sanitized_object_name #:nodoc: + @sanitized_object_name ||= @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") + end + + def humanized_attribute_name(method) #:nodoc: + if @object && @object.class.respond_to?(:human_attribute_name) + humanized_name = @object.class.human_attribute_name(method.to_s) + if humanized_name == method.to_s.send(:humanize) + method.to_s.send(@@label_str_method) + else + humanized_name + end + else + method.to_s.send(@@label_str_method) + end + end + + # Internal generic method for looking up localized values within Formtastic + # using I18n, if no explicit value is set and I18n-lookups are enabled. + # + # Enabled/Disable this by setting: + # + # Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true/false + # + # Lookup priority: + # + # 'formtastic.{{type}}.{{model}}.{{action}}.{{attribute}}' + # 'formtastic.{{type}}.{{model}}.{{attribute}}' + # 'formtastic.{{type}}.{{attribute}}' + # + # Example: + # + # 'formtastic.labels.post.edit.title' + # 'formtastic.labels.post.title' + # 'formtastic.labels.title' + # + # NOTE: Generic, but only used for form input titles/labels/hints/actions (titles = legends, actions = buttons). + # + def localized_string(key, value, type, options = {}) #:nodoc: + key = value if value.is_a?(::Symbol) + + if value.is_a?(::String) + value + else + use_i18n = value.nil? ? @@i18n_lookups_by_default : (value != false) + + if use_i18n + model_name, nested_model_name = normalize_model_name(self.model_name.underscore) + action_name = template.params[:action].to_s rescue '' + attribute_name = key.to_s + + defaults = ::Formtastic::I18n::SCOPES.collect do |i18n_scope| + i18n_path = i18n_scope.dup + i18n_path.gsub!('{{action}}', action_name) + i18n_path.gsub!('{{model}}', model_name) + i18n_path.gsub!('{{nested_model}}', nested_model_name) unless nested_model_name.nil? + i18n_path.gsub!('{{attribute}}', attribute_name) + i18n_path.gsub!('..', '.') + i18n_path.to_sym + end + defaults << '' + + i18n_value = ::Formtastic::I18n.t(defaults.shift, + options.merge(:default => defaults, :scope => type.to_s.pluralize.to_sym)) + i18n_value.blank? ? nil : i18n_value + end + end + end + + def model_name + @object.present? ? @object.class.name : @object_name.to_s.classify + end + + def normalize_model_name(name) + if name =~ /(.+)\[(.+)\]/ + [$1, $2] + else + [name] + end + end + + def send_or_call(duck, object) + if duck.is_a?(Proc) + duck.call(object) + else + object.send(duck) + end + end + + 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 + + # Wrappers around form_for (etc) with :builder => SemanticFormBuilder. + # + # * semantic_form_for(@post) + # * semantic_fields_for(@post) + # * semantic_form_remote_for(@post) + # * semantic_remote_form_for(@post) + # + # Each of which are the equivalent of: + # + # * form_for(@post, :builder => Formtastic::SemanticFormBuilder)) + # * fields_for(@post, :builder => Formtastic::SemanticFormBuilder)) + # * form_remote_for(@post, :builder => Formtastic::SemanticFormBuilder)) + # * remote_form_for(@post, :builder => Formtastic::SemanticFormBuilder)) + # + # Example Usage: + # + # <% semantic_form_for @post do |f| %> + # <%= f.input :title %> + # <%= f.input :body %> + # <% end %> + # + # The above examples use a resource-oriented style of form_for() helper where only the @post + # object is given as an argument, but the generic style is also supported, as are forms with + # inline objects (Post.new) rather than objects with instance variables (@post): + # + # <% semantic_form_for :post, @post, :url => posts_path do |f| %> + # ... + # <% end %> + # + # <% semantic_form_for :post, Post.new, :url => posts_path do |f| %> + # ... + # <% end %> + module SemanticFormHelper + @@builder = ::Formtastic::SemanticFormBuilder + mattr_accessor :builder + + @@default_field_error_proc = nil + + # Override the default ActiveRecordHelper behaviour of wrapping the input. + # This gets taken care of semantically by adding an error class to the LI tag + # containing the input. + # + FIELD_ERROR_PROC = proc do |html_tag, instance_tag| + html_tag + end + + def with_custom_field_error_proc(&block) + @@default_field_error_proc = ::ActionView::Base.field_error_proc + ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC + result = yield + ::ActionView::Base.field_error_proc = @@default_field_error_proc + result + end + + [:form_for, :fields_for, :remote_form_for].each do |meth| + module_eval <<-END_SRC, __FILE__, __LINE__ + 1 + def semantic_#{meth}(record_or_name_or_array, *args, &proc) + options = args.extract_options! + options[:builder] ||= @@builder + options[:html] ||= {} + + class_names = options[:html][:class] ? options[:html][:class].split(" ") : [] + class_names << "formtastic" + class_names << case record_or_name_or_array + when String, Symbol then record_or_name_or_array.to_s # :post => "post" + when Array then ActionController::RecordIdentifier.singular_class_name(record_or_name_or_array.last.class) # [@post, @comment] # => "comment" + else ActionController::RecordIdentifier.singular_class_name(record_or_name_or_array.class) # @post => "post" + end + options[:html][:class] = class_names.join(" ") + + with_custom_field_error_proc do + #{meth}(record_or_name_or_array, *(args << options), &proc) + end + end + END_SRC + end + alias :semantic_form_remote_for :semantic_remote_form_for + + end +end diff --git a/vendor/gems/formtastic-0.9.10/lib/formtastic/i18n.rb b/vendor/gems/formtastic-0.9.10/lib/formtastic/i18n.rb new file mode 100644 index 0000000..ba38cbc --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/lib/formtastic/i18n.rb @@ -0,0 +1,36 @@ +# coding: utf-8 +module Formtastic + module I18n + + DEFAULT_SCOPE = [:formtastic].freeze + DEFAULT_VALUES = { + :required => 'required', + :yes => 'Yes', + :no => 'No', + :create => 'Create {{model}}', + :update => 'Update {{model}}' + }.freeze + SCOPES = [ + '{{model}}.{{nested_model}}.{{action}}.{{attribute}}', + '{{model}}.{{action}}.{{attribute}}', + '{{model}}.{{nested_model}}.{{attribute}}', + '{{model}}.{{attribute}}', + '{{nested_model}}.{{attribute}}', + '{{attribute}}' + ] + + class << self + + def translate(*args) + key = args.shift.to_sym + options = args.extract_options! + options.reverse_merge!(:default => DEFAULT_VALUES[key]) + options[:scope] = [DEFAULT_SCOPE, options[:scope]].flatten.compact + ::I18n.translate(key, *(args << options)) + end + alias :t :translate + + end + + end +end \ No newline at end of file diff --git a/vendor/gems/formtastic-0.9.10/lib/formtastic/layout_helper.rb b/vendor/gems/formtastic-0.9.10/lib/formtastic/layout_helper.rb new file mode 100644 index 0000000..2da3341 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/lib/formtastic/layout_helper.rb @@ -0,0 +1,11 @@ +# coding: utf-8 +module Formtastic + module LayoutHelper + + def formtastic_stylesheet_link_tag + stylesheet_link_tag("formtastic") + + stylesheet_link_tag("formtastic_changes") + end + + end +end \ No newline at end of file diff --git a/vendor/gems/formtastic-0.9.10/lib/formtastic/util.rb b/vendor/gems/formtastic-0.9.10/lib/formtastic/util.rb new file mode 100644 index 0000000..822d93a --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/lib/formtastic/util.rb @@ -0,0 +1,25 @@ +# Adapted from the rails3 compatibility shim in Haml 2.2 +module Formtastic + module Util + extend self + ## Rails XSS Safety + + # Returns the given text, marked as being HTML-safe. + # With older versions of the Rails XSS-safety mechanism, + # this destructively modifies the HTML-safety of `text`. + # + # @param text [String] + # @return [String] `text`, marked as HTML-safe + def html_safe(text) + return text if text.nil? + return text.html_safe if defined?(ActiveSupport::SafeBuffer) + return text.html_safe! + end + + def rails_safe_buffer_class + return ActionView::SafeBuffer if defined?(ActionView::SafeBuffer) + ActiveSupport::SafeBuffer + end + + end +end diff --git a/vendor/plugins/formtastic/lib/locale/en.yml b/vendor/gems/formtastic-0.9.10/lib/locale/en.yml similarity index 76% rename from vendor/plugins/formtastic/lib/locale/en.yml rename to vendor/gems/formtastic-0.9.10/lib/locale/en.yml index dac478b..cea06a0 100644 --- a/vendor/plugins/formtastic/lib/locale/en.yml +++ b/vendor/gems/formtastic-0.9.10/lib/locale/en.yml @@ -1,7 +1,7 @@ en: formtastic: - "yes": 'Yes' - "no": 'No' + :yes: 'Yes' + :no: 'No' create: 'Create' save: 'Save' submit: 'Submit' diff --git a/vendor/plugins/formtastic/rails/init.rb b/vendor/gems/formtastic-0.9.10/rails/init.rb similarity index 51% rename from vendor/plugins/formtastic/rails/init.rb rename to vendor/gems/formtastic-0.9.10/rails/init.rb index 653dffa..1db4664 100644 --- a/vendor/plugins/formtastic/rails/init.rb +++ b/vendor/gems/formtastic-0.9.10/rails/init.rb @@ -1,3 +1,5 @@ # coding: utf-8 require File.join(File.dirname(__FILE__), *%w[.. lib formtastic]) +require File.join(File.dirname(__FILE__), *%w[.. lib formtastic layout_helper]) ActionView::Base.send :include, Formtastic::SemanticFormHelper +ActionView::Base.send :include, Formtastic::LayoutHelper diff --git a/vendor/plugins/formtastic/spec/buttons_spec.rb b/vendor/gems/formtastic-0.9.10/spec/buttons_spec.rb similarity index 98% rename from vendor/plugins/formtastic/spec/buttons_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/buttons_spec.rb index 778ec04..ba69fc4 100644 --- a/vendor/plugins/formtastic/spec/buttons_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/buttons_spec.rb @@ -1,5 +1,5 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe 'SemanticFormBuilder#buttons' do diff --git a/vendor/gems/formtastic-0.9.10/spec/commit_button_spec.rb b/vendor/gems/formtastic-0.9.10/spec/commit_button_spec.rb new file mode 100644 index 0000000..7b88193 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/commit_button_spec.rb @@ -0,0 +1,363 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/spec_helper' + +describe 'SemanticFormBuilder#commit_button' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + end + + describe 'when used on any record' do + + before do + @new_post.stub!(:new_record?).and_return(false) + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button) + end + end + + it 'should render a commit li' do + output_buffer.should have_tag('li.commit') + end + + it 'should render an input with a type attribute of "submit"' do + output_buffer.should have_tag('li.commit input[@type="submit"]') + end + + it 'should render an input with a name attribute of "commit"' do + output_buffer.should have_tag('li.commit input[@name="commit"]') + end + + it 'should pass options given in :button_html to the button' do + @new_post.stub!(:new_record?).and_return(false) + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button('text', :button_html => {:class => 'my_class', :id => 'my_id'})) + end + + output_buffer.should have_tag('li.commit input#my_id') + output_buffer.should have_tag('li.commit input.my_class') + end + + end + + describe "its accesskey" do + + it 'should allow nil default' do + with_config :default_commit_button_accesskey, nil do + output_buffer.should_not have_tag('li.commit input[@accesskey]') + end + end + + it 'should use the default if set' do + with_config :default_commit_button_accesskey, 's' do + @new_post.stub!(:new_record?).and_return(false) + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button('text', :button_html => {})) + end + output_buffer.should have_tag('li.commit input[@accesskey="s"]') + end + end + + it 'should use the value set in options over the default' do + with_config :default_commit_button_accesskey, 's' do + @new_post.stub!(:new_record?).and_return(false) + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button('text', :accesskey => 'o')) + end + output_buffer.should_not have_tag('li.commit input[@accesskey="s"]') + output_buffer.should have_tag('li.commit input[@accesskey="o"]') + end + end + + it 'should use the value set in button_html over options' do + with_config :default_commit_button_accesskey, 's' do + @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'})) + end + output_buffer.should_not have_tag('li.commit input[@accesskey="s"]') + output_buffer.should_not have_tag('li.commit input[@accesskey="o"]') + output_buffer.should have_tag('li.commit input[@accesskey="t"]') + end + end + + end + + describe 'when the first option is a string and the second is a hash' do + + before do + @new_post.stub!(:new_record?).and_return(false) + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button("a string", :button_html => { :class => "pretty"})) + end + end + + it "should render the string as the value of the button" do + output_buffer.should have_tag('li input[@value="a string"]') + end + + it "should deal with the options hash" do + output_buffer.should have_tag('li input.pretty') + end + + end + + describe 'when the first option is a hash' do + + before do + @new_post.stub!(:new_record?).and_return(false) + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button(:button_html => { :class => "pretty"})) + end + end + + it "should deal with the options hash" do + output_buffer.should have_tag('li input.pretty') + end + + end + + describe 'label' do + + # No object + describe 'when used without object' do + describe 'when explicit label is provided' do + it 'should render an input with the explicitly specified label' do + semantic_form_for(:post, :url => 'http://example.com') do |builder| + concat(builder.commit_button("Click!")) + end + output_buffer.should have_tag('li.commit input[@value="Click!"][@class~="submit"]') + end + end + + describe 'when no explicit label is provided' do + describe 'when no I18n-localized label is provided' do + before do + ::I18n.backend.store_translations :en, :formtastic => {:submit => 'Submit {{model}}'} + end + + after do + ::I18n.backend.reload! + end + + it 'should render an input with default I18n-localized label (fallback)' do + semantic_form_for(:post, :url => 'http://example.com') do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag('li.commit input[@value="Submit Post"][@class~="submit"]') + end + end + + describe 'when I18n-localized label is provided' do + before do + ::I18n.backend.store_translations :en, + :formtastic => { + :actions => { + :submit => 'Custom Submit', + } + } + ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true + end + + after do + ::I18n.backend.reload! + end + + it 'should render an input with localized label (I18n)' do + ::I18n.backend.store_translations :en, + :formtastic => { + :actions => { + :post => { + :submit => 'Custom Submit {{model}}' + } + } + } + semantic_form_for(:post, :url => 'http://example.com') do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag(%Q{li.commit input[@value="Custom Submit Post"][@class~="submit"]}) + end + + it 'should render an input with anoptional localized label (I18n) - if first is not set' do + semantic_form_for(:post, :url => 'http://example.com') do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag(%Q{li.commit input[@value="Custom Submit"][@class~="submit"]}) + end + + end + end + end + + # New record + describe 'when used on a new record' do + before do + @new_post.stub!(:new_record?).and_return(true) + end + + describe 'when explicit label is provided' do + it 'should render an input with the explicitly specified label' do + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button("Click!")) + end + output_buffer.should have_tag('li.commit input[@value="Click!"][@class~="create"]') + end + end + + describe 'when no explicit label is provided' do + describe 'when no I18n-localized label is provided' do + before do + ::I18n.backend.store_translations :en, :formtastic => {:create => 'Create {{model}}'} + end + + after do + ::I18n.backend.reload! + end + + it 'should render an input with default I18n-localized label (fallback)' do + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag('li.commit input[@value="Create Post"][@class~="create"]') + end + end + + describe 'when I18n-localized label is provided' do + before do + ::I18n.backend.store_translations :en, + :formtastic => { + :actions => { + :create => 'Custom Create', + } + } + ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true + end + + after do + ::I18n.backend.reload! + end + + it 'should render an input with localized label (I18n)' do + ::I18n.backend.store_translations :en, + :formtastic => { + :actions => { + :post => { + :create => 'Custom Create {{model}}' + } + } + } + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create Post"][@class~="create"]}) + end + + it 'should render an input with anoptional localized label (I18n) - if first is not set' do + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create"][@class~="create"]}) + end + + end + end + end + + # Existing record + describe 'when used on an existing record' do + before do + @new_post.stub!(:new_record?).and_return(false) + end + + describe 'when explicit label is provided' do + it 'should render an input with the explicitly specified label' do + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button("Click!")) + end + output_buffer.should have_tag('li.commit input[@value="Click!"][@class~="update"]') + end + end + + describe 'when no explicit label is provided' do + describe 'when no I18n-localized label is provided' do + before do + ::I18n.backend.store_translations :en, :formtastic => {:update => 'Save {{model}}'} + end + + after do + ::I18n.backend.reload! + end + + it 'should render an input with default I18n-localized label (fallback)' do + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag('li.commit input[@value="Save Post"][@class~="update"]') + end + end + + describe 'when I18n-localized label is provided' do + before do + ::I18n.backend.store_translations :en, + :formtastic => { + :actions => { + :update => 'Custom Save', + } + } + ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true + end + + after do + ::I18n.backend.reload! + end + + it 'should render an input with localized label (I18n)' do + ::I18n.backend.store_translations :en, + :formtastic => { + :actions => { + :post => { + :update => 'Custom Save {{model}}' + } + } + } + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag(%Q{li.commit input[@value="Custom Save Post"][@class~="update"]}) + end + + it 'should render an input with anoptional localized label (I18n) - if first is not set' do + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button) + end + output_buffer.should have_tag(%Q{li.commit input[@value="Custom Save"][@class~="update"]}) + ::I18n.backend.store_translations :en, :formtastic => {} + end + + end + end + end + end + + describe 'when the model is two words' do + before do + output_buffer = '' + class ::UserPost; def id; end; def self.human_name; "Userpost"; end; end # Rails does crappy human_name + @new_user_post = ::UserPost.new + + @new_user_post.stub!(:new_record?).and_return(true) + semantic_form_for(@new_user_post, :url => '') do |builder| + concat(builder.commit_button()) + end + end + + it "should render the string as the value of the button" do + output_buffer.should have_tag('li input[@value="Create User post"]') + end + + end + +end diff --git a/vendor/plugins/formtastic/spec/custom_builder_spec.rb b/vendor/gems/formtastic-0.9.10/spec/custom_builder_spec.rb similarity index 61% rename from vendor/plugins/formtastic/spec/custom_builder_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/custom_builder_spec.rb index 0027577..5fd6a72 100644 --- a/vendor/plugins/formtastic/spec/custom_builder_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/custom_builder_spec.rb @@ -1,11 +1,11 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe 'Formtastic::SemanticFormHelper.builder' do 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 diff --git a/vendor/gems/formtastic-0.9.10/spec/custom_macros.rb b/vendor/gems/formtastic-0.9.10/spec/custom_macros.rb new file mode 100644 index 0000000..589753d --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/custom_macros.rb @@ -0,0 +1,460 @@ +module CustomMacros + + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + + def it_should_have_input_wrapper_with_class(class_name) + it "should have input wrapper with class '#{class_name}'" do + output_buffer.should have_tag("form li.#{class_name}") + end + end + + def it_should_have_input_wrapper_with_id(id_string) + it "should have input wrapper with id '#{id_string}'" do + output_buffer.should have_tag("form li##{id_string}") + end + end + + def it_should_not_have_a_label + it "should not have a label" do + output_buffer.should_not have_tag("form li label") + end + end + + def it_should_have_a_nested_fieldset + it "should have a nested_fieldset" do + output_buffer.should have_tag("form li fieldset") + end + end + + def it_should_have_label_with_text(string_or_regex) + it "should have a label with text '#{string_or_regex}'" do + output_buffer.should have_tag("form li label", string_or_regex) + end + end + + def it_should_have_label_for(element_id) + it "should have a label for ##{element_id}" do + output_buffer.should have_tag("form li label[@for='#{element_id}']") + end + end + + def it_should_have_input_with_id(element_id) + it "should have an input with id '#{element_id}'" do + output_buffer.should have_tag("form li input##{element_id}") + end + end + + def it_should_have_input_with_type(input_type) + it "should have a #{input_type} input" do + output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]") + end + end + + def it_should_have_input_with_name(name) + it "should have an input named #{name}" do + output_buffer.should have_tag("form li input[@name=\"#{name}\"]") + end + end + + def it_should_have_textarea_with_name(name) + it "should have an input named #{name}" do + output_buffer.should have_tag("form li textarea[@name=\"#{name}\"]") + end + end + + def it_should_have_textarea_with_id(element_id) + it "should have an input with id '#{element_id}'" do + output_buffer.should have_tag("form li textarea##{element_id}") + end + end + + def it_should_use_default_text_field_size_when_method_has_no_database_column(as) + it 'should use default_text_field_size when method has no database column' do + @new_post.stub!(:column_for_attribute).and_return(nil) # Return a nil column + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => as)) + end + output_buffer.should have_tag("form li input[@size='#{Formtastic::SemanticFormBuilder.default_text_field_size}']") + end + end + + def it_should_apply_custom_input_attributes_when_input_html_provided(as) + it 'it should apply custom input attributes when input_html provided' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => as, :input_html => { :class => 'myclass' })) + end + output_buffer.should have_tag("form li input.myclass") + end + end + + def it_should_apply_custom_for_to_label_when_input_html_id_provided(as) + it 'it should apply custom for to label when input_html :id provided' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => as, :input_html => { :id => 'myid' })) + end + output_buffer.should have_tag('form li label[@for="myid"]') + end + end + + def it_should_have_maxlength_matching_column_limit + it 'should have a maxlength matching column limit' do + @new_post.column_for_attribute(:title).limit.should == 50 + output_buffer.should have_tag("form li input[@maxlength='50']") + end + end + + def it_should_use_default_text_field_size_for_columns_longer_than_default_text_field_size(as) + it 'should use default_text_field_size for columns longer than default_text_field_size' do + default_size = Formtastic::SemanticFormBuilder.default_text_field_size + @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => as, :limit => default_size * 2)) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => as)) + end + + output_buffer.should have_tag("form li input[@size='#{default_size}']") + end + end + + def it_should_use_column_size_for_columns_shorter_than_default_text_field_size(as) + it 'should use the column size for columns shorter than default_text_field_size' do + column_limit_shorted_than_default = 1 + @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => as, :limit => column_limit_shorted_than_default)) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => as)) + end + + output_buffer.should have_tag("form li input[@size='#{column_limit_shorted_than_default}']") + end + end + + def it_should_apply_error_logic_for_input_type(type) + describe 'when there are errors on the object for this method' do + before do + @title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome'] + @errors = mock('errors') + @errors.stub!(:[]).with(:title).and_return(@title_errors) + @new_post.stub!(:errors).and_return(@errors) + end + + it 'should apply an errors class to the list item' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => type)) + end + output_buffer.should have_tag('form li.error') + end + + it 'should not wrap the input with the Rails default error wrapping' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => type)) + end + output_buffer.should_not have_tag('div.fieldWithErrors') + end + + it 'should render a paragraph for the errors' do + ::Formtastic::SemanticFormBuilder.inline_errors = :sentence + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => type)) + end + output_buffer.should have_tag('form li.error p.inline-errors') + end + + it 'should not display an error list' do + ::Formtastic::SemanticFormBuilder.inline_errors = :list + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => type)) + end + output_buffer.should have_tag('form li.error ul.errors') + end + end + + describe 'when there are no errors on the object for this method' do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => type)) + end + end + + it 'should not apply an errors class to the list item' do + output_buffer.should_not have_tag('form li.error') + end + + it 'should not render a paragraph for the errors' do + output_buffer.should_not have_tag('form li.error p.inline-errors') + end + + it 'should not display an error list' do + output_buffer.should_not have_tag('form li.error ul.errors') + end + end + + describe 'when no object is provided' do + before do + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:title, :as => type)) + end + end + + it 'should not apply an errors class to the list item' do + output_buffer.should_not have_tag('form li.error') + end + + it 'should not render a paragraph for the errors' do + output_buffer.should_not have_tag('form li.error p.inline-errors') + end + + it 'should not display an error list' do + output_buffer.should_not have_tag('form li.error ul.errors') + end + end + end + + def it_should_call_find_on_association_class_when_no_collection_is_provided(as) + it "should call find on the association class when no collection is provided" do + ::Author.should_receive(:find) + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => as)) + end + end + end + + def it_should_use_the_collection_when_provided(as, countable) + describe 'when the :collection option is provided' do + + before do + @authors = ::Author.find(:all) * 2 + output_buffer.replace '' # clears the output_buffer from the before block, hax! + end + + it 'should not call find() on the parent class' do + ::Author.should_not_receive(:find) + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => as, :collection => @authors)) + end + end + + it 'should use the provided collection' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => as, :collection => @authors)) + end + output_buffer.should have_tag("form li.#{as} #{countable}", :count => @authors.size + (as == :select ? 1 : 0)) + end + + describe 'and the :collection is an array of strings' do + before do + @categories = [ 'General', 'Design', 'Development', 'Quasi-Serious Inventions' ] + end + + it "should use the string as the label text and value for each #{countable}" do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:category_name, :as => as, :collection => @categories)) + end + + @categories.each do |value| + output_buffer.should have_tag("form li.#{as}", /#{value}/) + output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value}']") + end + end + + if as == :radio + it 'should generate a sanitized label for attribute' do + @bob.stub!(:category_name).and_return(@categories) + semantic_form_for(@new_post) do |builder| + builder.semantic_fields_for(@bob) do |bob_builder| + concat(bob_builder.input(:category_name, :as => as, :collection => @categories)) + end + end + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_general']") + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_design']") + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_development']") + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_quasiserious_inventions']") + end + end + end + + describe 'and the :collection is a hash of strings' do + before do + @categories = { 'General' => 'gen', 'Design' => 'des','Development' => 'dev' } + end + + it "should use the key as the label text and the hash value as the value attribute for each #{countable}" do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:category_name, :as => as, :collection => @categories)) + end + + @categories.each do |label, value| + output_buffer.should have_tag("form li.#{as}", /#{label}/) + output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value}']") + end + end + end + + describe 'and the :collection is an array of arrays' do + before do + @categories = { 'General' => 'gen', 'Design' => 'des', 'Development' => 'dev' }.to_a + end + + it "should use the first value as the label text and the last value as the value attribute for #{countable}" do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:category_name, :as => as, :collection => @categories)) + end + + @categories.each do |text, value| + label = as == :select ? :option : :label + output_buffer.should have_tag("form li.#{as} #{label}", /#{text}/i) + output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value.to_s}']") + output_buffer.should have_tag("form li.#{as} #{countable}#post_category_name_#{value.to_s}") if as == :radio + end + end + end + + if as == :radio + describe 'and the :collection is an array of arrays with boolean values' do + before do + @choices = { 'Yeah' => true, 'Nah' => false }.to_a + end + + it "should use the first value as the label text and the last value as the value attribute for #{countable}" do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:category_name, :as => as, :collection => @choices)) + end + + output_buffer.should have_tag("form li.#{as} #{countable}#post_category_name_true") + output_buffer.should have_tag("form li.#{as} #{countable}#post_category_name_false") + end + end + end + + describe 'and the :collection is an array of symbols' do + before do + @categories = [ :General, :Design, :Development ] + end + + it "should use the symbol as the label text and value for each #{countable}" do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:category_name, :as => as, :collection => @categories)) + end + + @categories.each do |value| + label = as == :select ? :option : :label + output_buffer.should have_tag("form li.#{as} #{label}", /#{value}/i) + output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value.to_s}']") + end + end + end + + describe 'and the :collection is an OrderedHash of strings' do + before do + @categories = ActiveSupport::OrderedHash.new('General' => 'gen', 'Design' => 'des','Development' => 'dev') + end + + it "should use the key as the label text and the hash value as the value attribute for each #{countable}" do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:category_name, :as => as, :collection => @categories)) + end + + @categories.each do |label, value| + output_buffer.should have_tag("form li.#{as}", /#{label}/) + output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{value}']") + end + end + + end + + describe 'when the :label_method option is provided' do + + describe 'as a symbol' do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => as, :label_method => :login)) + end + end + + it 'should have options with text content from the specified method' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li.#{as}", /#{author.login}/) + end + end + end + + describe 'as a proc' do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => as, :label_method => Proc.new {|a| a.login.reverse })) + end + end + + it 'should have options with the proc applied to each' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li.#{as}", /#{author.login.reverse}/) + end + end + end + + end + + describe 'when the :label_method option is not provided' do + ::Formtastic::SemanticFormBuilder.collection_label_methods.each do |label_method| + + describe "when the collection objects respond to #{label_method}" do + before do + @fred.stub!(:respond_to?).and_return { |m| m.to_s == label_method } + ::Author.find(:all).each { |a| a.stub!(label_method).and_return('The Label Text') } + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => as)) + end + end + + it "should render the options with #{label_method} as the label" do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li.#{as}", /The Label Text/) + end + end + end + + end + end + + describe 'when the :value_method option is provided' do + + describe 'as a symbol' do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => as, :value_method => :login)) + end + end + + it 'should have options with values from specified method' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{author.login}']") + end + end + end + + describe 'as a proc' do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => as, :value_method => Proc.new {|a| a.login.reverse })) + end + end + + it 'should have options with the proc applied to each value' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li.#{as} #{countable}[@value='#{author.login.reverse}']") + end + end + end + end + + end + end + + end +end \ No newline at end of file diff --git a/vendor/gems/formtastic-0.9.10/spec/defaults_spec.rb b/vendor/gems/formtastic-0.9.10/spec/defaults_spec.rb new file mode 100644 index 0000000..fab91f1 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/defaults_spec.rb @@ -0,0 +1,20 @@ +# coding: utf-8 +require File.join(File.dirname(__FILE__), *%w[spec_helper]) + +describe 'Formtastic::SemanticFormBuilder-defaults' do + + # Note: This spec might make better sense somewhere else. Just temporary. + + describe "required string" do + + it "should render proc with I18n correctly" do + ::I18n.backend.store_translations :en, :formtastic => {:required => 'Haha!'} + + required_string = Formtastic::SemanticFormBuilder.required_string + required_string = required_string.is_a?(::Proc) ? required_string.call : required_string.to_s + required_string.should == %{*} + end + + end + +end \ No newline at end of file diff --git a/vendor/plugins/formtastic/spec/error_proc_spec.rb b/vendor/gems/formtastic-0.9.10/spec/error_proc_spec.rb similarity index 92% rename from vendor/plugins/formtastic/spec/error_proc_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/error_proc_spec.rb index 38ddaab..6225e78 100644 --- a/vendor/plugins/formtastic/spec/error_proc_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/error_proc_spec.rb @@ -1,5 +1,5 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe 'Rails field_error_proc' do diff --git a/vendor/plugins/formtastic/spec/errors_spec.rb b/vendor/gems/formtastic-0.9.10/spec/errors_spec.rb similarity index 58% rename from vendor/plugins/formtastic/spec/errors_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/errors_spec.rb index da51774..b765332 100644 --- a/vendor/plugins/formtastic/spec/errors_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/errors_spec.rb @@ -1,5 +1,5 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe 'SemanticFormBuilder#errors_on' do @@ -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| @@ -34,9 +34,16 @@ end end end + + it 'should render a paragraph with the first error when inline_errors config is :first' do + ::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 @@ -51,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 @@ -66,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 @@ -74,5 +81,24 @@ end end + + describe 'when there are errors on the association and column' do + + it "should list all unique errors" do + ::Formtastic::SemanticFormBuilder.inline_errors = :list + ::Post.stub!(:reflections).and_return({:author => mock('reflection', :options => {}, :macro => :belongs_to)}) + + @errors.stub!(:[]).with(:author).and_return(['must not be blank']) + @errors.stub!(:[]).with(:author_id).and_return(['is already taken', 'must not be blank']) # note the duplicate of association + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author)) + end + output_buffer.should have_tag("ul.errors li", /must not be blank/, :count => 1) + output_buffer.should have_tag("ul.errors li", /is already taken/, :count => 1) + end + + end + end diff --git a/vendor/plugins/formtastic/spec/form_helper_spec.rb b/vendor/gems/formtastic-0.9.10/spec/form_helper_spec.rb similarity index 78% rename from vendor/plugins/formtastic/spec/form_helper_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/form_helper_spec.rb index 548c315..ef48e51 100644 --- a/vendor/plugins/formtastic/spec/form_helper_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/form_helper_spec.rb @@ -1,5 +1,5 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe 'SemanticFormHelper' do @@ -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 @@ -40,6 +40,12 @@ output_buffer.should have_tag("form.post") end + it 'adds a namespaced class to the generated form' do + semantic_form_for(::Namespaced::Post.new, :url => '/hello') do |builder| + end + output_buffer.should have_tag("form.namespaced_post") + end + describe 'allows :html options' do before(:each) do semantic_form_for(:post, ::Post.new, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder| @@ -79,13 +85,23 @@ builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere? end end - + + describe "with :builder option" do + it "yields an instance of the given builder" do + class MyAwesomeCustomBuilder < ::Formtastic::SemanticFormBuilder + end + semantic_form_for(:post, ::Post.new, :url => '/hello', :builder => MyAwesomeCustomBuilder) do |builder| + builder.class.should == MyAwesomeCustomBuilder + end + end + end + end 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 +109,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 +117,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 diff --git a/vendor/gems/formtastic-0.9.10/spec/i18n_spec.rb b/vendor/gems/formtastic-0.9.10/spec/i18n_spec.rb new file mode 100644 index 0000000..b9cfb40 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/i18n_spec.rb @@ -0,0 +1,148 @@ +# coding: utf-8 +require File.join(File.dirname(__FILE__), *%w[spec_helper]) + +describe 'Formtastic::I18n' do + + FORMTASTIC_KEYS = [:required, :yes, :no, :create, :update].freeze + + it "should be defined" do + lambda { ::Formtastic::I18n }.should_not raise_error(::NameError) + end + + describe "default translations" do + it "should be defined" do + lambda { ::Formtastic::I18n::DEFAULT_VALUES }.should_not raise_error(::NameError) + ::Formtastic::I18n::DEFAULT_VALUES.is_a?(::Hash).should == true + end + + it "should exists for the core I18n lookup keys" do + (::Formtastic::I18n::DEFAULT_VALUES.keys & FORMTASTIC_KEYS).size.should == FORMTASTIC_KEYS.size + end + end + + describe "when I18n locales are available" do + + before do + @formtastic_strings = { + :yes => 'Default Yes', + :no => 'Default No', + :create => 'Default Create {{model}}', + :update => 'Default Update {{model}}', + :custom_scope => { + :duck => 'Duck', + :duck_pond => '{{ducks}} ducks in a pond' + } + } + ::I18n.backend.store_translations :en, :formtastic => @formtastic_strings + end + + after do + ::I18n.backend.reload! + end + + it "should translate core strings correctly" do + ::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}} + ::Formtastic::I18n.t(:required).should == "Default Required" + ::Formtastic::I18n.t(:yes).should == "Default Yes" + ::Formtastic::I18n.t(:no).should == "Default No" + ::Formtastic::I18n.t(:create, :model => 'Post').should == "Default Create Post" + ::Formtastic::I18n.t(:update, :model => 'Post').should == "Default Update Post" + end + + it "should all belong to scope 'formtastic'" do + ::Formtastic::I18n.t(:duck, :scope => [:custom_scope]).should == 'Duck' + end + + it "should override default I18n lookup args if these are specified" do + ::Formtastic::I18n.t(:duck_pond, :scope => [:custom_scope], :ducks => 15).should == '15 ducks in a pond' + end + + it "should be possible to override default values" do + ::Formtastic::I18n.t(:required, :default => 'Nothing found!').should == 'Nothing found!' + end + + end + + describe "when no I18n locales are available" do + + before do + ::I18n.backend.reload! + end + + it "should use default strings" do + (::Formtastic::I18n::DEFAULT_VALUES.keys).each do |key| + ::Formtastic::I18n.t(key, :model => '{{model}}').should == ::Formtastic::I18n::DEFAULT_VALUES[key] + end + end + + end + + describe "I18n string lookups" do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + ::I18n.backend.store_translations :en, :formtastic => { + :labels => { + :title => "Hello world!", + :post => {:title => "Hello post!"}, + :project => {:title => "Hello project!", :task => {:name => "Hello task name!"}}, + :line_item => {:name => "Hello line item name!"} + } + } + ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true + + @new_post.stub!(:title) + @new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 255)) + end + + after do + ::I18n.backend.reload! + ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false + end + + it "lookup scopes should be defined" do + lambda { ::Formtastic::I18n::SCOPES }.should_not raise_error(::NameError) + end + + it "should be able to translate with namespaced object" do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) + end + output_buffer.should have_tag("form label", /Hello post!/) + end + + it "should be able to translate without form-object" do + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:title)) + end + output_buffer.should have_tag("form label", /Hello project!/) + end + + it 'should be able to translate nested objects with nested translations' do + semantic_form_for(:project, :url => 'http://test.host') do |builder| + builder.semantic_fields_for(:task) do |f| + concat(f.input(:name)) + end + end + output_buffer.should have_tag("form label", /Hello task name!/) + end + + it 'should be able to translated nested objects with top level translations' do + semantic_form_for(:order, :url => 'http://test.host') do |builder| + builder.semantic_fields_for(:line_item) do |f| + concat(f.input(:name)) + end + end + output_buffer.should have_tag("form label", /Hello line item name!/) + end + + + # TODO: Add spec for namespaced models? + + end + +end \ No newline at end of file diff --git a/vendor/plugins/formtastic/spec/include_blank_spec.rb b/vendor/gems/formtastic-0.9.10/spec/include_blank_spec.rb similarity index 97% rename from vendor/plugins/formtastic/spec/include_blank_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/include_blank_spec.rb index a3922ae..29174eb 100644 --- a/vendor/plugins/formtastic/spec/include_blank_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/include_blank_spec.rb @@ -1,5 +1,5 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe "*select: options[:include_blank]" do diff --git a/vendor/gems/formtastic-0.9.10/spec/input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/input_spec.rb new file mode 100644 index 0000000..2d9f826 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/input_spec.rb @@ -0,0 +1,629 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/spec_helper' + +describe 'SemanticFormBuilder#input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + end + + describe 'with inline order customization' do + it 'should allow input, hints, errors as order' do + ::Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors] + + semantic_form_for(@new_post) do |builder| + builder.should_receive(:inline_input_for).once.ordered + builder.should_receive(:inline_hints_for).once.ordered + builder.should_receive(:inline_errors_for).once.ordered + concat(builder.input(:title)) + end + end + + it 'should allow hints, input, errors as order' do + ::Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors] + + semantic_form_for(@new_post) do |builder| + builder.should_receive(:inline_hints_for).once.ordered + builder.should_receive(:inline_input_for).once.ordered + builder.should_receive(:inline_errors_for).once.ordered + concat(builder.input(:title)) + end + end + end + + describe 'arguments and options' do + + it 'should require the first argument (the method on form\'s object)' do + lambda { + semantic_form_for(@new_post) do |builder| + concat(builder.input()) # no args passed in at all + end + }.should raise_error(ArgumentError) + end + + describe ':required option' do + + describe 'when true' do + + before do + @old_string = ::Formtastic::SemanticFormBuilder.required_string + @new_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 = @old_string + end + + it 'should set a "required" class' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :required => true)) + end + output_buffer.should_not have_tag('form li.optional') + output_buffer.should have_tag('form li.required') + end + + it 'should append the "required" string to the label' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :required => true)) + end + output_buffer.should have_tag('form li.required label', /#{@new_string}$/) + end + + end + + describe 'when false' do + + before do + @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 = '' + end + + it 'should set an "optional" class' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :required => false)) + end + output_buffer.should_not have_tag('form li.required') + output_buffer.should have_tag('form li.optional') + end + + it 'should append the "optional" string to the label' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :required => false)) + end + output_buffer.should have_tag('form li.optional label', /#{@string}$/) + end + + end + + describe 'when not provided' do + + 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 + + semantic_form_for(:project, :url => 'http://test.host/') do |builder| + concat(builder.input(:title)) + end + 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 + end + + end + + describe 'and an object was given' do + + describe 'and the validation reflection plugin is available' do + + before do + @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true) + end + + describe 'and validates_presence_of was called for the method' do + it 'should be required' do + @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([ + mock('MacroReflection', :macro => :validates_presence_of, :name => :title, :options => nil) + ]) + @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([ + mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => {:if => true}) + ]) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) + concat(builder.input(:body)) + end + output_buffer.should have_tag('form li.required') + output_buffer.should_not have_tag('form li.optional') + end + + it 'should be not be required if the optional :if condition is not satisifed' do + should_be_required(:required => false, :options => { :if => false }) + end + + it 'should not be required if the optional :if proc evaluates to false' do + should_be_required(:required => false, :options => { :if => proc { |record| false } }) + end + + it 'should be required if the optional :if proc evaluates to true' do + should_be_required(:required => true, :options => { :if => proc { |record| true } }) + end + + it 'should not be required if the optional :unless proc evaluates to true' do + should_be_required(:required => false, :options => { :unless => proc { |record| true } }) + end + + it 'should be required if the optional :unless proc evaluates to false' do + should_be_required(:required => true, :options => { :unless => proc { |record| false } }) + end + + it 'should be required if the optional :if with a method string evaluates to true' do + @new_post.should_receive(:required_condition).and_return(true) + should_be_required(:required => true, :options => { :if => :required_condition }) + end + + it 'should be required if the optional :if with a method string evaluates to false' do + @new_post.should_receive(:required_condition).and_return(false) + should_be_required(:required => false, :options => { :if => :required_condition }) + end + + it 'should not be required if the optional :unless with a method string evaluates to false' do + @new_post.should_receive(:required_condition).and_return(false) + should_be_required(:required => true, :options => { :unless => :required_condition }) + end + + it 'should be required if the optional :unless with a method string evaluates to true' do + @new_post.should_receive(:required_condition).and_return(true) + should_be_required(:required => false, :options => { :unless => :required_condition }) + end + end + + # TODO make a matcher for this? + def should_be_required(options) + @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([ + mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => options[:options]) + ]) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:body)) + end + + if options[:required] + output_buffer.should_not have_tag('form li.optional') + output_buffer.should have_tag('form li.required') + else + output_buffer.should have_tag('form li.optional') + output_buffer.should_not have_tag('form li.required') + end + end + + describe 'and validates_presence_of was not called for the method' do + before do + @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([]) + end + + it 'should not be required' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) + end + output_buffer.should_not have_tag('form li.required') + output_buffer.should have_tag('form li.optional') + end + end + + end + + 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 + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) + end + 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 + end + + end + + end + + end + + end + + describe ':as option' do + + describe 'when not provided' do + + it 'should default to a string for forms without objects unless column is password' do + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:anything)) + end + output_buffer.should have_tag('form li.string') + end + + it 'should default to password for forms without objects if column is password' do + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:password)) + concat(builder.input(:password_confirmation)) + concat(builder.input(:confirm_password)) + end + output_buffer.should have_tag('form li.password', :count => 3) + end + + it 'should default to a string for methods on objects that don\'t respond to "column_for_attribute"' do + @new_post.stub!(:method_without_a_database_column) + @new_post.stub!(:column_for_attribute).and_return(nil) + default_input_type(nil, :method_without_a_database_column).should == :string + end + + it 'should default to :password for methods that don\'t have a column in the database but "password" is in the method name' do + @new_post.stub!(:password_method_without_a_database_column) + @new_post.stub!(:column_for_attribute).and_return(nil) + default_input_type(nil, :password_method_without_a_database_column).should == :password + end + + it 'should default to :password for methods on objects that don\'t respond to "column_for_attribute" but "password" is in the method name' do + @new_post.stub!(:password_method_without_a_database_column) + @new_post.stub!(:column_for_attribute).and_return(nil) + default_input_type(nil, :password_method_without_a_database_column).should == :password + end + + it 'should default to :select for column names ending in "_id"' do + default_input_type(:integer, :user_id).should == :select + default_input_type(:integer, :section_id).should == :select + end + + it 'should default to :password for :string column types with "password" in the method name' do + default_input_type(:string, :password).should == :password + default_input_type(:string, :hashed_password).should == :password + default_input_type(:string, :password_hash).should == :password + end + + it 'should default to :text for :text column types' do + default_input_type(:text).should == :text + end + + it 'should default to :date for :date column types' do + default_input_type(:date).should == :date + end + + it 'should default to :datetime for :datetime and :timestamp column types' do + default_input_type(:datetime).should == :datetime + default_input_type(:timestamp).should == :datetime + end + + it 'should default to :time for :time column types' do + default_input_type(:time).should == :time + end + + it 'should default to :boolean for :boolean column types' do + default_input_type(:boolean).should == :boolean + end + + it 'should default to :string for :string column types' do + default_input_type(:string).should == :string + end + + it 'should default to :numeric for :integer, :float and :decimal column types' do + default_input_type(:integer).should == :numeric + default_input_type(:float).should == :numeric + default_input_type(:decimal).should == :numeric + end + + it 'should default to :country for :string columns named country' do + default_input_type(:string, :country).should == :country + end + + describe 'defaulting to file column' do + ::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| + column.stub!(:respond_to?).with(test).and_return(method == test) + end + + @new_post.should_receive(method).and_return(column) + + semantic_form_for(@new_post) do |builder| + builder.send(:default_input_type, method).should == :file + end + end + end + + end + end + + it 'should call the corresponding input method' do + [:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes, :hidden].each do |input_style| + @new_post.stub!(:generic_column_name) + @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255)) + semantic_form_for(@new_post) do |builder| + builder.should_receive(:"#{input_style}_input").once.and_return("fake HTML output from #input") + concat(builder.input(:generic_column_name, :as => input_style)) + end + end + + [:string, :password, :numeric, :text, :file].each do |input_style| + @new_post.stub!(:generic_column_name) + @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255)) + semantic_form_for(@new_post) do |builder| + builder.should_receive(:basic_input_helper).once.and_return("fake HTML output from #input") + concat(builder.input(:generic_column_name, :as => input_style)) + end + end + end + + end + + describe ':label option' do + + describe 'when provided' do + it 'should be passed down to the label tag' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :label => "Kustom")) + end + output_buffer.should have_tag("form li label", /Kustom/) + end + + it 'should not generate a label if false' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :label => false)) + end + output_buffer.should_not have_tag("form li label") + end + + it 'should be dupped if frozen' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :label => "Kustom".freeze)) + end + output_buffer.should have_tag("form li label", /Kustom/) + end + end + + describe 'when not provided' do + describe 'when localized label is provided' do + describe 'and object is given' do + describe 'and label_str_method not default' do + it 'should render a label with localized label (I18n)' do + with_config :label_str_method, :capitalize do + @localized_label_text = 'Localized title' + @new_post.stub!(:meta_description) + @new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return(@localized_label_text) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:meta_description)) + end + + output_buffer.should have_tag('form li label', @localized_label_text) + end + end + end + end + end + + 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 + + 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 + + describe 'and object is given with label_str_method set to :capitalize' do + it 'should capitalize method name, passing it down to the label tag' do + with_config :label_str_method, :capitalize do + @new_post.stub!(:meta_description) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:meta_description)) + end + + output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/) + end + end + end + end + + describe 'when localized label is provided' do + before do + @localized_label_text = 'Localized title' + @default_localized_label_text = 'Default localized title' + ::I18n.backend.store_translations :en, + :formtastic => { + :labels => { + :title => @default_localized_label_text, + :published => @default_localized_label_text, + :post => { + :title => @localized_label_text, + :published => @default_localized_label_text + } + } + } + ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false + end + + it 'should render a label with localized label (I18n)' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :label => true)) + concat(builder.input(:published, :as => :boolean, :label => true)) + end + output_buffer.should have_tag('form li label', @localized_label_text) + end + + it 'should render a hint paragraph containing an optional localized label (I18n) if first is not set' do + ::I18n.backend.store_translations :en, + :formtastic => { + :labels => { + :post => { + :title => nil, + :published => nil + } + } + } + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :label => true)) + concat(builder.input(:published, :as => :boolean, :label => true)) + end + output_buffer.should have_tag('form li label', @default_localized_label_text) + end + end + end + + end + + describe ':hint option' do + + describe 'when provided' do + it 'should be passed down to the paragraph tag' do + hint_text = "this is the title of the post" + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :hint => hint_text)) + end + output_buffer.should have_tag("form li p.inline-hints", hint_text) + end + end + + describe 'when not provided' do + describe 'when localized hint (I18n) is provided' do + before do + @localized_hint_text = "This is the localized hint." + @default_localized_hint_text = "This is the default localized hint." + ::I18n.backend.store_translations :en, + :formtastic => { + :hints => { + :title => @default_localized_hint_text, + } + } + ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false + end + + after do + ::I18n.backend.reload! + end + + describe 'when provided value (hint value) is set to TRUE' do + it 'should render a hint paragraph containing a localized hint (I18n)' do + ::I18n.backend.store_translations :en, + :formtastic => { + :hints => { + :post => { + :title => @localized_hint_text + } + } + } + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :hint => true)) + end + output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text) + end + + it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :hint => true)) + end + output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text) + end + end + + describe 'when provided value (label value) is set to FALSE' do + it 'should not render a hint paragraph' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :hint => false)) + end + output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text) + end + end + end + + describe 'when localized hint (I18n) is not provided' do + it 'should not render a hint paragraph' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) + end + output_buffer.should_not have_tag('form li p.inline-hints') + end + end + end + + end + + describe ':wrapper_html option' do + + describe 'when provided' do + it 'should be passed down to the li tag' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :wrapper_html => {:id => :another_id})) + end + output_buffer.should have_tag("form li#another_id") + end + + it 'should append given classes to li default classes' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true)) + end + output_buffer.should have_tag("form li.string") + output_buffer.should have_tag("form li.required") + output_buffer.should have_tag("form li.another_class") + end + + it 'should allow classes to be an array' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]})) + end + output_buffer.should have_tag("form li.string") + output_buffer.should have_tag("form li.my_class") + output_buffer.should have_tag("form li.another_class") + end + end + + describe 'when not provided' do + it 'should use default id and class' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) + end + output_buffer.should have_tag("form li#post_title_input") + output_buffer.should have_tag("form li.string") + end + end + + end + end + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/boolean_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/boolean_input_spec.rb new file mode 100644 index 0000000..138ee5d --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/boolean_input_spec.rb @@ -0,0 +1,97 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'boolean input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:allow_comments, :as => :boolean)) + end + end + + it_should_have_input_wrapper_with_class("boolean") + it_should_have_input_wrapper_with_id("post_allow_comments_input") + it_should_apply_error_logic_for_input_type(:boolean) + + it 'should generate a label containing the input' do + output_buffer.should have_tag('form li label', :count => 1) + output_buffer.should have_tag('form li label[@for="post_allow_comments"]') + output_buffer.should have_tag('form li label', /Allow comments/) + output_buffer.should have_tag('form li label input[@type="checkbox"]') + end + + it 'should generate a checkbox input' do + output_buffer.should have_tag('form li label input') + output_buffer.should have_tag('form li label input#post_allow_comments') + output_buffer.should have_tag('form li label input[@type="checkbox"]') + output_buffer.should have_tag('form li label input[@name="post[allow_comments]"]') + output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]') + end + + it 'should allow checked and unchecked values to be sent' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked')) + end + + output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]') + output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]') + end + + it 'should generate a label and a checkbox even if no object is given' do + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:allow_comments, :as => :boolean)) + end + + output_buffer.should have_tag('form li label[@for="project_allow_comments"]') + output_buffer.should have_tag('form li label', /Allow comments/) + output_buffer.should have_tag('form li label input[@type="checkbox"]') + + output_buffer.should have_tag('form li label input#project_allow_comments') + output_buffer.should have_tag('form li label input[@type="checkbox"]') + output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]') + end + + describe 'when :selected is set' do + before do + @output_buffer = '' + end + + describe "not selected" do + before do + @new_post.stub!(:allow_comments).and_return(true) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:allow_comments, :as => :boolean, :selected => false)) + end + end + end + + it 'should not be selected' do + output_buffer.should_not have_tag("form li label input[@type='checkbox'][@checked='checked']") + end + end + + describe "selected" do + before do + @new_post.stub!(:allow_comments).and_return(false) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:allow_comments, :as => :boolean, :selected => true)) + end + end + end + + it 'should be selected' do + output_buffer.should have_tag("form li label input[@type='checkbox'][@checked='checked']") + end + end + end + +end diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/check_boxes_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/check_boxes_input_spec.rb new file mode 100644 index 0000000..f0017bd --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/check_boxes_input_spec.rb @@ -0,0 +1,240 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'check_boxes input' do + + include FormtasticSpecHelper + + describe 'for a has_many association' do + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@fred) do |builder| + concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true)) + end + end + + it_should_have_input_wrapper_with_class("check_boxes") + it_should_have_input_wrapper_with_id("author_posts_input") + it_should_have_a_nested_fieldset + it_should_apply_error_logic_for_input_type(:check_boxes) + it_should_call_find_on_association_class_when_no_collection_is_provided(:check_boxes) + it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]') + + it 'should generate a legend containing a label with text for the input' do + output_buffer.should have_tag('form li fieldset legend.label label') + output_buffer.should have_tag('form li fieldset legend.label label', /Posts/) + end + + it 'should not link the label within the legend to any input' do + output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]') + end + + + it 'should generate an ordered list with a list item for each choice' do + output_buffer.should have_tag('form li fieldset ol') + output_buffer.should have_tag('form li fieldset ol li', :count => ::Post.find(:all).size) + end + + it 'should have one option with a "checked" attribute' do + output_buffer.should have_tag('form li input[@checked]', :count => 1) + end + + it 'should generate hidden inputs with default value blank' do + output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size) + end + + describe "each choice" do + + it 'should contain a label for the radio input with a nested input and label text' do + ::Post.find(:all).each do |post| + output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/) + output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']") + end + end + + it 'should use values as li.class when value_as_class is true' do + ::Post.find(:all).each do |post| + output_buffer.should have_tag("form li fieldset ol li.post_#{post.id} label") + end + end + + it 'should have a checkbox input for each post' do + ::Post.find(:all).each do |post| + output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}") + output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2) + end + end + + it "should mark input as checked if it's the the existing choice" do + ::Post.find(:all).include?(@fred.posts.first).should be_true + output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']") + end + end + + describe 'and no object is given' do + before(:each) do + output_buffer.replace '' + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.find(:all))) + end + end + + it 'should generate a fieldset with legend' do + output_buffer.should have_tag('form li fieldset legend', /Author/) + end + + it 'shold generate an li tag for each item in the collection' do + output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size) + end + + it 'should generate labels for each item' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/) + output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']") + end + end + + it 'should generate inputs for each item' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}") + output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']") + output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']") + output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']") + end + end + end + + describe 'when :selected is set' do + before do + @output_buffer = '' + end + + describe "no selected items" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :check_boxes, :selected => nil)) + end + end + end + + it 'should not have any selected item(s)' do + output_buffer.should_not have_tag("form li fieldset ol li label input[@checked='checked']") + end + end + + describe "single selected item" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :check_boxes, :selected => @fred.id)) + end + end + end + + it "should have one item selected; the specified one" do + output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']", :count => 1) + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i) + output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@fred.id}']") + end + end + + describe "multiple selected items" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :check_boxes, :selected => [@bob.id, @fred.id])) + end + end + end + + it "should have multiple items selected; the specified ones" do + output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']", :count => 2) + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i) + output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@bob.id}']") + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i) + output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked'][@value='#{@fred.id}']") + end + end + + end + + it 'should warn about :selected deprecation' do + with_deprecation_silenced do + ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :check_boxes, :selected => @bob.id)) + end + end + end + + + describe 'when :disabled is set' do + before do + @output_buffer = '' + end + + describe "no disabled items" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :check_boxes, :disabled => nil)) + end + end + + it 'should not have any disabled item(s)' do + output_buffer.should_not have_tag("form li fieldset ol li label input[@disabled='disabled']") + end + end + + describe "single disabled item" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :check_boxes, :disabled => @fred.id)) + end + end + + it "should have one item disabled; the specified one" do + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1) + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i) + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']") + end + end + + describe "multiple disabled items" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :check_boxes, :disabled => [@bob.id, @fred.id])) + end + end + + it "should have multiple items disabled; the specified ones" do + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2) + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i) + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']") + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i) + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']") + end + end + + end + + end + + + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/country_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/country_input_spec.rb new file mode 100644 index 0000000..525da86 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/country_input_spec.rb @@ -0,0 +1,114 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'country input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + end + + describe "when country_select is not available as a helper from a plugin" do + + it "should raise an error, sugesting the author installs a plugin" do + lambda { + semantic_form_for(@new_post) do |builder| + concat(builder.input(:country, :as => :country)) + end + }.should raise_error + end + + end + + describe "when country_select is available as a helper (from a plugin)" do + + before do + semantic_form_for(@new_post) do |builder| + builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("")) + concat(builder.input(:country, :as => :country)) + end + end + + it_should_have_input_wrapper_with_class("country") + it_should_have_input_wrapper_with_id("post_country_input") + + # TODO -- needs stubbing inside the builder block, tricky! + #it_should_apply_error_logic_for_input_type(:country) + + it 'should generate a label for the input' do + output_buffer.should have_tag('form li label') + output_buffer.should have_tag('form li label[@for="post_country"]') + output_buffer.should have_tag('form li label', /Country/) + end + + it "should generate a select" do + output_buffer.should have_tag("form li select") + end + + end + + describe ":priority_countries option" do + + it "should be passed down to the country_select helper when provided" do + priority_countries = ["Foo", "Bah"] + semantic_form_for(@new_post) do |builder| + builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("")) + builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return(Formtastic::Util.html_safe("")) + + concat(builder.input(:country, :as => :country, :priority_countries => priority_countries)) + end + end + + it "should default to the @@priority_countries config when absent" do + priority_countries = ::Formtastic::SemanticFormBuilder.priority_countries + priority_countries.should_not be_empty + priority_countries.should_not be_nil + + semantic_form_for(@new_post) do |builder| + builder.stub!(:country_select).and_return("") + builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return(Formtastic::Util.html_safe("")) + + concat(builder.input(:country, :as => :country)) + end + end + + end + + describe "matching" do + + describe "when the attribute is 'country'" do + + before do + semantic_form_for(@new_post) do |builder| + builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("")) + concat(builder.input(:country)) + end + end + + it "should render a country input" do + output_buffer.should have_tag "form li.country" + end + end + + describe "whent the attribute is 'country_something'" do + + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:country_subdivision)) + concat(builder.input(:country_code)) + end + end + + it "should render a country input" do + output_buffer.should_not have_tag "form li.country" + output_buffer.should have_tag "form li.string", :count => 2 + end + + end + + end + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/date_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/date_input_spec.rb new file mode 100644 index 0000000..1ed149f --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/date_input_spec.rb @@ -0,0 +1,156 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'date input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + end + + describe "general" do + + before do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:publish_at, :as => :date)) + end + end + + it_should_have_input_wrapper_with_class("date") + it_should_have_input_wrapper_with_id("post_publish_at_input") + it_should_have_a_nested_fieldset + it_should_apply_error_logic_for_input_type(:date) + + it 'should have a legend and label with the label text inside the fieldset' do + output_buffer.should have_tag('form li.date fieldset legend.label label', /Publish at/) + end + + it 'should associate the legend label with the first select' do + output_buffer.should have_tag('form li.date fieldset legend.label') + output_buffer.should have_tag('form li.date fieldset legend.label label') + output_buffer.should have_tag('form li.date fieldset legend.label label[@for]') + output_buffer.should have_tag('form li.date fieldset legend.label label[@for="post_publish_at_1i"]') + end + + it 'should have an ordered list of three items inside the fieldset' do + output_buffer.should have_tag('form li.date fieldset ol') + output_buffer.should have_tag('form li.date fieldset ol li', :count => 3) + end + + it 'should have three labels for year, month and day' do + output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3) + output_buffer.should have_tag('form li.date fieldset ol li label', /year/i) + output_buffer.should have_tag('form li.date fieldset ol li label', /month/i) + output_buffer.should have_tag('form li.date fieldset ol li label', /day/i) + end + + it 'should have three selects for year, month and day' do + output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3) + end + end + + describe ':selected option' do + + describe "when the object has a value" do + it "should select the object value (ignoring :selected)" do + output_buffer.replace '' + @new_post.stub!(:created_at => Time.mktime(2012)) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999))) + end + end + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1) + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='2012'][@selected]", :count => 1) + end + end + + describe 'when the object has no value (nil)' do + it "should select the :selected if provided as a Date" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date, :selected => Date.new(1999))) + end + end + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1) + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1) + end + + it "should select the :selected if provided as a Time" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999))) + end + end + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1) + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1) + end + + it "should not select an option if the :selected is provided as nil" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date, :selected => nil)) + end + end + output_buffer.should_not have_tag("form li ol li select#post_created_at_1i option[@selected]") + end + + it "should select nothing if a :selected is not provided" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date)) + end + output_buffer.should_not have_tag("form li ol li select option[@selected]") + + end + end + + end + + describe ':labels option' do + fields = [:year, :month, :day] + fields.each do |field| + it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date, :labels => { field => "another #{field} label" })) + end + output_buffer.should have_tag('form li.date fieldset ol li label', :count => fields.length) + fields.each do |f| + output_buffer.should have_tag('form li.date fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i) + end + end + + it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date, :labels => { field => "" })) + end + output_buffer.should have_tag('form li.date fieldset ol li label', :count => fields.length-1) + fields.each do |f| + output_buffer.should have_tag('form li.date fieldset ol li label', /#{f}/i) unless field == f + end + end + end + end + + it 'should warn about :selected deprecation' do + with_deprecation_silenced do + ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date, :selected => Date.new(1999))) + end + end + end + +end diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/datetime_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/datetime_input_spec.rb new file mode 100644 index 0000000..0fb1ac3 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/datetime_input_spec.rb @@ -0,0 +1,291 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'datetime input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + @new_post.should_receive(:publish_at=).any_number_of_times + @new_post.should_receive(:created_at=).any_number_of_times + @bob.should_receive(:created_at=).any_number_of_times + @new_post.should_receive(:title=).any_number_of_times # Macro stuff forces this. + end + + describe "general" do + + before do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:publish_at, :as => :datetime)) + end + end + + it_should_have_input_wrapper_with_class("datetime") + it_should_have_input_wrapper_with_id("post_publish_at_input") + it_should_have_a_nested_fieldset + it_should_apply_error_logic_for_input_type(:datetime) + + it 'should have a legend and label with the label text inside the fieldset' do + output_buffer.should have_tag('form li.datetime fieldset legend.label label', /Publish at/) + end + + it 'should associate the legend label with the first select' do + output_buffer.should have_tag('form li.datetime fieldset legend.label label[@for="post_publish_at_1i"]') + end + + it 'should have an ordered list of five items inside the fieldset' do + output_buffer.should have_tag('form li.datetime fieldset ol') + output_buffer.should have_tag('form li.datetime fieldset ol li', :count => 5) + end + + it 'should have five labels for year, month, day, hour and minute' do + output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /year/i) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /month/i) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /day/i) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /hour/i) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /minute/i) + end + + it 'should have five selects for year, month, day, hour and minute' do + output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5) + end + + it 'should generate a sanitized label and matching ids for attribute' do + semantic_form_for(@new_post) do |builder| + builder.semantic_fields_for(@bob, :index => 10) do |bob_builder| + concat(bob_builder.input(:created_at, :as => :datetime)) + end + end + + 1.upto(5) do |i| + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_10_created_at_#{i}i']") + output_buffer.should have_tag("form li fieldset ol li #post_author_10_created_at_#{i}i") + end + end + end + + describe 'when :discard_input => true is set' do + it 'should use default attribute value when it is not nil' do + @new_post.stub!(:publish_at).and_return(Date.new(2007,12,27)) + semantic_form_for(@new_post) do |builder| + concat(builder.input(:publish_at, :as => :datetime, :discard_day => true)) + end + + output_buffer.should have_tag("form li input[@type='hidden'][@value='27']") + end + end + + describe 'inputs order' do + it 'should have a default' do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + self.should_receive(:select_year).once.ordered.and_return('') + self.should_receive(:select_month).once.ordered.and_return('') + self.should_receive(:select_day).once.ordered.and_return('') + builder.input(:publish_at, :as => :datetime) + end + end + + it 'should be specified with :order option' do + ::I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] } + output_buffer.replace '' + 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('') + self.should_receive(:select_day).once.ordered.and_return('') + builder.input(:publish_at, :as => :datetime) + ::I18n.backend.store_translations 'en', :date => nil + end + end + + it 'should be changed through I18n' do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + self.should_receive(:select_day).once.ordered.and_return('') + self.should_receive(:select_month).once.ordered.and_return('') + self.should_receive(:select_year).once.ordered.and_return('') + builder.input(:publish_at, :as => :datetime, :order => [:day, :month, :year]) + end + end + end + + describe 'when the locale changes the label text' do + before do + ::I18n.backend.store_translations 'en', :datetime => {:prompts => { + :year => 'The Year', :month => 'The Month', :day => 'The Day', + :hour => 'The Hour', :minute => 'The Minute' + }} + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:publish_at, :as => :datetime)) + end + end + + after do + ::I18n.backend.store_translations 'en', :formtastic => { + :year => nil, :month => nil, :day => nil, + :hour => nil, :minute => nil + } + end + + it 'should have translated labels for year, month, day, hour and minute' do + output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Year/) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Month/) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Day/) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Hour/) + output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Minute/) + end + end + + describe 'when no object is given' do + before(:each) do + output_buffer.replace '' + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:publish_at, :as => :datetime)) + end + end + + it 'should have fieldset with legend - classified as a label' do + output_buffer.should have_tag('form li.datetime fieldset legend.label', /Publish at/) + end + + it 'should have labels for each input' do + output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5) + end + + it 'should have selects for each inputs' do + output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5) + end + end + + #describe 'when an object is given and the attribute value is nil' do + # before(:each) do + # @new_post.stub!(:publish_at).and_return(nil) + # output_buffer.replace '' + # semantic_form_for(@new_post) do |builder| + # concat(builder.input(:publish_at, :as => :datetime)) + # end + # end + # + # it 'should not select a value' do + # output_buffer.should_not have_tag('form li.datetime option[@selected]') + # end + # + #end + + describe ':selected option' do + + describe "when the object has a value" do + it "should select the object value (ignoring :selected)" do + output_buffer.replace '' + @new_post.stub!(:created_at => Time.mktime(2012)) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :datetime, :selected => Time.mktime(1999))) + end + end + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1) + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='2012'][@selected]", :count => 1) + end + end + + describe 'when the object has no value' do + it "should select the :selected if provided as a Date" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :datetime, :selected => Date.new(1999))) + end + end + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1) + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1) + end + + it "should select the :selected if provided as a Time" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :datetime, :selected => Time.mktime(1999))) + end + end + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@selected]", :count => 1) + output_buffer.should have_tag("form li ol li select#post_created_at_1i option[@value='1999'][@selected]", :count => 1) + end + + it "should not select an option if the :selected is provided as nil" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :datetime, :selected => nil)) + end + end + output_buffer.should_not have_tag("form li ol li select#post_created_at_1i option[@selected]") + end + + it "should select nothing if a :selected is not provided" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :datetime)) + end + output_buffer.should_not have_tag("form li ol li select option[@selected]") + end + end + + it 'should warn about :selected deprecation' do + with_deprecation_silenced do + ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :date, :selected => Time.mktime(1999))) + end + end + end + + end + + describe ':labels option' do + fields = [:year, :month, :day, :hour, :minute] + fields.each do |field| + it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :datetime, :labels => { field => "another #{field} label" })) + end + output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => fields.length) + fields.each do |f| + output_buffer.should have_tag('form li.datetime fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i) + end + end + + it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :datetime, :labels => { field => "" })) + end + output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => fields.length-1) + fields.each do |f| + output_buffer.should have_tag('form li.datetime fieldset ol li label', /#{f}/i) unless field == f + end + end + end + end + + it 'should warn about :selected deprecation' do + with_deprecation_silenced do + ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :datetime, :selected => Time.mktime(1999))) + end + end + end + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/file_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/file_input_spec.rb new file mode 100644 index 0000000..0354835 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/file_input_spec.rb @@ -0,0 +1,33 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'file input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:body, :as => :file)) + end + end + + it_should_have_input_wrapper_with_class("file") + it_should_have_input_wrapper_with_id("post_body_input") + it_should_have_label_with_text(/Body/) + it_should_have_label_for("post_body") + it_should_have_input_with_id("post_body") + it_should_have_input_with_name("post[body]") + it_should_apply_error_logic_for_input_type(:file) + + it 'should use input_html to style inputs' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => :file, :input_html => { :class => 'myclass' })) + end + output_buffer.should have_tag("form li input.myclass") + end + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/hidden_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/hidden_input_spec.rb new file mode 100644 index 0000000..c1f3b60 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/hidden_input_spec.rb @@ -0,0 +1,61 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'hidden input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:secret, :as => :hidden)) + concat(builder.input(:author_id, :as => :hidden, :value => 99)) + concat(builder.input(:published, :as => :hidden, :input_html => {:value => true})) + end + end + + it_should_have_input_wrapper_with_class("hidden") + it_should_have_input_wrapper_with_id("post_secret_input") + it_should_not_have_a_label + + it "should generate a input field" do + output_buffer.should have_tag("form li input#post_secret") + output_buffer.should have_tag("form li input#post_secret[@type=\"hidden\"]") + output_buffer.should have_tag("form li input#post_secret[@name=\"post[secret]\"]") + end + + it "should pass any explicitly specified value - using :value" do + output_buffer.should have_tag("form li input#post_author_id[@type=\"hidden\"][@value=\"99\"]") + end + + # Handle Formtastic :input_html options for consistency. + it "should pass any explicitly specified value - using :input_html options" do + output_buffer.should have_tag("form li input#post_published[@type=\"hidden\"][@value=\"true\"]") + end + + it "should not render inline errors" do + @errors = mock('errors') + @errors.stub!(:[]).with(:secret).and_return(["foo", "bah"]) + @new_post.stub!(:errors).and_return(@errors) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:secret, :as => :hidden)) + end + + output_buffer.should_not have_tag("form li p.inline-errors") + output_buffer.should_not have_tag("form li ul.errors") + end + + it "should not render inline hints" do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:secret, :as => :hidden, :hint => "all your base are belong to use")) + end + + output_buffer.should_not have_tag("form li p.inline-hints") + output_buffer.should_not have_tag("form li ul.hints") + end + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/numeric_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/numeric_input_spec.rb new file mode 100644 index 0000000..9eb5b7a --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/numeric_input_spec.rb @@ -0,0 +1,44 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'numeric input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => :numeric)) + end + end + + it_should_have_input_wrapper_with_class(:numeric) + it_should_have_input_wrapper_with_id("post_title_input") + it_should_have_label_with_text(/Title/) + it_should_have_label_for("post_title") + it_should_have_input_with_id("post_title") + it_should_have_input_with_type(:text) + it_should_have_input_with_name("post[title]") + it_should_use_default_text_field_size_when_method_has_no_database_column(:string) + it_should_apply_custom_input_attributes_when_input_html_provided(:string) + it_should_apply_custom_for_to_label_when_input_html_id_provided(:string) + it_should_apply_error_logic_for_input_type(:numeric) + + describe "when no object is provided" do + before do + semantic_form_for(:project, :url => 'http://test.host/') do |builder| + concat(builder.input(:title, :as => :numeric)) + end + end + + it_should_have_label_with_text(/Title/) + it_should_have_label_for("project_title") + it_should_have_input_with_id("project_title") + it_should_have_input_with_type(:text) + it_should_have_input_with_name("project[title]") + end + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/password_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/password_input_spec.rb new file mode 100644 index 0000000..cfc37d0 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/password_input_spec.rb @@ -0,0 +1,46 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'password input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => :password)) + end + end + + it_should_have_input_wrapper_with_class(:password) + it_should_have_input_wrapper_with_id("post_title_input") + it_should_have_label_with_text(/Title/) + it_should_have_label_for("post_title") + it_should_have_input_with_id("post_title") + it_should_have_input_with_type(:password) + it_should_have_input_with_name("post[title]") + it_should_have_maxlength_matching_column_limit + it_should_use_default_text_field_size_for_columns_longer_than_default_text_field_size(:string) + it_should_use_column_size_for_columns_shorter_than_default_text_field_size(:string) + it_should_use_default_text_field_size_when_method_has_no_database_column(:string) + it_should_apply_custom_input_attributes_when_input_html_provided(:string) + it_should_apply_custom_for_to_label_when_input_html_id_provided(:string) + it_should_apply_error_logic_for_input_type(:password) + + describe "when no object is provided" do + before do + semantic_form_for(:project, :url => 'http://test.host/') do |builder| + concat(builder.input(:title, :as => :password)) + end + end + + it_should_have_label_with_text(/Title/) + it_should_have_label_for("project_title") + it_should_have_input_with_id("project_title") + it_should_have_input_with_type(:password) + it_should_have_input_with_name("project[title]") + end + +end diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/radio_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/radio_input_spec.rb new file mode 100644 index 0000000..8ba38e6 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/radio_input_spec.rb @@ -0,0 +1,155 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'radio input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + end + + describe 'for belongs_to association' do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :radio, :value_as_class => true)) + end + end + + it_should_have_input_wrapper_with_class("radio") + it_should_have_input_wrapper_with_id("post_author_input") + it_should_have_a_nested_fieldset + it_should_apply_error_logic_for_input_type(:radio) + it_should_use_the_collection_when_provided(:radio, 'input') + + it 'should generate a legend containing a label with text for the input' do + output_buffer.should have_tag('form li fieldset legend.label label') + output_buffer.should have_tag('form li fieldset legend.label label', /Author/) + end + + it 'should not link the label within the legend to any input' do + output_buffer.should_not have_tag('form li fieldset legend label[@for^="post_author_id_"]') + end + + it 'should generate an ordered list with a list item for each choice' do + output_buffer.should have_tag('form li fieldset ol') + output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size) + end + + it 'should have one option with a "checked" attribute' do + output_buffer.should have_tag('form li input[@checked]', :count => 1) + end + + describe "each choice" do + + it 'should contain a label for the radio input with a nested input and label text' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/) + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_id_#{author.id}']") + end + end + + it 'should use values as li.class when value_as_class is true' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li fieldset ol li.author_#{author.id} label") + end + end + + it "should have a radio input" do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li fieldset ol li label input#post_author_id_#{author.id}") + output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']") + output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']") + output_buffer.should have_tag("form li fieldset ol li label input[@name='post[author_id]']") + end + end + + it "should mark input as checked if it's the the existing choice" do + @new_post.author_id.should == @bob.id + @new_post.author.id.should == @bob.id + @new_post.author.should == @bob + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :radio)) + end + + output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']") + end + end + + describe 'and no object is given' do + before(:each) do + output_buffer.replace '' + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:author_id, :as => :radio, :collection => ::Author.find(:all))) + end + end + + it 'should generate a fieldset with legend' do + output_buffer.should have_tag('form li fieldset legend', /Author/) + end + + it 'should generate an li tag for each item in the collection' do + output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size) + end + + it 'should generate labels for each item' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/) + output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']") + end + end + + it 'should generate inputs for each item' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}") + output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']") + output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']") + output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id]']") + end + end + end + end + + describe 'when :selected is set' do + before do + @output_buffer = '' + end + + describe "no selected items" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :radio, :selected => nil)) + end + end + end + + it 'should not have any selected item(s)' do + output_buffer.should_not have_tag("form li fieldset ol li label input[@checked='checked']") + end + end + + describe "single selected item" do + before do + @new_post.stub!(:author_ids).and_return(nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :radio, :selected => @fred.id)) + end + end + end + + it "should have one item selected; the specified one" do + output_buffer.should have_tag("form li fieldset ol li label input[@type='radio'][@checked='checked']", :count => 1) + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i) + output_buffer.should have_tag("form li fieldset ol li label input[@type='radio'][@checked='checked'][@value='#{@fred.id}']") + end + end + + end + +end diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/select_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/select_input_spec.rb new file mode 100644 index 0000000..b91de3a --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/select_input_spec.rb @@ -0,0 +1,506 @@ +# coding: utf-8 +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 boolean columns' 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 + + describe 'for a belongs_to association' do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :select)) + concat(builder.input(:reviewer, :as => :select)) + end + end + + it_should_have_input_wrapper_with_class("select") + it_should_have_input_wrapper_with_id("post_author_input") + it_should_have_label_with_text(/Author/) + it_should_have_label_for('post_author_id') + it_should_apply_error_logic_for_input_type(:select) + it_should_call_find_on_association_class_when_no_collection_is_provided(:select) + it_should_use_the_collection_when_provided(:select, 'option') + + it 'should have a select inside the wrapper' do + output_buffer.should have_tag('form li select') + output_buffer.should have_tag('form li select#post_author_id') + output_buffer.should have_tag('form li select#post_reviewer_id') + end + + it 'should have a valid name' do + output_buffer.should have_tag("form li select[@name='post[author_id]']") + output_buffer.should_not have_tag("form li select[@name='post[author_id][]']") + output_buffer.should_not have_tag("form li select[@name='post[reviewer_id][]']") + end + + it 'should not create a multi-select' do + output_buffer.should_not have_tag('form li select[@multiple]') + end + + it 'should create a select without size' do + output_buffer.should_not have_tag('form li select[@size]') + end + + it 'should have a blank option' do + output_buffer.should have_tag("form li select option[@value='']") + end + + it 'should have a select option for each Author' do + output_buffer.should have_tag("form li select[@name='post[author_id]'] option", :count => ::Author.find(:all).size + 1) + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/) + end + end + + it 'should have one option with a "selected" attribute' do + output_buffer.should have_tag("form li select[@name='post[author_id]'] option[@selected]", :count => 1) + end + + it 'should not singularize the association name' do + @new_post.stub!(:author_status).and_return(@bob) + @new_post.stub!(:author_status_id).and_return(@bob.id) + @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255)) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author_status, :as => :select)) + end + + output_buffer.should have_tag('form li select#post_author_status_id') + end + end + + describe "for a belongs_to association with :group_by => :author" do + it "should call author.posts" do + [@freds_post].each { |post| post.stub!(:to_label).and_return("Post - #{post.id}") } + @fred.should_receive(:posts) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:main_post, :as => :select, :group_by => :author ) ) + end + end + end + + describe "for a belongs_to association with :conditions" do + before do + ::Post.stub!(:reflect_on_association).with(:author).and_return do + mock = mock('reflection', :options => {:conditions => {:active => true}}, :klass => ::Author, :macro => :belongs_to) + mock.stub!(:[]).with(:class_name).and_return("Author") + mock + end + end + + it "should call author.find with association conditions" do + ::Author.should_receive(:merge_conditions).with({:active => true}, nil).and_return(:active => true) + ::Author.should_receive(:find).with(:all, :conditions => {:active => true}) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :select)) + end + end + + it "should call author.find with association conditions and find_options conditions" do + ::Author.should_receive(:merge_conditions).with({:active => true}, {:publisher => true}).and_return(:active => true, :publisher => true) + ::Author.should_receive(:find).with(:all, :conditions => {:active => true, :publisher => true}) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :select, :find_options => {:conditions => {:publisher => true}})) + end + end + end + + describe 'for a belongs_to association with :group_by => :continent' do + before do + @authors = [@bob, @fred, @fred, @fred] + ::Author.stub!(:find).and_return(@authors) + @continent_names = %w(Europe Africa) + @continents = (0..1).map { |i| c = ::Continent.new; c.stub!(:id).and_return(100 - i);c } + @authors[0..1].each_with_index { |author, i| author.stub!(:continent).and_return(@continents[i]) } + ::Continent.stub!(:reflect_on_all_associations).and_return {|macro| mock('reflection', :klass => Author) if macro == :has_many} + ::Continent.stub!(:reflect_on_association).and_return {|column_name| mock('reflection', :klass => Author) if column_name == :authors} + ::Author.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => Continent, :macro => :belongs_to) if column_name == :continent } + + + @continents.each_with_index do |continent, i| + continent.stub!(:to_label).and_return(@continent_names[i]) + continent.stub!(:authors).and_return([@authors[i]]) + end + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :select, :group_by => :continent ) ) + concat(builder.input(:author, :as => :select, :group_by => :continent, :group_label_method => :id ) ) + end + end + + it_should_have_input_wrapper_with_class("select") + it_should_have_input_wrapper_with_id("post_author_input") + it_should_have_label_with_text(/Author/) + it_should_have_label_for('post_author_id') + + # TODO, need to find a way to repeat some of the specs and logic from the belongs_to specs without grouping + + 0.upto(1) do |i| + it 'should have all option groups and the right values' do + output_buffer.should have_tag("form li select optgroup[@label='#{@continent_names[i]}']", @authors[i].to_label) + end + + it 'should have custom group labels' do + output_buffer.should have_tag("form li select optgroup[@label='#{@continents[i].id}']", @authors[i].to_label) + end + end + + it 'should have no duplicate groups' do + output_buffer.should have_tag('form li select optgroup', :count => 4) + end + + it 'should sort the groups on the label method' do + output_buffer.should have_tag("form li select optgroup[@label='Africa']") + output_buffer.should have_tag("form li select optgroup[@label='99']") + end + + it 'should call find with :include for more optimized queries' do + Author.should_receive(:find).with(:all, :include => :continent) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :select, :group_by => :continent ) ) + end + end + end + + describe 'for a has_many association' do + before do + semantic_form_for(@fred) do |builder| + concat(builder.input(:posts, :as => :select)) + end + end + + it_should_have_input_wrapper_with_class("select") + it_should_have_input_wrapper_with_id("author_posts_input") + it_should_have_label_with_text(/Post/) + it_should_have_label_for('author_post_ids') + it_should_apply_error_logic_for_input_type(:select) + it_should_call_find_on_association_class_when_no_collection_is_provided(:select) + it_should_use_the_collection_when_provided(:select, 'option') + + it 'should have a select inside the wrapper' do + output_buffer.should have_tag('form li select') + output_buffer.should have_tag('form li select#author_post_ids') + end + + it 'should have a multi-select select' do + output_buffer.should have_tag('form li select[@multiple="multiple"]') + end + + it 'should have a select option for each Post' do + output_buffer.should have_tag('form li select option', :count => ::Post.find(:all).size) + ::Post.find(:all).each do |post| + output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/) + end + end + + it 'should not have a blank option' do + output_buffer.should_not have_tag("form li select option[@value='']") + end + + it 'should have one option with a "selected" attribute' do + output_buffer.should have_tag('form li select option[@selected]', :count => 1) + end + end + + describe 'for a has_and_belongs_to_many association' do + before do + semantic_form_for(@freds_post) do |builder| + concat(builder.input(:authors, :as => :select)) + end + end + + it_should_have_input_wrapper_with_class("select") + it_should_have_input_wrapper_with_id("post_authors_input") + it_should_have_label_with_text(/Author/) + it_should_have_label_for('post_author_ids') + it_should_apply_error_logic_for_input_type(:select) + it_should_call_find_on_association_class_when_no_collection_is_provided(:select) + it_should_use_the_collection_when_provided(:select, 'option') + + it 'should have a select inside the wrapper' do + output_buffer.should have_tag('form li select') + output_buffer.should have_tag('form li select#post_author_ids') + end + + it 'should have a multi-select select' do + output_buffer.should have_tag('form li select[@multiple="multiple"]') + end + + it 'should have a select option for each Author' do + output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size) + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/) + end + end + + it 'should not have a blank option' do + output_buffer.should_not have_tag("form li select option[@value='']") + end + + it 'should have one option with a "selected" attribute' do + output_buffer.should have_tag('form li select option[@selected]', :count => 1) + end + end + + describe 'when :prompt => "choose something" is set' do + before do + @new_post.stub!(:author_id).and_return(nil) + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :select, :prompt => "choose author")) + end + end + + it 'should have a select with prompt' do + output_buffer.should have_tag("form li select option[@value='']", /choose author/) + end + + it 'should not have a blank select option' do + output_buffer.should_not have_tag("form li select option[@value='']", "") + end + end + + describe 'when no object is given' do + before(:each) do + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:author, :as => :select, :collection => ::Author.find(:all))) + end + end + + it 'should generate label' do + output_buffer.should have_tag('form li label', /Author/) + output_buffer.should have_tag("form li label[@for='project_author']") + end + + it 'should generate select inputs' do + output_buffer.should have_tag('form li select#project_author') + output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1) + end + + it 'should generate an option to each item' do + ::Author.find(:all).each do |author| + output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/) + end + end + end + + describe 'when :selected is set' do + before do + @output_buffer = '' + end + + describe "no selected items" do + before do + @new_post.stub!(:author_id).and_return(nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :select, :selected => nil)) + end + end + end + + it 'should not have any selected item(s)' do + output_buffer.should_not have_tag("form li select option[@selected='selected']") + end + end + + describe "single selected item" do + before do + @new_post.stub!(:author_id).and_return(nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author, :as => :select, :selected => @bob.id)) + end + end + end + + it 'should have a selected item; the specified one' do + output_buffer.should have_tag("form li select option[@selected='selected']", :count => 1) + output_buffer.should have_tag("form li select option[@selected='selected']", /bob/i) + output_buffer.should have_tag("form li select option[@selected='selected'][@value='#{@bob.id}']") + end + end + + describe "multiple selected items" do + + describe "when :multiple => false" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :select, :selected => [@bob.id, @fred.id], :multiple => false)) + end + end + end + + it "should only select the first value" do + output_buffer.should have_tag("form li select option[@selected='selected']", :count => 1) + output_buffer.should have_tag("form li select:not([@multiple]) option[@selected='selected']", /bob/i) + output_buffer.should have_tag("form li select:not([@multiple]) option[@selected='selected'][@value='#{@bob.id}']") + end + end + + describe "when :multiple => true" do + before do + @new_post.stub!(:author_ids).and_return(nil) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:authors, :as => :select, :selected => [@bob.id, @fred.id])) + end + end + end + + it "should have multiple items selected; the specified ones" do + output_buffer.should have_tag("form li select option[@selected='selected']", :count => 2) + output_buffer.should have_tag("form li select[@multiple] option[@selected='selected']", /bob/i) + output_buffer.should have_tag("form li select[@multiple] option[@selected='selected'][@value='#{@bob.id}']") + output_buffer.should have_tag("form li select[@multiple] option[@selected='selected']", /fred/i) + output_buffer.should have_tag("form li select[@multiple] option[@selected='selected'][@value='#{@fred.id}']") + end + end + + end + + end + + describe "enum" do + before do + @output_buffer = '' + @some_meta_descriptions = ["One", "Two", "Three"] + @new_post.stub!(:meta_description).any_number_of_times + end + + describe ":as is not set" do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:meta_description, :collection => @some_meta_descriptions)) + end + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:meta_description, :collection => @some_meta_descriptions)) + end + end + + it "should render a select field" do + output_buffer.should have_tag("form li select", :count => 2) + end + end + + describe ":as is set" do + before do + # Should not be a case, but just checking :as got highest priority in setting input type. + semantic_form_for(@new_post) do |builder| + concat(builder.input(:meta_description, :as => :string, :collection => @some_meta_descriptions)) + end + semantic_form_for(:project, :url => 'http://test.host') do |builder| + concat(builder.input(:meta_description, :as => :string, :collection => @some_meta_descriptions)) + end + end + + it "should render a text field" do + output_buffer.should have_tag("form li input[@type='text']", :count => 2) + end + end + end + + it 'should warn about :selected deprecation' do + with_deprecation_silenced do + ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times + semantic_form_for(@new_post) do |builder| + concat(builder.input(:author_id, :as => :select, :selected => @bob.id)) + end + end + end + + + +end diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/string_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/string_input_spec.rb new file mode 100644 index 0000000..5175e8e --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/string_input_spec.rb @@ -0,0 +1,47 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'string input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => :string)) + end + end + + it_should_have_input_wrapper_with_class(:string) + it_should_have_input_wrapper_with_id("post_title_input") + it_should_have_label_with_text(/Title/) + it_should_have_label_for("post_title") + it_should_have_input_with_id("post_title") + it_should_have_input_with_type(:text) + it_should_have_input_with_name("post[title]") + it_should_have_maxlength_matching_column_limit + it_should_use_default_text_field_size_for_columns_longer_than_default_text_field_size(:string) + it_should_use_column_size_for_columns_shorter_than_default_text_field_size(:string) + it_should_use_default_text_field_size_when_method_has_no_database_column(:string) + it_should_apply_custom_input_attributes_when_input_html_provided(:string) + it_should_apply_custom_for_to_label_when_input_html_id_provided(:string) + it_should_apply_error_logic_for_input_type(:string) + + describe "when no object is provided" do + before do + semantic_form_for(:project, :url => 'http://test.host/') do |builder| + concat(builder.input(:title, :as => :string)) + end + end + + it_should_have_label_with_text(/Title/) + it_should_have_label_for("project_title") + it_should_have_input_with_id("project_title") + it_should_have_input_with_type(:text) + it_should_have_input_with_name("project[title]") + end + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/text_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/text_input_spec.rb new file mode 100644 index 0000000..55f107a --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/text_input_spec.rb @@ -0,0 +1,33 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'text input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:body, :as => :text)) + end + end + + it_should_have_input_wrapper_with_class("text") + it_should_have_input_wrapper_with_id("post_body_input") + it_should_have_label_with_text(/Body/) + it_should_have_label_for("post_body") + it_should_have_textarea_with_id("post_body") + it_should_have_textarea_with_name("post[body]") + it_should_apply_error_logic_for_input_type(:numeric) + + it 'should use input_html to style inputs' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :as => :text, :input_html => { :class => 'myclass' })) + end + output_buffer.should have_tag("form li textarea.myclass") + end + +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/time_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/time_input_spec.rb new file mode 100644 index 0000000..ae0b360 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/time_input_spec.rb @@ -0,0 +1,170 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'time input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + end + + describe "general" do + before do + ::I18n.backend.reload! + output_buffer.replace '' + end + + describe "without seconds" do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:publish_at, :as => :time)) + end + end + + it_should_have_input_wrapper_with_class("time") + it_should_have_input_wrapper_with_id("post_publish_at_input") + it_should_have_a_nested_fieldset + it_should_apply_error_logic_for_input_type(:time) + + it 'should have a legend and label with the label text inside the fieldset' do + output_buffer.should have_tag('form li.time fieldset legend.label label', /Publish at/) + end + + it 'should associate the legend label with the first select' do + output_buffer.should have_tag('form li.time fieldset legend.label label[@for="post_publish_at_1i"]') + end + + it 'should have an ordered list of two items inside the fieldset' do + output_buffer.should have_tag('form li.time fieldset ol') + output_buffer.should have_tag('form li.time fieldset ol li', :count => 2) + end + + it 'should have five labels for hour and minute' do + output_buffer.should have_tag('form li.time fieldset ol li label', :count => 2) + output_buffer.should have_tag('form li.time fieldset ol li label', /hour/i) + output_buffer.should have_tag('form li.time fieldset ol li label', /minute/i) + end + + it 'should have two selects for hour and minute' do + output_buffer.should have_tag('form li.time fieldset ol li', :count => 2) + end + end + + describe "with seconds" do + before do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:publish_at, :as => :time, :include_seconds => true)) + end + end + + it 'should have five labels for hour and minute' do + output_buffer.should have_tag('form li.time fieldset ol li label', :count => 3) + output_buffer.should have_tag('form li.time fieldset ol li label', /hour/i) + output_buffer.should have_tag('form li.time fieldset ol li label', /minute/i) + output_buffer.should have_tag('form li.time fieldset ol li label', /second/i) + end + + it 'should have three selects for hour, minute and seconds' do + output_buffer.should have_tag('form li.time fieldset ol li', :count => 3) + end + + it 'should generate a sanitized label and matching ids for attribute' do + 4.upto(6) do |i| + output_buffer.should have_tag("form li fieldset ol li label[@for='post_publish_at_#{i}i']") + output_buffer.should have_tag("form li fieldset ol li #post_publish_at_#{i}i") + end + end + end + end + + describe ':selected option' do + + describe "when the object has a value" do + it "should select the object value (ignoring :selected)" do + output_buffer.replace '' + @new_post.stub!(:created_at => Time.mktime(2012, 11, 30, 21, 45)) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :time, :selected => Time.mktime(1999, 12, 31, 22, 59))) + end + end + output_buffer.should have_tag("form li ol li select#post_created_at_4i option[@selected]", :count => 1) + output_buffer.should have_tag("form li ol li select#post_created_at_4i option[@value='21'][@selected]", :count => 1) + end + end + + describe 'when the object has no value (nil)' do + it "should select the :selected if provided as a Time" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :time, :selected => Time.mktime(1999, 12, 31, 22, 59))) + end + end + output_buffer.should have_tag("form li ol li select#post_created_at_4i option[@selected]", :count => 1) + output_buffer.should have_tag("form li ol li select#post_created_at_4i option[@value='22'][@selected]", :count => 1) + end + + it "should not select an option if the :selected is provided as nil" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :time, :selected => nil)) + end + end + output_buffer.should_not have_tag("form li ol li select#post_created_at_4i option[@selected]") + end + + it "should select nothing if a :selected is not provided" do + output_buffer.replace '' + @new_post.stub!(:created_at => nil) + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :time)) + end + output_buffer.should_not have_tag("form li ol li select option[@selected]") + end + end + + end + + describe ':labels option' do + fields = [:hour, :minute, :second] + fields.each do |field| + it "should replace the #{field} label with the specified text if :labels[:#{field}] is set" do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :time, :include_seconds => true, :labels => { field => "another #{field} label" })) + end + output_buffer.should have_tag('form li.time fieldset ol li label', :count => fields.length) + fields.each do |f| + output_buffer.should have_tag('form li.time fieldset ol li label', f == field ? /another #{f} label/i : /#{f}/i) + end + end + + it "should not display the label for the #{field} field when :labels[:#{field}] is blank" do + output_buffer.replace '' + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :time, :include_seconds => true, :labels => { field => "" })) + end + output_buffer.should have_tag('form li.time fieldset ol li label', :count => fields.length-1) + fields.each do |f| + output_buffer.should have_tag('form li.time fieldset ol li label', /#{f}/i) unless field == f + end + end + end + end + + it 'should warn about :selected deprecation' do + with_deprecation_silenced do + ::ActiveSupport::Deprecation.should_receive(:warn).any_number_of_times + semantic_form_for(@new_post) do |builder| + concat(builder.input(:created_at, :as => :time, :selected => Time.mktime(1999))) + end + end + end + +end diff --git a/vendor/gems/formtastic-0.9.10/spec/inputs/time_zone_input_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs/time_zone_input_spec.rb new file mode 100644 index 0000000..860455a --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/inputs/time_zone_input_spec.rb @@ -0,0 +1,104 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe 'time_zone input' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:time_zone)) + end + end + + it_should_have_input_wrapper_with_class("time_zone") + it_should_have_input_wrapper_with_id("post_time_zone_input") + it_should_apply_error_logic_for_input_type(:time_zone) + + it 'should generate a label for the input' do + output_buffer.should have_tag('form li label') + output_buffer.should have_tag('form li label[@for="post_time_zone"]') + output_buffer.should have_tag('form li label', /Time zone/) + end + + it "should generate a select" do + output_buffer.should have_tag("form li select") + output_buffer.should have_tag("form li select#post_time_zone") + output_buffer.should have_tag("form li select[@name=\"post[time_zone]\"]") + end + + it 'should use input_html to style inputs' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:time_zone, :input_html => { :class => 'myclass' })) + end + output_buffer.should have_tag("form li select.myclass") + end + + describe 'when no object is given' do + before(:each) do + semantic_form_for(:project, :url => 'http://test.host/') do |builder| + concat(builder.input(:time_zone, :as => :time_zone)) + end + end + + it 'should generate labels' do + output_buffer.should have_tag('form li label') + output_buffer.should have_tag('form li label[@for="project_time_zone"]') + output_buffer.should have_tag('form li label', /Time zone/) + end + + it 'should generate select inputs' do + output_buffer.should have_tag("form li select") + output_buffer.should have_tag("form li select#project_time_zone") + output_buffer.should have_tag("form li select[@name=\"project[time_zone]\"]") + end + end + + describe 'when :selected is set' do + before do + @output_buffer = '' + end + + # Note: Not possible to override default selected value for time_zone input + # without overriding Rails time_zone_select. This Rails helper works "a bit different". =/ + # + # describe "no selected items" do + # before do + # @new_post.stub!(:time_zone).and_return('Stockholm') + # + # semantic_form_for(@new_post) do |builder| + # concat(builder.input(:time_zone, :as => :time_zone, :selected => nil)) + # end + # end + # + # it 'should not have any selected item(s)' do + # output_buffer.should_not have_tag("form li select option[@selected='selected']") + # end + # end + + describe "single selected item" do + before do + # Note: See above...only works for the "attribute is nil" case. + # @new_post.stub!(:time_zone).and_return('Stockholm') + @new_post.stub!(:time_zone).and_return(nil) + + with_deprecation_silenced do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:time_zone, :as => :time_zone, :selected => 'Melbourne')) + end + end + end + + it 'should have a selected item; the specified one' do + output_buffer.should have_tag("form li select option[@selected='selected']", :count => 1) + output_buffer.should have_tag("form li select option[@selected='selected']", /Melbourne/i) + output_buffer.should have_tag("form li select option[@selected='selected'][@value='Melbourne']") + end + end + + end + +end diff --git a/vendor/plugins/formtastic/spec/inputs_spec.rb b/vendor/gems/formtastic-0.9.10/spec/inputs_spec.rb similarity index 84% rename from vendor/plugins/formtastic/spec/inputs_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/inputs_spec.rb index c1b635e..a697281 100644 --- a/vendor/plugins/formtastic/spec/inputs_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/inputs_spec.rb @@ -1,5 +1,5 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe 'SemanticFormBuilder#inputs' do @@ -174,44 +174,62 @@ 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" + @nested_forms_legend_text = "This is a nested form title" 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 + builder.inputs @nested_forms_legend_text, :for => :authors do |nf| + end end 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}$/) + output_buffer.should have_tag("form fieldset legend", /^#{@nested_forms_legend_text}$/) 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" + @localized_nested_forms_legend_text = "This is a localized nested form title" + ::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, + :nested_forms_title => @localized_nested_forms_legend_text } } } 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 + builder.inputs :nested_forms_title, :for => :authors do |nf| 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}$/) + output_buffer.should have_tag("form fieldset legend", /^#{@localized_nested_forms_legend_text}$/) end end end @@ -238,9 +256,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 +369,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,10 +385,10 @@ 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 end - end diff --git a/vendor/plugins/formtastic/spec/label_spec.rb b/vendor/gems/formtastic-0.9.10/spec/label_spec.rb similarity index 83% rename from vendor/plugins/formtastic/spec/label_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/label_spec.rb index c08eea7..55d639f 100644 --- a/vendor/plugins/formtastic/spec/label_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/label_spec.rb @@ -1,5 +1,5 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe 'SemanticFormBuilder#label' do @@ -16,12 +16,6 @@ end end - it 'should be printed as span' do - semantic_form_for(@new_post) do |builder| - builder.label(:login, nil, { :required => true, :as_span => true }).should have_tag('span.label abbr') - end - end - describe 'when required is given' do it 'should append a required note' do semantic_form_for(@new_post) do |builder| diff --git a/vendor/gems/formtastic-0.9.10/spec/layout_helper_spec.rb b/vendor/gems/formtastic-0.9.10/spec/layout_helper_spec.rb new file mode 100644 index 0000000..832c651 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/layout_helper_spec.rb @@ -0,0 +1,29 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/spec_helper' + +describe 'LayoutHelper' do + + include FormtasticSpecHelper + include Formtastic::LayoutHelper + + before do + @output_buffer = '' + end + + describe '#formtastic_stylesheet_link_tag' do + + before do + concat(formtastic_stylesheet_link_tag()) + end + + it 'should render a link to formtastic.css' do + output_buffer.should have_tag("link[@href='/stylesheets/formtastic.css']") + end + + it 'should render a link to formtastic_changes.css' do + output_buffer.should have_tag("link[@href='/stylesheets/formtastic_changes.css']") + end + + end +end + diff --git a/vendor/gems/formtastic-0.9.10/spec/semantic_errors_spec.rb b/vendor/gems/formtastic-0.9.10/spec/semantic_errors_spec.rb new file mode 100644 index 0000000..992d926 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/semantic_errors_spec.rb @@ -0,0 +1,98 @@ +# coding: utf-8 +require File.dirname(__FILE__) + '/spec_helper' + +describe 'SemanticFormBuilder#semantic_errors' do + + include FormtasticSpecHelper + + before do + @output_buffer = '' + mock_everything + @title_errors = ['must not be blank', 'must be awesome'] + @base_errors = ['base error message', 'nasty error'] + @base_error = 'one base error' + @errors = mock('errors') + @new_post.stub!(:errors).and_return(@errors) + end + + describe 'when there is only one error on base' do + before do + @errors.stub!(:on_base).and_return(@base_error) + end + + it 'should render an unordered list' do + semantic_form_for(@new_post) do |builder| + builder.semantic_errors.should have_tag('ul.errors li', @base_error) + end + end + end + + describe 'when there is more than one error on base' do + before do + @errors.stub!(:on_base).and_return(@base_errors) + end + + it 'should render an unordered list' do + semantic_form_for(@new_post) do |builder| + builder.semantic_errors.should have_tag('ul.errors') + @base_errors.each do |error| + builder.semantic_errors.should have_tag('ul.errors li', error) + end + end + end + end + + describe 'when there are errors on title' do + before do + @errors.stub!(:[]).with(:title).and_return(@title_errors) + @errors.stub!(:on_base).and_return([]) + end + + it 'should render an unordered list' do + semantic_form_for(@new_post) do |builder| + title_name = builder.send(:localized_string, :title, :title, :label) || builder.send(:humanized_attribute_name, :title) + builder.semantic_errors(:title).should have_tag('ul.errors li', title_name << " " << @title_errors.to_sentence) + end + end + end + + describe 'when there are errors on title and base' do + before do + @errors.stub!(:[]).with(:title).and_return(@title_errors) + @errors.stub!(:on_base).and_return(@base_error) + end + + it 'should render an unordered list' do + semantic_form_for(@new_post) do |builder| + title_name = builder.send(:localized_string, :title, :title, :label) || builder.send(:humanized_attribute_name, :title) + builder.semantic_errors(:title).should have_tag('ul.errors li', title_name << " " << @title_errors.to_sentence) + builder.semantic_errors(:title).should have_tag('ul.errors li', @base_error) + end + end + end + + describe 'when there are no errors' do + before do + @errors.stub!(:[]).with(:title).and_return(nil) + @errors.stub!(:on_base).and_return(nil) + end + + it 'should return nil' do + semantic_form_for(@new_post) do |builder| + builder.semantic_errors(:title).should be_nil + end + end + end + + describe 'when there is one error on base and options with class is passed' do + before do + @errors.stub!(:on_base).and_return(@base_error) + end + + it 'should render an unordered list with given class' do + semantic_form_for(@new_post) do |builder| + builder.semantic_errors(:class => "awesome").should have_tag('ul.awesome li', @base_error) + end + end + end +end diff --git a/vendor/plugins/formtastic/spec/semantic_fields_for_spec.rb b/vendor/gems/formtastic-0.9.10/spec/semantic_fields_for_spec.rb similarity index 75% rename from vendor/plugins/formtastic/spec/semantic_fields_for_spec.rb rename to vendor/gems/formtastic-0.9.10/spec/semantic_fields_for_spec.rb index 963362e..c2cff85 100644 --- a/vendor/plugins/formtastic/spec/semantic_fields_for_spec.rb +++ b/vendor/gems/formtastic-0.9.10/spec/semantic_fields_for_spec.rb @@ -1,5 +1,5 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' +require File.dirname(__FILE__) + '/spec_helper' describe 'SemanticFormBuilder#semantic_fields_for' do @@ -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 @@ -35,7 +35,9 @@ end end output_buffer.should have_tag('form fieldset.inputs #post_author_1_login_input') - output_buffer.should_not have_tag('form fieldset.inputs #post[author]_1_login_input') + # Not valid selector, so using good ol' regex + output_buffer.should_not =~ /id="post\[author\]_1_login_input"/ + # <=> output_buffer.should_not have_tag('form fieldset.inputs #post[author]_1_login_input') end end diff --git a/vendor/gems/formtastic-0.9.10/spec/spec.opts b/vendor/gems/formtastic-0.9.10/spec/spec.opts new file mode 100644 index 0000000..8f815c6 --- /dev/null +++ b/vendor/gems/formtastic-0.9.10/spec/spec.opts @@ -0,0 +1,2 @@ +--color +--format=progress diff --git a/vendor/plugins/formtastic/spec/test_helper.rb b/vendor/gems/formtastic-0.9.10/spec/spec_helper.rb similarity index 52% rename from vendor/plugins/formtastic/spec/test_helper.rb rename to vendor/gems/formtastic-0.9.10/spec/spec_helper.rb index cba87ce..b1156d2 100644 --- a/vendor/plugins/formtastic/spec/test_helper.rb +++ b/vendor/gems/formtastic-0.9.10/spec/spec_helper.rb @@ -1,32 +1,30 @@ # coding: utf-8 require 'rubygems' -def smart_require(lib_name, gem_name, gem_version = '>= 0.0.0') - begin - require lib_name if lib_name - rescue LoadError - if gem_name - gem gem_name, gem_version - require lib_name if lib_name - end - end -end +gem 'activesupport', '2.3.8' +gem 'actionpack', '2.3.8' +require 'active_support' +require 'action_pack' +require 'action_view' +require 'action_controller' -smart_require 'spec', 'spec', '>= 1.2.6' -smart_require false, 'rspec-rails', '>= 1.2.6' -smart_require 'hpricot', 'hpricot', '>= 0.6.1' -smart_require 'rspec_hpricot_matchers', 'rspec_hpricot_matchers', '>= 1.0.0' -smart_require 'active_support', 'activesupport', '>= 2.3.4' -smart_require 'action_controller', 'actionpack', '>= 2.3.4' -smart_require 'action_view', 'actionpack', '>= 2.3.4' +gem 'rspec', '>= 1.2.6' +gem 'rspec-rails', '>= 1.2.6' +gem 'hpricot', '>= 0.6.1' +gem 'rspec_tag_matchers', '>= 1.0.0' +require 'rspec_tag_matchers' + +require 'custom_macros' Spec::Runner.configure do |config| - config.include(RspecHpricotMatchers) + config.include(RspecTagMatchers) + config.include(CustomMacros) end -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +require File.expand_path(File.join(File.dirname(__FILE__), '../lib/formtastic')) +require File.expand_path(File.join(File.dirname(__FILE__), '../lib/formtastic/util')) +require File.expand_path(File.join(File.dirname(__FILE__), '../lib/formtastic/layout_helper')) -require 'formtastic' module FormtasticSpecHelper include ActionView::Helpers::FormHelper @@ -39,6 +37,7 @@ module FormtasticSpecHelper include ActionView::Helpers::RecordIdentificationHelper include ActionView::Helpers::DateHelper include ActionView::Helpers::CaptureHelper + include ActionView::Helpers::AssetTagHelper include ActiveSupport include ActionController::PolymorphicRoutes @@ -59,7 +58,17 @@ class ::Post def id end end + module ::Namespaced + class Post + def id + end + end + end class ::Author + def to_label + end + end + class ::Continent end def mock_everything @@ -86,17 +95,30 @@ def new_author_path; "/authors/new"; end @bob.stub!(:class).and_return(::Author) @bob.stub!(:to_label).and_return('Bob Rock') @bob.stub!(:login).and_return('bob') + @bob.stub!(:created_at) @bob.stub!(:id).and_return(42) @bob.stub!(:posts).and_return([]) @bob.stub!(:post_ids).and_return([]) @bob.stub!(:new_record?).and_return(false) @bob.stub!(:errors).and_return(mock('errors', :[] => nil)) + + @james = mock('user') + @james.stub!(:class).and_return(::Author) + @james.stub!(:to_label).and_return('James Shock') + @james.stub!(:login).and_return('james') + @james.stub!(:id).and_return(75) + @james.stub!(:posts).and_return([]) + @james.stub!(:post_ids).and_return([]) + @james.stub!(:new_record?).and_return(false) + @james.stub!(:errors).and_return(mock('errors', :[] => nil)) + ::Author.stub!(:find).and_return([@fred, @bob]) ::Author.stub!(:human_attribute_name).and_return { |column_name| column_name.humanize } ::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') @@ -105,6 +127,9 @@ def new_author_path; "/authors/new"; end @new_post.stub!(:new_record?).and_return(true) @new_post.stub!(:errors).and_return(mock('errors', :[] => nil)) @new_post.stub!(:author).and_return(nil) + @new_post.stub!(:reviewer).and_return(nil) + @new_post.stub!(:main_post).and_return(nil) + @new_post.stub!(:sub_posts).and_return([]) #TODO should be a mock with methods for adding sub posts @freds_post = mock('post') @freds_post.stub!(:class).and_return(::Post) @@ -123,23 +148,63 @@ def new_author_path; "/authors/new"; end ::Post.stub!(:human_name).and_return('Post') ::Post.stub!(:reflect_on_all_validations).and_return([]) ::Post.stub!(:reflect_on_validations_for).and_return([]) + ::Post.stub!(:reflections).and_return({}) ::Post.stub!(:reflect_on_association).and_return do |column_name| case column_name when :author, :author_status - mock('reflection', :options => {}, :klass => ::Author, :macro => :belongs_to) + mock = mock('reflection', :options => {}, :klass => ::Author, :macro => :belongs_to) + mock.stub!(:[]).with(:class_name).and_return("Author") + mock + when :reviewer + mock = mock('reflection', :options => {:class_name => 'Author'}, :klass => ::Author, :macro => :belongs_to) + mock.stub!(:[]).with(:class_name).and_return("Author") + mock when :authors mock('reflection', :options => {}, :klass => ::Author, :macro => :has_and_belongs_to_many) + when :sub_posts + mock('reflection', :options => {}, :klass => ::Post, :macro => :has_many) + when :main_post + mock('reflection', :options => {}, :klass => ::Post, :macro => :belongs_to) 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) + @new_post.stub!(:allow_comments) + @new_post.stub!(:country) + @new_post.stub!(:country_subdivision) + @new_post.stub!(:country_code) @new_post.stub!(:column_for_attribute).with(:meta_description).and_return(mock('column', :type => :string, :limit => 255)) - @new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 255)) + @new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 50)) @new_post.stub!(:column_for_attribute).with(:body).and_return(mock('column', :type => :text)) @new_post.stub!(:column_for_attribute).with(:published).and_return(mock('column', :type => :boolean)) + @new_post.stub!(:column_for_attribute).with(:publish_at).and_return(mock('column', :type => :date)) + @new_post.stub!(:column_for_attribute).with(:time_zone).and_return(mock('column', :type => :string)) + @new_post.stub!(:column_for_attribute).with(:allow_comments).and_return(mock('column', :type => :boolean)) + @new_post.stub!(:column_for_attribute).with(:author).and_return(mock('column', :type => :integer)) + @new_post.stub!(:column_for_attribute).with(:country).and_return(mock('column', :type => :string, :limit => 255)) + @new_post.stub!(:column_for_attribute).with(:country_subdivision).and_return(mock('column', :type => :string, :limit => 255)) + @new_post.stub!(:column_for_attribute).with(:country_code).and_return(mock('column', :type => :string, :limit => 255)) + + @new_post.stub!(:author).and_return(@bob) + @new_post.stub!(:author_id).and_return(@bob.id) + + @new_post.stub!(:reviewer).and_return(@fred) + @new_post.stub!(:reviewer_id).and_return(@fred.id) + + @new_post.should_receive(:publish_at=).any_number_of_times + @new_post.should_receive(:title=).any_number_of_times + @new_post.stub!(:main_post_id).and_return(nil) + end def self.included(base) @@ -154,8 +219,19 @@ def protect_against_forgery? end end + def with_config(config_method_name, value, &block) + old_value = ::Formtastic::SemanticFormBuilder.send(config_method_name) + ::Formtastic::SemanticFormBuilder.send(:"#{config_method_name}=", value) + yield + ::Formtastic::SemanticFormBuilder.send(:"#{config_method_name}=", old_value) + end + + def with_deprecation_silenced(&block) + ::ActiveSupport::Deprecation.silenced = true + yield + ::ActiveSupport::Deprecation.silenced = false + end + end - - - +::ActiveSupport::Deprecation.silenced = false diff --git a/vendor/plugins/formtastic/README.textile b/vendor/plugins/formtastic/README.textile deleted file mode 100644 index 45ef611..0000000 --- a/vendor/plugins/formtastic/README.textile +++ /dev/null @@ -1,469 +0,0 @@ -h1. Formtastic - -Formtastic is a Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications. - -h2. The Story - -One day, I finally had enough, so I opened up my text editor, and wrote a DSL for how I'd like to author forms: - -
    -  <% semantic_form_for @article do |form| %>
    -
    -    <% form.inputs :name => "Basic" do %>
    -      <%= form.input :title %>
    -      <%= form.input :body %>
    -      <%= form.input :section %>
    -      <%= form.input :publication_state, :as => :radio %>
    -      <%= form.input :category %>
    -      <%= form.input :allow_comments, :label => "Allow commenting on this article" %>
    -    <% end %>
    -
    -    <% form.inputs :name => "Advanced" do %>
    -      <%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
    -      <%= form.input :extract, :required => false %>
    -      <%= form.input :description, :required => false %>
    -      <%= form.input :url_title, :required => false %>
    -    <% end %>
    -
    -    <% form.inputs :name => "Author", :for => :author do |author_form| %>
    -      <%= author_form.input :first_name %>
    -      <%= author_form.input :last_name %>
    -    <% end %>
    -
    -    <% form.buttons do %>
    -      <%= form.commit_button %>
    -    <% end %>
    -
    -  <% end %>
    -
    - -I also wrote the accompanying HTML output I expected, favoring something very similar to the fieldsets, lists and other semantic elements Aaron Gustafson presented in "Learning to Love Forms":http://www.slideshare.net/AaronGustafson/learning-to-love-forms-web-directions-south-07, hacking together enough Ruby to prove it could be done. - - -h2. It's better than _SomeOtherFormBuilder_ because... - -* it can handle @belongs_to@ associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model -* it can handle @has_many@ and @has_and_belongs_to_many@ associations (like Post has_many :tags), rendering a multi-select with choices from the child models -* it's Rails 2.3-ready (including nested forms) -* it has internationalization (I18n)! -* it's _really_ quick to get started with a basic form in place (4 lines), then go back to add in more detail if you need it -* there's heaps of elements, id and class attributes for you to hook in your CSS and JS -* it handles real world stuff like inline hints, inline error messages & help text -* it doesn't hijack or change any of the standard Rails form inputs, so you can still use them as expected (even mix and match) -* it's got absolutely awesome spec coverage -* there's a bunch of people using and working on it (it's not just one developer building half a solution) - - -h2. Why? - -* web apps = lots of forms -* forms are so friggin' boring to code -* semantically rich & accessible forms really are possible -* the "V" is way behind the "M" and "C" in Rails' MVC – it's the ugly sibling -* best practices and common patterns have to start somewhere -* i need a challenge - - -h2. Opinions - -* it should be easier to do things the right way than the wrong way -* sometimes _more mark-up_ is better -* elements and attribute hooks are _gold_ for stylesheet authors -* make the common things we do easy, yet still ensure uncommon things are still possible - - -h2. Documentation - -RDoc documentation _should_ be automatically generated after each commit and made available on the "rdoc.info website":http://rdoc.info/projects/justinfrench/formtastic. - - -h2. Installation - -The gem is hosted on gemcutter, so if you haven't already, add it as a gem source: - -
    -  sudo gem sources -a http://gemcutter.org/
    -
    - -Then install the Formtastic gem:q - -
    -  sudo gem install formtastic
    -
    - -Optionally, run @./script/generate formtastic@ to copy the following files into your app: - -* config/initializers/formtastic.rb (a commented out Formtastic config initializer) -* public/stylesheets/formtastic.css -* public/stylesheets/formtastic_changes.css - -A proof-of-concept stylesheet is provided which you can include in your layout. Customization is best achieved by overriding these styles in an additional stylesheet so that the Formtastic styles can be updated without clobbering your changes. If you want to use these stylesheets, add both to your layout: - -
    -  <%= stylesheet_link_tag "formtastic" %>
    -  <%= stylesheet_link_tag "formtastic_changes" %>
    -
    - - -h2. Usage - -Forms are really boring to code... you want to get onto the good stuff as fast as possible. - -This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord belongs_to association), followed by a submit button: - -
    -  <% semantic_form_for @user do |form| %>
    -    <%= form.inputs %>
    -    <%= form.buttons %>
    -  <% end %>
    -
    - -If you want to specify the order of the fields, skip some of the fields or even add in fields that Formtastic couldn't detect, you can pass in a list of field names to @inputs@ and list of button names to @buttons@: - -
    -  <% semantic_form_for @user do |form| %>
    -    <%= form.inputs :title, :body, :section, :categories, :created_at %>
    -    <%= form.buttons :commit %>
    -  <% end %>
    -
    - -If you want control over the input type Formtastic uses for each field, you can expand the @inputs@ and @buttons@ blocks. This specifies the :section input should be a set of radio buttons (rather than the default select box), and that the :created_at field should be a string (rather than the default datetime selects): - -
    -  <% semantic_form_for @post do |form| %>
    -    <% form.inputs do %>
    -      <%= form.input :title %>
    -      <%= form.input :body %>
    -      <%= form.input :section, :as => :radio %>
    -      <%= form.input :categories %>
    -      <%= form.input :created_at, :as => :string %>
    -    <% end %>
    -    <% form.buttons do %>
    -      <%= form.commit_button %>
    -    <% end %>
    -  <% end %>
    -
    - -If you want to customize the label text, or render some hint text below the field, specify which fields are required/optional, or break the form into two fieldsets, the DSL is pretty comprehensive: - -
    -  <% semantic_form_for @post do |form| %>
    -    <% form.inputs :name => "Basic", :id => "basic" do %>
    -      <%= form.input :title %>
    -      <%= form.input :body %>
    -    <% end %>
    -    <% form.inputs :name => "Advanced Options", :id => "advanced" do %>
    -      <%= form.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
    -      <%= form.input :section, :as => :radio %>
    -      <%= form.input :user, :label => "Author", :label_method => :full_name,  %>
    -      <%= form.input :categories, :required => false %>
    -      <%= form.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
    -    <% end %>
    -    <% form.buttons do %>
    -      <%= form.commit_button %>
    -    <% end %>
    -  <% end %>
    -
    - - -Nested forms (Rails 2.3) are also supported. You can do it in the Rails way: - -
    -  <% semantic_form_for @post do |form| %>
    -    <%= form.inputs :title, :body, :created_at %>
    -    <% form.semantic_fields_for :author do |author| %>
    -      <%= author.inputs :first_name, :last_name, :name => 'Author' %>
    -    <% end %>
    -    <%= form.buttons %>
    -  <% end %>
    -
    - -Or the Formtastic way with the @:for@ option: - -
    -  <% semantic_form_for @post do |form| %>
    -    <%= form.inputs :title, :body, :created_at %>
    -    <%= form.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
    -    <%= form.buttons %>
    -  <% end %>
    -
    - -When working in has many association, you can even supply "%i" in your fieldset name that it will be properly interpolated with the child index. For example: - -
    -  <% semantic_form_for @post do |form| %>
    -    <%= form.inputs %>
    -    <%= form.inputs :name => 'Category #%i', :for => :categories %>
    -    <%= form.buttons %>
    -  <% end %>
    -
    - - -Customize HTML attributes for any input using the @:input_html@ option. Typically his is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like "autogrow":http://plugins.jquery.com/project/autogrow textareas: - -
    -  <% semantic_form_for @post do |form| %>
    -    <%= form.input :title,      :input_html => { :size => 60 } %>
    -    <%= form.input :body,       :input_html => { :class => 'autogrow' } %>
    -    <%= form.input :created_at, :input_html => { :disabled => true } %>
    -    <%= form.buttons %>
    -  <% end %>
    -
    - -The same can be done for buttons with the @:button_html@ option: - -
    -  <% semantic_form_for @post do |form| %>
    -    ...
    -    <% form.buttons do %>
    -      <%= form.commit_button :button_html => { :class => "primary" } %>
    -    <% end %>
    -  <% end %>
    -
    - -Customize the HTML attributes for the @
  • @ wrapper around every input with the @:wrapper_html@ option hash. There's one special key in the hash (:class), which will actually _append_ your string of classes to the existing classes provided by Formtastic (like "required string error") - -
    -  <% semantic_form_for @post do |form| %>
    -    <%= form.input :title, :wrapper_html => { :class => "important" } %>
    -    <%= form.input :body %>
    -    <%= form.input :description, :wrapper_html => { :style => "display:none;" } %>
    -    ...
    -  <% end %>
    -
    - - - -h2. The Available Inputs - -* :select (a select menu) - default for ActiveRecord associations (belongs_to, has_many, has_and_belongs_to_many) -* :check_boxes (a set of check_box inputs) - alternative to :select has_many and has_and_belongs_to_many associations -* :radio (a set of radio inputs) - alternative to :select for ActiveRecord belongs_to associations -* :time_zone (a select input) - default for :string column types with 'time_zone' in the method name -* :password (a password input) - default for :string column types with 'password' in the method name -* :text (a textarea) - default for :text column types -* :date (a date select) - default for :date column types -* :datetime (a date and time select) - default for :datetime and :timestamp column types -* :time (a time select) - default for :time column types -* :boolean (a checkbox) - default for :boolean column types -* :string (a text field) - default for :string column types -* :numeric (a text field, like string) - default for :integer, :float and :decimal column types -* :file (a file field) - default for paperclip or attachment_fu attributes -* :country (a select menu of country names) - default for :string columns named "country", requires a country_select plugin to be installed -* :hidden (a hidden field) - creates a hidden field (added for compatibility) - - -The documentation is pretty good for each of these (what it does, what the output is, what the options are, etc) so go check it out. - -h2. Internationalization (I18n) - -Formtastic got some neat I18n-features. ActiveRecord object names and attributes are, by default, taken from calling @object.human_name and @object.human_attribute_name(attr) respectively. There are a few words specific to Formtastic that can be translated. See lib/locale/en.yml for more information. - - -h3. Label/Hint-localization - -Formtastic supports localized *labels* and *hints* using the I18n API for more advanced usage. Your forms can now be DRYer and more flexible than ever, and still fully localized. This is how: - -Basic localization (labels only, with ActiveRecord): - -
    -  <% semantic_form_for @post do |form| %>
    -    <%= form.input :title %>        # => :label => I18n.t('activerecord.attributes.user.title')    or 'Title'
    -    <%= form.input :body %>         # => :label => I18n.t('activerecord.attributes.user.body')     or 'Body'
    -    <%= form.input :section %>      # => :label => I18n.t('activerecord.attributes.user.section')  or 'Section'
    -  <% end %>
    -
    - -*Note:* This is perfectly fine if you just want your labels to be translated using *ActiveRecord I18n attribute translations*, and you don't use input hints. But what if you do? And what if you don't want same labels in all forms? - -Enhanced localization (labels + hints + titles/legends, with Formtastic): - -1. Enable I18n lookups by default (@config/initializers/formtastic.rb@): - -
    -  Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
    -
    - -2. Add some cool label-translations/variants (@config/locale/en.yml@): - -
    -  en:
    -    formtastic:
    -      titles:
    -        post_details: "Post details"
    -      labels:
    -        post:
    -          title: "Choose a title..."
    -          body: "Write something..."
    -          edit:
    -            title: "Edit title"
    -      hints:
    -        post:
    -          title: "Choose a good title for you post."
    -          body: "Write something inspiring here."
    -
    - -*Note:* We are using English here still, but you get the point. - -3. ...and now you'll get: - -
    -  <% semantic_form_for @post do |form| %>
    -    <% inputs do %>
    -      <%= form.input :title %>      # => :label => "Choose a title...", :hint => "Choose a good title for you post."
    -      <%= form.input :body %>       # => :label => "Write something...", :hint => "Write something inspiring here."
    -      <%= form.input :section %>    # => :label => I18n.t('activerecord.attributes.user.section')  or 'Section'
    -    <% end %>
    -    ...
    -  <% end %>
    -
    - -4. Localized titles (a.k.a. legends): - -_Note: Slightly different because Formtastic can't guess how you group fields in a form._ - -
    -  <% semantic_form_for @post do |form| %>
    -    <% inputs :title => :post_details do %>   # => :title => "Post details"
    -      # ...
    -    <% end %>
    -    # ...
    -<% end %>
    -
    - -5. Override I18n settings: - -
    -  <% semantic_form_for @post do |form| %>
    -    <% inputs do %>
    -      <%= form.input :title %>      # => :label => "Choose a title...", :hint => "Choose a good title for you post."
    -      <%= form.input :body, :hint => false %>                 # => :label => "Write something..."
    -      <%= form.input :section, :label => 'Some section' %>    # => :label => 'Some section'
    -    <% end %>
    -    # ...
    -  <% end %>
    -
    - -If I18n-lookups is disabled, i.e.: - -
    -  Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
    -
    - -...then you can enable I18n within the forms instead: - -
    -  <% semantic_form_for @post do |form| %>
    -    <% inputs do %>
    -      <%= form.input :title, :label => true %>      # => :label => "Choose a title..."
    -      <%= form.input :body, :label => true %>       # => :label => "Write something..."
    -      <%= form.input :section, :label => true %>    # => :label => I18n.t('activerecord.attributes.user.section')  or 'Section'
    -    <% end %>
    -    # ...
    -  <% end %>
    -
    - -6. Advanced I18n lookups - -For more flexible forms; Formtastic find translations using a bottom-up approach taking the following variables in account: - -* @MODEL@, e.g. "post" -* @ACTION@, e.g. "edit" -* @ATTRIBUTE@, e.g. "title" - -...in the following order: - -1. @formtastic.{titles,labels,hints}.MODEL.ACTION.ATTRIBUTE@ # By model and action -2. @formtastic.{titles,labels,hints}.MODEL.ATTRIBUTE@ # By model -3. @formtastic.{titles,labels,hints}.ATTRIBUTE@ # Global default - -...which means that you can define translations like this: - -
    -  en:
    -    formtastic:
    -      labels:
    -        title: "Title"  # Default global value
    -        article:
    -          body: "Article content"
    -        post:
    -          new:
    -            title: "Choose a title..."
    -            body: "Write something..."
    -          edit:
    -            title: "Edit title"
    -            body: "Edit body"
    -
    - -*Note:* For @title@: ATTRIBUTE is a KEY chosen by you, e.g. in step 4 example above: @:post_details@. - - -h2. ValidationReflection plugin - -If you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin installed, you won't have to specify the :required option (it checks the validations on the model instead). - - -h2. Configuration - -Run @./script/generate formtastic@ to copy a commented out config file into @config/initializers/formtastic.rb@. You can "view the configuration file on GitHub":http://github.com/justinfrench/formtastic/blob/master/generators/formtastic/templates/formtastic.rb - - -h2. Status - -Formtastic has been in active development for about a year. We've just recently jumped to an 0.9 version number, signalling that we consider this a 1.0 release candidate, and that the API won't change significantly for the 1.x series. - - -h2. Dependencies - -There are none, but... - -* if you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin is installed, you won't have to specify the :required option (it checks the validations on the model instead) -* if you want to use the :country input, you'll need to install the "iso-3166-country-select plugin":http://github.com/rails/iso-3166-country-select (or any other country_select plugin with the same API) -* rspec, rspec_hpricot_matchers and rcov gems (plus any of their own dependencies) are required for the test suite - - -h2. Compatibility - -I'm only testing Formtastic with the latest Rails 2.2.x stable release, and it should be fine under Rails 2.3 as well (including nested forms). Patches are welcome to allow backwards compatibility, but I don't have the energy! - - -h2. Got TextMate? - -Well...there's a TextMate-bundle in town, dedicated to make usage of Formtastic in the TextMate editor even more of a breeze: - -"Formtastic.tmbundle":http://github.com/grimen/formtastic_tmbundle - - -h2. Contributors - -Formtastic is maintained by "Justin French":http://justinfrench.com and "José Valim":http://github.com/josevalim, but it wouldn't be as awesome as it is today if it weren't for the wonderful contributions of these fine, fine coders. - -* "Jeff Smick":http://github.com/sprsquish -* "Tien Dung":http://github.com/tiendung -* "Mark Mansour":http://stateofflux.com -* "Andy Pearson":http://github.com/andypearson -* "negonicrac":http://github.com/negonicrac -* "Xavier Shay":http://rhnh.net -* "Pat Allan":http://github.com/freelancing-god -* "Gareth Townsend":http://github.com/quamen -* "Sascha Hoellger":http://github.com/mitnal -* "Andrew Carpenter":http://github.com/andrewcarpenter -* "Jack Dempsey":http://github.com/jackdempsey/ -* "Greg Fitzgerald":http://github.com/gregf/ -* "Hector E. Gomez Morales":http://github.com/hectoregm -* "Ben Hamill":http://blog.benhamill.com/ -* "Simon Chiu":http://github.com/tolatomeow -* "Bin Dong":http://github.com/dongbin - - -h2. Hey, join the Google group! - -Please join the "Formtastic Google Group":http://groups.google.com.au/group/formtastic, especially if you'd like to talk about a new feature, or report a bug. - - -h2. Project Info - -Formtastic is hosted on Github: http://github.com/justinfrench/formtastic/, where your contributions, forkings, comments and feedback are greatly welcomed. - - -Copyright (c) 2007-2008 Justin French, released under the MIT license. diff --git a/vendor/plugins/formtastic/RELEASE_PROCESS b/vendor/plugins/formtastic/RELEASE_PROCESS deleted file mode 100644 index 22d51ec..0000000 --- a/vendor/plugins/formtastic/RELEASE_PROCESS +++ /dev/null @@ -1,11 +0,0 @@ -git status # check you have a clean working directory -mate VERSION.yml # alter VERSION.yml to reflect new version number -rake gemspec # to generate the new gemspec file -git add VERSION.yml formtastic.gemspec # stage changes -git commit # commit and describe the reason for the new gem -git tag -am "0.2.3" 0.2.3 # tag the new version in the code base too -git log 0.2.2..0.2.3 # check the log since last tag -gem build formtastic.gemspec # build the gem -gem push formtastic-0.2.3.gem # publish the gem -git push # push to github -git push --tags # push the tags up to remote too diff --git a/vendor/plugins/formtastic/Rakefile b/vendor/plugins/formtastic/Rakefile deleted file mode 100644 index 2525f74..0000000 --- a/vendor/plugins/formtastic/Rakefile +++ /dev/null @@ -1,87 +0,0 @@ -# coding: utf-8 -require 'rake' -require 'rake/rdoctask' - -gem 'rspec-rails', '>= 1.0.0' -require 'spec/rake/spectask' - -begin - GEM = "formtastic" - AUTHOR = "Justin French" - EMAIL = "justin@indent.com.au" - SUMMARY = "A Rails form builder plugin/gem with semantically rich and accessible markup" - HOMEPAGE = "http://github.com/justinfrench/formtastic/tree/master" - INSTALL_MESSAGE = %q{ - ======================================================================== - - Thanks for installing Formtastic! - - You can now (optionally) run the generater to copy some stylesheets and - a config initializer into your application: - - ./script/generate formtastic - - The following files will be added: - - RAILS_ROOT/public/stylesheets/formtastic.css - RAILS_ROOT/public/stylesheets/formtastic_changes.css - RAILS_ROOT/config/initializers/formtastic.rb - - Find out more and get involved: - - http://github.com/justinfrench/formtastic - http://groups.google.com.au/group/formtastic - - ======================================================================== - } - - gem 'jeweler', '>= 1.0.0' - require 'jeweler' - - Jeweler::Tasks.new do |s| - s.name = GEM - s.summary = SUMMARY - s.email = EMAIL - s.homepage = HOMEPAGE - s.description = SUMMARY - s.author = AUTHOR - s.post_install_message = INSTALL_MESSAGE - - s.require_path = 'lib' - s.autorequire = GEM - s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,generators,spec}/**/*") - end -rescue LoadError - puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com" -end - -desc 'Default: run unit specs.' -task :default => :spec - -desc 'Test the formtastic plugin.' -Spec::Rake::SpecTask.new('spec') do |t| - t.spec_files = FileList['spec/**/*_spec.rb'] - t.spec_opts = ["-c"] -end - -desc 'Test the formtastic plugin with specdoc formatting and colors' -Spec::Rake::SpecTask.new('specdoc') do |t| - t.spec_files = FileList['spec/**/*_spec.rb'] - t.spec_opts = ["--format specdoc", "-c"] -end - -desc 'Generate documentation for the formtastic plugin.' -Rake::RDocTask.new(:rdoc) do |rdoc| - rdoc.rdoc_dir = 'rdoc' - rdoc.title = 'Formtastic' - rdoc.options << '--line-numbers' << '--inline-source' - rdoc.rdoc_files.include('README.textile') - rdoc.rdoc_files.include('lib/**/*.rb') -end - -desc "Run all examples with RCov" -Spec::Rake::SpecTask.new('examples_with_rcov') do |t| - t.spec_files = FileList['spec/**/*_spec.rb'] - t.rcov = true - t.rcov_opts = ['--exclude', 'spec,Library'] -end diff --git a/vendor/plugins/formtastic/VERSION.yml b/vendor/plugins/formtastic/VERSION.yml deleted file mode 100644 index a98ba78..0000000 --- a/vendor/plugins/formtastic/VERSION.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- -:major: 0 -:minor: 9 -:patch: 0 diff --git a/vendor/plugins/formtastic/formtastic.gemspec b/vendor/plugins/formtastic/formtastic.gemspec deleted file mode 100644 index fd43b6c..0000000 --- a/vendor/plugins/formtastic/formtastic.gemspec +++ /dev/null @@ -1,81 +0,0 @@ -# Generated by jeweler -# DO NOT EDIT THIS FILE -# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec` -# -*- encoding: utf-8 -*- - -Gem::Specification.new do |s| - s.name = %q{formtastic} - s.version = "0.9.0" - - s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Justin French"] - s.autorequire = %q{formtastic} - s.date = %q{2009-10-21} - s.description = %q{A Rails form builder plugin/gem with semantically rich and accessible markup} - s.email = %q{justin@indent.com.au} - s.extra_rdoc_files = [ - "README.textile" - ] - s.files = [ - "MIT-LICENSE", - "README.textile", - "Rakefile", - "generators/form/USAGE", - "generators/form/form_generator.rb", - "generators/form/templates/view__form.html.erb", - "generators/form/templates/view__form.html.haml", - "generators/formtastic/formtastic_generator.rb", - "generators/formtastic/templates/formtastic.css", - "generators/formtastic/templates/formtastic.rb", - "generators/formtastic/templates/formtastic_changes.css", - "generators/formtastic_stylesheets/formtastic_stylesheets_generator.rb", - "lib/formtastic.rb", - "lib/justin_french/formtastic.rb", - "lib/locale/en.yml", - "rails/init.rb", - "spec/formtastic_spec.rb", - "spec/test_helper.rb" - ] - s.homepage = %q{http://github.com/justinfrench/formtastic/tree/master} - s.post_install_message = %q{ - ======================================================================== - - Thanks for installing Formtastic! - - You can now (optionally) run the generater to copy some stylesheets and - a config initializer into your application: - - ./script/generate formtastic - - The following files will be added: - - RAILS_ROOT/public/stylesheets/formtastic.css - RAILS_ROOT/public/stylesheets/formtastic_changes.css - RAILS_ROOT/config/initializers/formtastic.rb - - Find out more and get involved: - - http://github.com/justinfrench/formtastic - http://groups.google.com.au/group/formtastic - - ======================================================================== - } - s.rdoc_options = ["--charset=UTF-8"] - s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.5} - s.summary = %q{A Rails form builder plugin/gem with semantically rich and accessible markup} - s.test_files = [ - "spec/formtastic_spec.rb", - "spec/test_helper.rb" - ] - - if s.respond_to? :specification_version then - current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION - s.specification_version = 3 - - if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then - else - end - else - end -end diff --git a/vendor/plugins/formtastic/generators/form/templates/view__form.html.erb b/vendor/plugins/formtastic/generators/form/templates/view__form.html.erb deleted file mode 100644 index d444c3c..0000000 --- a/vendor/plugins/formtastic/generators/form/templates/view__form.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<%% form.inputs do %> -<% attributes.each do |attribute| -%> - <%%= form.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>' %> -<% end -%> -<%% end %> \ No newline at end of file diff --git a/vendor/plugins/formtastic/generators/form/templates/view__form.html.haml b/vendor/plugins/formtastic/generators/form/templates/view__form.html.haml deleted file mode 100644 index 52b2fa9..0000000 --- a/vendor/plugins/formtastic/generators/form/templates/view__form.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -- form.inputs do -<% attributes.each do |attribute| -%> - = form.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>' -<% end -%> \ No newline at end of file diff --git a/vendor/plugins/formtastic/generators/formtastic/templates/formtastic.css b/vendor/plugins/formtastic/generators/formtastic/templates/formtastic.css deleted file mode 100644 index 8e6ce69..0000000 --- a/vendor/plugins/formtastic/generators/formtastic/templates/formtastic.css +++ /dev/null @@ -1,137 +0,0 @@ -/* ------------------------------------------------------------------------------------------------- - -It's *strongly* suggested that you don't modify this file. Instead, load a new stylesheet after -this one in your layouts (eg formtastic_changes.css) and override the styles to suit your needs. -This will allow you to update formtastic.css with new releases without clobbering your own changes. - -This stylesheet forms part of the Formtastic Rails Plugin -(c) 2008 Justin French - ---------------------------------------------------------------------------------------------------*/ - - -/* NORMALIZE AND RESET - obviously inspired by Yahoo's reset.css, but scoped to just form.formtastic ---------------------------------------------------------------------------------------------------*/ -form.formtastic, form.formtastic ul, form.formtastic ol, form.formtastic li, form.formtastic fieldset, form.formtastic legend, form.formtastic input, form.formtastic textarea, form.formtastic select, form.formtastic p { margin:0; padding:0; } -form.formtastic fieldset { border:0; } -form.formtastic em, form.formtastic strong { font-style:normal; font-weight:normal; } -form.formtastic ol, form.formtastic ul { list-style:none; } -form.formtastic abbr, form.formtastic acronym { border:0; font-variant:normal; } -form.formtastic input, form.formtastic textarea, form.formtastic select { font-family:inherit; font-size:inherit; font-weight:inherit; } -form.formtastic input, form.formtastic textarea, form.formtastic select { font-size:100%; } -form.formtastic legend { color:#000; } - - -/* FIELDSETS & LISTS ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset { } -form.formtastic fieldset.inputs { } -form.formtastic fieldset.buttons { padding-left:25%; } -form.formtastic fieldset ol { } -form.formtastic fieldset.buttons li { float:left; padding-right:0.5em; } - -/* clearfixing the fieldsets */ -form.formtastic fieldset { display: inline-block; } -form.formtastic fieldset:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } -html[xmlns] form.formtastic fieldset { display: block; } -* html form.formtastic fieldset { height: 1%; } - - -/* INPUT LIs ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li { margin-bottom:1.5em; } - -/* clearfixing the li's */ -form.formtastic fieldset ol li { display: inline-block; } -form.formtastic fieldset ol li:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } -html[xmlns] form.formtastic fieldset ol li { display: block; } -* html form.formtastic fieldset ol li { height: 1%; } - -form.formtastic fieldset ol li.required { } -form.formtastic fieldset ol li.optional { } -form.formtastic fieldset ol li.error { } - - -/* LABELS ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li label { display:block; width:25%; float:left; padding-top:.2em; } -form.formtastic fieldset ol li li label { line-height:100%; padding-top:0; } -form.formtastic fieldset ol li li label input { line-height:100%; vertical-align:middle; margin-top:-0.1em;} - - -/* NESTED FIELDSETS AND LEGENDS (radio, check boxes and date/time inputs use nested fieldsets) ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li fieldset { position:relative; } -form.formtastic fieldset ol li fieldset legend { position:absolute; width:25%; padding-top:0.1em; } -form.formtastic fieldset ol li fieldset legend span { position:absolute; } -form.formtastic fieldset ol li fieldset ol { float:left; width:74%; margin:0; padding:0 0 0 25%; } -form.formtastic fieldset ol li fieldset ol li { padding:0; border:0; } - - -/* INLINE HINTS ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li p.inline-hints { color:#666; margin:0.5em 0 0 25%; } - - -/* INLINE ERRORS ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li p.inline-errors { color:#cc0000; margin:0.5em 0 0 25%; } -form.formtastic fieldset ol li ul.errors { color:#cc0000; margin:0.5em 0 0 25%; list-style:square; } -form.formtastic fieldset ol li ul.errors li { padding:0; border:none; display:list-item; } - - -/* STRING & NUMERIC OVERRIDES ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li.string input { width:74%; } -form.formtastic fieldset ol li.password input { width:74%; } -form.formtastic fieldset ol li.numeric input { width:74%; } - - -/* TEXTAREA OVERRIDES ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li.text textarea { width:74%; } - - -/* HIDDEN OVERRIDES ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li.hidden { display:none; } - - -/* BOOLEAN OVERRIDES ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li.boolean label { padding-left:25%; width:auto; } -form.formtastic fieldset ol li.boolean label input { margin:0 0.5em 0 0.2em; } - - -/* RADIO OVERRIDES ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li.radio { } -form.formtastic fieldset ol li.radio fieldset ol { margin-bottom:-0.6em; } -form.formtastic fieldset ol li.radio fieldset ol li { margin:0.1em 0 0.5em 0; } -form.formtastic fieldset ol li.radio fieldset ol li label { float:none; width:100%; } -form.formtastic fieldset ol li.radio fieldset ol li label input { margin-right:0.2em; } - - -/* CHECK BOXES (COLLECTION) OVERRIDES ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li.check_boxes { } -form.formtastic fieldset ol li.check_boxes fieldset ol { margin-bottom:-0.6em; } -form.formtastic fieldset ol li.check_boxes fieldset ol li { margin:0.1em 0 0.5em 0; } -form.formtastic fieldset ol li.check_boxes fieldset ol li label { float:none; width:100%; } -form.formtastic fieldset ol li.check_boxes fieldset ol li label input { margin-right:0.2em; } - - - -/* DATE & TIME OVERRIDES ---------------------------------------------------------------------------------------------------*/ -form.formtastic fieldset ol li.date fieldset ol li, -form.formtastic fieldset ol li.time fieldset ol li, -form.formtastic fieldset ol li.datetime fieldset ol li { float:left; width:auto; margin:0 .3em 0 0; } - -form.formtastic fieldset ol li.date fieldset ol li label, -form.formtastic fieldset ol li.time fieldset ol li label, -form.formtastic fieldset ol li.datetime fieldset ol li label { display:none; } - -form.formtastic fieldset ol li.date fieldset ol li label input, -form.formtastic fieldset ol li.time fieldset ol li label input, -form.formtastic fieldset ol li.datetime fieldset ol li label input { display:inline; margin:0; padding:0; } diff --git a/vendor/plugins/formtastic/install.rb b/vendor/plugins/formtastic/install.rb deleted file mode 100644 index 06c1be5..0000000 --- a/vendor/plugins/formtastic/install.rb +++ /dev/null @@ -1 +0,0 @@ -puts "Run `./script/generate formtastic` if you want (copies optional config file and some stylesheets into your app)" \ No newline at end of file diff --git a/vendor/plugins/formtastic/lib/formtastic.rb b/vendor/plugins/formtastic/lib/formtastic.rb deleted file mode 100644 index e083774..0000000 --- a/vendor/plugins/formtastic/lib/formtastic.rb +++ /dev/null @@ -1,1370 +0,0 @@ -# coding: utf-8 - -module Formtastic #:nodoc: - - 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 { %{*} } - @@optional_string = '' - @@inline_errors = :sentence - @@label_str_method = :humanize - @@collection_label_methods = %w[to_label display_name full_name name title username login value to_s] - @@inline_order = [ :input, :hints, :errors ] - @@file_methods = [ :file?, :public_filename ] - @@priority_countries = ["Australia", "Canada", "United Kingdom", "United States"] - @@i18n_lookups_by_default = false - - cattr_accessor :default_text_field_size, :all_fields_required_by_default, :include_blank_for_select_by_default, - :required_string, :optional_string, :inline_errors, :label_str_method, :collection_label_methods, - :inline_order, :file_methods, :priority_countries, :i18n_lookups_by_default - - I18N_SCOPES = [ '{{model}}.{{action}}.{{attribute}}', - '{{model}}.{{attribute}}', - '{{attribute}}'] - - # Keeps simple mappings in a hash - INPUT_MAPPINGS = { - :string => :text_field, - :password => :password_field, - :numeric => :text_field, - :text => :text_area, - :file => :file_field - } - STRING_MAPPINGS = [ :string, :password, :numeric ] - - attr_accessor :template - - # Returns a suitable form input for the given +method+, using the database column information - # and other factors (like the method name) to figure out what you probably want. - # - # Options: - # - # * :as - override the input type (eg force a :string to render as a :password field) - # * :label - use something other than the method name as the label text, when false no label is printed - # * :required - specify if the column is required (true) or not (false) - # * :hint - provide some text to hint or help the user provide the correct information for a field - # * :input_html - provide options that will be passed down to the generated input - # * :wrapper_html - provide options that will be passed down to the li wrapper - # - # Input Types: - # - # Most inputs map directly to one of ActiveRecord's column types by default (eg string_input), - # but there are a few special cases and some simplification (:integer, :float and :decimal - # columns all map to a single numeric_input, for example). - # - # * :select (a select menu for associations) - default to association names - # * :check_boxes (a set of check_box inputs for associations) - alternative to :select has_many and has_and_belongs_to_many associations - # * :radio (a set of radio inputs for associations) - alternative to :select belongs_to associations - # * :time_zone (a select menu with time zones) - # * :password (a password input) - default for :string column types with 'password' in the method name - # * :text (a textarea) - default for :text column types - # * :date (a date select) - default for :date column types - # * :datetime (a date and time select) - default for :datetime and :timestamp column types - # * :time (a time select) - default for :time column types - # * :boolean (a checkbox) - default for :boolean column types (you can also have booleans as :select and :radio) - # * :string (a text field) - default for :string column types - # * :numeric (a text field, like string) - default for :integer, :float and :decimal column types - # * :country (a select menu of country names) - requires a country_select plugin to be installed - # * :hidden (a hidden field) - creates a hidden field (added for compatibility) - # - # Example: - # - # <% semantic_form_for @employee do |form| %> - # <% form.inputs do -%> - # <%= form.input :name, :label => "Full Name"%> - # <%= form.input :manager_id, :as => :radio %> - # <%= form.input :hired_at, :as => :date, :label => "Date Hired" %> - # <%= form.input :phone, :required => false, :hint => "Eg: +1 555 1234" %> - # <% end %> - # <% end %> - # - def input(method, options = {}) - options[:required] = method_required?(method) unless options.key?(:required) - options[:as] ||= default_input_type(method) - - html_class = [ options[:as], (options[:required] ? :required : :optional) ] - html_class << 'error' if @object && @object.respond_to?(:errors) && !@object.errors[method.to_sym].blank? - html_class << method.to_s - - wrapper_html = options.delete(:wrapper_html) || {} - wrapper_html[:id] ||= generate_html_id(method) - wrapper_html[:class] = (html_class << wrapper_html[:class]).flatten.compact.join(' ') - - if options[:input_html] && options[:input_html][:id] - options[:label_html] ||= {} - options[:label_html][:for] ||= options[:input_html][:id] - end - - input_parts = @@inline_order.dup - input_parts.delete(:errors) if options[:as] == :hidden - - list_item_content = input_parts.map do |type| - send(:"inline_#{type}_for", method, options) - end.compact.join("\n") - - return template.content_tag(:li, list_item_content, wrapper_html) - end - - # Creates an input fieldset and ol tag wrapping for use around a set of inputs. It can be - # called either with a block (in which you can do the usual Rails form stuff, HTML, ERB, etc), - # or with a list of fields. These two examples are functionally equivalent: - # - # # With a block: - # <% semantic_form_for @post do |form| %> - # <% form.inputs do %> - # <%= form.input :title %> - # <%= form.input :body %> - # <% end %> - # <% end %> - # - # # With a list of fields: - # <% semantic_form_for @post do |form| %> - # <%= form.inputs :title, :body %> - # <% end %> - # - # # Output: - #
    - #
    - #
      - #
    1. ...
    2. - #
    3. ...
    4. - #
    - #
    - #
    - # - # === Quick Forms - # - # When called without a block or a field list, an input is rendered for each column in the - # model's database table, just like Rails' scaffolding. You'll obviously want more control - # than this in a production application, but it's a great way to get started, then come back - # later to customise the form with a field list or a block of inputs. Example: - # - # <% semantic_form_for @post do |form| %> - # <%= form.inputs %> - # <% 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). - # - # # With a block: - # <% semantic_form_for @post do |form| %> - # <% form.inputs :name => "Create a new post", :style => "border:1px;" do %> - # ... - # <% end %> - # <% end %> - # - # # With a list (the options must come after the field list): - # <% semantic_form_for @post do |form| %> - # <%= form.inputs :title, :body, :name => "Create a new post", :style => "border:1px;" %> - # <% end %> - # - # === It's basically a fieldset! - # - # Instead of hard-coding fieldsets & legends into your form to logically group related fields, - # use inputs: - # - # <% semantic_form_for @post do |f| %> - # <% f.inputs do %> - # <%= f.input :title %> - # <%= f.input :body %> - # <% end %> - # <% f.inputs :name => "Advanced", :id => "advanced" do %> - # <%= f.input :created_at %> - # <%= f.input :user_id, :label => "Author" %> - # <% end %> - # <% end %> - # - # # Output: - #
    - #
    - #
      - #
    1. ...
    2. - #
    3. ...
    4. - #
    - #
    - #
    - # Advanced - #
      - #
    1. ...
    2. - #
    3. ...
    4. - #
    - #
    - #
    - # - # === Nested attributes - # - # As in Rails, you can use semantic_fields_for to nest attributes: - # - # <% semantic_form_for @post do |form| %> - # <%= form.inputs :title, :body %> - # - # <% form.semantic_fields_for :author, @bob do |author_form| %> - # <% author_form.inputs do %> - # <%= author_form.input :first_name, :required => false %> - # <%= author_form.input :last_name %> - # <% end %> - # <% end %> - # <% end %> - # - # But this does not look formtastic! This is equivalent: - # - # <% semantic_form_for @post do |form| %> - # <%= form.inputs :title, :body %> - # <% form.inputs :for => [ :author, @bob ] do |author_form| %> - # <%= author_form.input :first_name, :required => false %> - # <%= author_form.input :last_name %> - # <% end %> - # <% end %> - # - # And if you don't need to give options to your input call, you could do it - # in just one line: - # - # <% semantic_form_for @post do |form| %> - # <%= form.inputs :title, :body %> - # <%= form.inputs :first_name, :last_name, :for => @bob %> - # <% end %> - # - # Just remember that calling inputs generates a new fieldset to wrap your - # inputs. If you have two separate models, but, semantically, on the page - # they are part of the same fieldset, you should use semantic_fields_for - # instead (just as you would do with Rails' form builder). - # - def inputs(*args, &block) - html_options = args.extract_options! - html_options[:class] ||= "inputs" - - if html_options[:for] - inputs_for_nested_attributes(args, html_options, &block) - elsif block_given? - field_set_and_list_wrapping(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.compact! - end - contents = args.map { |method| input(method.to_sym) } - - field_set_and_list_wrapping(html_options, contents) - end - end - alias :input_field_set :inputs - - # Creates a fieldset and ol tag wrapping for form buttons / actions as list items. - # See inputs documentation for a full example. The fieldset's default class attriute - # is set to "buttons". - # - # See inputs for html attributes and special options. - def buttons(*args, &block) - html_options = args.extract_options! - html_options[:class] ||= "buttons" - - if block_given? - field_set_and_list_wrapping(html_options, &block) - else - args = [:commit] if args.empty? - contents = args.map { |button_name| send(:"#{button_name}_button") } - field_set_and_list_wrapping(html_options, contents) - end - end - alias :button_field_set :buttons - - # Creates a submit input tag with the value "Save [model name]" (for existing records) or - # "Create [model name]" (for new records) by default: - # - # <%= form.commit_button %> => - # - # The value of the button text can be overridden: - # - # <%= form.commit_button "Go" %> => - # - # And you can pass html atributes down to the input, with or without the button text: - # - # <%= form.commit_button "Go" %> => - # <%= form.commit_button :class => "pretty" %> => - - def commit_button(*args) - value = args.first.is_a?(String) ? args.shift : save_or_create_button_text - options = args.shift || {} - button_html = options.delete(:button_html) || {} - template.content_tag(:li, self.submit(value, button_html), :class => "commit") - end - - # A thin wrapper around #fields_for to set :builder => Formtastic::SemanticFormBuilder - # for nesting forms: - # - # # Example: - # <% semantic_form_for @post do |post| %> - # <% post.semantic_fields_for :author do |author| %> - # <% author.inputs :name %> - # <% end %> - # <% end %> - # - # # Output: - #
    - #
    - #
      - #
    1. - #
    - #
    - #
    - # - def semantic_fields_for(record_or_name_or_array, *args, &block) - opts = args.extract_options! - opts.merge!(:builder => Formtastic::SemanticFormHelper.builder) - args.push(opts) - fields_for(record_or_name_or_array, *args, &block) - end - - # Generates the label for the input. It also accepts the same arguments as - # Rails label method. It has three options that are not supported by Rails - # label method: - # - # * :required - Appends an abbr tag if :required is true - # * :label - An alternative form to give the label content. Whenever label - # is false, a blank string is returned. - # * :as_span - When true returns a span tag with class label instead of a label element - # * :input_name - Gives the input to match for. This is needed when you want to - # to call f.label :authors but it should match :author_ids. - # - # == Examples - # - # f.label :title # like in rails, except that it searches the label on I18n API too - # - # f.label :title, "Your post title" - # f.label :title, :label => "Your post title" # Added for formtastic API - # - # f.label :title, :required => true # Returns - # - def label(method, options_or_text=nil, options=nil) - if options_or_text.is_a?(Hash) - return "" if options_or_text[:label] == false - options = options_or_text - text = options.delete(:label) - else - text = options_or_text - options ||= {} - end - - text = localized_attribute_string(method, text, :label) || humanized_attribute_name(method) - text += required_or_optional_string(options.delete(:required)) - - input_name = options.delete(:input_name) || method - if options.delete(:as_span) - options[:class] ||= 'label' - template.content_tag(:span, text, options) - else - super(input_name, text, options) - end - end - - # Generates error messages for the given method. Errors can be shown as list - # or as sentence. If :none is set, no error is shown. - # - # This method is also aliased as errors_on, so you can call on your custom - # inputs as well: - # - # semantic_form_for :post do |f| - # f.text_field(:body) - # f.errors_on(:body) - # end - # - def inline_errors_for(method, options=nil) #:nodoc: - return nil unless @object && @object.respond_to?(:errors) && [:sentence, :list].include?(@@inline_errors) - - errors = @object.errors[method.to_sym] - send("error_#{@@inline_errors}", Array(errors)) unless errors.blank? - end - alias :errors_on :inline_errors_for - - protected - - # Prepare options to be sent to label - # - def options_for_label(options) - options.slice(:label, :required).merge!(options.fetch(:label_html, {})) - end - - # Deals with :for option when it's supplied to inputs methods. Additional - # options to be passed down to :for should be supplied using :for_options - # key. - # - # It should raise an error if a block with arity zero is given. - # - def inputs_for_nested_attributes(args, options, &block) - args << options.merge!(:parent => { :builder => self, :for => options[:for] }) - - fields_for_block = if block_given? - raise ArgumentError, 'You gave :for option with a block to inputs method, ' << - 'but the block does not accept any argument.' if block.arity <= 0 - - proc { |f| f.inputs(*args){ block.call(f) } } - else - proc { |f| f.inputs(*args) } - end - - fields_for_args = [options.delete(:for), options.delete(:for_options) || {}].flatten - semantic_fields_for(*fields_for_args, &fields_for_block) - end - - # Remove any Formtastic-specific options before passing the down options. - # - def set_options(options) - options.except(:value_method, :label_method, :collection, :required, :label, - :as, :hint, :input_html, :label_html, :value_as_class) - end - - # Create a default button text. If the form is working with a object, it - # defaults to "Create {{model}}" or "Save {{model}}" depending if we are working - # with a new_record or not. - # - # When not working with models, it defaults to "Submit object". - # - def save_or_create_button_text(prefix='Submit') #:nodoc: - if @object - prefix = @object.new_record? ? 'Create' : 'Save' - object_name = @object.class.human_name - else - object_name = @object_name.to_s.send(@@label_str_method) - end - I18n.t(prefix.downcase, :model => object_name, :default => "#{prefix} {{model}}", :scope => [:formtastic]) - end - - # Determins if the attribute (eg :title) should be considered required or not. - # - # * if the :required option was provided in the options hash, the true/false value will be - # returned immediately, allowing the view to override any guesswork that follows: - # - # * if the :required option isn't provided in the options hash, and the ValidationReflection - # plugin is installed (http://github.com/redinger/validation_reflection), true is returned - # if the validates_presence_of macro has been used in the class for this attribute, or false - # otherwise. - # - # * if the :required option isn't provided, and the plugin isn't available, the value of the - # configuration option @@all_fields_required_by_default is used. - # - def method_required?(attribute) #:nodoc: - if @object && @object.class.respond_to?(:reflect_on_validations_for) - attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym - - @object.class.reflect_on_validations_for(attribute_sym).any? do |validation| - validation.macro == :validates_presence_of && - validation.name == attribute_sym && - (validation.options.present? ? options_require_validation?(validation.options) : true) - end - else - @@all_fields_required_by_default - end - end - - # Determines whether the given options evaluate to true - def options_require_validation?(options) #nodoc - if_condition = !options[:if].nil? - condition = if_condition ? options[:if] : options[:unless] - - condition = if condition.respond_to?(:call) - condition.call(@object) - elsif condition.is_a?(::Symbol) && @object.respond_to?(condition) - @object.send(condition) - else - condition - end - - if_condition ? !!condition : !condition - end - - # A method that deals with most of inputs (:string, :password, :file, - # :textarea and :numeric). :select, :radio, :boolean and :datetime inputs - # are not handled by this method, since they need more detailed approach. - # - # If input_html is given as option, it's passed down to the input. - # - def input_simple(type, method, options) - html_options = options.delete(:input_html) || {} - html_options = default_string_options(method, type).merge(html_options) if STRING_MAPPINGS.include?(type) - - self.label(method, options_for_label(options)) + - self.send(INPUT_MAPPINGS[type], method, html_options) - end - - # Outputs a hidden field inside the wrapper, which should be hidden with CSS. - # Additionals options can be given and will be sent straight to hidden input - # element. - # - def hidden_input(method, options) - self.hidden_field(method, set_options(options)) - end - - # Outputs a label and a select box containing options from the parent - # (belongs_to, has_many, has_and_belongs_to_many) association. If an association - # is has_many or has_and_belongs_to_many the select box will be set as multi-select - # and size = 5 - # - # Example (belongs_to): - # - # f.input :author - # - # - # - # - # Example (has_many): - # - # f.input :chapters - # - # - # - # - # Example (has_and_belongs_to_many): - # - # f.input :authors - # - # - # - # - # - # You can customize the options available in the select by passing in a collection (an Array or - # Hash) through the :collection option. If not provided, the choices are found by inferring the - # parent's class name from the method name and simply calling find(:all) on it - # (VehicleOwner.find(:all) in the example above). - # - # Examples: - # - # f.input :author, :collection => @authors - # f.input :author, :collection => Author.find(:all) - # f.input :author, :collection => [@justin, @kate] - # f.input :author, :collection => {@justin.name => @justin.id, @kate.name => @kate.id} - # f.input :author, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"] - # - # The :label_method option allows you to customize the text label inside each option tag two ways: - # - # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc) - # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together - # - # Examples: - # - # f.input :author, :label_method => :full_name - # f.input :author, :label_method => :login - # f.input :author, :label_method => :full_name_with_post_count - # f.input :author, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" } - # - # The :value_method option provides the same customization of the value attribute of each option tag. - # - # Examples: - # - # f.input :author, :value_method => :full_name - # f.input :author, :value_method => :login - # f.input :author, :value_method => Proc.new { |a| "author_#{a.login}" } - # - # You can pass html_options to the select tag using :input_html => {} - # - # Examples: - # - # f.input :authors, :input_html => {:size => 20, :multiple => true} - # - # By default, all select inputs will have a blank option at the top of the list. You can add - # a prompt with the :prompt option, or disable the blank option with :include_blank => false. - # - def select_input(method, options) - collection = find_collection_for_column(method, options) - html_options = options.delete(:input_html) || {} - options = set_include_blank(options) - - reflection = find_reflection(method) - if reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro) - options[:include_blank] = false - html_options[:multiple] ||= true - html_options[:size] ||= 5 - end - - input_name = generate_association_input_name(method) - self.label(method, options_for_label(options).merge(:input_name => input_name)) + - self.select(input_name, collection, set_options(options), html_options) - end - alias :boolean_select_input :select_input - - # Outputs a timezone select input as Rails' time_zone_select helper. You - # can give priority zones as option. - # - # Examples: - # - # f.input :time_zone, :as => :time_zone, :priority_zones => /Australia/ - # - def time_zone_input(method, options) - html_options = options.delete(:input_html) || {} - - self.label(method, options_for_label(options)) + - self.time_zone_select(method, options.delete(:priority_zones), set_options(options), html_options) - end - - # Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list - # items, one for each possible choice in the belongs_to association. Each li contains a - # label and a radio input. - # - # Example: - # - # f.input :author, :as => :radio - # - # Output: - # - #
    - # Author - #
      - #
    1. - # - #
    2. - #
    3. - # - #
    4. - #
    - #
    - # - # You can customize the choices available in the radio button set by passing in a collection (an Array or - # Hash) through the :collection option. If not provided, the choices are found by reflecting on the association - # (Author.find(:all) in the example above). - # - # Examples: - # - # f.input :author, :as => :radio, :collection => @authors - # f.input :author, :as => :radio, :collection => Author.find(:all) - # f.input :author, :as => :radio, :collection => [@justin, @kate] - # f.input :author, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"] - # - # The :label_method option allows you to customize the label for each radio button two ways: - # - # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc) - # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together - # - # Examples: - # - # f.input :author, :as => :radio, :label_method => :full_name - # f.input :author, :as => :radio, :label_method => :login - # f.input :author, :as => :radio, :label_method => :full_name_with_post_count - # f.input :author, :as => :radio, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" } - # - # The :value_method option provides the same customization of the value attribute of each option tag. - # - # Examples: - # - # f.input :author, :as => :radio, :value_method => :full_name - # f.input :author, :as => :radio, :value_method => :login - # f.input :author, :as => :radio, :value_method => Proc.new { |a| "author_#{a.login}" } - # - # Finally, you can set :value_as_class => true if you want the li wrapper around each radio - # button / label combination to contain a class with the value of the radio button (useful for - # applying specific CSS or Javascript to a particular radio button). - def radio_input(method, options) - collection = find_collection_for_column(method, options) - html_options = set_options(options).merge(options.delete(:input_html) || {}) - - input_name = generate_association_input_name(method) - value_as_class = options.delete(:value_as_class) - - list_item_content = collection.map do |c| - label = c.is_a?(Array) ? c.first : c - value = c.is_a?(Array) ? c.last : c - - li_content = template.content_tag(:label, - "#{self.radio_button(input_name, value, html_options)} #{label}", - :for => generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase) - ) - - li_options = value_as_class ? { :class => value.to_s.downcase } : {} - template.content_tag(:li, li_content, li_options) - end - - field_set_and_list_wrapping_for_method(method, options, list_item_content) - end - alias :boolean_radio_input :radio_input - - # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list - # items (li), one for each fragment for the date (year, month, day). Each li contains a label - # (eg "Year") and a select box. See date_or_datetime_input for a more detailed output example. - # - # Some of Rails' options for select_date are supported, but not everything yet. - def date_input(method, options) - options = set_include_blank(options) - date_or_datetime_input(method, options.merge(:discard_hour => true)) - end - - - # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list - # items (li), one for each fragment for the date (year, month, day, hour, min, sec). Each li - # contains a label (eg "Year") and a select box. See date_or_datetime_input for a more - # detailed output example. - # - # Some of Rails' options for select_date are supported, but not everything yet. - def datetime_input(method, options) - options = set_include_blank(options) - date_or_datetime_input(method, options) - end - - - # Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list - # items (li), one for each fragment for the time (hour, minute, second). Each li contains a label - # (eg "Hour") and a select box. See date_or_datetime_input for a more detailed output example. - # - # Some of Rails' options for select_time are supported, but not everything yet. - def time_input(method, options) - options = set_include_blank(options) - date_or_datetime_input(method, options.merge(:discard_year => true, :discard_month => true, :discard_day => true)) - end - - - #
    - # Created At - #
      - #
    1. - # - # - #
    2. - #
    3. - # - # - #
    4. - #
    5. - # - # - #
    6. - #
    - #
    - # - # This is an absolute abomination, but so is the official Rails select_date(). - # - 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 - inputs = options.delete(:order) || i18n_date_order || [:year, :month, :day] - - time_inputs = [:hour, :minute] - time_inputs << [:second] if options[:include_seconds] - - list_items_capture = "" - hidden_fields_capture = "" - - # Gets the datetime object. It can be a Fixnum, Date or Time, or nil. - datetime = @object ? @object.send(method) : nil - html_options = options.delete(:input_html) || {} - - (inputs + time_inputs).each do |input| - html_id = generate_html_id(method, "#{position[input]}i") - field_name = "#{method}(#{position[input]}i)" - if options["discard_#{input}".intern] - break if time_inputs.include?(input) - - hidden_value = datetime.respond_to?(input) ? datetime.send(input) : datetime - 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]) - - list_items_capture << template.content_tag(:li, - template.content_tag(:label, item_label_text, :for => html_id) + - template.send("select_#{input}".intern, datetime, opts, html_options.merge(:id => html_id)) - ) - end - end - - hidden_fields_capture + field_set_and_list_wrapping_for_method(method, options, list_items_capture) - end - - - # Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list - # items, one for each possible choice in the belongs_to association. Each li contains a - # label and a check_box input. - # - # This is an alternative for has many and has and belongs to many associations. - # - # Example: - # - # f.input :author, :as => :check_boxes - # - # Output: - # - #
    - # Authors - #
      - #
    1. - # - # - #
    2. - #
    3. - # - # - #
    4. - #
    - #
    - # - # Notice that the value of the checkbox is the same as the id and the hidden - # field has empty value. You can override the hidden field value using the - # unchecked_value option. - # - # You can customize the options available in the set by passing in a collection (Array) of - # ActiveRecord objects through the :collection option. If not provided, the choices are found - # by inferring the parent's class name from the method name and simply calling find(:all) on - # it (Author.find(:all) in the example above). - # - # Examples: - # - # f.input :author, :as => :check_boxes, :collection => @authors - # f.input :author, :as => :check_boxes, :collection => Author.find(:all) - # f.input :author, :as => :check_boxes, :collection => [@justin, @kate] - # - # The :label_method option allows you to customize the label for each checkbox two ways: - # - # * by naming the correct method to call on each object in the collection as a symbol (:name, :login, etc) - # * by passing a Proc that will be called on each object in the collection, allowing you to use helpers or multiple model attributes together - # - # Examples: - # - # f.input :author, :as => :check_boxes, :label_method => :full_name - # f.input :author, :as => :check_boxes, :label_method => :login - # f.input :author, :as => :check_boxes, :label_method => :full_name_with_post_count - # f.input :author, :as => :check_boxes, :label_method => Proc.new { |a| "#{a.name} (#{pluralize("post", a.posts.count)})" } - # - # The :value_method option provides the same customization of the value attribute of each checkbox input tag. - # - # Examples: - # - # f.input :author, :as => :check_boxes, :value_method => :full_name - # f.input :author, :as => :check_boxes, :value_method => :login - # f.input :author, :as => :check_boxes, :value_method => Proc.new { |a| "author_#{a.login}" } - # - # Finally, you can set :value_as_class => true if you want the li wrapper around each checkbox / label - # combination to contain a class with the value of the radio button (useful for applying specific - # CSS or Javascript to a particular checkbox). - def check_boxes_input(method, options) - collection = find_collection_for_column(method, options) - html_options = options.delete(:input_html) || {} - - input_name = generate_association_input_name(method) - value_as_class = options.delete(:value_as_class) - unchecked_value = options.delete(:unchecked_value) || '' - html_options = { :name => "#{@object_name}[#{input_name}][]" }.merge(html_options) - - list_item_content = collection.map do |c| - label = c.is_a?(Array) ? c.first : c - value = c.is_a?(Array) ? c.last : c - - html_options.merge!(:id => generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase)) - - li_content = template.content_tag(:label, - "#{self.check_box(input_name, html_options, value, unchecked_value)} #{label}", - :for => html_options[:id] - ) - - li_options = value_as_class ? { :class => value.to_s.downcase } : {} - template.content_tag(:li, li_content, li_options) - end - - field_set_and_list_wrapping_for_method(method, options, list_item_content) - end - - - # Outputs a country select input, wrapping around a regular country_select helper. - # Rails doesn't come with a country_select helper by default any more, so you'll need to install - # the "official" plugin, or, if you wish, any other country_select plugin that behaves in the - # same way. - # - # The Rails plugin iso-3166-country-select plugin can be found "here":http://github.com/rails/iso-3166-country-select. - # - # By default, Formtastic includes a handfull of english-speaking countries as "priority counties", - # which you can change to suit your market and user base (see README for more info on config). - # - # Examples: - # f.input :location, :as => :country # use Formtastic::SemanticFormBuilder.priority_countries array for the priority countries - # f.input :location, :as => :country, :priority_countries => /Australia/ # set your own - # - def country_input(method, options) - 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) - - html_options = options.delete(:input_html) || {} - priority_countries = options.delete(:priority_countries) || @@priority_countries - - self.label(method, options_for_label(options)) + - self.country_select(method, priority_countries, set_options(options), html_options) - end - - - # Outputs a label containing a checkbox and the label text. The label defaults - # to the column name (method name) and can be altered with the :label option. - # :checked_value and :unchecked_value options are also available. - # - def boolean_input(method, options) - html_options = options.delete(:input_html) || {} - - input = self.check_box(method, set_options(options).merge(html_options), - options.delete(:checked_value) || '1', options.delete(:unchecked_value) || '0') - - self.label(method, input << self.label(method, options_for_label(options)), options_for_label(options)) - end - - # Generates an input for the given method using the type supplied with :as. - # - # If the input is included in INPUT_MAPPINGS, it uses input_simple - # implementation which maps most of the inputs. All others have specific - # code and then a proper handler should be called (like radio_input) for - # :radio types. - # - def inline_input_for(method, options) - input_type = options.delete(:as) - - if INPUT_MAPPINGS.key?(input_type) - input_simple(input_type, method, options) - else - send("#{input_type}_input", method, options) - end - end - - # Generates hints for the given method using the text supplied in :hint. - # - def inline_hints_for(method, options) #:nodoc: - options[:hint] = localized_attribute_string(method, options[:hint], :hint) - return if options[:hint].blank? - template.content_tag(:p, options[:hint], :class => 'inline-hints') - end - - # Creates an error sentence by calling to_sentence on the errors array. - # - def error_sentence(errors) #:nodoc: - template.content_tag(:p, errors.to_sentence.untaint, :class => 'inline-errors') - end - - # Creates an error li list. - # - def error_list(errors) #:nodoc: - list_elements = [] - errors.each do |error| - list_elements << template.content_tag(:li, error.untaint) - end - template.content_tag(:ul, list_elements.join("\n"), :class => 'errors') - end - - # Generates the required or optional string. If the value set is a proc, - # it evaluates the proc first. - # - def required_or_optional_string(required) #:nodoc: - string_or_proc = case required - when true - @@required_string - when false - @@optional_string - else - required - end - - if string_or_proc.is_a?(Proc) - string_or_proc.call - else - string_or_proc.to_s - end - end - - # Generates a fieldset and wraps the content in an ordered list. When working - # with nested attributes (in Rails 2.3), it allows %i as interpolation option - # in :name. So you can do: - # - # f.inputs :name => '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: - html_options[:name] ||= html_options.delete(:title) - html_options[:name] = localized_attribute_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] - legend = template.content_tag(:legend, template.content_tag(:span, legend)) unless legend.blank? - - if block_given? - contents = if template.respond_to?(:is_haml?) && template.is_haml? - template.capture_haml(&block) - else - template.capture(&block) - end - end - - # Ruby 1.9: String#to_s behavior changed, need to make an explicit join. - contents = contents.join if contents.respond_to?(:join) - fieldset = template.content_tag(:fieldset, - legend + template.content_tag(:ol, contents), - html_options.except(:builder, :parent) - ) - - template.concat(fieldset) if block_given? - fieldset - end - - # Also generates a fieldset and an ordered list but with label based in - # method. This methods is currently used by radio and datetime inputs. - # - def field_set_and_list_wrapping_for_method(method, options, contents) - contents = contents.join if contents.respond_to?(:join) - - template.content_tag(:fieldset, - %{#{self.label(method, options_for_label(options).merge!(:as_span => true))}} + - template.content_tag(:ol, contents) - ) - end - - # For methods that have a database column, take a best guess as to what the input method - # should be. In most cases, it will just return the column type (eg :string), but for special - # cases it will simplify (like the case of :integer, :float & :decimal to :numeric), or do - # something different (like :password and :select). - # - # If there is no column for the method (eg "virtual columns" with an attr_accessor), the - # default is a :string, a similar behaviour to Rails' scaffolding. - # - def default_input_type(method) #:nodoc: - column = @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute) - - if column - # handle the special cases where the column type doesn't map to an input method - return :time_zone if column.type == :string && method.to_s =~ /time_zone/ - return :select if column.type == :integer && method.to_s =~ /_id$/ - return :datetime if column.type == :timestamp - return :numeric if [:integer, :float, :decimal].include?(column.type) - return :password if column.type == :string && method.to_s =~ /password/ - return :country if column.type == :string && method.to_s =~ /country/ - - # otherwise assume the input name will be the same as the column type (eg string_input) - return column.type - else - if @object - return :select if find_reflection(method) - - file = @object.send(method) if @object.respond_to?(method) - return :file if file && @@file_methods.any? { |m| file.respond_to?(m) } - end - - return :password if method.to_s =~ /password/ - return :string - end - end - - # Used by select and radio inputs. The collection can be retrieved by - # three ways: - # - # * Explicitly provided through :collection - # * Retrivied through an association - # * Or a boolean column, which will generate a localized { "Yes" => true, "No" => false } hash. - # - # If the collection is not a hash or an array of strings, fixnums or arrays, - # we use label_method and value_method to retreive an array with the - # appropriate label and value. - # - def find_collection_for_column(column, options) - reflection = find_reflection(column) - - collection = if options[:collection] - options.delete(:collection) - elsif reflection - reflection.klass.find(:all) - else - create_boolean_collection(options) - end - - collection = collection.to_a if collection.is_a?(Hash) - - # Return if we have an Array of strings, fixnums or arrays - return collection if collection.instance_of?(Array) && - [Array, Fixnum, String, Symbol].include?(collection.first.class) - - label = options.delete(:label_method) || detect_label_method(collection) - value = options.delete(:value_method) || :id - - collection.map { |o| [send_or_call(label, o), send_or_call(value, o)] } - end - - # Detected the label collection method when none is supplied using the - # values set in @@collection_label_methods. - # - def detect_label_method(collection) #:nodoc: - @@collection_label_methods.detect { |m| collection.first.respond_to?(m) } - end - - # Returns a hash to be used by radio and select inputs when a boolean field - # 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[:value_as_class] = true unless options.key?(:value_as_class) - - [ [ options.delete(:true), true], [ options.delete(:false), false ] ] - end - - # Used by association inputs (select, radio) to generate the name that should - # be used for the input - # - # belongs_to :author; f.input :author; will generate 'author_id' - # belongs_to :entity, :foreign_key = :owner_id; f.input :author; will generate 'owner_id' - # has_many :authors; f.input :authors; will generate 'author_ids' - # has_and_belongs_to_many will act like has_many - # - def generate_association_input_name(method) - if reflection = find_reflection(method) - if [:has_and_belongs_to_many, :has_many].include?(reflection.macro) - "#{method.to_s.singularize}_ids" - else - reflection.options[:foreign_key] || "#{method}_id" - end - else - method - end - end - - # If an association method is passed in (f.input :author) try to find the - # reflection object. - # - def find_reflection(method) - @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association) - end - - # Generates default_string_options by retrieving column information from - # the database. - # - def default_string_options(method, type) #:nodoc: - column = @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute) - - if type == :numeric || column.nil? || column.limit.nil? - { :size => @@default_text_field_size } - else - { :maxlength => column.limit, :size => [column.limit, @@default_text_field_size].min } - end - end - - # Generate the html id for the li tag. - # It takes into account options[:index] and @auto_index to generate li - # elements with appropriate index scope. It also sanitizes the object - # and method names. - # - def generate_html_id(method_name, value='input') - if options.has_key?(:index) - index = "_#{options[:index]}" - elsif defined?(@auto_index) - index = "_#{@auto_index}" - else - index = "" - end - sanitized_method_name = method_name.to_s.gsub(/[\?\/\-]$/, '') - - "#{sanitized_object_name}#{index}_#{sanitized_method_name}_#{value}" - end - - # Gets the nested_child_index value from the parent builder. In Rails 2.3 - # it always returns a fixnum. In next versions it returns a hash with each - # association that the parent builds. - # - def parent_child_index(parent) - duck = parent[:builder].instance_variable_get('@nested_child_index') - - if duck.is_a?(Hash) - child = parent[:for] - child = child.first if child.respond_to?(:first) - duck[child].to_i + 1 - else - duck.to_i + 1 - end - end - - def sanitized_object_name - @sanitized_object_name ||= @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") - end - - def humanized_attribute_name(method) - if @object && @object.class.respond_to?(:human_attribute_name) - @object.class.human_attribute_name(method.to_s) - else - method.to_s.send(@@label_str_method) - end - end - - # Internal generic method for looking up localized values within Formtastic - # using I18n, if no explicit value is set and I18n-lookups are enabled. - # - # Enabled/Disable this by setting: - # - # Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true/false - # - # Lookup priority: - # - # 'formtastic.{{type}}.{{model}}.{{action}}.{{attribute}}' - # 'formtastic.{{type}}.{{model}}.{{attribute}}' - # 'formtastic.{{type}}.{{attribute}}' - # - # Example: - # - # 'formtastic.labels.post.edit.title' - # 'formtastic.labels.post.title' - # 'formtastic.labels.title' - # - # NOTE: Generic, but only used for form input labels/hints. - # - def localized_attribute_string(attr_name, attr_value, i18n_key) - if attr_value.is_a?(String) - attr_value - else - use_i18n = attr_value.nil? ? @@i18n_lookups_by_default : (attr_value != false) - - if use_i18n - model_name = @object.class.name.underscore - action_name = template.params[:action].to_s rescue '' - attribute_name = attr_name.to_s - - defaults = I18N_SCOPES.collect do |i18n_scope| - i18n_path = i18n_scope.dup - i18n_path.gsub!('{{action}}', action_name) - i18n_path.gsub!('{{model}}', model_name) - i18n_path.gsub!('{{attribute}}', attribute_name) - i18n_path.gsub!('..', '.') - i18n_path.to_sym - end - defaults << '' - - i18n_value = ::I18n.t(defaults.shift, :default => defaults, - :scope => "formtastic.#{i18n_key.to_s.pluralize}") - i18n_value.blank? ? nil : i18n_value - end - end - end - - def send_or_call(duck, object) - if duck.is_a?(Proc) - duck.call(object) - else - object.send(duck) - 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 - end - - end - - # Wrappers around form_for (etc) with :builder => SemanticFormBuilder. - # - # * semantic_form_for(@post) - # * semantic_fields_for(@post) - # * semantic_form_remote_for(@post) - # * semantic_remote_form_for(@post) - # - # Each of which are the equivalent of: - # - # * form_for(@post, :builder => Formtastic::SemanticFormBuilder)) - # * fields_for(@post, :builder => Formtastic::SemanticFormBuilder)) - # * form_remote_for(@post, :builder => Formtastic::SemanticFormBuilder)) - # * remote_form_for(@post, :builder => Formtastic::SemanticFormBuilder)) - # - # Example Usage: - # - # <% semantic_form_for @post do |f| %> - # <%= f.input :title %> - # <%= f.input :body %> - # <% end %> - # - # The above examples use a resource-oriented style of form_for() helper where only the @post - # object is given as an argument, but the generic style is also supported if you really want it, - # as is forms with inline objects (Post.new) rather than objects with instance variables (@post): - # - # <% semantic_form_for :post, @post, :url => posts_path do |f| %> - # ... - # <% end %> - # - # <% semantic_form_for :post, Post.new, :url => posts_path do |f| %> - # ... - # <% end %> - # - # The shorter, resource-oriented style is most definitely preferred, and has recieved the most - # testing to date. - # - # Please note: Although it's possible to call Rails' built-in form_for() helper without an - # object, all semantic forms *must* have an object (either Post.new or @post), as Formtastic - # has too many dependencies on an ActiveRecord object being present. - # - module SemanticFormHelper - @@builder = Formtastic::SemanticFormBuilder - mattr_accessor :builder - - @@default_field_error_proc = nil - - # Override the default ActiveRecordHelper behaviour of wrapping the input. - # This gets taken care of semantically by adding an error class to the LI tag - # containing the input. - # - FIELD_ERROR_PROC = proc do |html_tag, instance_tag| - html_tag - end - - def use_custom_field_error_proc(&block) - @@default_field_error_proc = ::ActionView::Base.field_error_proc - ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC - result = yield - ::ActionView::Base.field_error_proc = @@default_field_error_proc - result - end - - [:form_for, :fields_for, :remote_form_for].each do |meth| - src = <<-END_SRC - def semantic_#{meth}(record_or_name_or_array, *args, &proc) - options = args.extract_options! - options[:builder] = @@builder - options[:html] ||= {} - - class_names = options[:html][:class] ? options[:html][:class].split(" ") : [] - class_names << "formtastic" - class_names << case record_or_name_or_array - when String, Symbol then record_or_name_or_array.to_s # :post => "post" - when Array then record_or_name_or_array.last.class.to_s.underscore # [@post, @comment] # => "comment" - else record_or_name_or_array.class.to_s.underscore # @post => "post" - end - options[:html][:class] = class_names.join(" ") - - use_custom_field_error_proc do - #{meth}(record_or_name_or_array, *(args << options), &proc) - end - end - END_SRC - module_eval src, __FILE__, __LINE__ - end - alias :semantic_form_remote_for :semantic_remote_form_for - - end -end diff --git a/vendor/plugins/formtastic/spec/commit_button_spec.rb b/vendor/plugins/formtastic/spec/commit_button_spec.rb deleted file mode 100644 index cf96950..0000000 --- a/vendor/plugins/formtastic/spec/commit_button_spec.rb +++ /dev/null @@ -1,189 +0,0 @@ -# coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' - -describe 'SemanticFormBuilder#commit_button' do - - include FormtasticSpecHelper - - before do - @output_buffer = '' - mock_everything - end - - describe 'when used on any record' do - - before do - @new_post.stub!(:new_record?).and_return(false) - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button) - end - end - - it 'should render a commit li' do - output_buffer.should have_tag('li.commit') - end - - it 'should render an input with a type attribute of "submit"' do - output_buffer.should have_tag('li.commit input[@type="submit"]') - end - - it 'should render an input with a name attribute of "commit"' do - output_buffer.should have_tag('li.commit input[@name="commit"]') - end - - it 'should pass options given in :button_html to the button' do - @new_post.stub!(:new_record?).and_return(false) - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button('text', :button_html => {:class => 'my_class', :id => 'my_id'})) - end - - output_buffer.should have_tag('li.commit input#my_id') - output_buffer.should have_tag('li.commit input.my_class') - end - - end - - describe 'when the first option is a string and the second is a hash' do - - before do - @new_post.stub!(:new_record?).and_return(false) - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button("a string", :button_html => { :class => "pretty"})) - end - end - - it "should render the string as the value of the button" do - output_buffer.should have_tag('li input[@value="a string"]') - end - - it "should deal with the options hash" do - output_buffer.should have_tag('li input.pretty') - end - - end - - describe 'when the first option is a hash' do - - before do - @new_post.stub!(:new_record?).and_return(false) - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button(:button_html => { :class => "pretty"})) - end - end - - it "should deal with the options hash" do - output_buffer.should have_tag('li input.pretty') - end - - end - - describe 'when used on an existing record' do - - it 'should render an input with a value attribute of "Save Post"' do - @new_post.stub!(:new_record?).and_return(false) - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button) - end - output_buffer.should have_tag('li.commit input[@value="Save Post"]') - end - - describe 'when the locale sets the label text' do - before do - I18n.backend.store_translations 'en', :formtastic => {:save => 'Save Changes To {{model}}' } - @new_post.stub!(:new_record?).and_return(false) - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button) - end - end - - after do - I18n.backend.store_translations 'en', :formtastic => {:save => nil} - end - - it 'should allow translation of the labels' do - output_buffer.should have_tag('li.commit input[@value="Save Changes To Post"]') - end - end - - describe 'when the label text is set for a locale with different word order from the default' do - before do - I18n.locale = 'ja' - I18n.backend.store_translations 'ja', :formtastic => {:save => 'Save {{model}}'} - @new_post.stub!(:new_record?).and_return(false) - ::Post.stub!(:human_name).and_return('Post') - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button) - end - end - - after do - I18n.backend.store_translations 'ja', :formtastic => {:save => nil} - I18n.locale = 'en' - end - - it 'should allow the translated label to have a different word order' do - output_buffer.should have_tag('li.commit input[@value="Save Post"]') - end - end - end - - describe 'when used on a new record' do - - it 'should render an input with a value attribute of "Create Post"' do - @new_post.stub!(:new_record?).and_return(true) - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button) - end - output_buffer.should have_tag('li.commit input[@value="Create Post"]') - end - - describe 'when the locale sets the label text' do - before do - I18n.backend.store_translations 'en', :formtastic => {:create => 'Make {{model}}' } - semantic_form_for(@new_post) do |builder| - concat(builder.commit_button) - end - end - - after do - I18n.backend.store_translations 'en', :formtastic => {:create => nil} - end - - it 'should allow translation of the labels' do - output_buffer.should have_tag('li.commit input[@value="Make Post"]') - end - end - - end - - describe 'when used without object' do - - it 'should render an input with a value attribute of "Submit"' do - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.commit_button) - end - - output_buffer.should have_tag('li.commit input[@value="Submit Project"]') - end - - describe 'when the locale sets the label text' do - before do - I18n.backend.store_translations 'en', :formtastic => { :submit => 'Send {{model}}' } - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.commit_button) - end - end - - after do - I18n.backend.store_translations 'en', :formtastic => {:submit => nil} - end - - it 'should allow translation of the labels' do - output_buffer.should have_tag('li.commit input[@value="Send Project"]') - end - end - - end - -end - diff --git a/vendor/plugins/formtastic/spec/input_spec.rb b/vendor/plugins/formtastic/spec/input_spec.rb deleted file mode 100644 index f341882..0000000 --- a/vendor/plugins/formtastic/spec/input_spec.rb +++ /dev/null @@ -1,2157 +0,0 @@ -# coding: utf-8 -require File.dirname(__FILE__) + '/test_helper' - -describe 'SemanticFormBuilder#input' do - - include FormtasticSpecHelper - - before do - @output_buffer = '' - mock_everything - end - - describe 'with inline order customization' do - it 'should allow input, hints, errors as order' do - Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors] - - semantic_form_for(@new_post) do |builder| - builder.should_receive(:inline_input_for).once.ordered - builder.should_receive(:inline_hints_for).once.ordered - builder.should_receive(:inline_errors_for).once.ordered - concat(builder.input(:title)) - end - end - - it 'should allow hints, input, errors as order' do - Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors] - - semantic_form_for(@new_post) do |builder| - builder.should_receive(:inline_hints_for).once.ordered - builder.should_receive(:inline_input_for).once.ordered - builder.should_receive(:inline_errors_for).once.ordered - concat(builder.input(:title)) - end - end - end - - describe 'arguments and options' do - - it 'should require the first argument (the method on form\'s object)' do - lambda { - semantic_form_for(@new_post) do |builder| - concat(builder.input()) # no args passed in at all - end - }.should raise_error(ArgumentError) - end - - describe ':required option' do - - describe 'when true' do - - before do - @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 = %{*} - end - - it 'should set a "required" class' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :required => true)) - end - output_buffer.should_not have_tag('form li.optional') - output_buffer.should have_tag('form li.required') - end - - it 'should append the "required" string to the label' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :required => true)) - end - output_buffer.should have_tag('form li.required label', /#{@string}$/) - end - - end - - describe 'when false' do - - before do - @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 = '' - end - - it 'should set an "optional" class' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :required => false)) - end - output_buffer.should_not have_tag('form li.required') - output_buffer.should have_tag('form li.optional') - end - - it 'should append the "optional" string to the label' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :required => false)) - end - output_buffer.should have_tag('form li.optional label', /#{@string}$/) - end - - end - - describe 'when not provided' do - - 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 - - semantic_form_for(:project, :url => 'http://test.host/') do |builder| - concat(builder.input(:title)) - end - 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 - end - - end - - describe 'and an object was given' do - - describe 'and the validation reflection plugin is available' do - - before do - @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true) - end - - describe 'and validates_presence_of was called for the method' do - it 'should be required' do - @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([ - mock('MacroReflection', :macro => :validates_presence_of, :name => :title, :options => nil) - ]) - @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([ - mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => {:if => true}) - ]) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - concat(builder.input(:body)) - end - output_buffer.should have_tag('form li.required') - output_buffer.should_not have_tag('form li.optional') - end - - it 'should be not be required if the optional :if condition is not satisifed' do - should_be_required(:required => false, :options => { :if => false }) - end - - it 'should not be required if the optional :if proc evaluates to false' do - should_be_required(:required => false, :options => { :if => proc { |record| false } }) - end - - it 'should be required if the optional :if proc evaluates to true' do - should_be_required(:required => true, :options => { :if => proc { |record| true } }) - end - - it 'should not be required if the optional :unless proc evaluates to true' do - should_be_required(:required => false, :options => { :unless => proc { |record| true } }) - end - - it 'should be required if the optional :unless proc evaluates to false' do - should_be_required(:required => true, :options => { :unless => proc { |record| false } }) - end - - it 'should be required if the optional :if with a method string evaluates to true' do - @new_post.should_receive(:required_condition).and_return(true) - should_be_required(:required => true, :options => { :if => :required_condition }) - end - - it 'should be required if the optional :if with a method string evaluates to false' do - @new_post.should_receive(:required_condition).and_return(false) - should_be_required(:required => false, :options => { :if => :required_condition }) - end - - it 'should not be required if the optional :unless with a method string evaluates to false' do - @new_post.should_receive(:required_condition).and_return(false) - should_be_required(:required => true, :options => { :unless => :required_condition }) - end - - it 'should be required if the optional :unless with a method string evaluates to true' do - @new_post.should_receive(:required_condition).and_return(true) - should_be_required(:required => false, :options => { :unless => :required_condition }) - end - end - - # TODO make a matcher for this? - def should_be_required(options) - @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([ - mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => options[:options]) - ]) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:body)) - end - - if options[:required] - output_buffer.should_not have_tag('form li.optional') - output_buffer.should have_tag('form li.required') - else - output_buffer.should have_tag('form li.optional') - output_buffer.should_not have_tag('form li.required') - end - end - - describe 'and validates_presence_of was not called for the method' do - before do - @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([]) - end - - it 'should not be required' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - output_buffer.should_not have_tag('form li.required') - output_buffer.should have_tag('form li.optional') - end - end - - end - - 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 - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - 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 - end - - end - - end - - end - - end - - describe ':as option' do - - describe 'when not provided' do - - it 'should default to a string for forms without objects unless column is password' do - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.input(:anything)) - end - output_buffer.should have_tag('form li.string') - end - - it 'should default to password for forms without objects if column is password' do - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.input(:password)) - concat(builder.input(:password_confirmation)) - concat(builder.input(:confirm_password)) - end - output_buffer.should have_tag('form li.password', :count => 3) - end - - it 'should default to a string for methods on objects that don\'t respond to "column_for_attribute"' do - @new_post.stub!(:method_without_a_database_column) - @new_post.stub!(:column_for_attribute).and_return(nil) - default_input_type(nil, :method_without_a_database_column).should == :string - end - - it 'should default to :password for methods that don\'t have a column in the database but "password" is in the method name' do - @new_post.stub!(:password_method_without_a_database_column) - @new_post.stub!(:column_for_attribute).and_return(nil) - default_input_type(nil, :password_method_without_a_database_column).should == :password - end - - it 'should default to :password for methods on objects that don\'t respond to "column_for_attribute" but "password" is in the method name' do - @new_post.stub!(:password_method_without_a_database_column) - @new_post.stub!(:column_for_attribute).and_return(nil) - default_input_type(nil, :password_method_without_a_database_column).should == :password - end - - it 'should default to :select for column names ending in "_id"' do - default_input_type(:integer, :user_id).should == :select - default_input_type(:integer, :section_id).should == :select - end - - it 'should default to :password for :string column types with "password" in the method name' do - default_input_type(:string, :password).should == :password - default_input_type(:string, :hashed_password).should == :password - default_input_type(:string, :password_hash).should == :password - end - - it 'should default to :text for :text column types' do - default_input_type(:text).should == :text - end - - it 'should default to :date for :date column types' do - default_input_type(:date).should == :date - end - - it 'should default to :datetime for :datetime and :timestamp column types' do - default_input_type(:datetime).should == :datetime - default_input_type(:timestamp).should == :datetime - end - - it 'should default to :time for :time column types' do - default_input_type(:time).should == :time - end - - it 'should default to :boolean for :boolean column types' do - default_input_type(:boolean).should == :boolean - end - - it 'should default to :string for :string column types' do - default_input_type(:string).should == :string - end - - it 'should default to :numeric for :integer, :float and :decimal column types' do - default_input_type(:integer).should == :numeric - default_input_type(:float).should == :numeric - default_input_type(:decimal).should == :numeric - end - - it 'should default to :country for :string columns named country' do - default_input_type(:string, :country).should == :country - end - - describe 'defaulting to file column' do - 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| - column.stub!(:respond_to?).with(test).and_return(method == test) - end - - @new_post.should_receive(method).and_return(column) - - semantic_form_for(@new_post) do |builder| - builder.send(:default_input_type, method).should == :file - end - end - end - - end - end - - it 'should call the corresponding input method' do - [:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes, :hidden].each do |input_style| - @new_post.stub!(:generic_column_name) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255)) - semantic_form_for(@new_post) do |builder| - builder.should_receive(:"#{input_style}_input").once.and_return("fake HTML output from #input") - concat(builder.input(:generic_column_name, :as => input_style)) - end - end - - Formtastic::SemanticFormBuilder::INPUT_MAPPINGS.keys.each do |input_style| - @new_post.stub!(:generic_column_name) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255)) - semantic_form_for(@new_post) do |builder| - builder.should_receive(:input_simple).once.and_return("fake HTML output from #input") - concat(builder.input(:generic_column_name, :as => input_style)) - end - end - end - - end - - describe ':label option' do - - describe 'when provided' do - it 'should be passed down to the label tag' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :label => "Kustom")) - end - output_buffer.should have_tag("form li label", /Kustom/) - end - - it 'should not generate a label if false' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :label => false)) - end - output_buffer.should_not have_tag("form li label") - end - - it 'should be dupped if frozen' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :label => "Kustom".freeze)) - end - output_buffer.should have_tag("form li label", /Kustom/) - end - end - - describe 'when not provided' do - 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 - - 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 - end - - describe 'when localized label is provided' do - before do - @localized_label_text = 'Localized title' - @default_localized_label_text = 'Default localized title' - ::I18n.backend.store_translations :en, - :formtastic => { - :labels => { - :title => @default_localized_label_text, - :published => @default_localized_label_text, - :post => { - :title => @localized_label_text, - :published => @default_localized_label_text - } - } - } - ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false - end - - it 'should render a label with localized label (I18n)' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :label => true)) - concat(builder.input(:published, :as => :boolean, :label => true)) - end - output_buffer.should have_tag('form li label', @localized_label_text) - end - - it 'should render a hint paragraph containing an optional localized label (I18n) if first is not set' do - ::I18n.backend.store_translations :en, - :formtastic => { - :labels => { - :post => { - :title => nil, - :published => nil - } - } - } - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :label => true)) - concat(builder.input(:published, :as => :boolean, :label => true)) - end - output_buffer.should have_tag('form li label', @default_localized_label_text) - end - end - end - - end - - describe ':hint option' do - - describe 'when provided' do - it 'should be passed down to the paragraph tag' do - hint_text = "this is the title of the post" - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :hint => hint_text)) - end - output_buffer.should have_tag("form li p.inline-hints", hint_text) - end - end - - describe 'when not provided' do - describe 'when localized hint (I18n) is provided' do - before do - @localized_hint_text = "This is the localized hint." - @default_localized_hint_text = "This is the default localized hint." - ::I18n.backend.store_translations :en, - :formtastic => { - :hints => { - :title => @default_localized_hint_text, - :post => { - :title => @localized_hint_text - } - } - } - ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false - end - - describe 'when provided value (hint value) is set to TRUE' do - it 'should render a hint paragraph containing a localized hint (I18n)' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :hint => true)) - end - output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text) - end - - it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do - ::I18n.backend.store_translations :en, - :formtastic => { - :hints => { - :post => { - :title => nil - } - } - } - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :hint => true)) - end - output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text) - end - end - - describe 'when provided value (label value) is set to FALSE' do - it 'should not render a hint paragraph' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :hint => false)) - end - output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text) - end - end - end - - describe 'when localized hint (I18n) is not provided' do - it 'should not render a hint paragraph' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - output_buffer.should_not have_tag('form li p.inline-hints') - end - end - end - - end - - describe ':wrapper_html option' do - - describe 'when provided' do - it 'should be passed down to the li tag' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :wrapper_html => {:id => :another_id})) - end - output_buffer.should have_tag("form li#another_id") - end - - it 'should append given classes to li default classes' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true)) - end - output_buffer.should have_tag("form li.string") - output_buffer.should have_tag("form li.required") - output_buffer.should have_tag("form li.another_class") - output_buffer.should have_tag("form li.title") - end - - it 'should allow classes to be an array' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]})) - end - output_buffer.should have_tag("form li.string") - output_buffer.should have_tag("form li.my_class") - output_buffer.should have_tag("form li.another_class") - output_buffer.should have_tag("form li.title") - end - end - - describe 'when not provided' do - it 'should use default id and class' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - output_buffer.should have_tag("form li#post_title_input") - output_buffer.should have_tag("form li.string") - output_buffer.should have_tag("form li.title") - end - end - - end - end - - describe ':as any type of input' do - - it 'should create a list item for each input' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - concat(builder.input(:body)) - end - output_buffer.should have_tag('form li', :count => 2) - end - - describe 'when there are errors on the object for this method' do - before do - @title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome'] - @errors = mock('errors') - @errors.stub!(:[]).with(:title).and_return(@title_errors) - @new_post.stub!(:errors).and_return(@errors) - end - - it 'should apply an errors class to the list item' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - output_buffer.should have_tag('form li.error') - end - - it 'should not wrap the input with the Rails default error wrapping' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - output_buffer.should_not have_tag('div.fieldWithErrors') - end - - it 'should render a paragraph for the errors' do - Formtastic::SemanticFormBuilder.inline_errors = :sentence - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - output_buffer.should have_tag('form li.error p.inline-errors') - end - - it 'should not display an error list' do - Formtastic::SemanticFormBuilder.inline_errors = :list - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - output_buffer.should have_tag('form li.error ul.errors') - end - end - - describe 'when there are no errors on the object for this method' do - before do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - end - - it 'should not apply an errors class to the list item' do - output_buffer.should_not have_tag('form li.error') - end - - it 'should not render a paragraph for the errors' do - output_buffer.should_not have_tag('form li.error p.inline-errors') - end - - it 'should not display an error list' do - output_buffer.should_not have_tag('form li.error ul.errors') - end - end - - describe 'when no object is provided' do - before do - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.input(:title)) - end - end - - it 'should not apply an errors class to the list item' do - output_buffer.should_not have_tag('form li.error') - end - - it 'should not render a paragraph for the errors' do - output_buffer.should_not have_tag('form li.error p.inline-errors') - end - - it 'should not display an error list' do - output_buffer.should_not have_tag('form li.error ul.errors') - end - end - end - - # Test string_mappings: :string, :password and :numeric - string_mappings = Formtastic::SemanticFormBuilder::INPUT_MAPPINGS.slice(*Formtastic::SemanticFormBuilder::STRING_MAPPINGS) - string_mappings.each do |type, template_method| - describe ":as => #{type.inspect}" do - - before do - @new_post.stub!(:title) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => type, :limit => 50)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :as => type)) - end - end - - it "should have a #{type} class on the wrapper" do - output_buffer.should have_tag("form li.#{type}") - end - - it 'should have a post_title_input id on the wrapper' do - output_buffer.should have_tag('form li#post_title_input') - end - - it 'should generate a label for the input' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label[@for="post_title"]') - output_buffer.should have_tag('form li label', /Title/) - end - - input_type = template_method.to_s.split('_').first - - it "should generate a #{input_type} input" do - output_buffer.should have_tag("form li input") - output_buffer.should have_tag("form li input#post_title") - output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]") - output_buffer.should have_tag("form li input[@name=\"post[title]\"]") - end - - unless type == :numeric - it 'should have a maxlength matching the column limit' do - @new_post.column_for_attribute(:title).limit.should == 50 - output_buffer.should have_tag("form li input[@maxlength='50']") - end - - it 'should use default_text_field_size for columns longer than default_text_field_size' do - default_size = Formtastic::SemanticFormBuilder.default_text_field_size - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => type, :limit => default_size * 2)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :as => type)) - end - - output_buffer.should have_tag("form li input[@size='#{default_size}']") - end - - it 'should use the column size for columns shorter than default_text_field_size' do - column_limit_shorted_than_default = 1 - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => type, :limit => column_limit_shorted_than_default)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :as => type)) - end - - output_buffer.should have_tag("form li input[@size='#{column_limit_shorted_than_default}']") - end - end - - it 'should use default_text_field_size for methods without database columns' do - default_size = Formtastic::SemanticFormBuilder.default_text_field_size - @new_post.stub!(:column_for_attribute).and_return(nil) # Return a nil column - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :as => type)) - end - - output_buffer.should have_tag("form li input[@size='#{default_size}']") - end - - it 'should use input_html to style inputs' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :as => type, :input_html => { :class => 'myclass' })) - end - output_buffer.should have_tag("form li input.myclass") - end - - it 'should consider input_html :id in labels' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :as => type, :input_html => { :id => 'myid' })) - end - output_buffer.should have_tag('form li label[@for="myid"]') - end - - it 'should generate input and labels even if no object is given' do - semantic_form_for(:project, :url => 'http://test.host/') do |builder| - concat(builder.input(:title, :as => type)) - end - - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label[@for="project_title"]') - output_buffer.should have_tag('form li label', /Title/) - - output_buffer.should have_tag("form li input") - output_buffer.should have_tag("form li input#project_title") - output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]") - output_buffer.should have_tag("form li input[@name=\"project[title]\"]") - end - - end - end - - # Test other mappings that are not strings: :text and :file. - other_mappings = Formtastic::SemanticFormBuilder::INPUT_MAPPINGS.except(*Formtastic::SemanticFormBuilder::STRING_MAPPINGS) - other_mappings.each do |type, template_method| - describe ":as => #{type.inspect}" do - - before do - @new_post.stub!(:body) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => type)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:body, :as => type)) - end - end - - it "should have a #{type} class on the wrapper" do - output_buffer.should have_tag("form li.#{type}") - end - - it 'should have a post_title_input id on the wrapper' do - output_buffer.should have_tag('form li#post_body_input') - end - - it 'should generate a label for the input' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label[@for="post_body"]') - output_buffer.should have_tag('form li label', /Body/) - end - - input_type = template_method.to_s.gsub(/_field|_/, '') - - if template_method.to_s =~ /_field$/ # password_field - - it "should generate a #{input_type} input" do - output_buffer.should have_tag("form li input") - output_buffer.should have_tag("form li input#post_body") - output_buffer.should have_tag("form li input[@name=\"post[body]\"]") - output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]") - end - - it 'should use input_html to style inputs' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :as => type, :input_html => { :class => 'myclass' })) - end - output_buffer.should have_tag("form li input.myclass") - end - - else # text_area - - it "should generate a #{input_type} input" do - output_buffer.should have_tag("form li #{input_type}") - output_buffer.should have_tag("form li #{input_type}#post_body") - output_buffer.should have_tag("form li #{input_type}[@name=\"post[body]\"]") - end - - it 'should use input_html to style inputs' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:title, :as => type, :input_html => { :class => 'myclass' })) - end - output_buffer.should have_tag("form li #{input_type}.myclass") - end - - end - - describe 'when no object is given' do - before(:each) do - semantic_form_for(:project, :url => 'http://test.host/') do |builder| - concat(builder.input(:title, :as => type)) - end - end - - it 'should generate input' do - if template_method.to_s =~ /_field$/ # password_field - output_buffer.should have_tag("form li input") - output_buffer.should have_tag("form li input#project_title") - output_buffer.should have_tag("form li input[@type=\"#{input_type}\"]") - output_buffer.should have_tag("form li input[@name=\"project[title]\"]") - else - output_buffer.should have_tag("form li #{input_type}") - output_buffer.should have_tag("form li #{input_type}#project_title") - output_buffer.should have_tag("form li #{input_type}[@name=\"project[title]\"]") - end - end - - it 'should generate labels' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label[@for="project_title"]') - output_buffer.should have_tag('form li label', /Title/) - end - end - - end - end - - describe ":as => :hidden" do - before do - @new_post.stub!(:secret) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:secret, :as => :hidden)) - end - end - - it "should have a hidden class on the wrapper" do - output_buffer.should have_tag('form li.hidden') - end - - it 'should have a post_hidden_input id on the wrapper' do - output_buffer.should have_tag('form li#post_secret_input') - end - - it 'should not generate a label for the input' do - output_buffer.should_not have_tag('form li label') - end - - it "should generate a input field" do - output_buffer.should have_tag("form li input#post_secret") - output_buffer.should have_tag("form li input[@type=\"hidden\"]") - output_buffer.should have_tag("form li input[@name=\"post[secret]\"]") - end - - it "should not render inline errors" do - @errors = mock('errors') - @errors.stub!(:[]).with(:secret).and_return(["foo", "bah"]) - @new_post.stub!(:errors).and_return(@errors) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:secret, :as => :hidden)) - end - - output_buffer.should_not have_tag("form li p.inline-errors") - output_buffer.should_not have_tag("form li ul.errors") - end - - end - - describe ":as => :time_zone" do - before do - @new_post.stub!(:time_zone) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:time_zone)) - end - end - - it "should have a time_zone class on the wrapper" do - output_buffer.should have_tag('form li.time_zone') - end - - it 'should have a post_title_input id on the wrapper' do - output_buffer.should have_tag('form li#post_time_zone_input') - end - - it 'should generate a label for the input' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label[@for="post_time_zone"]') - output_buffer.should have_tag('form li label', /Time zone/) - end - - it "should generate a select" do - output_buffer.should have_tag("form li select") - output_buffer.should have_tag("form li select#post_time_zone") - output_buffer.should have_tag("form li select[@name=\"post[time_zone]\"]") - end - - it 'should use input_html to style inputs' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:time_zone, :input_html => { :class => 'myclass' })) - end - output_buffer.should have_tag("form li select.myclass") - end - - describe 'when no object is given' do - before(:each) do - semantic_form_for(:project, :url => 'http://test.host/') do |builder| - concat(builder.input(:time_zone, :as => :time_zone)) - end - end - - it 'should generate labels' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label[@for="project_time_zone"]') - output_buffer.should have_tag('form li label', /Time zone/) - end - - it 'should generate select inputs' do - output_buffer.should have_tag("form li select") - output_buffer.should have_tag("form li select#project_time_zone") - output_buffer.should have_tag("form li select[@name=\"project[time_zone]\"]") - end - end - end - - describe ":as => :country" do - - before do - @new_post.stub!(:country) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string)) - end - - describe "when country_select is not available as a helper from a plugin" do - - it "should raise an error, sugesting the author installs a plugin" do - lambda { - semantic_form_for(@new_post) do |builder| - concat(builder.input(:country, :as => :country)) - end - }.should raise_error - end - - end - - describe "when country_select is available as a helper (from a plugin)" do - - before do - semantic_form_for(@new_post) do |builder| - builder.stub!(:country_select).and_return("") - concat(builder.input(:country, :as => :country)) - end - end - - it "should have a time_zone class on the wrapper" do - output_buffer.should have_tag('form li.country') - end - - it 'should have a post_title_input id on the wrapper' do - output_buffer.should have_tag('form li#post_country_input') - end - - it 'should generate a label for the input' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label[@for="post_country"]') - output_buffer.should have_tag('form li label', /Country/) - end - - it "should generate a select" do - output_buffer.should have_tag("form li select") - end - - end - - describe ":priority_countries option" do - - it "should be passed down to the country_select helper when provided" do - priority_countries = ["Foo", "Bah"] - semantic_form_for(@new_post) do |builder| - builder.stub!(:country_select).and_return("") - builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("") - - concat(builder.input(:country, :as => :country, :priority_countries => priority_countries)) - end - end - - it "should default to the @@priority_countries config when absent" do - priority_countries = Formtastic::SemanticFormBuilder.priority_countries - priority_countries.should_not be_empty - priority_countries.should_not be_nil - - semantic_form_for(@new_post) do |builder| - builder.stub!(:country_select).and_return("") - builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("") - - concat(builder.input(:country, :as => :country)) - end - end - - end - - end - - describe ':as => :radio' do - - before do - @new_post.stub!(:author).and_return(@bob) - @new_post.stub!(:author_id).and_return(@bob.id) - ::Post.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => ::Author, :macro => :belongs_to) } - end - - describe 'for belongs_to association' do - before do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => :radio, :value_as_class => true)) - end - end - - it 'should have a radio class on the wrapper' do - output_buffer.should have_tag('form li.radio') - end - - it 'should have a post_author_input id on the wrapper' do - output_buffer.should have_tag('form li#post_author_input') - end - - it 'should generate a fieldset and legend containing label text for the input' do - output_buffer.should have_tag('form li fieldset') - output_buffer.should have_tag('form li fieldset legend') - output_buffer.should have_tag('form li fieldset legend', /Author/) - end - - it 'should generate an ordered list with a list item for each choice' do - output_buffer.should have_tag('form li fieldset ol') - output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size) - end - - it 'should have one option with a "checked" attribute' do - output_buffer.should have_tag('form li input[@checked]', :count => 1) - end - - describe "each choice" do - - it 'should contain a label for the radio input with a nested input and label text' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/) - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_id_#{author.id}']") - end - end - - it 'should use values as li.class when value_as_class is true' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li fieldset ol li.#{author.id} label") - end - end - - it "should have a radio input" do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li fieldset ol li label input#post_author_id_#{author.id}") - output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']") - output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']") - output_buffer.should have_tag("form li fieldset ol li label input[@name='post[author_id]']") - end - end - - it "should mark input as checked if it's the the existing choice" do - @new_post.author_id.should == @bob.id - @new_post.author.id.should == @bob.id - @new_post.author.should == @bob - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => :radio)) - end - - output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']") - end - end - - describe 'and no object is given' do - before(:each) do - output_buffer.replace '' - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.input(:author_id, :as => :radio, :collection => ::Author.find(:all))) - end - end - - it 'should generate a fieldset with legend' do - output_buffer.should have_tag('form li fieldset legend', /Author/) - end - - it 'should generate an li tag for each item in the collection' do - output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size) - end - - it 'should generate labels for each item' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/) - output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']") - end - end - - it 'should generate inputs for each item' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}") - output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']") - output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']") - output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id]']") - end - end - end - end - end - - describe ':as => :select' do - - before do - @new_post.stub!(:author).and_return(@bob) - @new_post.stub!(:author_id).and_return(@bob.id) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255)) - end - - describe 'for a belongs_to association' do - before do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => :select)) - end - end - - it 'should have a select class on the wrapper' do - output_buffer.should have_tag('form li.select') - end - - it 'should have a post_author_input id on the wrapper' do - output_buffer.should have_tag('form li#post_author_input') - end - - it 'should have a label inside the wrapper' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label', /Author/) - output_buffer.should have_tag("form li label[@for='post_author_id']") - end - - it 'should have a select inside the wrapper' do - output_buffer.should have_tag('form li select') - output_buffer.should have_tag('form li select#post_author_id') - end - - it 'should not create a multi-select' do - output_buffer.should_not have_tag('form li select[@multiple]') - end - - it 'should create a select without size' do - output_buffer.should_not have_tag('form li select[@size]') - end - - it 'should have a blank option' do - output_buffer.should have_tag("form li select option[@value='']") - end - - it 'should have a select option for each Author' do - output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1) - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/) - end - end - - it 'should have one option with a "selected" attribute' do - output_buffer.should have_tag('form li select option[@selected]', :count => 1) - end - - it 'should not singularize the association name' do - @new_post.stub!(:author_status).and_return(@bob) - @new_post.stub!(:author_status_id).and_return(@bob.id) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author_status, :as => :select)) - end - - output_buffer.should have_tag('form li select#post_author_status_id') - end - end - - describe 'for a has_many association' do - before do - semantic_form_for(@fred) do |builder| - concat(builder.input(:posts, :as => :select)) - end - end - - it 'should have a select class on the wrapper' do - output_buffer.should have_tag('form li.select') - end - - it 'should have a post_author_input id on the wrapper' do - output_buffer.should have_tag('form li#author_posts_input') - end - - it 'should have a label inside the wrapper' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label', /Post/) - output_buffer.should have_tag("form li label[@for='author_post_ids']") - end - - it 'should have a select inside the wrapper' do - output_buffer.should have_tag('form li select') - output_buffer.should have_tag('form li select#author_post_ids') - end - - it 'should have a multi-select select' do - output_buffer.should have_tag('form li select[@multiple="multiple"]') - end - - it 'should have a select option for each Post' do - output_buffer.should have_tag('form li select option', :count => ::Post.find(:all).size) - ::Post.find(:all).each do |post| - output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/) - end - end - - it 'should not have a blank option' do - output_buffer.should_not have_tag("form li select option[@value='']") - end - - it 'should have one option with a "selected" attribute' do - output_buffer.should have_tag('form li select option[@selected]', :count => 1) - end - end - - describe 'for a has_and_belongs_to_many association' do - before do - semantic_form_for(@freds_post) do |builder| - concat(builder.input(:authors, :as => :select)) - end - end - - it 'should have a select class on the wrapper' do - output_buffer.should have_tag('form li.select') - end - - it 'should have a post_author_input id on the wrapper' do - output_buffer.should have_tag('form li#post_authors_input') - end - - it 'should have a label inside the wrapper' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label', /Author/) - output_buffer.should have_tag("form li label[@for='post_author_ids']") - end - - it 'should have a select inside the wrapper' do - output_buffer.should have_tag('form li select') - output_buffer.should have_tag('form li select#post_author_ids') - end - - it 'should have a multi-select select' do - output_buffer.should have_tag('form li select[@multiple="multiple"]') - end - - it 'should have a select option for each Author' do - output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size) - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/) - end - end - - it 'should not have a blank option' do - output_buffer.should_not have_tag("form li select option[@value='']") - end - - it 'should have one option with a "selected" attribute' do - output_buffer.should have_tag('form li select option[@selected]', :count => 1) - end - end - - describe 'when :prompt => "choose something" is set' do - before do - @new_post.stub!(:author_id).and_return(nil) - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => :select, :prompt => "choose author")) - end - end - - it 'should have a select with prompt' do - output_buffer.should have_tag("form li select option[@value='']", /choose author/) - end - - it 'should not have a blank select option' do - output_buffer.should_not have_tag("form li select option[@value='']", "") - end - end - - describe 'when no object is given' do - before(:each) do - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.input(:author, :as => :select, :collection => ::Author.find(:all))) - end - end - - it 'should generate label' do - output_buffer.should have_tag('form li label', /Author/) - output_buffer.should have_tag("form li label[@for='project_author']") - end - - it 'should generate select inputs' do - output_buffer.should have_tag('form li select#project_author') - output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1) - end - - it 'should generate an option to each item' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/) - end - end - end - end - - describe ':as => :check_boxes' do - - describe 'for a has_many association' do - before do - semantic_form_for(@fred) do |builder| - concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true)) - end - end - - it 'should have a check_boxes class on the wrapper' do - output_buffer.should have_tag('form li.check_boxes') - end - - it 'should have a author_posts_input id on the wrapper' do - output_buffer.should have_tag('form li#author_posts_input') - end - - it 'should generate a fieldset and legend containing label text for the input' do - output_buffer.should have_tag('form li fieldset') - output_buffer.should have_tag('form li fieldset legend') - output_buffer.should have_tag('form li fieldset legend', /Posts/) - end - - it 'should generate an ordered list with a list item for each choice' do - output_buffer.should have_tag('form li fieldset ol') - output_buffer.should have_tag('form li fieldset ol li', :count => ::Post.find(:all).size) - end - - it 'should have one option with a "checked" attribute' do - output_buffer.should have_tag('form li input[@checked]', :count => 1) - end - - it 'should generate hidden inputs with default value blank' do - output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size) - end - - describe "each choice" do - - it 'should contain a label for the radio input with a nested input and label text' do - ::Post.find(:all).each do |post| - output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/) - output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']") - end - end - - it 'should use values as li.class when value_as_class is true' do - ::Post.find(:all).each do |post| - output_buffer.should have_tag("form li fieldset ol li.#{post.id} label") - end - end - - it 'should have a checkbox input for each post' do - ::Post.find(:all).each do |post| - output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}") - output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2) - end - end - - it "should mark input as checked if it's the the existing choice" do - ::Post.find(:all).include?(@fred.posts.first).should be_true - output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']") - end - end - - describe 'and no object is given' do - before(:each) do - output_buffer.replace '' - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.find(:all))) - end - end - - it 'should generate a fieldset with legend' do - output_buffer.should have_tag('form li fieldset legend', /Author/) - end - - it 'shold generate an li tag for each item in the collection' do - output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size) - end - - it 'should generate labels for each item' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/) - output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']") - end - end - - it 'should generate inputs for each item' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}") - output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']") - output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']") - output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']") - end - end - end - end - end - - describe 'for collections' do - - before do - @new_post.stub!(:author).and_return(@bob) - @new_post.stub!(:author_id).and_return(@bob.id) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255)) - end - - { :select => :option, :radio => :input, :check_boxes => :'input[@type="checkbox"]' }.each do |type, countable| - - describe ":as => #{type.inspect}" do - - describe 'when the :collection option is not provided' do - it 'should perform a basic find on the association class' do - ::Author.should_receive(:find) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => type)) - end - end - end - - describe 'when the :collection option is provided' do - - before do - @authors = ::Author.find(:all) * 2 - output_buffer.replace '' # clears the output_buffer from the before block, hax! - end - - it 'should not call find() on the parent class' do - ::Author.should_not_receive(:find) - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => type, :collection => @authors)) - end - end - - it 'should use the provided collection' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => type, :collection => @authors)) - end - output_buffer.should have_tag("form li.#{type} #{countable}", :count => @authors.size + (type == :select ? 1 : 0)) - end - - describe 'and the :collection is an array of strings' do - before do - @new_post.stub!(:category_name).and_return('') - @categories = [ 'General', 'Design', 'Development', 'Quasi-Serious Inventions' ] - end - - it "should use the string as the label text and value for each #{countable}" do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:category_name, :as => type, :collection => @categories)) - end - - @categories.each do |value| - output_buffer.should have_tag("form li.#{type}", /#{value}/) - output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value}']") - end - end - - if type == :radio - it 'should generate a sanitized label for attribute' do - @bob.stub!(:category_name).and_return(@categories) - semantic_form_for(@new_post) do |builder| - builder.semantic_fields_for(@bob) do |bob_builder| - concat(bob_builder.input(:category_name, :as => type, :collection => @categories)) - end - end - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_general']") - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_design']") - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_development']") - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_category_name_quasiserious_inventions']") - end - end - end - - describe 'and the :collection is a hash of strings' do - before do - @new_post.stub!(:category_name).and_return('') - @categories = { 'General' => 'gen', 'Design' => 'des','Development' => 'dev' } - end - - it "should use the key as the label text and the hash value as the value attribute for each #{countable}" do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:category_name, :as => type, :collection => @categories)) - end - - @categories.each do |label, value| - output_buffer.should have_tag("form li.#{type}", /#{label}/) - output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value}']") - end - end - end - - describe 'and the :collection is an array of arrays' do - before do - @new_post.stub!(:category_name).and_return('') - @categories = { 'General' => 'gen', 'Design' => 'des', 'Development' => 'dev' }.to_a - end - - it "should use the first value as the label text and the last value as the value attribute for #{countable}" do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:category_name, :as => type, :collection => @categories)) - end - - @categories.each do |text, value| - label = type == :select ? :option : :label - output_buffer.should have_tag("form li.#{type} #{label}", /#{text}/i) - output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value.to_s}']") - output_buffer.should have_tag("form li.#{type} #{countable}#post_category_name_#{value.to_s}") if type == :radio - end - end - end - - if type == :radio - describe 'and the :collection is an array of arrays with boolean values' do - before do - @new_post.stub!(:category_name).and_return('') - @choices = { 'Yeah' => true, 'Nah' => false }.to_a - end - - it "should use the first value as the label text and the last value as the value attribute for #{countable}" do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:category_name, :as => type, :collection => @choices)) - end - - output_buffer.should have_tag("form li.#{type} #{countable}#post_category_name_true") - output_buffer.should have_tag("form li.#{type} #{countable}#post_category_name_false") - end - end - end - - - describe 'and the :collection is an array of symbols' do - before do - @new_post.stub!(:category_name).and_return('') - @categories = [ :General, :Design, :Development ] - end - - it "should use the symbol as the label text and value for each #{countable}" do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:category_name, :as => type, :collection => @categories)) - end - - @categories.each do |value| - label = type == :select ? :option : :label - output_buffer.should have_tag("form li.#{type} #{label}", /#{value}/i) - output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value.to_s}']") - end - end - end - - describe 'and the :collection is an OrderedHash of strings' do - before do - @new_post.stub!(:category_name).and_return('') - @categories = ActiveSupport::OrderedHash.new('General' => 'gen', 'Design' => 'des','Development' => 'dev') - end - - it "should use the key as the label text and the hash value as the value attribute for each #{countable}" do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:category_name, :as => type, :collection => @categories)) - end - - @categories.each do |label, value| - output_buffer.should have_tag("form li.#{type}", /#{label}/) - output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{value}']") - end - end - - end - - describe 'when the :label_method option is provided' do - - describe 'as a symbol' do - before do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => type, :label_method => :login)) - end - end - - it 'should have options with text content from the specified method' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li.#{type}", /#{author.login}/) - end - end - end - - describe 'as a proc' do - before do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => type, :label_method => Proc.new {|a| a.login.reverse })) - end - end - - it 'should have options with the proc applied to each' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li.#{type}", /#{author.login.reverse}/) - end - end - end - - end - - describe 'when the :label_method option is not provided' do - Formtastic::SemanticFormBuilder.collection_label_methods.each do |label_method| - - describe "when the collection objects respond to #{label_method}" do - before do - @fred.stub!(:respond_to?).and_return { |m| m.to_s == label_method } - ::Author.find(:all).each { |a| a.stub!(label_method).and_return('The Label Text') } - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => type)) - end - end - - it "should render the options with #{label_method} as the label" do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li.#{type}", /The Label Text/) - end - end - end - - end - end - - describe 'when the :value_method option is provided' do - - describe 'as a symbol' do - before do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => type, :value_method => :login)) - end - end - - it 'should have options with values from specified method' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{author.login}']") - end - end - end - - describe 'as a proc' do - before do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:author, :as => type, :value_method => Proc.new {|a| a.login.reverse })) - end - end - - it 'should have options with the proc applied to each value' do - ::Author.find(:all).each do |author| - output_buffer.should have_tag("form li.#{type} #{countable}[@value='#{author.login.reverse}']") - end - end - end - end - - end - end - end - - describe 'for boolean attributes' do - - { :select => :option, :radio => :input }.each do |type, countable| - checked_or_selected = { :select => :selected, :radio => :checked }[type] - - describe ":as => #{type.inspect}" do - - before do - @new_post.stub!(:allow_comments) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:allow_comments, :as => type)) - end - end - - it "should have a #{type} class on the wrapper" do - output_buffer.should have_tag("form li.#{type}") - end - - it 'should have a post_allow_comments_input id on the wrapper' do - output_buffer.should have_tag('form li#post_allow_comments_input') - end - - it 'should generate a fieldset containing a legend' do - output_buffer.should have_tag("form li.#{type}", /Allow comments/) - end - - it "should generate two #{countable}" do - output_buffer.should have_tag("form li.#{type} #{countable}", :count => (type == :select ? 3 : 2)) - output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="true"]}) - output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="false"]}) - end - - describe 'when the locale sets the label text' do - before do - I18n.backend.store_translations 'en', :formtastic => {:yes => 'Absolutely!', :no => 'Never!'} - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:allow_comments, :as => type)) - end - end - - after do - I18n.backend.store_translations 'en', :formtastic => {:yes => nil, :no => nil} - end - - it 'should allow translation of the labels' do - output_buffer.should have_tag("form li.#{type}", /Absolutely\!/) - output_buffer.should have_tag("form li.#{type}", /Never\!/) - end - end - - describe 'when the value is nil' do - before do - @new_post.stub!(:allow_comments).and_return(nil) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:allow_comments, :as => type)) - end - end - - it "should not mark either #{countable} as #{checked_or_selected}" do - output_buffer.should_not have_tag(%{form li.#{type} input[@#{checked_or_selected}="#{checked_or_selected}"]}) - end - end - - describe 'when the value is true' do - before do - @new_post.stub!(:allow_comments).and_return(true) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean)) - semantic_form_for(@new_post) do |builder| - concat(builder.input(:allow_comments, :as => type)) - end - end - - it "should mark the true #{countable} as #{checked_or_selected}" do - output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="true"][@#{checked_or_selected}="#{checked_or_selected}"]}, :count => 1) - end - - it "should not mark the false #{countable} as #{checked_or_selected}" do - output_buffer.should_not have_tag(%{form li.#{type} #{countable}[@value="false"][@#{checked_or_selected}="#{checked_or_selected}"]}) - end - end - - describe 'when the value is false' do - before do - @new_post.stub!(:allow_comments).and_return(false) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean)) - semantic_form_for(@new_post) do |builder| - concat(builder.input(:allow_comments, :as => type)) - end - end - - it "should not mark the true #{countable} as #{checked_or_selected}" do - output_buffer.should_not have_tag(%{form li.#{type} #{countable}[@value="true"][@#{checked_or_selected}="#{checked_or_selected}"]}) - end - - it "should mark the false #{countable} as #{checked_or_selected}" do - output_buffer.should have_tag(%{form li.#{type} #{countable}[@value="false"][@#{checked_or_selected}="#{checked_or_selected}"]}, :count => 1) - end - end - - describe 'when :true and :false options are provided' do - before do - @new_post.stub!(:allow_comments) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean)) - semantic_form_for(@new_post) do |builder| - concat(builder.input(:allow_comments, :as => type, :true => "Absolutely", :false => "No Way")) - end - end - - it 'should use them as labels' do - output_buffer.should have_tag("form li.#{type}", /Absolutely/) - output_buffer.should have_tag("form li.#{type}", /No Way/) - end - end - end - - end - end - end - - describe ':as => :date' do - - before do - @new_post.stub!(:publish_at) - #@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :date)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:publish_at, :as => :date)) - @builder = builder - end - end - - it 'should have a date class on the wrapper li' do - output_buffer.should have_tag('form li.date') - end - - it 'should have a fieldset inside the li wrapper' do - output_buffer.should have_tag('form li.date fieldset') - end - - it 'should have a legend containing the label text inside the fieldset' do - output_buffer.should have_tag('form li.date fieldset legend', /Publish at/) - end - - it 'should have an ordered list of three items inside the fieldset' do - output_buffer.should have_tag('form li.date fieldset ol') - output_buffer.should have_tag('form li.date fieldset ol li', :count => 3) - end - - it 'should have three labels for year, month and day' do - output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3) - output_buffer.should have_tag('form li.date fieldset ol li label', /year/i) - output_buffer.should have_tag('form li.date fieldset ol li label', /month/i) - output_buffer.should have_tag('form li.date fieldset ol li label', /day/i) - end - - it 'should have three selects for year, month and day' do - output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3) - end - end - - describe ':as => :datetime' do - - before do - @new_post.stub!(:publish_at) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :datetime)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:publish_at, :as => :datetime)) - end - end - - it 'should have a datetime class on the wrapper li' do - output_buffer.should have_tag('form li.datetime') - end - - it 'should have a fieldset inside the li wrapper' do - output_buffer.should have_tag('form li.datetime fieldset') - end - - it 'should have a legend containing the label text inside the fieldset' do - output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/) - end - - it 'should have an ordered list of five items inside the fieldset' do - output_buffer.should have_tag('form li.datetime fieldset ol') - output_buffer.should have_tag('form li.datetime fieldset ol li', :count => 5) - end - - it 'should have five labels for year, month, day, hour and minute' do - output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /year/i) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /month/i) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /day/i) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /hour/i) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /minute/i) - end - - it 'should have five selects for year, month, day, hour and minute' do - output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5) - end - - it 'should generate a sanitized label and matching ids for attribute' do - @bob.stub!(:publish_at) - @bob.stub!(:column_for_attribute).and_return(mock('column', :type => :datetime)) - - semantic_form_for(@new_post) do |builder| - builder.semantic_fields_for(@bob, :index => 10) do |bob_builder| - concat(bob_builder.input(:publish_at, :as => :datetime)) - end - end - - 1.upto(5) do |i| - output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_10_publish_at_#{i}i']") - output_buffer.should have_tag("form li fieldset ol li #post_author_10_publish_at_#{i}i") - end - end - - describe 'when :discard_input => true is set' do - it 'should use default hidden value equals to 1 when attribute returns nil' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:publish_at, :as => :datetime, :discard_day => true)) - end - - output_buffer.should have_tag("form li input[@type='hidden'][@value='1']") - end - - it 'should use default attribute value when it is not nil' do - @new_post.stub!(:publish_at).and_return(Date.new(2007,12,27)) - semantic_form_for(@new_post) do |builder| - concat(builder.input(:publish_at, :as => :datetime, :discard_day => true)) - end - - output_buffer.should have_tag("form li input[@type='hidden'][@value='27']") - end - end - - describe 'inputs order' do - it 'should have a default' do - semantic_form_for(@new_post) do |builder| - self.should_receive(:select_year).once.ordered.and_return('') - self.should_receive(:select_month).once.ordered.and_return('') - self.should_receive(:select_day).once.ordered.and_return('') - builder.input(:publish_at, :as => :datetime) - end - end - - it 'should be specified with :order option' do - 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('') - self.should_receive(:select_day).once.ordered.and_return('') - builder.input(:publish_at, :as => :datetime) - end - end - - it 'should be changed through I18n' do - semantic_form_for(@new_post) do |builder| - self.should_receive(:select_day).once.ordered.and_return('') - self.should_receive(:select_month).once.ordered.and_return('') - self.should_receive(:select_year).once.ordered.and_return('') - builder.input(:publish_at, :as => :datetime, :order => [:day, :month, :year]) - end - end - end - - describe 'when the locale changes the label text' do - before do - I18n.backend.store_translations 'en', :datetime => {:prompts => { - :year => 'The Year', :month => 'The Month', :day => 'The Day', - :hour => 'The Hour', :minute => 'The Minute' - }} - semantic_form_for(@new_post) do |builder| - concat(builder.input(:publish_at, :as => :datetime)) - end - end - - after do - I18n.backend.store_translations 'en', :formtastic => { - :year => nil, :month => nil, :day => nil, - :hour => nil, :minute => nil - } - end - - it 'should have translated labels for year, month, day, hour and minute' do - output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Year/) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Month/) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Day/) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Hour/) - output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Minute/) - end - end - - describe 'when no object is given' do - before(:each) do - output_buffer.replace '' - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.input(:publish_at, :as => :datetime)) - @builder = builder - end - end - - it 'should have fieldset with legend' do - output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/) - end - - it 'should have labels for each input' do - output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5) - end - - it 'should have selects for each inputs' do - output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5) - end - end - end - - describe ':as => :time' do - before do - @new_post.stub!(:publish_at) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :time)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:publish_at, :as => :time)) - end - end - - it 'should have a time class on the wrapper li' do - output_buffer.should have_tag('form li.time') - end - - it 'should have a fieldset inside the li wrapper' do - output_buffer.should have_tag('form li.time fieldset') - end - - it 'should have a legend containing the label text inside the fieldset' do - output_buffer.should have_tag('form li.time fieldset legend', /Publish at/) - end - - it 'should have an ordered list of two items inside the fieldset' do - output_buffer.should have_tag('form li.time fieldset ol') - output_buffer.should have_tag('form li.time fieldset ol li', :count => 2) - end - - it 'should have five labels for hour and minute' do - output_buffer.should have_tag('form li.time fieldset ol li label', :count => 2) - output_buffer.should have_tag('form li.time fieldset ol li label', /hour/i) - output_buffer.should have_tag('form li.time fieldset ol li label', /minute/i) - end - - it 'should have two selects for hour and minute' do - output_buffer.should have_tag('form li.time fieldset ol li', :count => 2) - end - end - - describe ':as => :boolean' do - - before do - @new_post.stub!(:allow_comments) - @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :boolean)) - - semantic_form_for(@new_post) do |builder| - concat(builder.input(:allow_comments, :as => :boolean)) - end - end - - it 'should have a boolean class on the wrapper' do - output_buffer.should have_tag('form li.boolean') - end - - it 'should have a post_allow_comments_input id on the wrapper' do - output_buffer.should have_tag('form li#post_allow_comments_input') - end - - it 'should generate a label containing the input' do - output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label[@for="post_allow_comments"]') - output_buffer.should have_tag('form li label', /Allow comments/) - output_buffer.should have_tag('form li label input[@type="checkbox"]') - end - - it 'should generate a checkbox input' do - output_buffer.should have_tag('form li label input') - output_buffer.should have_tag('form li label input#post_allow_comments') - output_buffer.should have_tag('form li label input[@type="checkbox"]') - output_buffer.should have_tag('form li label input[@name="post[allow_comments]"]') - output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]') - end - - it 'should allow checked and unchecked values to be sent' do - semantic_form_for(@new_post) do |builder| - concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked')) - end - - output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]') - output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]') - end - - it 'should generate a label and a checkbox even if no object is given' do - semantic_form_for(:project, :url => 'http://test.host') do |builder| - concat(builder.input(:allow_comments, :as => :boolean)) - end - - output_buffer.should have_tag('form li label[@for="project_allow_comments"]') - output_buffer.should have_tag('form li label', /Allow comments/) - output_buffer.should have_tag('form li label input[@type="checkbox"]') - - output_buffer.should have_tag('form li label input#project_allow_comments') - output_buffer.should have_tag('form li label input[@type="checkbox"]') - output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]') - end - - end -end - diff --git a/vendor/plugins/formtastic/uninstall.rb b/vendor/plugins/formtastic/uninstall.rb deleted file mode 100644 index 9738333..0000000 --- a/vendor/plugins/formtastic/uninstall.rb +++ /dev/null @@ -1 +0,0 @@ -# Uninstall hook code here