module FilterHelper
  def filter_option_link(site, type, options = {})
    selected = options[:selected]

    link_to filter_site_mappings_path(site),
        'class'         => "filter-option #{'filter-selected' if selected}",
        'data-toggle'   => 'dropdown',
        'role'          => 'button' do
      "#{type} <span class=\"glyphicon glyphicon-chevron-down\"></span>".html_safe
    end
  end

  def filter_remove_option_link(site, type, type_sym)
    link_to site_mappings_path(site, params.except(type_sym, :page)), title: 'Remove filter', class: 'filter-option filter-selected' do
      "<span class=\"glyphicon glyphicon-remove\"></span><span class=\"rm\">Remove</span> #{type}".html_safe
    end
  end

  ##
  # When rendering a single filter form for a dropdown we need to pass through
  # all the other existing filter field values
  def hidden_filter_fields_except(filter, field)
    hidden_fields = (View::Mappings::Filter.fields - [field]).map do |name|
      value = filter.try(name)
      hidden_field_tag(name, value) unless value.blank?
    end
    hidden_fields.join("\n").html_safe
  end
end

module FilterHelper
  def filter_option_link(site, type, options = {})
    selected = options[:selected]

    link_to filter_site_mappings_path(site),
        'class'         => "filter-option #{'filter-selected' if selected}",
        'data-toggle'   => 'dropdown',
        'role'          => 'button' do
      "#{type} <span class=\"glyphicon glyphicon-chevron-down\"></span>".html_safe
    end
  end

  def filter_remove_option_link(site, type, type_sym)
    link_to site_mappings_path(params.except(type_sym, :page)), title: 'Remove filter', class: 'filter-option filter-selected' do
      "<span class=\"glyphicon glyphicon-remove\"></span><span class=\"rm\">Remove</span> #{type}".html_safe
    end
  end

  ##
  # When rendering a single filter form for a dropdown we need to pass through
  # all the other existing filter field values
  def hidden_filter_fields_except(filter, field)
    hidden_fields = (View::Mappings::Filter.fields - [field]).map do |name|
      value = filter.try(name)
      hidden_field_tag(name, value) unless value.blank?
    end
    hidden_fields.join("\n").html_safe
  end
end

Fix generation of links to remove individual filters
Fix generation of links to remove individual filters

The links to remove individual filters from the mappings index pages
were being wrongly generated by the site_mappings_path helper using
the site_id from the params hash as the format, for example:

    /sites/aaib/mappings.aaib?type=redirect

Visiting those links raised ActionController::UnknownFormat, so 3 features
were failing.

This started happening between Rails 4.1.9 and 4.1.10, and it seems to be
related to changes to handling of positional arguments which were backported
to 4.1.10 [1]. Those changes were made by @evilstreak and @tekin to fix
a bug they encountered in Whitehall [2] and the comment there explains
more of the context.

In this case we were passing the site object to the helper method as well as
site_id appearing in params, and the latter was being interpreted as the
format. To work around this we're no longer passing the site object, only
the params.

[1]: https://github.com/rails/rails/pull/18803
[2]: https://github.com/alphagov/whitehall/commit/5d46001d7846a85180327797f95c72abbeac3b5e
