Class: MightyGrid::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/mighty_grid/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Column

Returns a new instance of Column


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mighty_grid/column.rb', line 6

def initialize(options = {}, &block)
  @attrs = {}
  @th_attrs = {}

  @attribute = options.delete(:attribute) if options.key?(:attribute)

  @options ||= options

  if block_given?
    @render_value = block
  else
    @render_value = @attribute
  end

  @model = @options[:model]
  fail MightyGrid::Exceptions::ArgumentError.new('Model of field for filtering should have type ActiveRecord') if @model && @model.superclass != ActiveRecord::Base

  @attrs = @options[:html] if @options.key?(:html)
  @th_attrs = @options[:th_html] if @options.key?(:th_html)
  @title = @options.key?(:title) && @options[:title] || ''
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute


3
4
5
# File 'lib/mighty_grid/column.rb', line 3

def attribute
  @attribute
end

#attrsObject (readonly)

Returns the value of attribute attrs


3
4
5
# File 'lib/mighty_grid/column.rb', line 3

def attrs
  @attrs
end

#modelObject (readonly)

Returns the value of attribute model


3
4
5
# File 'lib/mighty_grid/column.rb', line 3

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options


3
4
5
# File 'lib/mighty_grid/column.rb', line 3

def options
  @options
end

#partialObject (readonly)

Returns the value of attribute partial


3
4
5
# File 'lib/mighty_grid/column.rb', line 3

def partial
  @partial
end

#render_valueObject

Returns the value of attribute render_value


4
5
6
# File 'lib/mighty_grid/column.rb', line 4

def render_value
  @render_value
end

#th_attrsObject (readonly)

Returns the value of attribute th_attrs


3
4
5
# File 'lib/mighty_grid/column.rb', line 3

def th_attrs
  @th_attrs
end

#titleObject (readonly)

Returns the value of attribute title


3
4
5
# File 'lib/mighty_grid/column.rb', line 3

def title
  @title
end

Instance Method Details

#render(record) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mighty_grid/column.rb', line 28

def render(record)
  case @render_value
  when String, Symbol
    rec = @model ? record.send(@model.to_s.underscore) : record
    return rec[@render_value.to_sym]
  when Proc
    value, attrs = @render_value.call(record)
    @attrs.merge!(attrs || {})
    return ERB::Util.h(value).to_s.html_safe
  else
    # raise
  end
end