Skip to content

Commit

Permalink
Refactor require_label form helper in order to use it with namespaced…
Browse files Browse the repository at this point in the history
… classes.
  • Loading branch information
ugisozols committed Aug 4, 2011
1 parent 5c13361 commit 8c9b76a
Showing 1 changed file with 11 additions and 35 deletions.
46 changes: 11 additions & 35 deletions core/lib/refinery/helpers/form_helper.rb
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 8c9b76a

Please sign in to comment.