Module: Datagrid::Columns::ClassMethods

Defined in:
lib/datagrid/columns.rb

Overview

self.included

Instance Method Summary collapse

Instance Method Details

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

Defines new datagrid column

Arguments:

  • name - column name

  • query - a string representing the query to select this column (supports only ActiveRecord)

  • options - hash of options

  • block - proc to calculate a column value

Available options:

  • :html - determines if current column should be present in html table and how is it formatted

  • :order - determines if this column could be sortable and how. The value of order is explicitly passed to ORM ordering method. Ex: "created_at, id" for ActiveRecord, [:created_at, :id] for Mongoid

  • :order_desc - determines a descending order for given column (only in case when :order can not be easily reversed by ORM)

  • :order_by_value - 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

  • :mandatory - if true, column will never be hidden with #column_names selection

  • :url - a proc with one argument, pass this option to easily convert the value into an URL

  • :before - determines the position of this column, by adding it before the column passed here

  • :after - determines the position of this column, by adding it after the column passed here

  • :if - the column is shown if the reult of calling this argument is true

  • :unless - the column is shown unless the reult of calling this argument is true

  • :preload - spefies which associations of the scope should be preloaded for this column

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



124
125
126
# File 'lib/datagrid/columns.rb', line 124

def column(name, options_or_query = {}, options = {}, &block)
  define_column(columns_array, name, options_or_query, options, &block)
end

#column_by_name(name) ⇒ Object

Returns column definition with given name



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

def column_by_name(name)
  find_column_by_name(columns_array, name)
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:

GridClass.columns(:id, :name)

Supported options:

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



90
91
92
# File 'lib/datagrid/columns.rb', line 90

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


171
172
173
174
175
176
177
178
179
180
# File 'lib/datagrid/columns.rb', line 171

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:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/datagrid/columns.rb', line 198

def define_column(columns, name, options_or_query = {}, options = {}, &block) #:nodoc:
  if options_or_query.is_a?(String)
    query = options_or_query
  else
    options = options_or_query
  end
  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, options)
  column = Datagrid::Columns::Column.new(
    self, name, query, default_column_options.merge(options), &block
  )
  columns.insert(position, column)
end

#filter_columns(columns, *args) ⇒ Object

:nodoc:



187
188
189
190
191
192
193
194
195
196
# File 'lib/datagrid/columns.rb', line 187

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

#find_column_by_name(columns, name) ⇒ Object

:nodoc:



215
216
217
218
219
220
# File 'lib/datagrid/columns.rb', line 215

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|
    (:strong, value)
  end
end


150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/datagrid/columns.rb', line 150

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:



182
183
184
185
# File 'lib/datagrid/columns.rb', line 182

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