Class: Tableling::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/tableling-rails/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, view, options = {}, &block) ⇒ Field

Returns a new instance of Field.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tableling-rails/field.rb', line 7

def initialize name, view, options = {}, &block

  @name, @view = name.to_s, view
  @value_column = options[:value].try :to_s
  @includes = options[:includes]

  if options[:order] == false
    @no_order = true
  elsif options[:order]
    @order_column = options[:order].to_s
  end

  instance_eval &block if block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/tableling-rails/field.rb', line 5

def name
  @name
end

Instance Method Details

#extract(object) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/tableling-rails/field.rb', line 57

def extract object
  if @value_block
    @value_block.call object
  else
    serialize object.send(@value_column || @name)
  end
end

#includes(&block) ⇒ Object



34
35
36
# File 'lib/tableling-rails/field.rb', line 34

def includes &block
  @includes_block = block
end

#no_orderObject



26
27
28
# File 'lib/tableling-rails/field.rb', line 26

def no_order
  @no_order = true
end

#order(&block) ⇒ Object



22
23
24
# File 'lib/tableling-rails/field.rb', line 22

def order &block
  @order_block = block
end

#value(&block) ⇒ Object



30
31
32
# File 'lib/tableling-rails/field.rb', line 30

def value &block
  @value_block = block
end

#with_includes(query) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/tableling-rails/field.rb', line 47

def with_includes query
  if @includes_block
    @includes_block.call query
  elsif @includes
    query.includes @includes
  else
    query
  end
end

#with_order(query, direction) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/tableling-rails/field.rb', line 38

def with_order query, direction
  return if @no_order
  if @order_block
    @order_block.call query, direction
  else
    query.order "#{model.table_name}.#{@order_column || @name} #{direction}"
  end
end