Module: GoodSort::ViewHelpers

Defined in:
lib/good_sort/view_helpers.rb

Constant Summary collapse

@@field_tag =
'__FIELD__'

Instance Method Summary collapse

Instance Method Details

#sort_field_tagObject



4
# File 'lib/good_sort/view_helpers.rb', line 4

def sort_field_tag; @@field_tag; end

#sort_header(f, text, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/good_sort/view_helpers.rb', line 50

def sort_header(f, text, options )
  params[:sort] ||= {}

  tag_options = options[:header].dup
  if params[:sort][:field] == f.to_s
    tag_options[:class] ||= ''
    (tag_options[:class] += params[:sort][:down].blank? ? ' up' : ' down' ).strip!
  end
  ( options[:tag], sort_link(f, text, options), tag_options )
end

#sort_header_title(field, options) ⇒ Object



70
71
72
# File 'lib/good_sort/view_helpers.rb', line 70

def sort_header_title( field, options )
  options[:html][:title].gsub(sort_field_tag, field)
end

#sort_headers_for(m, h, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/good_sort/view_helpers.rb', line 5

def sort_headers_for( m, h, options = {} )

  id = m.to_s.singularize
  m = m.to_s.classify
  c = m.constantize

  options.symbolize_keys!
  options[:spinner] ||= :spinner
  options[:tag] ||= :th

  options[:header] ||= {}
  options[:header].symbolize_keys!

  options[:remote] ||= {}
  options[:remote].symbolize_keys!
  options[:remote][:update] ||= m.tableize
  options[:remote][:before] = "$('#{options[:spinner]}').show()" unless options[:remote].has_key? :before
  options[:remote][:complete]  = "$('#{options[:spinner]}').hide()" unless options[:remote].has_key? :complete
  options[:remote][:method] ||= :get

  # save these for pagination calls later in the request
  @remote_options = options[:remote].dup

  options[:html] ||= {}
  options[:html].symbolize_keys!
  options[:html][:title] ||= "Sort by #{sort_field_tag}"

  sf = c.sort_fields
  logger.warn "GoodSort: #{m} has not had any sort_on fields set" if sf.nil?

  h.each do |f|
    options[:header][:id] = "#{id}_header_#{f}"

    text = yield(f) if block_given?
    text ||= f.to_s.titleize

    unless sf[f.to_sym]
      concat (options[:tag], text, options[:header])
      next
    end

    concat sort_header( f, text, options )
  end
end


61
62
63
64
65
66
67
68
# File 'lib/good_sort/view_helpers.rb', line 61

def sort_link(f, text, options)
  s = { :field => f, :down => params[:sort][:field] == f.to_s && params[:sort][:down].blank? ? true : nil }
  ps = params.merge( :sort => s, :page => nil )

  options[:remote][:url] = options[:html][:href] = url_for( :params => ps )
  title = sort_header_title( text, options)
  link_to_remote(text, options[:remote], options[:html].merge( :title => title))
end