From bef968d37942bfb2b7a59fca0e4451e096197c0a Mon Sep 17 00:00:00 2001 From: Carsten Gehling Date: Tue, 29 Dec 2009 13:06:19 +0100 Subject: I18n label helper [#745 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/lib/action_view/helpers/form_helper.rb | 39 +++++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index d0c66eda60..9baa9f1fff 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -504,8 +504,9 @@ module ActionView end # 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 you specify - # it explicitly. Additional options on the label tag can be passed as a hash with +options+. These options will be tagged + # 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..) 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 :value option, which is designed to # target labels for radio_button tags (where the value is used in the ID of the input tag). # @@ -513,6 +514,29 @@ module ActionView # label(:post, :title) # # => # + # 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: + # post: + # body: "Write your entire text here" + # + # Which then will result in + # + # label(:post, :body) + # # => + # + # Localization can also be based purely on the translation of the attribute-name like this: + # + # activemodel: + # attribute: + # post: + # cost: "Total cost" + # + # label(:post, :cost) + # # => + # # label(:post, :title, "A short title") # # => # @@ -751,7 +775,16 @@ module ActionView add_default_name_and_id_for_value(tag_value, name_and_id) options.delete("index") options["for"] ||= name_and_id["id"] - content = (text.blank? ? nil : text.to_s) || method_name.humanize + + content = if text.blank? + i18n_label = I18n.t("views.labels.#{object_name}.#{method_name}", :default => "") + i18n_label if i18n_label.present? + else + text.to_s + end + content ||= object.class.human_attribute_name(method_name) if object + content ||= method_name.humanize + label_tag(name_and_id["id"], content, options) end -- cgit v1.2.3 From f50bb48e048b63186684e220b03235936e646ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 31 Dec 2009 14:31:03 +0100 Subject: Do not enforce human_attribute_name as required API. --- actionpack/lib/action_view/helpers/form_helper.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 9baa9f1fff..81c9c88820 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -777,12 +777,15 @@ module ActionView options["for"] ||= name_and_id["id"] content = if text.blank? - i18n_label = I18n.t("views.labels.#{object_name}.#{method_name}", :default => "") - i18n_label if i18n_label.present? + I18n.t("views.labels.#{object_name}.#{method_name}", :default => "").presence else text.to_s end - content ||= object.class.human_attribute_name(method_name) if object + + content ||= if object && object.class.respond_to?(:human_attribute_name) + object.class.human_attribute_name(method_name) + end + content ||= method_name.humanize label_tag(name_and_id["id"], content, options) -- cgit v1.2.3 From 42aa9b87c2d8ae9e19e387f304d2463b6b8c02c5 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Fri, 1 Jan 2010 20:45:28 +1100 Subject: Silence warnings --- actionpack/lib/action_view/base.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index d69e5109fa..4970c768e8 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -274,6 +274,7 @@ module ActionView #:nodoc: end def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil, formats = nil)#:nodoc: + @config = nil @formats = formats @assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) } @controller = controller -- cgit v1.2.3 From d531cbc80959dfc35ee1dcbac133d52d63beacc9 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Sun, 3 Jan 2010 00:56:55 -0600 Subject: fixed missing or incorrect session data error message --- actionpack/lib/action_dispatch/middleware/session/cookie_store.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb index f27f22c7e7..04a101dbb2 100644 --- a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb +++ b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb @@ -179,7 +179,7 @@ module ActionDispatch 'cookie containing the session data. Use ' + 'config.action_controller.session = { :key => ' + '"_myapp_session", :secret => "some secret phrase" } in ' + - 'config/environment.rb' + 'config/application.rb' end end -- cgit v1.2.3 From 6fbe9ef2ffb1858027130789246f3ae24a0a182f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 3 Jan 2010 20:39:42 +0100 Subject: Use namespaces in notifications. --- actionpack/lib/action_controller/caching.rb | 4 ++-- actionpack/lib/action_controller/caching/fragments.rb | 14 ++++++++------ actionpack/lib/action_controller/caching/pages.rb | 8 ++++++-- actionpack/lib/action_controller/metal/logger.rb | 7 ++++--- actionpack/lib/action_view/render/partials.rb | 7 ++++--- actionpack/lib/action_view/render/rendering.rb | 5 +++-- 6 files changed, 27 insertions(+), 18 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb index 69ed84da95..22360735b9 100644 --- a/actionpack/lib/action_controller/caching.rb +++ b/actionpack/lib/action_controller/caching.rb @@ -62,9 +62,9 @@ module ActionController #:nodoc: end def log_event(name, before, after, instrumenter_id, payload) - if name.to_s =~ /(read|write|cache|expire|exist)_(fragment|page)\??/ + if name.to_s =~ /actioncontroller\.((read|write|expire|exist)_(fragment|page)\??)/ key_or_path = payload[:key] || payload[:path] - human_name = name.to_s.humanize + human_name = $1.humanize duration = (after - before) * 1000 logger.info("#{human_name} #{key_or_path.inspect} (%.1fms)" % duration) else diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index f569d0dd8b..91f781a44d 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -53,7 +53,7 @@ module ActionController #:nodoc: return content unless cache_configured? key = fragment_cache_key(key) - ActiveSupport::Notifications.instrument(:write_fragment, :key => key) do + instrument_fragment_cache :write_fragment, key do cache_store.write(key, content, options) end content @@ -64,7 +64,7 @@ module ActionController #:nodoc: return unless cache_configured? key = fragment_cache_key(key) - ActiveSupport::Notifications.instrument(:read_fragment, :key => key) do + instrument_fragment_cache :read_fragment, key do cache_store.read(key, options) end end @@ -74,7 +74,7 @@ module ActionController #:nodoc: return unless cache_configured? key = fragment_cache_key(key) - ActiveSupport::Notifications.instrument(:exist_fragment?, :key => key) do + instrument_fragment_cache :exist_fragment?, key do cache_store.exist?(key, options) end end @@ -101,16 +101,18 @@ module ActionController #:nodoc: key = fragment_cache_key(key) unless key.is_a?(Regexp) message = nil - ActiveSupport::Notifications.instrument(:expire_fragment, :key => key) do + instrument_fragment_cache :expire_fragment, key do if key.is_a?(Regexp) - message = "Expired fragments matching: #{key.source}" cache_store.delete_matched(key, options) else - message = "Expired fragment: #{key}" cache_store.delete(key, options) end end end + + def instrument_fragment_cache(name, key) + ActiveSupport::Notifications.instrument("actioncontroller.#{name}", :key => key){ yield } + end end end end diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index d46f528c7e..4498abcdbe 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -64,7 +64,7 @@ module ActionController #:nodoc: return unless perform_caching path = page_cache_path(path) - ActiveSupport::Notifications.instrument(:expire_page, :path => path) do + instrument_page_cache :expire_page, path do File.delete(path) if File.exist?(path) end end @@ -75,7 +75,7 @@ module ActionController #:nodoc: return unless perform_caching path = page_cache_path(path) - ActiveSupport::Notifications.instrument(:cache_page, :path => path) do + instrument_page_cache :write_page, path do FileUtils.makedirs(File.dirname(path)) File.open(path, "wb+") { |f| f.write(content) } end @@ -107,6 +107,10 @@ module ActionController #:nodoc: def page_cache_path(path) page_cache_directory + page_cache_file(path) end + + def instrument_page_cache(name, path) + ActiveSupport::Notifications.instrument("actioncontroller.#{name}", :path => path){ yield } + end end # Expires the page that was cached with the +options+ as a key. Example: diff --git a/actionpack/lib/action_controller/metal/logger.rb b/actionpack/lib/action_controller/metal/logger.rb index 4f4370e5f0..0dcab86f10 100644 --- a/actionpack/lib/action_controller/metal/logger.rb +++ b/actionpack/lib/action_controller/metal/logger.rb @@ -15,7 +15,8 @@ module ActionController attr_internal :view_runtime def process_action(action) - ActiveSupport::Notifications.instrument(:process_action, :controller => self, :action => action) do + ActiveSupport::Notifications.instrument("actioncontroller.process_action", + :controller => self, :action => action) do super end end @@ -50,7 +51,7 @@ module ActionController # This is the hook invoked by ActiveSupport::Notifications.subscribe. # If you need to log any event, overwrite the method and do it here. def log_event(name, before, after, instrumenter_id, payload) #:nodoc: - if name == :process_action + if name == "actioncontroller.process_action" duration = [(after - before) * 1000, 0.01].max controller = payload[:controller] request = controller.request @@ -66,7 +67,7 @@ module ActionController message << " [#{request.request_uri rescue "unknown"}]" logger.info(message) - elsif name == :render_template + elsif name == "actionview.render_template" # TODO Make render_template logging work if you are using just ActionView duration = (after - before) * 1000 message = "Rendered #{payload[:identifier]}" diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 5158415c20..4459ef6124 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -215,12 +215,13 @@ module ActionView options = @options if @collection - ActiveSupport::Notifications.instrument(:render_collection, :path => @path, - :count => @collection.size) do + ActiveSupport::Notifications.instrument("actionview.render_collection", + :path => @path, :count => @collection.size) do render_collection end else - content = ActiveSupport::Notifications.instrument(:render_partial, :path => @path) do + content = ActiveSupport::Notifications.instrument("actionview.render_partial", + :path => @path) do render_partial end diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 48316cac53..578cfe1177 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -93,7 +93,7 @@ module ActionView def _render_template(template, layout = nil, options = {}) locals = options[:locals] || {} - content = ActiveSupport::Notifications.instrument(:render_template, + content = ActiveSupport::Notifications.instrument("actionview.render_template", :identifier => template.identifier, :layout => (layout ? layout.identifier : nil)) do template.render(self, locals) end @@ -109,7 +109,8 @@ module ActionView end def _render_layout(layout, locals, &block) - ActiveSupport::Notifications.instrument(:render_layout, :identifier => layout.identifier) do + ActiveSupport::Notifications.instrument("actionview.render_layout", + :identifier => layout.identifier) do layout.render(self, locals){ |*name| _layout_for(*name, &block) } end end -- cgit v1.2.3 From 53c6984944b03b5de036167a418593dfcd12e886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 3 Jan 2010 23:33:34 +0100 Subject: Add notifications to ActionDispatch::ShowExceptions, this can be used as hooks for plugins like ExceptionNotifier. --- .../action_dispatch/middleware/show_exceptions.rb | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb index 4ebc8a2ab9..af356707c6 100644 --- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb @@ -1,7 +1,24 @@ require 'active_support/core_ext/exception' +require 'active_support/notifications' require 'action_dispatch/http/request' module ActionDispatch + # This middleware rescues any exception returned by the application and renders + # nice exception pages if it's being rescued locally. + # + # Every time an exception is caught, a notification is published, becoming a good API + # to deal with exceptions. So, if you want send an e-mail through ActionMailer + # everytime this notification is published, you just need to do the following: + # + # ActiveSupport::Notifications.subscribe "action_dispatch.show_exception" do |name, start, end, instrumentation_id, payload| + # ExceptionNotifier.deliver_exception(start, payload) + # end + # + # The payload is a hash which has to pairs: + # + # * :env - Contains the rack env for the given request; + # * :exception - The exception raised; + # class ShowExceptions LOCALHOST = '127.0.0.1'.freeze @@ -44,8 +61,11 @@ module ActionDispatch def call(env) @app.call(env) rescue Exception => exception - raise exception if env['action_dispatch.show_exceptions'] == false - render_exception(env, exception) + ActiveSupport::Notifications.instrument 'action_dispatch.show_exception', + :env => env, :exception => exception do + raise exception if env['action_dispatch.show_exceptions'] == false + render_exception(env, exception) + end end private -- cgit v1.2.3 From 3990310a2bedd0dff5753e3e9b1282e686cff0cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 4 Jan 2010 00:03:56 +0100 Subject: Use underscore in notification namespaces. --- actionpack/lib/action_controller/caching.rb | 2 +- actionpack/lib/action_controller/caching/fragments.rb | 2 +- actionpack/lib/action_controller/caching/pages.rb | 2 +- actionpack/lib/action_controller/metal/logger.rb | 6 +++--- actionpack/lib/action_view/render/partials.rb | 4 ++-- actionpack/lib/action_view/render/rendering.rb | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb index 22360735b9..5c0d754ad6 100644 --- a/actionpack/lib/action_controller/caching.rb +++ b/actionpack/lib/action_controller/caching.rb @@ -62,7 +62,7 @@ module ActionController #:nodoc: end def log_event(name, before, after, instrumenter_id, payload) - if name.to_s =~ /actioncontroller\.((read|write|expire|exist)_(fragment|page)\??)/ + if name.to_s =~ /action_controller\.((read|write|expire|exist)_(fragment|page)\??)/ key_or_path = payload[:key] || payload[:path] human_name = $1.humanize duration = (after - before) * 1000 diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index 91f781a44d..a0c5ed797e 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -111,7 +111,7 @@ module ActionController #:nodoc: end def instrument_fragment_cache(name, key) - ActiveSupport::Notifications.instrument("actioncontroller.#{name}", :key => key){ yield } + ActiveSupport::Notifications.instrument("action_controller.#{name}", :key => key){ yield } end end end diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index 4498abcdbe..5797eeebd6 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -109,7 +109,7 @@ module ActionController #:nodoc: end def instrument_page_cache(name, path) - ActiveSupport::Notifications.instrument("actioncontroller.#{name}", :path => path){ yield } + ActiveSupport::Notifications.instrument("action_controller.#{name}", :path => path){ yield } end end diff --git a/actionpack/lib/action_controller/metal/logger.rb b/actionpack/lib/action_controller/metal/logger.rb index 0dcab86f10..bf5f3b774f 100644 --- a/actionpack/lib/action_controller/metal/logger.rb +++ b/actionpack/lib/action_controller/metal/logger.rb @@ -15,7 +15,7 @@ module ActionController attr_internal :view_runtime def process_action(action) - ActiveSupport::Notifications.instrument("actioncontroller.process_action", + ActiveSupport::Notifications.instrument("action_controller.process_action", :controller => self, :action => action) do super end @@ -51,7 +51,7 @@ module ActionController # This is the hook invoked by ActiveSupport::Notifications.subscribe. # If you need to log any event, overwrite the method and do it here. def log_event(name, before, after, instrumenter_id, payload) #:nodoc: - if name == "actioncontroller.process_action" + if name == "action_controller.process_action" duration = [(after - before) * 1000, 0.01].max controller = payload[:controller] request = controller.request @@ -67,7 +67,7 @@ module ActionController message << " [#{request.request_uri rescue "unknown"}]" logger.info(message) - elsif name == "actionview.render_template" + elsif name == "action_view.render_template" # TODO Make render_template logging work if you are using just ActionView duration = (after - before) * 1000 message = "Rendered #{payload[:identifier]}" diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 4459ef6124..67a8ee4472 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -215,12 +215,12 @@ module ActionView options = @options if @collection - ActiveSupport::Notifications.instrument("actionview.render_collection", + ActiveSupport::Notifications.instrument("action_view.render_collection", :path => @path, :count => @collection.size) do render_collection end else - content = ActiveSupport::Notifications.instrument("actionview.render_partial", + content = ActiveSupport::Notifications.instrument("action_view.render_partial", :path => @path) do render_partial end diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 578cfe1177..2fdfad694d 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -93,7 +93,7 @@ module ActionView def _render_template(template, layout = nil, options = {}) locals = options[:locals] || {} - content = ActiveSupport::Notifications.instrument("actionview.render_template", + content = ActiveSupport::Notifications.instrument("action_view.render_template", :identifier => template.identifier, :layout => (layout ? layout.identifier : nil)) do template.render(self, locals) end @@ -109,7 +109,7 @@ module ActionView end def _render_layout(layout, locals, &block) - ActiveSupport::Notifications.instrument("actionview.render_layout", + ActiveSupport::Notifications.instrument("action_view.render_layout", :identifier => layout.identifier) do layout.render(self, locals){ |*name| _layout_for(*name, &block) } end -- cgit v1.2.3