class TheGrid::Builder::Context

Attributes

columns[R]
scope[R]

Public Class Methods

new(options = {}, &dsl) click to toggle source
# File lib/the_grid/builder/context.rb, line 5
def initialize(options = {}, &dsl)
  @scope    = options.delete(:scope)
  @columns  = build_columns_with(options.delete(:id))
  @options  = options.merge(:features => [])

  self.instance_eval(&dsl)
end

Public Instance Methods

assemble(records) click to toggle source
# File lib/the_grid/builder/context.rb, line 57
def assemble(records)
  records.map{ |record| assemble_row_for(record) }
end
column(name, attributes = {}, &block) click to toggle source
# File lib/the_grid/builder/context.rb, line 13
def column(name, attributes = {}, &block)
  find_or_build_column(name).tap do |column|
    column.merge! attributes
    column[:as] = block if block_given?
  end
end
column_titles() click to toggle source
# File lib/the_grid/builder/context.rb, line 51
def column_titles
  self.options[:titles] ||= columns.flat_map do |name, options|
    options[:as].kind_of?(self.class) ? options[:as].column_titles : options.fetch(:title, name.to_s.titleize)
  end
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/the_grid/builder/context.rb, line 20
def method_missing(method_name, *args, &block)
  if @scope.respond_to?(method_name)
    @scope.send(method_name, *args, &block)
  elsif method_name.to_s.ends_with?("ble_columns")
    @options[:features] << method_name.to_s.chomp("_columns").to_sym
    mark_columns_with(@options[:features].last, args)
  else
    @options[method_name] = args.size == 1 ? args.first : args
  end
end
options() click to toggle source
# File lib/the_grid/builder/context.rb, line 36
def options
  @options.except(:features)
end
params() click to toggle source
# File lib/the_grid/builder/context.rb, line 40
def params
  self.options.merge self.featured_columns
end
scope_for(scope_name, attributes = {}, &block) click to toggle source
# File lib/the_grid/builder/context.rb, line 31
def scope_for(scope_name, attributes = {}, &block)
  name = attributes.delete(:as) || scope_name
  column name, attributes.merge(:as => Builder::Context.new(:scope => scope, &block), :scope_name => scope_name)
end
visible_columns() click to toggle source
# File lib/the_grid/builder/context.rb, line 44
def visible_columns
  columns.each_with_object({}) do |(name,options), vc|
    vc[name] = options.except(:as, :if, :unless) unless options[:hidden]
    vc[name] = {:columns => options[:as].visible_columns} if options[:as].respond_to?(:visible_columns)
  end
end

Protected Instance Methods

assemble_column_for(record, name, options) click to toggle source
# File lib/the_grid/builder/context.rb, line 92
def assemble_column_for(record, name, options)
  formatter = options[:as]

  if formatter.respond_to?(:call)
    formatter.call(record)
  elsif formatter.is_a? Symbol
    record.send(formatter)
  elsif formatter.respond_to?(:assemble)
    formatter.assemble(record.send(options[:scope_name])) if may_assemble?(record, options)
  else
    record.send(name)
  end
end
assemble_row_for(record) click to toggle source
# File lib/the_grid/builder/context.rb, line 85
def assemble_row_for(record)
  columns.each_with_object({}) do |column, row|
    name, options = column
    row[name] = assemble_column_for(record, name, options)
  end
end
build_columns_with(id_field) click to toggle source
# File lib/the_grid/builder/context.rb, line 63
def build_columns_with(id_field)
  columns = {}
  columns[id_field || :id] = {:hidden => true} unless id_field === false
  columns
end
find_or_build_column(name) click to toggle source
# File lib/the_grid/builder/context.rb, line 69
def find_or_build_column(name)
  @columns[name.to_sym] ||= {}
end
mark_columns_with(feature, column_names) click to toggle source
# File lib/the_grid/builder/context.rb, line 73
def mark_columns_with(feature, column_names)
  column_names.each do |name|
    find_or_build_column(name).store(feature, true)
  end
end
may_assemble?(record, options) click to toggle source
# File lib/the_grid/builder/context.rb, line 106
def may_assemble?(record, options)
  column_or_proc = options[:if] || options[:unless]

  result = true
  if column_or_proc.is_a? Symbol
    result = assemble_column_for(record, column_or_proc, columns[column_or_proc])
  elsif column_or_proc.respond_to?(:call)
    result = column_or_proc.call(record)
  end
  result.present? ^ options.has_key?(:unless)
end