Skip to content

Commit

Permalink
More Ruby 1.9 hash syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Oct 7, 2012
1 parent e1e4b1d commit e1dd284
Showing 1 changed file with 47 additions and 52 deletions.
99 changes: 47 additions & 52 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -207,7 +207,7 @@ module FormHelper
# if the object's class is +Post+. However, this can be overwritten using # if the object's class is +Post+. However, this can be overwritten using
# the <tt>:as</tt> option, e.g. - # the <tt>:as</tt> option, e.g. -
# #
# <%= form_for(@person, :as => :client) do |f| %> # <%= form_for(@person, as: :client) do |f| %>
# ... # ...
# <% end %> # <% end %>
# #
Expand Down Expand Up @@ -242,7 +242,7 @@ module FormHelper
# #
# is then equivalent to something like: # is then equivalent to something like:
# #
# <%= form_for @post, :as => :post, :url => post_path(@post), :method => :put, :html => { :class => "edit_post", :id => "edit_post_45" } do |f| %> # <%= form_for @post, as: :post, url: post_path(@post), method: :put, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
# ... # ...
# <% end %> # <% end %>
# #
Expand All @@ -254,19 +254,19 @@ module FormHelper
# #
# is equivalent to something like: # is equivalent to something like:
# #
# <%= form_for @post, :as => :post, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %> # <%= form_for @post, as: :post, url: posts_path, html: { class: "new_post", id: "new_post" } do |f| %>
# ... # ...
# <% end %> # <% end %>
# #
# However you can still overwrite individual conventions, such as: # However you can still overwrite individual conventions, such as:
# #
# <%= form_for(@post, :url => super_posts_path) do |f| %> # <%= form_for(@post, url: super_posts_path) do |f| %>
# ... # ...
# <% end %> # <% end %>
# #
# You can also set the answer format, like this: # You can also set the answer format, like this:
# #
# <%= form_for(@post, :format => :json) do |f| %> # <%= form_for(@post, format: :json) do |f| %>
# ... # ...
# <% end %> # <% end %>
# #
Expand All @@ -290,7 +290,7 @@ module FormHelper
# #
# You can force the form to use the full array of HTTP verbs by setting # You can force the form to use the full array of HTTP verbs by setting
# #
# :method => (:get|:post|:patch|:put|:delete) # method: (:get|:post|:patch|:put|:delete)
# #
# in the options hash. If the verb is not GET or POST, which are natively # in the options hash. If the verb is not GET or POST, which are natively
# supported by HTML forms, the form will be set to POST and a hidden input # supported by HTML forms, the form will be set to POST and a hidden input
Expand All @@ -300,7 +300,7 @@ module FormHelper
# #
# Specifying: # Specifying:
# #
# :remote => true # remote: true
# #
# in the options hash creates a form that will allow the unobtrusive JavaScript drivers to modify its # in the options hash creates a form that will allow the unobtrusive JavaScript drivers to modify its
# behavior. The expected default behavior is an XMLHttpRequest in the background instead of the regular # behavior. The expected default behavior is an XMLHttpRequest in the background instead of the regular
Expand All @@ -310,7 +310,7 @@ module FormHelper
# #
# Example: # Example:
# #
# <%= form_for(@post, :remote => true) do |f| %> # <%= form_for(@post, remote: true) do |f| %>
# ... # ...
# <% end %> # <% end %>
# #
Expand Down Expand Up @@ -354,7 +354,7 @@ module FormHelper
# Example: # Example:
# #
# <%= form_for(@post) do |f| %> # <%= form_for(@post) do |f| %>
# <%= f.fields_for(:comments, :include_id => false) do |cf| %> # <%= f.fields_for(:comments, include_id: false) do |cf| %>
# ... # ...
# <% end %> # <% end %>
# <% end %> # <% end %>
Expand All @@ -366,7 +366,7 @@ module FormHelper
# custom builder. For example, let's say you made a helper to # custom builder. For example, let's say you made a helper to
# automatically add labels to form inputs. # automatically add labels to form inputs.
# #
# <%= form_for @person, :url => { :action => "create" }, :builder => LabellingFormBuilder do |f| %> # <%= form_for @person, url: { action: "create" }, builder: LabellingFormBuilder do |f| %>
# <%= f.text_field :first_name %> # <%= f.text_field :first_name %>
# <%= f.text_field :last_name %> # <%= f.text_field :last_name %>
# <%= f.text_area :biography %> # <%= f.text_area :biography %>
Expand All @@ -390,7 +390,7 @@ module FormHelper
# #
# def labelled_form_for(record_or_name_or_array, *args, &proc) # def labelled_form_for(record_or_name_or_array, *args, &proc)
# options = args.extract_options! # options = args.extract_options!
# form_for(record_or_name_or_array, *(args << options.merge(:builder => LabellingFormBuilder)), &proc) # form_for(record_or_name_or_array, *(args << options.merge(builder: LabellingFormBuilder)), &proc)
# end # end
# #
# If you don't need to attach a form to a model instance, then check out # If you don't need to attach a form to a model instance, then check out
Expand All @@ -403,13 +403,13 @@ module FormHelper
# #
# To set an authenticity token you need to pass an <tt>:authenticity_token</tt> parameter # To set an authenticity token you need to pass an <tt>:authenticity_token</tt> parameter
# #
# <%= form_for @invoice, :url => external_url, :authenticity_token => 'external_token' do |f| # <%= form_for @invoice, url: external_url, authenticity_token: 'external_token' do |f|
# ... # ...
# <% end %> # <% end %>
# #
# If you don't want to an authenticity token field be rendered at all just pass <tt>false</tt>: # If you don't want to an authenticity token field be rendered at all just pass <tt>false</tt>:
# #
# <%= form_for @invoice, :url => external_url, :authenticity_token => false do |f| # <%= form_for @invoice, url: external_url, authenticity_token: false do |f|
# ... # ...
# <% end %> # <% end %>
def form_for(record, options = {}, &proc) def form_for(record, options = {}, &proc)
Expand All @@ -435,7 +435,7 @@ def form_for(record, options = {}, &proc)


