From e1dd284614d137f47a4f8a4ce79316845f9df295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sat, 6 Oct 2012 23:00:52 -0300 Subject: [PATCH] More Ruby 1.9 hash syntax. --- .../lib/action_view/helpers/form_helper.rb | 99 +++++++++---------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index d3703c9186..b87c2e936f 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -207,7 +207,7 @@ module FormHelper # if the object's class is +Post+. However, this can be overwritten using # the :as option, e.g. - # - # <%= form_for(@person, :as => :client) do |f| %> + # <%= form_for(@person, as: :client) do |f| %> # ... # <% end %> # @@ -242,7 +242,7 @@ module FormHelper # # 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 %> # @@ -254,19 +254,19 @@ module FormHelper # # 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 %> # # 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 %> # # You can also set the answer format, like this: # - # <%= form_for(@post, :format => :json) do |f| %> + # <%= form_for(@post, format: :json) do |f| %> # ... # <% end %> # @@ -290,7 +290,7 @@ module FormHelper # # 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 # supported by HTML forms, the form will be set to POST and a hidden input @@ -300,7 +300,7 @@ module FormHelper # # Specifying: # - # :remote => true + # remote: true # # 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 @@ -310,7 +310,7 @@ module FormHelper # # Example: # - # <%= form_for(@post, :remote => true) do |f| %> + # <%= form_for(@post, remote: true) do |f| %> # ... # <% end %> # @@ -354,7 +354,7 @@ module FormHelper # Example: # # <%= 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 %> @@ -366,7 +366,7 @@ module FormHelper # custom builder. For example, let's say you made a helper to # 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 :last_name %> # <%= f.text_area :biography %> @@ -390,7 +390,7 @@ module FormHelper # # def labelled_form_for(record_or_name_or_array, *args, &proc) # 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 # # If you don't need to attach a form to a model instance, then check out @@ -403,13 +403,13 @@ module FormHelper # # To set an authenticity token you need to pass an :authenticity_token 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 %> # # If you don't want to an authenticity token field be rendered at all just pass false: # - # <%= form_for @invoice, :url => external_url, :authenticity_token => false do |f| + # <%= form_for @invoice, url: external_url, authenticity_token: false do |f| # ... # <% end %> def form_for(record, options = {}, &proc) @@ -435,7 +435,7 @@ def form_for(record, options = {}, &proc) builder = options[:parent_builder] = instantiate_builder(object_name, object, options) 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)) form_tag(options.delete(:url) || {}, default_options) { fields_for } @@ -447,12 +447,12 @@ def apply_form_for_options!(record, object, options) #:nodoc: as = options[:as] action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post] options[:html].reverse_merge!( - :class => as ? "#{action}_#{as}" : dom_class(object, action), - :id => as ? "#{action}_#{as}" : [options[:namespace], dom_id(object, action)].compact.join("_").presence, - :method => method + class: as ? "#{action}_#{as}" : dom_class(object, action), + id: as ? "#{action}_#{as}" : [options[:namespace], dom_id(object, action)].compact.join("_").presence, + method: method ) - options[:url] ||= polymorphic_path(record, :format => options.delete(:format)) + options[:url] ||= polymorphic_path(record, format: options.delete(:format)) end private :apply_form_for_options! @@ -578,7 +578,7 @@ def apply_form_for_options!(record, object, options) #:nodoc: # # class Person < ActiveRecord::Base # has_one :address - # accepts_nested_attributes_for :address, :allow_destroy => true + # accepts_nested_attributes_for :address, allow_destroy: true # end # # Now, when you use a form element with the _destroy parameter, @@ -674,7 +674,7 @@ def apply_form_for_options!(record, object, options) #:nodoc: # # class Person < ActiveRecord::Base # has_many :projects - # accepts_nested_attributes_for :projects, :allow_destroy => true + # accepts_nested_attributes_for :projects, allow_destroy: true # end # # This will allow you to specify which models to destroy in the @@ -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", :class => "title_label") + # label(:post, :title, "A short title", class: "title_label") # # => # - # label(:post, :privacy, "Public Post", :value => "public") + # label(:post, :privacy, "Public Post", value: "public") # # => # # label(:post, :terms) do @@ -766,18 +766,17 @@ def label(object_name, method, content_or_options = nil, options = nil, &block) # shown. # # ==== Examples - # text_field(:post, :title, :size => 20) + # text_field(:post, :title, size: 20) # # => # - # text_field(:post, :title, :class => "create_input") + # text_field(: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!'); }") # # => # - # text_field(:snippet, :code, :size => 20, :class => 'code_input') + # text_field(:snippet, :code, size: 20, class: 'code_input') # # => - # def text_field(object_name, method, options = {}) Tags::TextField.new(object_name, method, self, options).render end @@ -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. # # ==== Examples - # password_field(:login, :pass, :size => 20) + # password_field(:login, :pass, size: 20) # # => # - # password_field(:account, :secret, :class => "form_input", :value => @account.secret) + # password_field(:account, :secret, class: "form_input", value: @account.secret) # # => # - # 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!'); }") # # => # - # password_field(:account, :pin, :size => 20, :class => 'form_input') + # password_field(:account, :pin, size: 20, class: 'form_input') # # => - # def password_field(object_name, method, options = {}) Tags::PasswordField.new(object_name, method, self, options).render end @@ -833,12 +831,11 @@ def hidden_field(object_name, method, options = {}) # file_field(:user, :avatar) # # => # - # file_field(:post, :attached, :accept => 'text/html') + # file_field(:post, :attached, accept: 'text/html') # # => # - # file_field(:attachment, :file, :class => 'file_input') + # file_field(:attachment, :file, class: 'file_input') # # => - # def file_field(object_name, method, options = {}) Tags::FileField.new(object_name, method, self, options).render end @@ -848,22 +845,22 @@ def file_field(object_name, method, options = {}) # hash with +options+. # # ==== Examples - # text_area(:post, :body, :cols => 20, :rows => 40) + # text_area(:post, :body, cols: 20, rows: 40) # # => # - # text_area(:comment, :text, :size => "20x30") + # text_area(:comment, :text, size: "20x30") # # => # - # text_area(:application, :notes, :cols => 40, :rows => 15, :class => 'app_input') + # text_area(:application, :notes, cols: 40, rows: 15, class: 'app_input') # # => # - # text_area(:entry, :body, :size => "20x20", :disabled => 'disabled') + # text_area(:entry, :body, size: "20x20", disabled: 'disabled') # # => @@ -902,7 +899,7 @@ def text_area(object_name, method, options = {}) # Unfortunately that workaround does not work when the check box goes # 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 %> # ... # <% end %> @@ -924,10 +921,9 @@ def text_area(object_name, method, options = {}) # # => # # # - # check_box("eula", "accepted", { :class => 'eula_check' }, "yes", "no") + # check_box("eula", "accepted", { class: 'eula_check' }, "yes", "no") # # => # # - # 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 end @@ -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 # radio button will be checked. # - # To force the radio button to be checked pass :checked => true in the + # To force the radio button to be checked pass checked: true in the # +options+ hash. You may pass HTML options there as well. # # # Let's say that @post.category returns "rails": @@ -957,7 +953,6 @@ def radio_button(object_name, method, tag_value, options = {}) # # color_field("car", "color") # # => - # def color_field(object_name, method, options = {}) Tags::ColorField.new(object_name, method, self, options).render end @@ -968,18 +963,18 @@ def color_field(object_name, method, options = {}) # # search_field(:user, :name) # # => - # search_field(:user, :name, :autosave => false) + # search_field(:user, :name, autosave: false) # # => - # search_field(:user, :name, :results => 3) + # search_field(:user, :name, results: 3) # # => # # Assume request.host returns "www.example.com" - # search_field(:user, :name, :autosave => true) + # search_field(:user, :name, autosave: true) # # => - # search_field(:user, :name, :onsearch => true) + # search_field(:user, :name, onsearch: true) # # => - # search_field(:user, :name, :autosave => false, :onsearch => true) + # search_field(:user, :name, autosave: false, onsearch: true) # # => - # search_field(:user, :name, :autosave => true, :onsearch => true) + # search_field(:user, :name, autosave: true, onsearch: true) # # => def search_field(object_name, method, options = {}) Tags::SearchField.new(object_name, method, self, options).render @@ -1351,7 +1346,7 @@ def emitted_hidden_id? private def objectify_options(options) - @default_options.merge(options.merge(:object => @object)) + @default_options.merge(options.merge(object: @object)) end def submit_default_value @@ -1369,7 +1364,7 @@ def submit_default_value defaults << :"helpers.submit.#{key}" defaults << "#{key.to_s.humanize} #{model}" - I18n.t(defaults.shift, :model => model, :default => defaults) + I18n.t(defaults.shift, model: model, default: defaults) end def nested_attributes_association?(association_name) -- GitLab