Class: Slugable::SlugBuilder::TreeAncestry

Inherits:
Object
  • Object
show all
Defined in:
lib/slugable/slug_builder/tree_ancestry.rb

Direct Known Subclasses

CachingTreeAncestry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TreeAncestry

Returns a new instance of TreeAncestry.



6
7
8
9
# File 'lib/slugable/slug_builder/tree_ancestry.rb', line 6

def initialize(options)
  @slug_column = options.fetch(:slug_column)
  @formatter = options.fetch(:formatter)
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter.



4
5
6
# File 'lib/slugable/slug_builder/tree_ancestry.rb', line 4

def formatter
  @formatter
end

#slug_columnObject (readonly)

Returns the value of attribute slug_column.



4
5
6
# File 'lib/slugable/slug_builder/tree_ancestry.rb', line 4

def slug_column
  @slug_column
end

Instance Method Details

#to_slug(record) ⇒ Object



11
12
13
# File 'lib/slugable/slug_builder/tree_ancestry.rb', line 11

def to_slug(record)
  record.path.map{ |path_record| path_record.public_send(slug_column) }.select(&:present?)
end

#to_slug_was(record) ⇒ Object



15
16
17
18
19
# File 'lib/slugable/slug_builder/tree_ancestry.rb', line 15

def to_slug_was(record)
  old_slugs = record.ancestry_was.to_s.split('/').map { |ancestor_id| record.class.find(ancestor_id).public_send(slug_column) }
  old_slugs << record.public_send(:"#{slug_column}_was")
  old_slugs
end

#to_slug_will(record) ⇒ Object



21
22
23
24
25
# File 'lib/slugable/slug_builder/tree_ancestry.rb', line 21

def to_slug_will(record)
  new_slugs = record.ancestry.to_s.split('/').map { |ancestor_id| record.class.find(ancestor_id).public_send(slug_column) }
  new_slugs << formatter.call(record.public_send(slug_column))
  new_slugs
end