Class: Slugable::SlugBuilder::TreeAncestry
- Inherits:
-
Object
- Object
- Slugable::SlugBuilder::TreeAncestry
- Defined in:
- lib/slugable/slug_builder/tree_ancestry.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#slug_column ⇒ Object
readonly
Returns the value of attribute slug_column.
Instance Method Summary collapse
-
#initialize(options) ⇒ TreeAncestry
constructor
A new instance of TreeAncestry.
- #to_slug(record) ⇒ Object
- #to_slug_was(record) ⇒ Object
- #to_slug_will(record) ⇒ Object
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() @slug_column = .fetch(:slug_column) @formatter = .fetch(:formatter) end |
Instance Attribute Details
#formatter ⇒ Object (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_column ⇒ Object (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 |