Module: Sorted::ActionView::Helper

Defined in:
lib/sorted/actionview/helper.rb

Instance Method Summary collapse

Instance Method Details

Creates a link tag of the given name and attribute creating a url using a set of options.

Examples

Basic usage

link_to_sorted "Email", :email
# => <a href="/profiles?sort=email_asc" class="desc">Email</a>

Or use a block

link_to_sorted :email do
  <strong>Sort by email</strong> -- <span></span>
end
# => <a href="/profiles?sort=email_asc" class="desc"><strong>Sort by email</strong> -- <span></span></a>


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sorted/actionview/helper.rb', line 24

def link_to_sorted(*args, &block)
  if block_given?
    order        = args[0]
    options      = args[1] || {}
    html_options = args[2] || {}
  else
    block        = proc { args[0].to_s }
    order        = args[1]
    options      = args[2] || {}
    html_options = args[3] || {}
  end

  sorter          = ::Sorted::ActionView::Builder.new(order, ((request.get? && !params.nil?) ? params.dup : {}))
  options[:class] = [options[:class], sorter.css].join(' ').strip
  link_to(url_for(sorter.params), options, html_options, &block)
end

#sortable_by(*columns) ⇒ Object

Convenience method for quickly spitting out a sorting menu.

Examples

Basic usage

sort_by :first_name, :last_name

To provide a string to use instead of a column name, pass an array composed of your label string and the column name (symbol):

sortable_by :author_name, :title, ["Date of Publication", :published_at]


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sorted/actionview/helper.rb', line 54

def sortable_by(*columns)
  links =  :span, 'Sort by: '
  columns.each do |c|
    if c.is_a? Array
      links += link_to_sorted(c[0], c[1].to_sym)
    else
      links += link_to_sorted(c.to_s.titleize, c.to_sym)
    end
  end
   :div, links, class: 'sortable'
end