diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-17 03:20:30 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-17 03:20:30 +0530 |
commit | b04230e3bbf912d60601e9e7b797c4cd43581d51 (patch) | |
tree | 97a2f784a2ec2bfae4f960af56a9280dad6f7774 /actionpack/lib/action_view | |
parent | 867829b187969607aa12f2b0457f25da9c204db0 (diff) | |
parent | 6e3bee6cf1f0d2684152292db0a8b757249824fd (diff) | |
download | rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.tar.gz rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.tar.bz2 rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.zip |
Merge remote branch 'mainstream/master'
Conflicts:
actionpack/lib/action_controller/metal/flash.rb
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r-- | actionpack/lib/action_view/helpers/active_model_helper.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/date_helper.rb | 10 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 68 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_options_helper.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/number_helper.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/prototype_helper.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/url_helper.rb | 13 | ||||
-rw-r--r-- | actionpack/lib/action_view/locale/en.yml | 40 | ||||
-rw-r--r-- | actionpack/lib/action_view/railtie.rb | 17 | ||||
-rw-r--r-- | actionpack/lib/action_view/railties/subscriber.rb | 24 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/partials.rb | 17 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/rendering.rb | 24 | ||||
-rw-r--r-- | actionpack/lib/action_view/template/text.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/test_case.rb | 3 |
15 files changed, 162 insertions, 66 deletions
diff --git a/actionpack/lib/action_view/helpers/active_model_helper.rb b/actionpack/lib/action_view/helpers/active_model_helper.rb index c70f29f098..87b7adf6c4 100644 --- a/actionpack/lib/action_view/helpers/active_model_helper.rb +++ b/actionpack/lib/action_view/helpers/active_model_helper.rb @@ -216,7 +216,7 @@ module ActionView end options[:object_name] ||= params.first - I18n.with_options :locale => options[:locale], :scope => [:activemodel, :errors, :template] do |locale| + I18n.with_options :locale => options[:locale], :scope => [:errors, :template] do |locale| header_message = if options.include?(:header_message) options[:header_message] else diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 4b51dc7856..34f38b0a8a 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -616,7 +616,7 @@ module ActionView build_selects_from_types(order) else - "#{select_date}#{@options[:datetime_separator]}#{select_time}" + "#{select_date}#{@options[:datetime_separator]}#{select_time}".html_safe! end end @@ -835,7 +835,7 @@ module ActionView select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt] select_html << select_options_as_html.to_s - content_tag(:select, select_html, select_options) + "\n" + (content_tag(:select, select_html, select_options) + "\n").html_safe! end # Builds a prompt option tag with supplied options or from default options @@ -860,12 +860,12 @@ module ActionView # build_hidden(:year, 2008) # => "<input id="post_written_on_1i" name="post[written_on(1i)]" type="hidden" value="2008" />" def build_hidden(type, value) - tag(:input, { + (tag(:input, { :type => "hidden", :id => input_id_from_type(type), :name => input_name_from_type(type), :value => value - }) + "\n" + }) + "\n").html_safe! end # Returns the name attribute for the input tag @@ -896,7 +896,7 @@ module ActionView separator = separator(type) unless type == order.first # don't add on last field select.insert(0, separator.to_s + send("select_#{type}").to_s) end - select + select.html_safe! end # Returns the separator for a given datetime component diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 81c9c88820..20e9916d62 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -505,7 +505,7 @@ module ActionView # Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object # assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation - # is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or you specify it explicitly. + # is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly. # Additional options on the label tag can be passed as a hash with +options+. These options will be tagged # onto the HTML as an HTML element attribute as in the example shown, except for the <tt>:value</tt> option, which is designed to # target labels for radio_button tags (where the value is used in the ID of the input tag). @@ -517,8 +517,8 @@ module ActionView # You can localize your labels based on model and attribute names. # For example you can define the following in your locale (e.g. en.yml) # - # views: - # labels: + # helpers: + # label: # post: # body: "Write your entire text here" # @@ -777,7 +777,7 @@ module ActionView options["for"] ||= name_and_id["id"] content = if text.blank? - I18n.t("views.labels.#{object_name}.#{method_name}", :default => "").presence + I18n.t("helpers.label.#{object_name}.#{method_name}", :default => "").presence else text.to_s end @@ -798,7 +798,7 @@ module ActionView if field_type == "hidden" options.delete("size") end - options["type"] = field_type + options["type"] ||= field_type options["value"] ||= value_before_type_cast(object) unless field_type == "file" options["value"] &&= html_escape(options["value"]) add_default_name_and_id(options) @@ -842,7 +842,12 @@ module ActionView checked = self.class.check_box_checked?(value(object), checked_value) end options["checked"] = "checked" if checked - add_default_name_and_id(options) + if options["multiple"] + add_default_name_and_id_for_value(checked_value, options) + options.delete("multiple") + else + add_default_name_and_id(options) + end hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value) checkbox = tag("input", options) (hidden + checkbox).html_safe! @@ -1058,7 +1063,7 @@ module ActionView def radio_button(method, tag_value, options = {}) @template.radio_button(@object_name, method, tag_value, objectify_options(options)) end - + def hidden_field(method, options = {}) @emitted_hidden_id = true if method == :id @template.hidden_field(@object_name, method, objectify_options(options)) @@ -1072,7 +1077,36 @@ module ActionView @template.error_messages_for(@object_name, objectify_options(options)) end - def submit(value = "Save changes", options = {}) + # Add the submit button for the given form. When no value is given, it checks + # if the object is a new resource or not to create the proper label: + # + # <% form_for @post do |f| %> + # <%= f.submit %> + # <% end %> + # + # In the example above, if @post is a new record, it will use "Create Post" as + # submit button label, otherwise, it uses "Update Post". + # + # Those labels can be customized using I18n, under the helpers.submit key and accept + # the {{model}} as translation interpolation: + # + # en: + # helpers: + # submit: + # create: "Create a {{model}}" + # update: "Confirm changes to {{model}}" + # + # It also searches for a key specific for the given object: + # + # en: + # helpers: + # submit: + # post: + # create: "Add {{model}}" + # + def submit(value=nil, options={}) + value, options = nil, value if value.is_a?(Hash) + value ||= submit_default_value @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit")) end @@ -1085,6 +1119,24 @@ module ActionView @default_options.merge(options.merge(:object => @object)) end + def submit_default_value + object = @object.respond_to?(:to_model) ? @object.to_model : @object + key = object ? (object.new_record? ? :create : :update) : :submit + + model = if object.class.respond_to?(:model_name) + object.class.model_name.human + else + @object_name.to_s.humanize + end + + defaults = [] + defaults << :"helpers.submit.#{object_name}.#{key}" + defaults << :"helpers.submit.#{key}" + defaults << "#{key.to_s.humanize} #{model}" + + I18n.t(defaults.shift, :model => model, :default => defaults) + end + def nested_attributes_association?(association_name) @object.respond_to?("#{association_name}_attributes=") end diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 935ab5f3e8..02ad637509 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -571,7 +571,7 @@ module ActionView option_tags = "<option value=\"\">#{options[:include_blank] if options[:include_blank].kind_of?(String)}</option>\n" + option_tags end if value.blank? && options[:prompt] - prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('support.select.prompt', :default => 'Please select') + prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select') "<option value=\"\">#{prompt}</option>\n" + option_tags else option_tags diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 397871b85e..64b71663c3 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -92,7 +92,7 @@ module ActionView :precision => precision, :delimiter => delimiter, :separator => separator) - ).gsub(/%u/, unit) + ).gsub(/%u/, unit).html_safe! rescue number end diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 8c1f0ad81f..ff7bc3b34e 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -1030,7 +1030,7 @@ module ActionView # page.hide 'spinner' # end def update_page(&block) - JavaScriptGenerator.new(@template, &block).to_s + JavaScriptGenerator.new(@template, &block).to_s.html_safe! end # Works like update_page but wraps the generated JavaScript in a <script> diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index a3bee3e8c2..814d86812d 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -565,7 +565,7 @@ module ActionView end link_text = block_given?? yield(href) : href - href = 'http://' + href unless href.index('http') == 0 + href = 'http://' + href unless href =~ %r{^[a-z]+://}i content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation.reverse.join('') end diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 5b136d4f54..14628c5404 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -11,6 +11,11 @@ module ActionView module UrlHelper include JavaScriptHelper + # Need to map default url options to controller one. + def default_url_options(*args) #:nodoc: + @controller.send(:default_url_options, *args) + end + # Returns the URL for the set of +options+ provided. This takes the # same options as +url_for+ in Action Controller (see the # documentation for <tt>ActionController::Base#url_for</tt>). Note that by default @@ -461,10 +466,10 @@ module ActionView string = '' extras = '' - extras << "cc=#{CGI.escape(cc).gsub("+", "%20")}&" unless cc.nil? - extras << "bcc=#{CGI.escape(bcc).gsub("+", "%20")}&" unless bcc.nil? - extras << "body=#{CGI.escape(body).gsub("+", "%20")}&" unless body.nil? - extras << "subject=#{CGI.escape(subject).gsub("+", "%20")}&" unless subject.nil? + extras << "cc=#{Rack::Utils.escape(cc).gsub("+", "%20")}&" unless cc.nil? + extras << "bcc=#{Rack::Utils.escape(bcc).gsub("+", "%20")}&" unless bcc.nil? + extras << "body=#{Rack::Utils.escape(body).gsub("+", "%20")}&" unless body.nil? + extras << "subject=#{Rack::Utils.escape(subject).gsub("+", "%20")}&" unless subject.nil? extras = "?" << extras.gsub!(/&?$/,"") unless extras.empty? email_address = email_address.to_s diff --git a/actionpack/lib/action_view/locale/en.yml b/actionpack/lib/action_view/locale/en.yml index 5e2a92b89a..a3548051c1 100644 --- a/actionpack/lib/action_view/locale/en.yml +++ b/actionpack/lib/action_view/locale/en.yml @@ -9,7 +9,7 @@ delimiter: "," # Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00) precision: 3 - + # Used in number_to_currency() currency: format: @@ -20,15 +20,15 @@ separator: "." delimiter: "," precision: 2 - + # Used in number_to_percentage() percentage: format: # These three are to override number.format and are optional - # separator: + # separator: delimiter: "" - # precision: - + # precision: + # Used in number_to_precision() precision: format: @@ -36,12 +36,12 @@ # separator: delimiter: "" # precision: - + # Used in number_to_human_size() human: format: # These three are to override number.format and are optional - # separator: + # separator: delimiter: "" precision: 1 storage_units: @@ -102,16 +102,22 @@ minute: "Minute" second: "Seconds" - activemodel: - errors: - template: - header: - one: "1 error prohibited this {{model}} from being saved" - other: "{{count}} errors prohibited this {{model}} from being saved" - # The variable :count is also available - body: "There were problems with the following fields:" + errors: + template: + header: + one: "1 error prohibited this {{model}} from being saved" + other: "{{count}} errors prohibited this {{model}} from being saved" + # The variable :count is also available + body: "There were problems with the following fields:" - support: + helpers: select: - # default value for :prompt => true in FormOptionsHelper + # Default value for :prompt => true in FormOptionsHelper prompt: "Please select" + + # Default translation keys for submit FormHelper + submit: + create: 'Create {{model}}' + update: 'Update {{model}}' + submit: 'Save {{model}}' + diff --git a/actionpack/lib/action_view/railtie.rb b/actionpack/lib/action_view/railtie.rb index a90e0636b9..968dc7b25e 100644 --- a/actionpack/lib/action_view/railtie.rb +++ b/actionpack/lib/action_view/railtie.rb @@ -1,2 +1,17 @@ require "action_view" -require "rails"
\ No newline at end of file +require "rails" + +module ActionView + class Railtie < Rails::Railtie + plugin_name :action_view + + require "action_view/railties/subscriber" + subscriber ActionView::Railties::Subscriber.new + + initializer "action_view.cache_asset_timestamps" do |app| + unless app.config.cache_classes + ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false + end + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_view/railties/subscriber.rb b/actionpack/lib/action_view/railties/subscriber.rb new file mode 100644 index 0000000000..803f19379c --- /dev/null +++ b/actionpack/lib/action_view/railties/subscriber.rb @@ -0,0 +1,24 @@ +module ActionView + module Railties + class Subscriber < Rails::Subscriber + def render_template(event) + message = "Rendered #{from_rails_root(event.payload[:identifier])}" + message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] + message << (" (%.1fms)" % event.duration) + info(message) + end + alias :render_partial :render_template + alias :render_collection :render_template + + def logger + ActionController::Base.logger + end + + protected + + def from_rails_root(string) + string.sub("#{Rails.root}/", "").sub(/^app\/views\//, "") + end + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 5158415c20..8c936ae09e 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -212,34 +212,34 @@ module ActionView end def render - options = @options + identifier = ((@template = find_template) ? @template.identifier : @path) if @collection - ActiveSupport::Notifications.instrument(:render_collection, :path => @path, - :count => @collection.size) do + ActiveSupport::Notifications.instrument("action_view.render_collection", + :identifier => identifier || "collection", :count => @collection.size) do render_collection end else - content = ActiveSupport::Notifications.instrument(:render_partial, :path => @path) do + content = ActiveSupport::Notifications.instrument("action_view.render_partial", + :identifier => identifier) do render_partial end - if !@block && options[:layout] - content = @view._render_layout(find_template(options[:layout]), @locals){ content } + if !@block && (layout = @options[:layout]) + content = @view._render_layout(find_template(layout), @locals){ content } end content end end def render_collection - @template = template = find_template return nil if @collection.blank? if @options.key?(:spacer_template) spacer = find_template(@options[:spacer_template]).render(@view, @locals) end - result = template ? collection_with_template : collection_without_template + result = @template ? collection_with_template : collection_without_template result.join(spacer).html_safe! end @@ -277,7 +277,6 @@ module ActionView end def render_partial(object = @object) - @template = template = find_template locals, view = @locals, @view object ||= locals[template.variable_name] diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 48316cac53..ec278ca783 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -93,25 +93,23 @@ module ActionView def _render_template(template, layout = nil, options = {}) locals = options[:locals] || {} - content = ActiveSupport::Notifications.instrument(:render_template, - :identifier => template.identifier, :layout => (layout ? layout.identifier : nil)) do - template.render(self, locals) - end + ActiveSupport::Notifications.instrument("action_view.render_template", + :identifier => template.identifier, :layout => layout.try(:identifier)) do - @_content_for[:layout] = content + content = template.render(self, locals) + @_content_for[:layout] = content - if layout - @_layout = layout.identifier - content = _render_layout(layout, locals) - end + if layout + @_layout = layout.identifier + content = _render_layout(layout, locals) + end - content + content + end end def _render_layout(layout, locals, &block) - ActiveSupport::Notifications.instrument(:render_layout, :identifier => layout.identifier) do - layout.render(self, locals){ |*name| _layout_for(*name, &block) } - end + layout.render(self, locals){ |*name| _layout_for(*name, &block) } end end end diff --git a/actionpack/lib/action_view/template/text.rb b/actionpack/lib/action_view/template/text.rb index 67e086d8bd..2abb352d4e 100644 --- a/actionpack/lib/action_view/template/text.rb +++ b/actionpack/lib/action_view/template/text.rb @@ -13,7 +13,7 @@ module ActionView #:nodoc: end def identifier - self + 'text template' end def inspect diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index be9a2ed50d..16d66b6eca 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -1,6 +1,3 @@ -require 'active_support/test_case' -require 'action_controller/test_case' - module ActionView class Base alias_method :initialize_without_template_tracking, :initialize |