module ActionView::Helpers::TagHelper include UnobtrusiveJavascript::Helpers JAVASCRIPT_EVENTS = %w(click mouseup mousedown dblclick mousemove mouseover mouseout submit change keypress keyup keydown) alias_method :rails_tag_options, :tag_options protected # Patch to the built-in Rails tag_options method. Looks for any # javascript event handlers, extracts them and registers them # as unobtrusive behaviours. # # This behaviour affects any built-in Rails helpers that generate # HTML. Event extraction behaviour can be bypassed by passing in # :inline => true as part of a helper's HTML options hash. def tag_options(opts) unless opts[:inline] JAVASCRIPT_EVENTS.each do |event| if opts.include?("on#{event}") opts['id'] = generate_html_id unless opts['id'] register_js_behaviour("##{opts['id']}:#{event}", opts["on#{event}"]) unless opts["on#{event}"].nil? opts.delete("on#{event}") end end end rails_tag_options(opts) end # Generate a unique id to be used as the HTML +id+ attribute. def generate_html_id @tag_counter ||= 0 @tag_counter = @tag_counter.next "#{UnobtrusiveJavascript::Settings.generated_id_prefix}#{@tag_counter}" end end