Class: Tableling::Field
- Inherits:
-
Object
- Object
- Tableling::Field
- Defined in:
- lib/tableling-rails/field.rb
Instance Attribute Summary collapse
-
#alias ⇒ Object
Returns the value of attribute alias.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #as(name) ⇒ Object
- #extract(object) ⇒ Object
- #includes(&block) ⇒ Object
-
#initialize(name, view, options = {}, &block) ⇒ Field
constructor
A new instance of Field.
- #no_order ⇒ Object
- #order(&block) ⇒ Object
- #value(&block) ⇒ Object
- #with_includes(query) ⇒ Object
- #with_order(query, direction) ⇒ Object
- #working_name ⇒ Object
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, = {}, &block @name, @view = name.to_s, view @alias = [:as].try :to_s @value_column = [:value].try :to_s @includes = [:includes] if [:order] == false @no_order = true elsif [:order] @order_column = [:order].to_s end instance_eval &block if block end |
Instance Attribute Details
#alias ⇒ Object
Returns the value of attribute alias.
5 6 7 |
# File 'lib/tableling-rails/field.rb', line 5 def alias @alias end |
#name ⇒ Object
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_order ⇒ Object
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_name ⇒ Object
23 24 25 |
# File 'lib/tableling-rails/field.rb', line 23 def working_name @alias || @name end |