Class: Symphonia::QueryColumns::GenericColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/symphonia/query_columns/generic_column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, klass = nil, options = {}) ⇒ GenericColumn

Returns a new instance of GenericColumn.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/symphonia/query_columns/generic_column.rb', line 9

def initialize(name, klass = nil, options={})
  ::ActiveSupport::Deprecation.warn 'Maybe this class is over...'
  @name = name
  if klass.is_a? Symphonia::Query
    @query = klass
    @model = @query.model
  elsif klass < ActiveRecord::Base
    @model = klass
  else # default fallback
    @model = ActiveRecord::Base
  end
  @options = options

  # @sort_options = @options.delete(:sort_options) || {}
  self.header = @options.delete(:header).nil?
  self.sort = @options.delete(:sort).nil?

  @includes = Array(options.delete(:includes)) || []
  @preload = Array(options.delete(:preload)) || []
  @joins = Array(options.delete(:joins)) || []
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



7
8
9
# File 'lib/symphonia/query_columns/generic_column.rb', line 7

def default
  @default
end

#header(view) ⇒ Object

Returns the value of attribute header.



7
8
9
# File 'lib/symphonia/query_columns/generic_column.rb', line 7

def header
  @header
end

#includesObject (readonly)

Returns the value of attribute includes.



5
6
7
# File 'lib/symphonia/query_columns/generic_column.rb', line 5

def includes
  @includes
end

#joinsObject (readonly)

Returns the value of attribute joins.



5
6
7
# File 'lib/symphonia/query_columns/generic_column.rb', line 5

def joins
  @joins
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/symphonia/query_columns/generic_column.rb', line 5

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/symphonia/query_columns/generic_column.rb', line 5

def name
  @name
end

#preloadObject (readonly)

Returns the value of attribute preload.



5
6
7
# File 'lib/symphonia/query_columns/generic_column.rb', line 5

def preload
  @preload
end

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'lib/symphonia/query_columns/generic_column.rb', line 5

def query
  @query
end

#sortObject

Returns the value of attribute sort.



7
8
9
# File 'lib/symphonia/query_columns/generic_column.rb', line 7

def sort
  @sort
end

#sort_column_name=(value) ⇒ Object

Sets the attribute sort_column_name

Parameters:

  • value

    the value to set the attribute sort_column_name to.



6
7
8
# File 'lib/symphonia/query_columns/generic_column.rb', line 6

def sort_column_name=(value)
  @sort_column_name = value
end

#sort_definitionObject



56
57
58
59
# File 'lib/symphonia/query_columns/generic_column.rb', line 56

def sort_definition
  return nil unless sort?
  @sort_definition ||= SortableTable::SortColumnCustomDefinition.new(@name, {asc: "#{sort_column_name} asc", desc: "#{sort_column_name} desc"})
end

Instance Method Details

#css_classesObject



98
99
100
# File 'lib/symphonia/query_columns/generic_column.rb', line 98

def css_classes
  @name.to_s
end

#default?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/symphonia/query_columns/generic_column.rb', line 43

def default?
  !!@default
end

#format_value(view, entity) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/symphonia/query_columns/generic_column.rb', line 74

def format_value(view, entity)
  if @options[:formatter].is_a?(Proc)
    @options[:formatter].call(view, entity).to_s
  else
    case @options[:with_link]
    when :entity, true
      view.link_to(value(entity).to_s, entity)
    when :value
      view.link_to(value(entity).to_s, value(entity))
    else
      value(entity).to_s
    end
  end
end

#header?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/symphonia/query_columns/generic_column.rb', line 35

def header?
  !(@header === false)
end

#inspectObject



102
103
104
# File 'lib/symphonia/query_columns/generic_column.rb', line 102

def inspect
  "#<#{self.class.name} name='#{name}' options=#{@options.inspect}>"
end

#sort?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/symphonia/query_columns/generic_column.rb', line 39

def sort?
  !(@sort === false)
end

#sort_optionsObject



66
67
68
# File 'lib/symphonia/query_columns/generic_column.rb', line 66

def sort_options
  @sort_options
end

#sumObject



93
94
95
96
# File 'lib/symphonia/query_columns/generic_column.rb', line 93

def sum
  return nil unless summarize?
  @sum ||= query.entities.sum(name)
end

#sum_value(view) ⇒ Object



89
90
91
# File 'lib/symphonia/query_columns/generic_column.rb', line 89

def sum_value(view)
  format_value(view, OpenStruct.new(name => sum))
end

#summarize?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/symphonia/query_columns/generic_column.rb', line 31

def summarize?
  false
end

#titleObject



61
62
63
64
# File 'lib/symphonia/query_columns/generic_column.rb', line 61

def title
  ::ActiveSupport::Deprecation.warn 'method is deprecated'
  @model.human_attribute_name(@name, @options[:i18n] || {})
end

#value(entity) ⇒ Object



70
71
72
# File 'lib/symphonia/query_columns/generic_column.rb', line 70

def value(entity)
  entity.send(name)
end