Skip to content

Instantly share code, notes, and snippets.

@ghiculescu
Created June 3, 2017 03:55
Show Gist options
  • Save ghiculescu/d8c04204841b6fc8164be7d8b641e7e0 to your computer and use it in GitHub Desktop.
Save ghiculescu/d8c04204841b6fc8164be7d8b641e7e0 to your computer and use it in GitHub Desktop.
If you say `placeholder: true` on a form element, Rails gets it from i18n. Do the same for `tooltip: true`
# based on https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/tags/placeholderable.rb
# config/initializers/tooltipable.rb
module ActionView
module Helpers
module Tags # :nodoc:
module Tooltipable # :nodoc:
def initialize(*)
super
if tag_value = @options[:tooltip]
tooltip = tag_value if tag_value.is_a?(String)
method_and_value = tag_value.is_a?(TrueClass) ? @method_name : "#{@method_name}.#{tag_value}"
tooltip ||= Tags::Translator
.new(object, @object_name, method_and_value, "helpers.tooltip")
.translate
tooltip ||= @method_name.humanize
@options[:tooltip] = tooltip
end
end
end
end
end
end
ActionView::Helpers::Tags::TextField.send(:include, ActionView::Helpers::Tags::Tooltipable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment