Permalink
Browse files

Initial checkin

  • Loading branch information...
0 parents commit b4fb1e24414d7a45d5a1b2e4a49a2aac16ca9966 Wynn Netherland committed Apr 21, 2008
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64 README
@@ -0,0 +1,64 @@
+=UnobtrusiveSortHelper
+
+
+This is a plugin version of Stuart Rackham's most excellent SortHelper helper. The link_to_remote ajax calls have been removed in favor of unobtrusive javascript using lowpro.js.
+
+=== Features
+
+- Consecutive clicks toggle the column's sort order.
+- Sort state is maintained by a session hash entry.
+- Icon image identifies sort column and state.
+- Typically used in conjunction with will_paginate plugin
+
+=== Usage
+
+====Controller:
+
+ def list
+ sort_init 'last_name'
+ sort_update
+ @items = Contact.find_all nil, sort_clause
+ end
+
+====Controller (using will_paginate)
+
+ def list
+ sort_init 'last_name'
+ sort_update
+
+ options = {:page => params[:page], :include => :addresses, :order => sort_clause
+
+ @contacts = Contact.paginate(options)
+ end
+
+====Layout (app/views/layouts/application.html.erb):
+
+ <%= stylesheet_link_tag "ujs_sort_helper"%>
+styles for nifty sort arrow images
+
+ <%= javascript_include_tag :defaults%>
+as long as prototype.js is included before lowpro and ujs_sort_helper
+
+ <%= javascript_include_tag "lowpro"%>
+you SHOULD already have this ;-)
+
+ <%= javascript_include_tag "usj_sort_helper"%>
+this is where ujs comes in
+====View (table header in index.rhtml):
+
+ <thead>
+ <tr>
+ <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
+ <%= sort_header_tag('last_name', :caption => 'Name') %>
+ <%= sort_header_tag('phone') %>
+ <%= sort_header_tag('address', :width => 200) %>
+ </tr>
+ </thead>
+
+- The ascending and descending sort icon images are sort_asc.png and sort_desc.png and reside in the application's images directory.
+- Introduces instance variables: @sort_name, @sort_default.
+- Introduces params :sort_key and :sort_order.
+
+
+
+Copyright (c) 2005 Stuart Rackham, (c) 2008 Wynn Netherland released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the unobtrusive_sort_helper plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the unobtrusive_sort_helper plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'UnobtrusiveSortHelper'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
11 init.rb
@@ -0,0 +1,11 @@
+require 'unobtrusive_sort_helper'
+
+ActionView::Base.send(:include, UnobtrusiveSortHelper)
+ActionController::Base.send(:include, UnobtrusiveSortHelper)
+
+# install files
+['/public/javascripts', '/public/stylesheets', '/public/images'].each{|dir|
+ source = File.join(directory,dir)
+ dest = RAILS_ROOT + dir
+ FileUtils.cp_r(Dir.glob(source+'/*.*'), dest)
+} unless File.exists?(RAILS_ROOT + '/public/javascripts/ujs_sort_helper.js')
@@ -0,0 +1,157 @@
+# Helpers to sort tables using clickable column headers.
+#
+# Author: Stuart Rackham <srackham@methods.co.nz>, March 2005.
+# Modified by: Wynn Netherland <wynn@squeejee.com>, February 2008 to use UJS
+# License: This source code is released under the MIT license.
+#
+# - Consecutive clicks toggle the column's sort order.
+# - Sort state is maintained by a session hash entry.
+# - Icon image identifies sort column and state.
+# - Typically used in conjunction with the Pagination module.
+#
+# Example code snippets:
+#
+# Controller:
+#
+# helper :sort
+# include SortHelper
+#
+# def list
+# sort_init 'last_name'
+# sort_update
+# @items = Contact.find_all nil, sort_clause
+# end
+#
+# Controller (using Pagination module):
+#
+# helper :sort
+# include SortHelper
+#
+# def list
+# sort_init 'last_name'
+# sort_update
+# @contact_pages, @items = paginate :contacts,
+# :order_by => sort_clause,
+# :per_page => 10
+# end
+#
+# View (table header in list.rhtml):
+#
+# <thead>
+# <tr>
+# <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
+# <%= sort_header_tag('last_name', :caption => 'Name') %>
+# <%= sort_header_tag('phone') %>
+# <%= sort_header_tag('address', :width => 200) %>
+# </tr>
+# </thead>
+#
+# - The ascending and descending sort icon images are sort_asc.png and
+# sort_desc.png and reside in the application's images directory.
+# - Introduces instance variables: @sort_name, @sort_default.
+# - Introduces params :sort_key and :sort_order.
+#
+module UnobtrusiveSortHelper
+
+ # Initializes the default sort column (default_key) and sort order
+ # (default_order).
+ #
+ # - default_key is a column attribute name.
+ # - default_order is 'asc' or 'desc'.
+ # - name is the name of the session hash entry that stores the sort state,
+ # defaults to '<controller_name>_sort'.
+ #
+ def sort_init(default_key, default_order='asc', name=nil)
+ @sort_name = name || params[:controller] + params[:action] + '_sort'
+ @sort_default = {:key => default_key, :order => default_order}
+ end
+
+ # Updates the sort state. Call this in the controller prior to calling
+ # sort_clause.
+ #
+ def sort_update()
+ if params[:sort_key]
+ sort = {:key => params[:sort_key], :order => params[:sort_order]}
+ elsif session[@sort_name]
+ sort = session[@sort_name] # Previous sort.
+ else
+ sort = @sort_default
+ end
+ session[@sort_name] = sort
+ end
+
+ # Returns an SQL sort clause corresponding to the current sort state.
+ # Use this to sort the controller's table items collection.
+ #
+ def sort_clause()
+ session[@sort_name][:key] + ' ' + session[@sort_name][:order]
+ end
+
+ # Returns a link which sorts by the named column.
+ #
+ # - column is the name of an attribute in the sorted record collection.
+ # - The optional caption explicitly specifies the displayed link text.
+ # - A sort icon image is positioned to the right of the sort link.
+ #
+ def sort_link(column, caption=nil, initial_order="asc")
+
+ key, order = session[@sort_name][:key], session[@sort_name][:order]
+ if key == column
+ if order.downcase == 'asc'
+ icon = 'sort_asc.png'
+ order = 'desc'
+ else
+ icon = 'sort_desc.png'
+ order = 'asc'
+ end
+ else
+ icon = nil
+ order = initial_order # JPM - Updated original helper to allow for a dynamic initial sort order
+ end
+ caption = titleize(Inflector::humanize(column)) unless caption
+
+ url = { :sort_key => column, :sort_order => order, :filter => params[:filter]}
+ url.merge!({:q => params[:q]}) unless params[:q].nil?
+
+ link_to(caption, url, :class => "sort_link #{order if key == column}")
+ end
+
+ # Returns a table header <th> tag with a sort link for the named column
+ # attribute.
+ #
+ # Options:
+ # :caption The displayed link name (defaults to titleized column name).
+ # :title The tag's 'title' attribute (defaults to 'Sort by :caption').
+ #
+ # Other options hash entries generate additional table header tag attributes.
+ #
+ # Example:
+ #
+ # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %>
+ #
+ # Renders:
+ #
+ # <th title="Sort by contact ID" width="40">
+ # <a href="/contact/list?sort_order=desc&amp;sort_key=id">Id</a>
+ # </th>
+ #
+ def sort_header_tag(column, options = {})
+ options[:initial_order].nil? ? initial_order = "asc" : initial_order = options[:initial_order]
+ caption = options.delete(:caption) || titleize(Inflector::humanize(column))
+ #options[:title] = caption unless options[:title]
+ content_tag('th', sort_link(column, caption, initial_order), options.except(:initial_order))
+ end
+
+ private
+
+ # Return n non-breaking spaces.
+ def nbsp(n)
+ '&nbsp;' * n
+ end
+
+ # Return capitalized title.
+ def titleize(title)
+ title.split.map {|w| w.capitalize }.join(' ')
+ end
+
+end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit b4fb1e2

Please sign in to comment.