Module: Datagrid::Columns::ClassMethods

Defined in:
lib/datagrid/columns.rb

Overview

self.included

Instance Method Summary collapse

Instance Method Details

#column(name, options = {}, &block) ⇒ Object

Defines new datagrid column

Arguments:

* <tt>name</tt> - column name
* <tt>options</tt> - hash of options
* <tt>block</tt> - proc to calculate a column value

Available options:

* <tt>:html</tt> - determines if current column should be present in html table and how is it formatted
* <tt>:order</tt> - determines if this column could be sortable and how
* <tt>:order_desc</tt> - determines a descending order for given column (only in case when <tt>:order</tt> can not be easily inverted
* <tt>:url</tt> - a proc with one argument, pass this option to easily convert the value into an URL
* <tt>:before</tt> - determines the position of this column, by adding it before the column passed here
* <tt>:after</tt> - determines the position of this column, by adding it after the column passed here

See: github.com/bogdan/datagrid/wiki/Columns for examples



57
58
59
60
61
62
63
64
65
# File 'lib/datagrid/columns.rb', line 57

def column(name, options = {}, &block)
  check_scope_defined!("Scope should be defined before columns")
  block ||= lambda do |model|
    model.send(name)
  end
  position = Datagrid::Utils.extract_position_from_options(columns_array, options)
  column = Datagrid::Columns::Column.new(self, name, options, &block)
  columns_array.insert(position, column)
end

#column_by_name(name) ⇒ Object

Returns column definition with given name



68
69
70
71
72
# File 'lib/datagrid/columns.rb', line 68

def column_by_name(name)
  self.columns.find do |col|
    col.name.to_sym == name.to_sym
  end
end

#column_namesObject

Returns an array of all defined column names



75
76
77
# File 'lib/datagrid/columns.rb', line 75

def column_names
  columns.map(&:name)
end

#columns(*args) ⇒ Object

Returns a list of columns defined. All column definistion are returned by default You can limit the output with only columns you need like:

grid.columns(:id, :name)

Supported options:

  • :data - if true returns only non-html columns. Default: false.



30
31
32
33
34
35
36
37
# File 'lib/datagrid/columns.rb', line 30

def columns(*args)
  options = args.extract_options!
  args.compact!
  args.map!(&:to_sym)
  columns_array.select do |column|
    (!options[:data] || column.data?) && (!options[:html] || column.html?)&& (args.empty? || args.include?(column.name))
  end
end

#format(value, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/datagrid/columns.rb', line 83

def format(value, &block)
  if block_given?
    respond_to do |f|
      f.data { value }
      f.html do
        instance_exec(value, &block)
      end
    end
  else
    # Ruby Object#format exists. 
    # We don't want to change the behaviour and overwrite it.
    super
  end
end

#inherited(child_class) ⇒ Object

:nodoc:



98
99
100
101
# File 'lib/datagrid/columns.rb', line 98

def inherited(child_class) #:nodoc:
  super(child_class)
  child_class.columns_array = self.columns_array.clone
end

#respond_to(&block) ⇒ Object

:nodoc:



79
80
81
# File 'lib/datagrid/columns.rb', line 79

def respond_to(&block) #:nodoc:
  Datagrid::Columns::Column::ResponseFormat.new(&block)
end