Project

raymond

0.0
No commit activity in last 3 years
No release in over 3 years
Enhance your views with sortable tables
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 1.2.9
>= 0
 Project Readme

raymond¶ ↑

Gives your view tables nice sorting ability with ease.

Usage¶ ↑

Prepare your model: specify, which columns may get sorted.

class VeryImportantData < ActiveRecord::Base

  include Raymond::Model

  # sort by these db table columns
  allow_sort_by :name, :created_at

  # sort by this method, that is _not_ a db table column
  allow_sort_by :my_custom_method, :db => false

  # do a very customized sort
  allow_sort_by :my_sort_key, :method => Proc.new{|obj| obj.my_method(:my_param)}

  ...

Not very much to do in the Controller:

class VeryImportantDataController < ApplicationController

  def index
    # create the sorting object and pass the sorting parameters
    @sorting = Raymond::Control.new(self, params[:sort])

    # fetch the ordered result from the db; do method based sorting if necessary.
    @very_important_data = @sorting.result

    respond_to do |format|
      format.html # index.html.erb
    end
  end

  ...

Use a helper method in your view to show the table headers:

<tr>
  <th><%= sort_header :name, 'Name' %></th>
  <th><%= sort_header :my_custom_method, 'Column #2' %></th>
  <th><%= sort_header :my_sort_key, 'Column #3' %></th>
  <th><%= sort_header :created_at, 'Since' %></th>
</tr>

And the helper method (not included in Gem):

def sort_header(key, label)
  current = @sorting.current_attr?(key)
  symbol = current ? (@sorting.dir == :up ? '▲' : '▼') : ''
  link_to "#{symbol}#{label}", "?sort=#{key}-#{current ? @sorting.inverse_dir : Raymond::DEFAULT_SORTING_DIR.to_s}"
end

More Customization¶ ↑

@sorting = Raymond::Control.new(self, params[:sort])

In the example above, the class to query it determined by the name of the controller applying Ruby on Rails conventions (PeopleController will use the class Person). Passing the option :class will allow you to use any other class:

@sorting = Raymond::Control.new(self, params[:sort], :class => PlzUseThisClass)

Often, you may want more control of the underlying SQL statement. Thanks to Arel, anything is possible:

@comments = @sorting.result do |active_relation|
  active_relation.where(:deleted => false).limit(20)
end

What’s next?¶ ↑

There’s more to come, this is only version 0.0.1.

Copyright © 2010 Christoph Petschnig. See LICENSE for details.