builder = options[:parent_builder] = instantiate_builder(object_name, object, options) builder = options[:parent_builder] = instantiate_builder(object_name, object, options)
fields_for = fields_for(object_name, object, options, &proc) fields_for = fields_for(object_name, object, options, &proc)
default_options = builder.multipart? ? { :multipart => true } : {} default_options = builder.multipart? ? { multipart: true } : {}
default_options.merge!(options.delete(:html)) default_options.merge!(options.delete(:html))


form_tag(options.delete(:url) || {}, default_options) { fields_for } form_tag(options.delete(:url) || {}, default_options) { fields_for }
Expand All @@ -447,12 +447,12 @@ def apply_form_for_options!(record, object, options) #:nodoc:
as = options[:as] as = options[:as]
action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post] action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post]
options[:html].reverse_merge!( options[:html].reverse_merge!(
:class => as ? "#{action}_#{as}" : dom_class(object, action), class: as ? "#{action}_#{as}" : dom_class(object, action),
:id => as ? "#{action}_#{as}" : [options[:namespace], dom_id(object, action)].compact.join("_").presence, id: as ? "#{action}_#{as}" : [options[:namespace], dom_id(object, action)].compact.join("_").presence,
:method => method method: method
) )


options[:url] ||= polymorphic_path(record, :format => options.delete(:format)) options[:url] ||= polymorphic_path(record, format: options.delete(:format))
end end
private :apply_form_for_options! private :apply_form_for_options!


