Class: TypicalSort::PathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/typical_sort/path_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/typical_sort/path_resolver.rb', line 5

def path
  @path
end

#recordsObject (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_nameObject



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

Returns:

  • (Boolean)


32
33
34
# File 'lib/typical_sort/path_resolver.rb', line 32

def association_sort?
  path.include?(".")
end

#collection_association?Boolean

Returns:

  • (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_nameObject



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

Returns:

  • (Boolean)


22
23
24
# File 'lib/typical_sort/path_resolver.rb', line 22

def nested_association_sort?
  column_name.include?(".")
end

#reflectionObject



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

Returns:

  • (Boolean)


40
41
42
# File 'lib/typical_sort/path_resolver.rb', line 40

def relation_association?
  reflection.present? && !collection_association?
end