Sha256: e40e8bf2d79dbe075957f1a0de9d7f9eea3718409d2f0f7331c3d2e4716f81c8

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

ActionView::Helpers::FormBuilder.class_eval do

  def spec_label(field, text = nil, options = {})
    label_html = label(field)
    element = parse_element(label_html, 'label')
    id = element['for']
    text ||= element.text
    @template.spec_label_tag(id, text, options)
  end

  private

  def parse_element(html, tag)
    doc = Nokogiri::XML(html)
    doc.css(tag).first or raise "Could not find CSS #{tag.inspect} in HTML #{html.inspect}"
  end

end

ActionView::Helpers::FormTagHelper.class_eval do

  def spec_label_tag(id, text = nil, options = {})
    count = SpecLabelCounter.next(controller, text)
    label = count == 1 ? text : "#{text} (#{count})"
    options.merge!(:class => 'hidden') unless Rails.env.test?
    html = label_tag(id, label, options)
    html
  end

end

class SpecLabelCounter
  class << self

    def next(controller, text)
      counter(controller)[text] ||= 0
      counter(controller)[text] += 1
    end

    def counter(controller)
      ivar = :"@_spec_label_counter"
      controller.instance_variable_get(ivar) || controller.instance_variable_set(ivar, {})
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
katapult-0.5.0 lib/generators/katapult/basics/templates/lib/ext/action_view/spec_label.rb
katapult-0.4.1 lib/generators/katapult/basics/templates/lib/ext/action_view/spec_label.rb
katapult-0.4.0 lib/generators/katapult/basics/templates/lib/ext/action_view/spec_label.rb
katapult-0.3.0 lib/generators/katapult/basics/templates/lib/ext/action_view/spec_label.rb
katapult-0.2.0 lib/generators/katapult/basics/templates/lib/ext/action_view/spec_label.rb