Class: Sortiri::Column

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

Direct Known Subclasses

MissingColumn

Constant Summary collapse

ASCENDING_SIGN =
''
DESCENDING_SIGN =
'-'
ASCENDING_SQL =
'ASC'
DESCENDING_SQL =
'DESC'
ASSOCIATION_DELIMITER =
'.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column:) ⇒ Column

Returns a new instance of Column.



13
14
15
# File 'lib/sortiri/column.rb', line 13

def initialize(column:)
  @name_with_sign = column.to_s
end

Instance Attribute Details

#name_with_signObject (readonly)

Returns the value of attribute name_with_sign.



11
12
13
# File 'lib/sortiri/column.rb', line 11

def name_with_sign
  @name_with_sign
end

Instance Method Details

#asc?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/sortiri/column.rb', line 63

def asc?
  direction == ASCENDING_SIGN
end

#associationObject



31
32
33
34
35
36
37
# File 'lib/sortiri/column.rb', line 31

def association
  return unless foreign_column?

  # name might have '-' sign, delete_prefix removes that
  # if no sign in there, it just returns the whole string
  name_with_sign.delete_prefix(DESCENDING_SIGN).split(ASSOCIATION_DELIMITER).first
end

#desc?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/sortiri/column.rb', line 67

def desc?
  direction == DESCENDING_SIGN
end

#directionObject



39
40
41
42
43
# File 'lib/sortiri/column.rb', line 39

def direction
  return DESCENDING_SIGN if name_with_sign.start_with?(DESCENDING_SIGN)

  ASCENDING_SIGN
end

#direction_sqlObject



45
46
47
48
49
# File 'lib/sortiri/column.rb', line 45

def direction_sql
  return DESCENDING_SQL if direction == DESCENDING_SIGN

  ASCENDING_SQL
end

#foreign_column?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/sortiri/column.rb', line 71

def foreign_column?
  name_with_sign.include?(ASSOCIATION_DELIMITER)
end

#nameObject



17
18
19
20
21
22
23
# File 'lib/sortiri/column.rb', line 17

def name
  raw_name = name_with_sign
  raw_name = raw_name[1..] if raw_name.start_with?(DESCENDING_SIGN)
  raw_name = raw_name.split(ASSOCIATION_DELIMITER)[1] if foreign_column?

  raw_name
end

#name_with_associationObject



25
26
27
28
29
# File 'lib/sortiri/column.rb', line 25

def name_with_association
  return [association, name].join(ASSOCIATION_DELIMITER) if foreign_column?

  name
end

#toggle_directionObject



51
52
53
54
55
# File 'lib/sortiri/column.rb', line 51

def toggle_direction
  return ASCENDING_SIGN if desc?

  DESCENDING_SIGN
end

#toggle_icon_classObject



57
58
59
60
61
# File 'lib/sortiri/column.rb', line 57

def toggle_icon_class
  return Sortiri.configuration.down_arrow if desc?

  Sortiri.configuration.up_arrow
end