Class: HaystackFactory
- Inherits:
-
BaseFactory
- Object
- BaseFactory
- HaystackFactory
- Defined in:
- lib/needle_in_a_haystack/factories/haystack_factory.rb
Instance Attribute Summary collapse
-
#tag_strategy ⇒ Object
Returns the value of attribute tag_strategy.
Instance Method Summary collapse
- #create_tag(name, description) ⇒ Object
- #create_tagging(tag, taggable) ⇒ Object
- #create_tags(tag_hash, parent_tag = nil) ⇒ Object
- #find_or_create_tag(name, attributes = {}) ⇒ Object
-
#initialize(tag_strategy = DefaultTagStrategy.new) ⇒ HaystackFactory
constructor
A new instance of HaystackFactory.
Constructor Details
#initialize(tag_strategy = DefaultTagStrategy.new) ⇒ HaystackFactory
Returns a new instance of HaystackFactory.
4 5 6 7 |
# File 'lib/needle_in_a_haystack/factories/haystack_factory.rb', line 4 def initialize(tag_strategy = DefaultTagStrategy.new) super() @tag_strategy = tag_strategy end |
Instance Attribute Details
#tag_strategy ⇒ Object
Returns the value of attribute tag_strategy.
2 3 4 |
# File 'lib/needle_in_a_haystack/factories/haystack_factory.rb', line 2 def tag_strategy @tag_strategy end |
Instance Method Details
#create_tag(name, description) ⇒ Object
9 10 11 |
# File 'lib/needle_in_a_haystack/factories/haystack_factory.rb', line 9 def create_tag(name, description) @tag_strategy.create_tag(name, description) end |
#create_tagging(tag, taggable) ⇒ Object
13 14 15 |
# File 'lib/needle_in_a_haystack/factories/haystack_factory.rb', line 13 def create_tagging(tag, taggable) HaystackTagging.create(haystack_tag: tag, taggable: taggable) end |
#create_tags(tag_hash, parent_tag = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/needle_in_a_haystack/factories/haystack_factory.rb', line 23 def (tag_hash, parent_tag = nil) tag_hash.each do |name, data| next if %w[description children].include?(name) tag = find_or_create_tag(name, description: data["description"], haystack_marker: data["marker"]) tag.update(parent_tag_id: parent_tag&.id) Rails.logger.info("Created tag: #{tag.name}, Parent: #{parent_tag&.name}, Parent ID: #{parent_tag&.id}") (data["children"], tag) if data["children"] end end |
#find_or_create_tag(name, attributes = {}) ⇒ Object
17 18 19 20 21 |
# File 'lib/needle_in_a_haystack/factories/haystack_factory.rb', line 17 def find_or_create_tag(name, attributes = {}) tag = HaystackTag.find_or_create_by(name: name) tag.persisted? ? @tag_strategy.update_tag(tag, attributes) : tag.update(attributes) tag end |