Class: GridTable::Control

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/grid_table/control.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Control

Initializes a new model with the given params.

class Person
  include ActiveModel::Model
  attr_accessor :name, :age
end

person = Person.new(name: 'bob', age: '18')
person.name # => "bob"
person.age  # => "18"


18
19
20
21
22
# File 'lib/grid_table/control.rb', line 18

def initialize(params = {})
  params&.each { |attr, value| public_send("#{attr}=", value) }

  super()
end

Instance Attribute Details

#attribute=(value) ⇒ Object (writeonly)

Sets the attribute attribute

Parameters:

  • value

    the value to set the attribute attribute to.



37
38
39
# File 'lib/grid_table/control.rb', line 37

def attribute=(value)
  @attribute = value
end

#filter(param_filter_value, records) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/grid_table/control.rb', line 39

def filter(param_filter_value, records)
  return records if @filter == false

  arel_query = nil
  strategy_map = {
    exact_match: ->(col) { "(#{col}) = #{param_filter_value}" },
    prefix:      ->(col) { "(#{col}) ILIKE '#{param_filter_value}%'" },
    suffix:      ->(col) { "(#{col}) ILIKE '%#{param_filter_value}'" },
    fuzzy:       ->(col) { "(#{col}) ILIKE '%#{param_filter_value}%'" },
    array:       ->(col) { "(#{col}) @> ARRAY[#{[param_filter_value].flatten.join(',')}]" },
    date_range:  ->(_col) { "(#{col}) BETWEEN #{param_filter_value}" }
  }

  polymorphic_models.each_with_index do |klass, i|
    # TODO: implement array filtering
    arel_query = i.zero? ? strategy_map[strategy].call(polymorphic_table_with_column(klass)) : arel_query.or(strategy_map[strategy].call(polymorphic_table_with_column(klass)))
  end

  arel_query ||= strategy_map[strategy].call(table_with_column)
  prepared_records(records).where(arel_query)
end

#model=(value) ⇒ Object (writeonly)

Sets the attribute model

Parameters:

  • value

    the value to set the attribute model to.



37
38
39
# File 'lib/grid_table/control.rb', line 37

def model=(value)
  @model = value
end

#polymorphic=(value) ⇒ Object (writeonly)

Sets the attribute polymorphic

Parameters:

  • value

    the value to set the attribute polymorphic to.



37
38
39
# File 'lib/grid_table/control.rb', line 37

def polymorphic=(value)
  @polymorphic = value
end

#source=(value) ⇒ Object (writeonly)

Sets the attribute source

Parameters:

  • value

    the value to set the attribute source to.



37
38
39
# File 'lib/grid_table/control.rb', line 37

def source=(value)
  @source = value
end

#source_class=(value) ⇒ Object (writeonly)

Sets the attribute source_class

Parameters:

  • value

    the value to set the attribute source_class to.



37
38
39
# File 'lib/grid_table/control.rb', line 37

def source_class=(value)
  @source_class = value
end

#source_column=(value) ⇒ Object (writeonly)

Sets the attribute source_column

Parameters:

  • value

    the value to set the attribute source_column to.



37
38
39
# File 'lib/grid_table/control.rb', line 37

def source_column=(value)
  @source_column = value
end

#source_sql=(value) ⇒ Object (writeonly)

Sets the attribute source_sql

Parameters:

  • value

    the value to set the attribute source_sql to.



37
38
39
# File 'lib/grid_table/control.rb', line 37

def source_sql=(value)
  @source_sql = value
end

Class Method Details

.find_by_param(param, controls) ⇒ Object



165
166
167
# File 'lib/grid_table/control.rb', line 165

def find_by_param(param, controls)
  controls.detect { |control| control.url_param == param.try(:to_sym) }
end

Instance Method Details

#persisted?Boolean

Indicates if the model is persisted. Default is false.

class Person
  include ActiveModel::Model
  attr_accessor :id, :name
end

person = Person.new(id: 1, name: 'bob')
person.persisted? # => false

Returns:

  • (Boolean)


33
34
35
# File 'lib/grid_table/control.rb', line 33

def persisted?
  false
end

#selectObject



77
78
79
# File 'lib/grid_table/control.rb', line 77

def select
  "#{table_with_column} as #{column}"
end

#sort(param_sort_order, records) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/grid_table/control.rb', line 61

def sort(param_sort_order, records)
  sort_order = %w[asc desc].include?(param_sort_order) ? param_sort_order : 'asc'

  if @polymorphic
    models = polymorphic_models
    if models.present?
      prepared_records(records).select("\"#{@model.to_s.tableize}\".*, #{polymorphic_select_sql(models)} AS #{active_source}")
                               .order("#{active_source} #{sort_order}")
    else
      records
    end
  else
    prepared_records(records).order("#{column} #{sort_order}")
  end
end

#url_paramObject



81
82
83
# File 'lib/grid_table/control.rb', line 81

def url_param
  @attribute
end