Option Switch Helper for Rails

There are always some options for your page. Perhaps the number of items to display (articles, picture, projects, …). Or the sort key for those. Preferably you set some sane defaults. But you’d like to enable your visitors to change those?

This is what I did first so people could change the sort order:

lengthy RHTML code for option switches

1
2
3
4
sort by <%= link_to_if(session[:tag_sort] == 'alpha',
'alpha', '?settagsort=alpha') %> |
<%= link_to_if(session[:tag_sort] == 'freq',
'freq', '?settagsort=freq') %>.

That way a link to the current sort order will be suppresed and you get the visual impresion of flipping a switch.

Remember DRY? Ain’t looking much like it as you keep adding options…

Application helpers to the rescue!

application helper for option switches

1
2
3
4
5
6
7
8
9
10
11
def link_to_switch(parameter, name,
html_options = {},
*parameters_for_method_reference,
&block)
html_param = 'set' + parameter.to_s.delete('_')
link_to_unless(session[parameter] == name, name,
'?'+html_param+'='+name,
html_options,
*parameters_for_method_reference,
&block)
end

Put this function in your application_helper.rb (its not that lengthy if you ignore the last three arguments, they just add full compatibility to the link_to’s arguments).

Then call it this way:

nice RHTML for option switches

1
2
3
4
5
6
sort by <%= link_to_switch(:tag_sort, 'alpha') %> |
<%= link_to_switch(:tag_sort, 'freq') %>.
hide minimum: <%= link_to_switch(:min_posts, '1') %>,
<%= link_to_switch(:min_posts, '2') %>,
<%= link_to_switch(:min_posts, '5') %>.

And in your controller you would be doing something like this

reading the options in your controller

1
2
3
session[:min_posts] = params[:setminposts] unless
params[:setminposts].nil?
session[:min_posts] ||= 1

Happy optionizing…!

MP3 Scan + Repair

The audio repair tool to check and fix all your MP3 files.

Check and repair your MP3 audio files.

Some of your audio players might be picky or play choppy sound. Scan and fix all your MP3 files for best performance! And it's free, too!

MP3 Scan + Repair

Mouse Acceleration for OS X

Set up extended speed parameters for your mouse.

Extended mouse settings

The Mouse Acceleration PrefPane is a GUI and startup item to Richard Bentley's MouseFix.

You can set up extended speed parameters and acceleration curves for your mouse.

  • Increase mouse pointer precision
  • Speed up your mouse movement
  • Set mouse acceleration parameters
  • Full control with just two simple knobs

Mouse Acceleration for OS X

About the Author

Hi there! I'm Christian Zuckschwerdt a freelance software designer from Germany. On this blog I'd like to share some free and open source software for GNU/Linux and Apple Mac OS X.
There are also some themes and tricks for the Drupal CMS. And then the occasional hardware or gadget hack. It's all free and mostly licensed under the GPL or CC.
Write a comment or send me a mail. I love to hear from you!

About FOSS

Do you know how much free and open source software is involved in creating and delivering some basic web pages?

This triq.net blog is rendered by hexo themed with nunjucks compiled by nodejs served by apache httpd secured with openssl stored on MariaDB running on gentoo linux, edited with vim deployed by openssh, graphics done with inkscape and gimp.

triq

zany@triq.net
triq.net blog
© 1999 – 2017 Christian Zuckschwerdt.

Links