Class: Katalyst::Tables::Collection::Type::Value

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/katalyst/tables/collection/type/value.rb

Direct Known Subclasses

Boolean, Date, Enum, Float, Integer, Query, Search, String

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope: nil, filter: true) ⇒ Value

Returns a new instance of Value.



12
13
14
15
16
17
# File 'lib/katalyst/tables/collection/type/value.rb', line 12

def initialize(scope: nil, filter: true)
  super()

  @scope = scope
  @filterable = filter
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



10
11
12
# File 'lib/katalyst/tables/collection/type/value.rb', line 10

def scope
  @scope
end

Instance Method Details

#filter(scope, attribute, value: filter_value(attribute)) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/katalyst/tables/collection/type/value.rb', line 33

def filter(scope, attribute, value: filter_value(attribute))
  return scope unless filter?(attribute, value)

  scope, model, column = model_and_column_for(scope, attribute)
  condition = filter_condition(model, column, value)

  scope.merge(condition)
end

#filter?(attribute, value) ⇒ Boolean

Returns:



23
24
25
26
27
28
29
30
31
# File 'lib/katalyst/tables/collection/type/value.rb', line 23

def filter?(attribute, value)
  return false unless filterable?

  if attribute.came_from_user?
    attribute.value_before_type_cast.present?
  else
    value.present?
  end
end

#filterable?Boolean

Returns:



19
20
21
# File 'lib/katalyst/tables/collection/type/value.rb', line 19

def filterable?
  @filterable
end

#suggestions(scope, attribute, limit: 10, order: :asc) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/katalyst/tables/collection/type/value.rb', line 46

def suggestions(scope, attribute, limit: 10, order: :asc)
  scope, model, column = model_and_column_for(scope, attribute)

  unless model.attribute_types.has_key?(column)
    raise(ArgumentError, "Unknown column '#{column}' for #{model}. " \
                         "Consider defining '#{attribute.name.parameterize.underscore}_suggestions'")
  end

  arel_column = model.arel_table[column]

  filter(scope, attribute)
    .group(arel_column)
    .distinct
    .limit(limit)
    .reorder(arel_column => order)
    .pluck(arel_column)
    .map { |v| database_suggestion(attribute:, model:, column:, value: deserialize(v)) }
end

#to_param(value) ⇒ Object



42
43
44
# File 'lib/katalyst/tables/collection/type/value.rb', line 42

def to_param(value)
  serialize(value)
end