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.
-
#decorate(model = nil, &block) ⇒ Object
Defines a model decorator that will be used to define a column value.
-
#define_column(columns, name, options_or_query = {}, options = {}, &block) ⇒ Object
:nodoc:.
-
#dynamic(&block) ⇒ Object
Allows dynamic columns definition, that could not be defined at class level.
-
#filter_columns(columns, *args) ⇒ Object
:nodoc:.
-
#find_column_by_name(columns, name) ⇒ Object
:nodoc:.
-
#format(value, &block) ⇒ Object
Formats column value for HTML.
-
#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
* <tt>:preload</tt> - spefies which associations of the scope should be preloaded for this column
See: github.com/bogdan/datagrid/wiki/Columns for examples
125 126 127 |
# File 'lib/datagrid/columns.rb', line 125 def column(name, = {}, = {}, &block) define_column(columns_array, name, , , &block) end |
#column_by_name(name) ⇒ Object
Returns column definition with given name
130 131 132 |
# File 'lib/datagrid/columns.rb', line 130 def column_by_name(name) find_column_by_name(columns_array, name) end |
#column_names ⇒ Object
Returns an array of all defined column names
135 136 137 |
# File 'lib/datagrid/columns.rb', line 135 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:
GridClass.columns(:id, :name)
Supported options:
-
:data - if true returns only non-html columns. Default: false.
91 92 93 |
# File 'lib/datagrid/columns.rb', line 91 def columns(*args) filter_columns(columns_array, *args) end |
#decorate(model = nil, &block) ⇒ Object
Defines a model decorator that will be used to define a column value. All column blocks will be given a decorated version of the model.
decorate { |user| UserPresenter.new(user) }
decorate { UserPresenter } # a shortcut
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/datagrid/columns.rb', line 208 def decorate(model = nil, &block) if !model && !block raise ArgumentError, "decorate needs either a block to define decoration or a model to decorate" end return self.decorator = block unless model return model unless decorator presenter = ::Datagrid::Utils.apply_args(model, &decorator) presenter = presenter.is_a?(Class) ? presenter.new(model) : presenter block_given? ? yield(presenter) : presenter end |
#define_column(columns, name, options_or_query = {}, options = {}, &block) ⇒ Object
:nodoc:
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/datagrid/columns.rb', line 235 def define_column(columns, name, = {}, = {}, &block) #:nodoc: 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, ) column = Datagrid::Columns::Column.new( self, name, query, .merge(), &block ) columns.insert(position, column) end |
#dynamic(&block) ⇒ Object
Allows dynamic columns definition, that could not be defined at class level
class MerchantsGrid
scope { Merchant }
column(:name)
dynamic do
PurchaseCategory.all.each do |category|
column(:"#{category.name.underscore}_sales") do |merchant|
merchant.purchases.where(:category_id => category.id).count
end
end
end
end
grid = MerchantsGrid.new
grid.data # => [
# [ "Name", "Swimwear Sales", "Sportswear Sales", ... ]
# [ "Reebok", 2083382, 8382283, ... ]
# [ "Nike", 8372283, 18734783, ... ]
# ]
189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/datagrid/columns.rb', line 189 def dynamic(&block) previous_block = dynamic_block self.dynamic_block = if previous_block proc { instance_eval(&previous_block) instance_eval(&block) } else block end end |
#filter_columns(columns, *args) ⇒ Object
:nodoc:
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/datagrid/columns.rb', line 224 def filter_columns(columns, *args) #:nodoc: = args. args.compact! args.map!(&:to_sym) columns.select do |column| (![:data] || column.data?) && (![:html] || column.html?) && (column.mandatory? || args.empty? || args.include?(column.name)) end end |
#find_column_by_name(columns, name) ⇒ Object
:nodoc:
252 253 254 255 256 257 |
# File 'lib/datagrid/columns.rb', line 252 def find_column_by_name(columns,name) #:nodoc: return name if name.is_a?(Datagrid::Columns::Column) columns.find do |col| col.name.to_sym == name.to_sym end end |
#format(value, &block) ⇒ Object
Formats column value for HTML. Helps to distinguish formatting as plain data and HTML
column(:name) do |model|
format(model.name) do |value|
content_tag(:strong, value)
end
end
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/datagrid/columns.rb', line 151 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:
219 220 221 222 |
# File 'lib/datagrid/columns.rb', line 219 def inherited(child_class) #:nodoc: super(child_class) child_class.columns_array = self.columns_array.clone end |
#respond_to(&block) ⇒ Object
:nodoc:
139 140 141 |
# File 'lib/datagrid/columns.rb', line 139 def respond_to(&block) #:nodoc: Datagrid::Columns::Column::ResponseFormat.new(&block) end |