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. 
  The value of order is explicitly passed to ORM ordering method. 
  Ex: <tt>"created_at, id"</tt> for ActiveRecord, <tt>[:created_at, :id]</tt> for Mongoid
* <tt>:order_desc</tt> - determines a descending order for given column 
  (only in case when <tt>:order</tt> can not be easily reversed by ORM)
* <tt>:order_by_value</tt> - used in case it is easier to perform ordering at ruby level not on database level. 
  Warning: using ruby to order large datasets is very unrecommended. 
  If set to true - datagrid will use column value to order by this column
  If block is given - datagrid will use value returned from block
* <tt>:mandatory</tt> - if true, column will never be hidden with #column_names selection
* <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



116
117
118
119
120
121
122
123
124
# File 'lib/datagrid/columns.rb', line 116

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, default_column_options.merge(options), &block)
  columns_array.insert(position, column)
end

#column_by_name(name) ⇒ Object

Returns column definition with given name



127
128
129
130
131
# File 'lib/datagrid/columns.rb', line 127

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



134
135
136
# File 'lib/datagrid/columns.rb', line 134

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.



81
82
83
84
85
86
87
88
# File 'lib/datagrid/columns.rb', line 81

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?) && (column.mandatory? || args.empty? || args.include?(column.name))
  end
end

#format(value, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/datagrid/columns.rb', line 142

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:



157
158
159
160
# File 'lib/datagrid/columns.rb', line 157

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

#respond_to(&block) ⇒ Object

:nodoc:



138
139
140
# File 'lib/datagrid/columns.rb', line 138

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