Expand Down Expand Up @@ -578,7 +578,7 @@ def apply_form_for_options!(record, object, options) #:nodoc:
# #
# class Person < ActiveRecord::Base # class Person < ActiveRecord::Base
# has_one :address # has_one :address
# accepts_nested_attributes_for :address, :allow_destroy => true # accepts_nested_attributes_for :address, allow_destroy: true
# end # end
# #
# Now, when you use a form element with the <tt>_destroy</tt> parameter, # Now, when you use a form element with the <tt>_destroy</tt> parameter,
Expand Down Expand Up @@ -674,7 +674,7 @@ def apply_form_for_options!(record, object, options) #:nodoc:
# #
# class Person < ActiveRecord::Base # class Person < ActiveRecord::Base
# has_many :projects # has_many :projects
# accepts_nested_attributes_for :projects, :allow_destroy => true # accepts_nested_attributes_for :projects, allow_destroy: true
# end # end
# #
# This will allow you to specify which models to destroy in the # This will allow you to specify which models to destroy in the
Expand Down Expand Up @@ -747,10 +747,10 @@ def fields_for(record_name, record_object = nil, options = {}, &block)
# label(:post, :title, "A short title") # label(:post, :title, "A short title")
# # => <label for="post_title">A short title</label> # # => <label for="post_title">A short title</label>
# #
# label(:post, :title, "A short title", :class => "title_label") # label(:post, :title, "A short title", class: "title_label")
# # => <label for="post_title" class="title_label">A short title</label> # # => <label for="post_title" class="title_label">A short title</label>
# #
# label(:post, :privacy, "Public Post", :value => "public") # label(:post, :privacy, "Public Post", value: "public")
# # => <label for="post_privacy_public">Public Post</label> # # => <label for="post_privacy_public">Public Post</label>
# #
# label(:post, :terms) do # label(:post, :terms) do
Expand All @@ -766,18 +766,17 @@ def label(object_name, method, content_or_options = nil, options = nil, &block)
# shown. # shown.
# #
# ==== Examples # ==== Examples
# text_field(:post, :title, :size => 20) # text_field(:post, :title, size: 20)
# # => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" /> # # => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" />
# #
# text_field(:post, :title, :class => "create_input") # text_field(:post, :title, class: "create_input")
# # => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" class="create_input" /> # # => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" class="create_input" />
# #
# text_field(:session, :user, :onchange => "if $('session[user]').value == 'admin' { alert('Your login can not be admin!'); }") # text_field(:session, :user, onchange: "if $('session[user]').value == 'admin' { alert('Your login can not be admin!'); }")
# # => <input type="text" id="session_user" name="session[user]" value="#{@session.user}" onchange = "if $('session[user]').value == 'admin' { alert('Your login can not be admin!'); }"/> # # => <input type="text" id="session_user" name="session[user]" value="#{@session.user}" onchange = "if $('session[user]').value == 'admin' { alert('Your login can not be admin!'); }"/>
# #
# text_field(:snippet, :code, :size => 20, :class => 'code_input') # text_field(:snippet, :code, size: 20, class: 'code_input')
# # => <input type="text" id="snippet_code" name="snippet[code]" size="20" value="#{@snippet.code}" class="code_input" /> # # => <input type="text" id="snippet_code" name="snippet[code]" size="20" value="#{@snippet.code}" class="code_input" />
#
def text_field(object_name, method, options = {}) def text_field(object_name, method, options = {})
Tags::TextField.new(object_name, method, self, options).render Tags::TextField.new(object_name, method, self, options).render
end end
Expand All @@ -788,18 +787,17 @@ def text_field(object_name, method, options = {})
# shown. For security reasons this field is blank by default; pass in a value via +options+ if this is not desired. # shown. For security reasons this field is blank by default; pass in a value via +options+ if this is not desired.
# #
# ==== Examples # ==== Examples
# password_field(:login, :pass, :size => 20) # password_field(:login, :pass, size: 20)
# # => <input type="password" id="login_pass" name="login[pass]" size="20" /> # # => <input type="password" id="login_pass" name="login[pass]" size="20" />
# #
# password_field(:account, :secret, :class => "form_input", :value => @account.secret) # password_field(:account, :secret, class: "form_input", value: @account.secret)
# # => <input type="password" id="account_secret" name="account[secret]" value="#{@account.secret}" class="form_input" /> # # => <input type="password" id="account_secret" name="account[secret]" value="#{@account.secret}" class="form_input" />
# #
# password_field(:user, :password, :onchange => "if $('user[password]').length > 30 { alert('Your password needs to be shorter!'); }") # password_field(:user, :password, onchange: "if $('user[password]').length > 30 { alert('Your password needs to be shorter!'); }")
# # => <input type="password" id="user_password" name="user[password]" onchange = "if $('user[password]').length > 30 { alert('Your password needs to be shorter!'); }"/> # # => <input type="password" id="user_password" name="user[password]" onchange = "if $('user[password]').length > 30 { alert('Your password needs to be shorter!'); }"/>
# #
# password_field(:account, :pin, :size => 20, :class => 'form_input') # password_field(:account, :pin, size: 20, class: 'form_input')
# # => <input type="password" id="account_pin" name="account[pin]" size="20" class="form_input" /> # # => <input type="password" id="account_pin" name="account[pin]" size="20" class="form_input" />
#
def password_field(object_name, method, options = {}) def password_field(object_name, method, options = {})
Tags::PasswordField.new(object_name, method, self, options).render Tags::PasswordField.new(object_name, method, self, options).render
end end
Expand Down Expand Up @@ -833,12 +831,11 @@ def hidden_field(object_name, method, options = {})
# file_field(:user, :avatar) # file_field(:user, :avatar)
# # => <input type="file" id="user_avatar" name="user[avatar]" /> # # => <input type="file" id="user_avatar" name="user[avatar]" />
# #
# file_field(:post, :attached, :accept => 'text/html') # file_field(:post, :attached, accept: 'text/html')
# # => <input accept="text/html" type="file" id="post_attached" name="post[attached]" /> # # => <input accept="text/html" type="file" id="post_attached" name="post[attached]" />
# #
# file_field(:attachment, :file, :class => 'file_input') # file_field(:attachment, :file, class: 'file_input')
# # => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" /> # # => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" />
#
def file_field(object_name, method, options = {}) def file_field(object_name, method, options = {})
Tags::FileField.new(object_name, method, self, options).render Tags::FileField.new(object_name, method, self, options).render
end end
Expand All @@ -848,22 +845,22 @@ def file_field(object_name, method, options = {})
# hash with +options+. # hash with +options+.
# #
# ==== Examples # ==== Examples
# text_area(:post, :body, :cols => 20, :rows => 40) # text_area(:post, :body, cols: 20, rows: 40)
# # => <textarea cols="20" rows="40" id="post_body" name="post[body]"> # # => <textarea cols="20" rows="40" id="post_body" name="post[body]">
# # #{@post.body} # # #{@post.body}
# # </textarea> # # </textarea>
# #
# text_area(:comment, :text, :size => "20x30") # text_area(:comment, :text, size: "20x30")
# # => <textarea cols="20" rows="30" id="comment_text" name="comment[text]"> # # => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
# # #{@comment.text} # # #{@comment.text}
# # </textarea> # # </textarea>
# #
# text_area(:application, :notes, :cols => 40, :rows => 15, :class => 'app_input') # text_area(:application, :notes, cols: 40, rows: 15, class: 'app_input')
# # => <textarea cols="40" rows="15" id="application_notes" name="application[notes]" class="app_input"> # # => <textarea cols="40" rows="15" id="application_notes" name="application[notes]" class="app_input">
# # #{@application.notes} # # #{@application.notes}
# # </textarea> # # </textarea>
# #
# text_area(:entry, :body, :size => "20x20", :disabled => 'disabled') # text_area(:entry, :body, size: "20x20", disabled: 'disabled')
# # => <textarea cols="20" rows="20" id="entry_body" name="entry[body]" disabled="disabled"> # # => <textarea cols="20" rows="20" id="entry_body" name="entry[body]" disabled="disabled">
# # #{@entry.body} # # #{@entry.body}
# # </textarea> # # </textarea>
Expand Down Expand Up @@ -902,7 +899,7 @@ def text_area(object_name, method, options = {})
# Unfortunately that workaround does not work when the check box goes # Unfortunately that workaround does not work when the check box goes
# within an array-like parameter, as in # within an array-like parameter, as in
# #
# <%= fields_for "project[invoice_attributes][]", invoice, :index => nil do |form| %> # <%= fields_for "project[invoice_attributes][]", invoice, index: nil do |form| %>
# <%= form.check_box :paid %> # <%= form.check_box :paid %>
# ... # ...
# <% end %> # <% end %>
Expand All @@ -924,10 +921,9 @@ def text_area(object_name, method, options = {})
# # => <input name="puppy[gooddog]" type="hidden" value="no" /> # # => <input name="puppy[gooddog]" type="hidden" value="no" />
# # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" /> # # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
# #
# check_box("eula", "accepted", { :class => 'eula_check' }, "yes", "no") # check_box("eula", "accepted", { class: 'eula_check' }, "yes", "no")
# # => <input name="eula[accepted]" type="hidden" value="no" /> # # => <input name="eula[accepted]" type="hidden" value="no" />
# # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" /> # # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
#
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0") def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render
end end
Expand All @@ -936,7 +932,7 @@ def check_box(object_name, method, options = {}, checked_value = "1", unchecked_
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the # assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
# radio button will be checked. # radio button will be checked.
# #
# To force the radio button to be checked pass <tt>:checked => true</tt> in the # To force the radio button to be checked pass <tt>checked: true</tt> in the
# +options+ hash. You may pass HTML options there as well. # +options+ hash. You may pass HTML options there as well.
# #
# # Let's say that @post.category returns "rails": # # Let's say that @post.category returns "rails":
Expand All @@ -957,7 +953,6 @@ def radio_button(object_name, method, tag_value, options = {})
# #
# color_field("car", "color") # color_field("car", "color")
# # => <input id="car_color" name="car[color]" type="color" value="#000000" /> # # => <input id="car_color" name="car[color]" type="color" value="#000000" />
#
def color_field(object_name, method, options = {}) def color_field(object_name, method, options = {})
Tags::ColorField.new(object_name, method, self, options).render Tags::ColorField.new(object_name, method, self, options).render
end end
Expand All @@ -968,18 +963,18 @@ def color_field(object_name, method, options = {})
# #
# search_field(:user, :name) # search_field(:user, :name)
# # => <input id="user_name" name="user[name]" type="search" /> # # => <input id="user_name" name="user[name]" type="search" />
# search_field(:user, :name, :autosave => false) # search_field(:user, :name, autosave: false)
# # => <input autosave="false" id="user_name" name="user[name]" type="search" /> # # => <input autosave="false" id="user_name" name="user[name]" type="search" />
# search_field(:user, :name, :results => 3) # search_field(:user, :name, results: 3)
# # => <input id="user_name" name="user[name]" results="3" type="search" /> # # => <input id="user_name" name="user[name]" results="3" type="search" />
# # Assume request.host returns "www.example.com" # # Assume request.host returns "www.example.com"
# search_field(:user, :name, :autosave => true) # search_field(:user, :name, autosave: true)
# # => <input autosave="com.example.www" id="user_name" name="user[name]" results="10" type="search" /> # # => <input autosave="com.example.www" id="user_name" name="user[name]" results="10" type="search" />
# search_field(:user, :name, :onsearch => true) # search_field(:user, :name, onsearch: true)
# # => <input id="user_name" incremental="true" name="user[name]" onsearch="true" type="search" /> # # => <input id="user_name" incremental="true" name="user[name]" onsearch="true" type="search" />
# search_field(:user, :name, :autosave => false, :onsearch => true) # search_field(:user, :name, autosave: false, onsearch: true)
# # => <input autosave="false" id="user_name" incremental="true" name="user[name]" onsearch="true" type="search" /> # # => <input autosave="false" id="user_name" incremental="true" name="user[name]" onsearch="true" type="search" />
# search_field(:user, :name, :autosave => true, :onsearch => true) # search_field(:user, :name, autosave: true, onsearch: true)
# # => <input autosave="com.example.www" id="user_name" incremental="true" name="user[name]" onsearch="true" results="10" type="search" /> # # => <input autosave="com.example.www" id="user_name" incremental="true" name="user[name]" onsearch="true" results="10" type="search" />
def search_field(object_name, method, options = {}) def search_field(object_name, method, options = {})
Tags::SearchField.new(object_name, method, self, options).render Tags::SearchField.new(object_name, method, self, options).render
Expand Down Expand Up @@ -1351,7 +1346,7 @@ def emitted_hidden_id?


private private
def objectify_options(options) def objectify_options(options)
@default_options.merge(options.merge(:object => @object)) @default_options.merge(options.merge(object: @object))
end end


def submit_default_value def submit_default_value
Expand All @@ -1369,7 +1364,7 @@ def submit_default_value
defaults << :"helpers.submit.#{key}" defaults << :"helpers.submit.#{key}"
defaults << "#{key.to_s.humanize} #{model}" defaults << "#{key.to_s.humanize} #{model}"


I18n.t(defaults.shift, :model => model, :default => defaults) I18n.t(defaults.shift, model: model, default: defaults)
end end


def nested_attributes_association?(association_name) def nested_attributes_association?(association_name)
Expand Down

0 comments on commit e1dd284

Please sign in to comment.