Class: Sortiri::Column
- Inherits:
-
Object
- Object
- Sortiri::Column
- Defined in:
- lib/sortiri/column.rb
Direct Known Subclasses
Constant Summary collapse
- ASCENDING_SIGN =
''
- DESCENDING_SIGN =
'-'
- ASCENDING_SQL =
'ASC'
- DESCENDING_SQL =
'DESC'
- ASSOCIATION_DELIMITER =
'.'
Instance Attribute Summary collapse
-
#name_with_sign ⇒ Object
readonly
Returns the value of attribute name_with_sign.
Instance Method Summary collapse
- #asc? ⇒ Boolean
- #association ⇒ Object
- #desc? ⇒ Boolean
- #direction ⇒ Object
- #direction_sql ⇒ Object
- #foreign_column? ⇒ Boolean
-
#initialize(column:) ⇒ Column
constructor
A new instance of Column.
- #name ⇒ Object
- #name_with_association ⇒ Object
- #toggle_direction ⇒ Object
- #toggle_icon_class ⇒ Object
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_sign ⇒ Object (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
63 64 65 |
# File 'lib/sortiri/column.rb', line 63 def asc? direction == ASCENDING_SIGN end |
#association ⇒ Object
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
67 68 69 |
# File 'lib/sortiri/column.rb', line 67 def desc? direction == DESCENDING_SIGN end |
#direction ⇒ Object
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_sql ⇒ Object
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
71 72 73 |
# File 'lib/sortiri/column.rb', line 71 def foreign_column? name_with_sign.include?(ASSOCIATION_DELIMITER) end |
#name ⇒ Object
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_association ⇒ Object
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_direction ⇒ Object
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_class ⇒ Object
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 |