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
41
42
43
44
45
46
47
48
# File 'lib/katalyst/tables/collection/type/value.rb', line 33

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

  if self.scope.present?
    scope.public_send(self.scope, value)
  elsif attribute.name.include?(".")
    table_name, = attribute.name.split(".")
    association = scope.model.reflections[table_name]

    raise(ArgumentError, "Unknown association '#{table_name}' for #{scope.model}") unless association

    apply_filter(scope.joins(table_name.to_sym), association.klass, attribute, value)
  else
    apply_filter(scope, scope.model, attribute, value)
  end
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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/katalyst/tables/collection/type/value.rb', line 54

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

  if attribute.name.include?(".")
    table_name, column = attribute.name.split(".")
    model = scope.model.reflections[table_name].klass

    raise(ArgumentError, "Unknown association '#{table_name}' for #{scope.model}") unless model

    scope = scope.joins(table_name.to_sym)
  end

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

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

#to_param(value) ⇒ Object



50
51
52
# File 'lib/katalyst/tables/collection/type/value.rb', line 50

def to_param(value)
  serialize(value)
end