Class: TypicalSort::PathResolver
- Inherits:
-
Object
- Object
- TypicalSort::PathResolver
- Defined in:
- lib/typical_sort/path_resolver.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
- #association_name ⇒ Object
- #association_sort? ⇒ Boolean
- #collection_association? ⇒ Boolean
- #column_name ⇒ Object
-
#initialize(records:, path:) ⇒ PathResolver
constructor
A new instance of PathResolver.
- #nested_association_sort? ⇒ Boolean
- #reflection ⇒ Object
- #relation_association? ⇒ Boolean
Constructor Details
#initialize(records:, path:) ⇒ PathResolver
Returns a new instance of PathResolver.
7 8 9 10 |
# File 'lib/typical_sort/path_resolver.rb', line 7 def initialize(records:, path:) @records = records @path = path.to_s end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/typical_sort/path_resolver.rb', line 5 def path @path end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
5 6 7 |
# File 'lib/typical_sort/path_resolver.rb', line 5 def records @records end |
Instance Method Details
#association_name ⇒ Object
12 13 14 15 16 |
# File 'lib/typical_sort/path_resolver.rb', line 12 def association_name return nil unless association_sort? path.split(".", 2).first end |
#association_sort? ⇒ Boolean
32 33 34 |
# File 'lib/typical_sort/path_resolver.rb', line 32 def association_sort? path.include?(".") end |
#collection_association? ⇒ Boolean
36 37 38 |
# File 'lib/typical_sort/path_resolver.rb', line 36 def collection_association? %i[has_many has_and_belongs_to_many].include?(reflection&.macro) end |
#column_name ⇒ Object
18 19 20 |
# File 'lib/typical_sort/path_resolver.rb', line 18 def column_name association_sort? ? path.split(".", 2).last : path end |
#nested_association_sort? ⇒ Boolean
22 23 24 |
# File 'lib/typical_sort/path_resolver.rb', line 22 def nested_association_sort? column_name.include?(".") end |
#reflection ⇒ Object
26 27 28 29 30 |
# File 'lib/typical_sort/path_resolver.rb', line 26 def reflection return nil unless association_name records.klass.reflect_on_association(association_name.to_sym) end |
#relation_association? ⇒ Boolean
40 41 42 |
# File 'lib/typical_sort/path_resolver.rb', line 40 def relation_association? reflection.present? && !collection_association? end |