Skip to content

Instantly share code, notes, and snippets.

@mrsimo
Created September 28, 2012 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrsimo/3800518 to your computer and use it in GitHub Desktop.
Save mrsimo/3800518 to your computer and use it in GitHub Desktop.
Ugly helper
def filter_dropdown(select_options, options)
current_text = select_options.find { |text, filter| filter == options[:current] }.try(:first) || select_options.first.first
image = '<img height="6" width="8" src="/img/v/transparent_r1.gif" class="icn icn-ext-ctr-arrow-down">'
link = content_tag :strong do
link_to("#{current_text} #{image}".html_safe, "#", :"data-simplelayer" => "##{options[:key]}")
end
uls = content_tag :ul, :id => options[:key], :class => "dropdown", :style => "display:none" do
select_options.map do |text,key|
klass = (options[:current] == key ? 'selected' : nil)
content_tag :li, link_to(text, params.merge(options[:key] => key), :class => klass)
end.join.html_safe
end
<<-EOS.html_safe
#{options[:label]}
#{link}
#{uls}
EOS
end
@mediafinger
Copy link

Yes, that looks cluttered ;-)

Separating your 20 line method into shorter helper methods makes the code easier to read already (imho).
That might be just a start, but maybe you can also come up with more generic helper methods that can be used on more pages.

def filter_dropdown(select_options, options)
  current_text = select_options.find { |text, filter| filter == options[:current] }.try(:first) || select_options.first.first

  <<-EOS.html_safe
    #{options[:label]}
    #{link_xy(current_text, image_xy, key)}
    #{uls_xy(select_options, options)}
  EOS
end

def image_xy
  '<img height="6" width="8" src="/img/v/transparent_r1.gif" class="icn icn-ext-ctr-arrow-down">'
end

def link_xy(text, image, key)
  content_tag :strong do
    link_to("#{text} #{image}".html_safe, "#", :"data-simplelayer" => "##{key}")
  end
end

def uls_xy(select_options, options)
  content_tag :ul, :id => options[:key], :class => "dropdown", :style => "display:none" do
    select_options.map do |text, key|
      klass = (options[:current] == key ? 'selected' : nil)
      content_tag :li, link_to(text, params.merge(options[:key] => key), :class => klass)
    end.join.html_safe
  end
end

@mediafinger
Copy link

Fix: in line 6 it should not be key but options[:key]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment