From 8c9b76a145f83820b082e3e5c587dc1ef40b3b12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=A3is=20Ozols?= Date: Thu, 4 Aug 2011 20:28:57 +0300 Subject: [PATCH] Refactor require_label form helper in order to use it with namespaced classes. --- core/lib/refinery/helpers/form_helper.rb | 46 ++++++------------------ 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/core/lib/refinery/helpers/form_helper.rb b/core/lib/refinery/helpers/form_helper.rb index 5167af1edf..d1d205853f 100644 --- a/core/lib/refinery/helpers/form_helper.rb +++ b/core/lib/refinery/helpers/form_helper.rb @@ -3,29 +3,29 @@ ActionView::Helpers::FormHelper.module_eval do - def required_label(object_name, method, text = nil, options = {}) + def required_label(object_name, method, options = {}) options = {:class => "required"}.merge!(options) - label(object_name, method, "#{label_humanize_text(object_name, method, text)} *", options) + label(object_name, method, "#{label_humanize_text(method, options)} *", options) end - def label_humanize_text object_name, method, text = nil - content = text unless text.is_a?(Hash) - if content.blank? - content = object_name.classify.constantize.respond_to?(:human_attribute_name) ? object_name.classify.constantize.human_attribute_name(method) : method.to_s - else - content = content.to_s + def label_humanize_text method, options = {} + object = options[:object] + + content ||= if object && object.class.respond_to?(:human_attribute_name) + object.class.human_attribute_name(method) end - content.humanize + + content ||= method.humanize end end ActionView::Helpers::FormBuilder.module_eval do - def required_label(method, text = nil, options = {}) - @template.required_label(@object_name, method, text, objectify_options(options)) + def required_label(method, options = {}) + @template.required_label(@object_name, method, objectify_options(options)) end end @@ -40,27 +40,3 @@ def required_label_tag(name, text = nil, options = {}) end end - -# I18n labels automatically -module ActionView - module Helpers - class InstanceTag - def to_label_tag(text = nil, options = {}) - options = options.stringify_keys - name_and_id = options.dup - add_default_name_and_id(name_and_id) - options.delete("index") - options["for"] ||= name_and_id["id"] - if text.blank? - content = method_name.humanize - if object.class.respond_to?(:human_attribute_name) - content = object.class.human_attribute_name(method_name) - end - else - content = text.to_s - end - label_tag(name_and_id["id"], content, options) - end - end - end -end