Class: SortOrder::Columns

Inherits:
Array
  • Object
show all
Defined in:
lib/sort_order/columns.rb

Instance Method Summary collapse

Constructor Details

#initialize(*defaults) ⇒ Columns

Returns a new instance of Columns.



4
5
6
7
# File 'lib/sort_order/columns.rb', line 4

def initialize(*defaults)
  super defaults.map { |d| Column.parse(d) }
  raise SortOrder::NoDefaultColumnsError, "Please supply some default columns e.g. 'name', 'email desc', etc." if empty?
end

Instance Method Details

#<<(token) ⇒ Object



37
38
39
40
# File 'lib/sort_order/columns.rb', line 37

def <<(token)
  merge token
  return self
end

#column(token) ⇒ Object



29
30
31
# File 'lib/sort_order/columns.rb', line 29

def column(token)
  SortOrder::Column.parse(token)
end

#currentObject



17
18
19
# File 'lib/sort_order/columns.rb', line 17

def current
  @current ||= first
end

#current=(token = nil) ⇒ Object



21
22
23
# File 'lib/sort_order/columns.rb', line 21

def current=(token=nil)
  @current = merge(token) if token.present?
end

#current?(other) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/sort_order/columns.rb', line 25

def current?(other)
  current == other
end

#fieldsObject



33
34
35
# File 'lib/sort_order/columns.rb', line 33

def fields
  map(&:field)
end

#find(name) ⇒ Object



50
51
52
# File 'lib/sort_order/columns.rb', line 50

def find(name)
  super() {|c| c.name == name }
end

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/sort_order/columns.rb', line 54

def include?(name)
  find(name).present?
end

#merge(token) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/sort_order/columns.rb', line 42

def merge(token) 
  new_col = column(token)
  if old_col = find(new_col.name)
    old_col.direction = new_col.direction
    return old_col
  end
end

#to_params(options = {}) ⇒ Object



13
14
15
# File 'lib/sort_order/columns.rb', line 13

def to_params(options={})
  { :order => options.delete(:current) ? current.to_param(options) : map {|c| c.to_param(options) } }
end

#to_sql(options = {}) ⇒ Object



9
10
11
# File 'lib/sort_order/columns.rb', line 9

def to_sql(options={})
  options.delete(:current) ? current.to_sql(options) : map {|c| c.to_sql(options) }.join(', ')
end