Module: Datagrid::Columns::ClassMethods
- Defined in:
- lib/datagrid/columns.rb
Overview
self.included
Instance Method Summary collapse
-
#column(name, options_or_query = {}, options = {}, &block) ⇒ Object
Defines new datagrid column.
-
#column_by_name(name) ⇒ Object
Returns column definition with given name.
-
#column_names ⇒ Object
Returns an array of all defined column names.
-
#columns(*args) ⇒ Object
Returns a list of columns defined.
- #format(value, &block) ⇒ Object
-
#inherited(child_class) ⇒ Object
:nodoc:.
-
#respond_to(&block) ⇒ Object
:nodoc:.
Instance Method Details
#column(name, options_or_query = {}, options = {}, &block) ⇒ Object
Defines new datagrid column
Arguments:
* <tt>name</tt> - column name
* <tt>query</tt> - a string representing the query to select this column (supports only ActiveRecord)
* <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
* <tt>:if</tt> - the column is shown if the reult of calling this argument is true
* <tt>:unless</tt> - the column is shown unless the reult of calling this argument is true
See: github.com/bogdan/datagrid/wiki/Columns for examples
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/datagrid/columns.rb', line 118 def column(name, = {}, = {}, &block) if .is_a?(String) query = else = end check_scope_defined!("Scope should be defined before columns") block ||= lambda do |model| model.send(name) end position = Datagrid::Utils.(columns_array, ) column = Datagrid::Columns::Column.new(self, name, query, .merge(), &block) columns_array.insert(position, column) end |
#column_by_name(name) ⇒ Object
Returns column definition with given name
134 135 136 137 138 |
# File 'lib/datagrid/columns.rb', line 134 def column_by_name(name) self.columns.find do |col| col.name.to_sym == name.to_sym end end |
#column_names ⇒ Object
Returns an array of all defined column names
141 142 143 |
# File 'lib/datagrid/columns.rb', line 141 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.
80 81 82 83 84 85 86 87 |
# File 'lib/datagrid/columns.rb', line 80 def columns(*args) = args. args.compact! args.map!(&:to_sym) columns_array.select do |column| (![:data] || column.data?) && (![:html] || column.html?) && (column.mandatory? || args.empty? || args.include?(column.name)) end end |
#format(value, &block) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/datagrid/columns.rb', line 149 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:
164 165 166 167 |
# File 'lib/datagrid/columns.rb', line 164 def inherited(child_class) #:nodoc: super(child_class) child_class.columns_array = self.columns_array.clone end |
#respond_to(&block) ⇒ Object
:nodoc:
145 146 147 |
# File 'lib/datagrid/columns.rb', line 145 def respond_to(&block) #:nodoc: Datagrid::Columns::Column::ResponseFormat.new(&block) end |