Class: ParamsReady::Ordering::Column

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

Constant Summary collapse

DIRECTIONS =
Set.new %i(none asc desc)
NULLS_FIRST =
{ null: 0, not_null: 1 }.freeze
NULLS_LAST =
{ null: 1, not_null: 0 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ordering, arel_table:, expression:, nulls: :default, required: false, pk: false) ⇒ Column

Returns a new instance of Column.

Raises:



14
15
16
17
18
19
20
21
22
# File 'lib/params_ready/ordering/column.rb', line 14

def initialize(ordering, arel_table:, expression:, nulls: :default, required: false, pk: false)
  raise ParamsReadyError, "Invalid ordering value: #{ordering}" unless DIRECTIONS.include? ordering
  @ordering = ordering
  @table = arel_table
  @expression = expression
  @nulls = nulls
  @required = required
  @pk = pk
end

Instance Attribute Details

#nullsObject (readonly)

Returns the value of attribute nulls.



9
10
11
# File 'lib/params_ready/ordering/column.rb', line 9

def nulls
  @nulls
end

#orderingObject (readonly)

Returns the value of attribute ordering.



9
10
11
# File 'lib/params_ready/ordering/column.rb', line 9

def ordering
  @ordering
end

#pkObject (readonly)

Returns the value of attribute pk.



9
10
11
# File 'lib/params_ready/ordering/column.rb', line 9

def pk
  @pk
end

#requiredObject (readonly)

Returns the value of attribute required.



9
10
11
# File 'lib/params_ready/ordering/column.rb', line 9

def required
  @required
end

#tableObject (readonly)

Returns the value of attribute table.



9
10
11
# File 'lib/params_ready/ordering/column.rb', line 9

def table
  @table
end

Instance Method Details

#attribute(name, default_table, context) ⇒ Object



28
29
30
31
32
# File 'lib/params_ready/ordering/column.rb', line 28

def attribute(name, default_table, context)
  arel_table = table || default_table
  arel_builder = Helpers::ArelBuilder::Attribute.instance(expression(name), arel_table: arel_table)
  arel_builder.to_arel(arel_table, context, self)
end

#clauses(attribute, direction, inverted: false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/params_ready/ordering/column.rb', line 34

def clauses(attribute, direction, inverted: false)
  clause = attribute.send direction
  if nulls == :default
    [clause]
  else
    values = null_substitution_values(nulls, inverted)

    nulls_last = Arel::Nodes::Case.new
      .when(attribute.eq(nil)).then(values[:null])
      .else(values[:not_null])
    [nulls_last, clause]
  end
end

#expression(name) ⇒ Object



24
25
26
# File 'lib/params_ready/ordering/column.rb', line 24

def expression(name)
  @expression || name
end

#null_substitution_values(policy, inverted) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/params_ready/ordering/column.rb', line 48

def null_substitution_values(policy, inverted)
  case [policy, inverted]
  when [:first, false], [:last, true]
    NULLS_FIRST
  when [:last, false], [:first, true]
    NULLS_LAST
  else
    raise ParamsReadyError, "Unimplemented null handling policy: '#{nulls}' (inverted: #{inverted})"
  end
end