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
21
# File 'lib/tableling-rails/field.rb', line 7

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

  @name, @view = name.to_s, view
  @alias = options[:as].try :to_s
  @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

#aliasObject

Returns the value of attribute alias.



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

def alias
  @alias
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#as(name) ⇒ Object



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

def as name
  @alias = name.to_s
end

#extract(object) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/tableling-rails/field.rb', line 66

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

#includes(&block) ⇒ Object



43
44
45
# File 'lib/tableling-rails/field.rb', line 43

def includes &block
  @includes_block = block
end

#no_orderObject



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

def no_order
  @no_order = true
end

#order(&block) ⇒ Object



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

def order &block
  @order_block = block
end

#value(&block) ⇒ Object



39
40
41
# File 'lib/tableling-rails/field.rb', line 39

def value &block
  @value_block = block
end

#with_includes(query) ⇒ Object



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

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



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

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

#working_nameObject



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

def working_name
  @alias || @name
end