class MagicGrid::Column

Attributes

order[RW]

Public Class Methods

columns_for_collection(collection, columns, searchables) click to toggle source
# 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) click to toggle source
# 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
new(collection, c, i) click to toggle source
# File lib/magic_grid/column.rb, line 71
def initialize(collection, c, i)
  @collection = collection
  @col =  case c
          when Symbol
            {:col => c}
          when String
            {:label => c}
          else
            c
          end
  @col[:id] = i
  if @collection.column_names.include?(@col[:col])
    @col[:sql] ||= @collection.quote_column_name(name)
  end
  @col[:label] ||= @col[:col].to_s.titleize
end
searchable_column(searchable, columns, collection) click to toggle source
# 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

Public Instance Methods

custom_sql() click to toggle source
# File lib/magic_grid/column.rb, line 49
def custom_sql
  @col[:sql]
end
html_classes() click to toggle source
# File lib/magic_grid/column.rb, line 61
def html_classes
  @html_classes ||= (Array(@col[:class]) << order.css_class)
  @html_classes.join(' ')
end
id() click to toggle source
# File lib/magic_grid/column.rb, line 53
def id
  @col[:id]
end
label() click to toggle source
# File lib/magic_grid/column.rb, line 41
def label
  @col[:label]
end
name() click to toggle source
# File lib/magic_grid/column.rb, line 57
def name
  @col[:col]
end
reader() click to toggle source
# File lib/magic_grid/column.rb, line 66
def reader
  @col[:to_s] || @col[:col]
end
sortable?() click to toggle source
# File lib/magic_grid/column.rb, line 45
def sortable?
  not custom_sql.blank?
end