Class: GridTable::Control
- Inherits:
-
Object
- Object
- GridTable::Control
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/grid_table/control.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
writeonly
Sets the attribute attribute.
- #filter(param_filter_value, records) ⇒ Object
-
#model ⇒ Object
writeonly
Sets the attribute model.
-
#polymorphic ⇒ Object
writeonly
Sets the attribute polymorphic.
-
#source ⇒ Object
writeonly
Sets the attribute source.
-
#source_class ⇒ Object
writeonly
Sets the attribute source_class.
-
#source_column ⇒ Object
writeonly
Sets the attribute source_column.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Control
constructor
Initializes a new model with the given
params. -
#persisted? ⇒ Boolean
Indicates if the model is persisted.
- #sort(param_sort_order, records) ⇒ Object
- #url_param ⇒ Object
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 23 24 |
# File 'lib/grid_table/control.rb', line 18 def initialize(params={}) params.each do |attr, value| self.public_send("#{attr}=", value) end if params super() end |
Instance Attribute Details
#attribute=(value) ⇒ Object (writeonly)
Sets the attribute attribute
39 40 41 |
# File 'lib/grid_table/control.rb', line 39 def attribute=(value) @attribute = value end |
#filter(param_filter_value, records) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/grid_table/control.rb', line 41 def filter(param_filter_value, records) unless @filter == false arel_query = nil strategy_map = { exact_match: ->(col) { col.eq(param_filter_value) }, prefix: ->(col) { col.matches("#{param_filter_value}%") }, suffix: ->(col) { col.matches("%#{param_filter_value}") }, fuzzy: ->(col) { col.matches("%#{param_filter_value}%") }, array: ->(col) { "#{column.to_s} @> ARRAY[#{[param_filter_value].flatten.join(',')}]" } } polymorphic_models.each_with_index do |klass, i| # TODO implement array filtering if i == 0 arel_query = strategy_map[strategy].call(klass.arel_table[column]) else arel_query = arel_query.or(strategy_map[strategy].call(klass.arel_table[column])) end end arel_query ||= strategy_map[strategy].call(source_table[column]) prepared_records(records).where(arel_query) end end |
#model=(value) ⇒ Object (writeonly)
Sets the attribute model
39 40 41 |
# File 'lib/grid_table/control.rb', line 39 def model=(value) @model = value end |
#polymorphic=(value) ⇒ Object (writeonly)
Sets the attribute polymorphic
39 40 41 |
# File 'lib/grid_table/control.rb', line 39 def polymorphic=(value) @polymorphic = value end |
#source=(value) ⇒ Object (writeonly)
Sets the attribute source
39 40 41 |
# File 'lib/grid_table/control.rb', line 39 def source=(value) @source = value end |
#source_class=(value) ⇒ Object (writeonly)
Sets the attribute source_class
39 40 41 |
# File 'lib/grid_table/control.rb', line 39 def source_class=(value) @source_class = value end |
#source_column=(value) ⇒ Object (writeonly)
Sets the attribute source_column
39 40 41 |
# File 'lib/grid_table/control.rb', line 39 def source_column=(value) @source_column = value end |
Class Method Details
.find_by_param(param, controls) ⇒ Object
162 163 164 |
# File 'lib/grid_table/control.rb', line 162 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
35 36 37 |
# File 'lib/grid_table/control.rb', line 35 def persisted? false end |
#sort(param_sort_order, records) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/grid_table/control.rb', line 66 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("#{table_with_column} #{sort_order}") end end |
#url_param ⇒ Object
82 83 84 |
# File 'lib/grid_table/control.rb', line 82 def url_param @attribute end |