Class: MagicGrid::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_grid/column.rb

Direct Known Subclasses

FilterOnlyColumn

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#orderObject

Returns the value of attribute order.



5
6
7
# File 'lib/magic_grid/column.rb', line 5

def order
  @order
end

Class Method Details

.columns_for_collection(collection, columns, searchables) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/magic_grid/column.rb', line 7

def self.columns_for_collection(collection, columns, searchables)
  columns.each_with_index.map { |c, i|
    MagicGrid::Column.new(collection, c, i)
  }.tap do |cols|
    if searchables == false
      searchables = []
    else
      searchables = Array(searchables).map { |s|
        searchable_column(s, cols, collection)
      }
    end
    collection.searchable_columns = searchables.compact
  end
end

.hash_string(column_or_columns) ⇒ Object



37
38
39
# File 'lib/magic_grid/column.rb', line 37

def self.hash_string(column_or_columns)
  Array(column_or_columns).map(&:label).join.hash.abs.to_s(36)
end

.searchable_column(searchable, columns, collection) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/magic_grid/column.rb', line 22

def self.searchable_column(searchable, columns, collection)
  case searchable
  when Symbol
    columns.find {|col| col.name == searchable} || FilterOnlyColumn.new(searchable, collection)
  when Integer
    columns[searchable]
  when String
    FilterOnlyColumn.new(searchable)
  when true
    nil
  else
    raise "Searchable must be identifiable: #{searchable}"
  end
end

Instance Method Details

#custom_sqlObject



49
50
51
# File 'lib/magic_grid/column.rb', line 49

def custom_sql
  @col[:sql]
end

#html_classesObject



61
62
63
64
# File 'lib/magic_grid/column.rb', line 61

def html_classes
  @html_classes ||= (Array(@col[:class]) << order.css_class)
  @html_classes.join(' ')
end

#idObject



53
54
55
# File 'lib/magic_grid/column.rb', line 53

def id
  @col[:id]
end

#labelObject



41
42
43
# File 'lib/magic_grid/column.rb', line 41

def label
  @col[:label]
end

#nameObject



57
58
59
# File 'lib/magic_grid/column.rb', line 57

def name
  @col[:col]
end

#readerObject



66
67
68
# File 'lib/magic_grid/column.rb', line 66

def reader
  @col[:to_s] || @col[:col]
end

#sortable?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/magic_grid/column.rb', line 45

def sortable?
  not custom_sql.blank?
end