Sha256: 8629d026b638e12bf06f0f5467d982d9ee83077d5f54ee599ee5a53ad1260274

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

class HaystackFactory < BaseFactory
  attr_accessor :tag_strategy

  def initialize(tag_strategy = DefaultTagStrategy.new)
    super()
    @tag_strategy = tag_strategy
  end

  def create_tag(name, description)
    @tag_strategy.create_tag(name, description)
  end

  def create_tagging(tag, taggable)
    HaystackTagging.create(haystack_tag: tag, taggable: taggable)
  end

  def find_or_create_tag(name, attributes = {})
    tag = HaystackTag.find_or_create_by(name: name)
    if tag.persisted?
      @tag_strategy.update_tag(tag, attributes)
    else
      tag.update(attributes)
    end
    tag
  end

  def create_tags(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}")

      create_tags(data["children"], tag) if data["children"]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
needle_in_a_haystack-1.0.8 lib/needle_in_a_haystack/factories/haystack_factory.rb
needle_in_a_haystack-1.0.7 lib/needle_in_a_haystack/factories/haystack_factory.rb
needle_in_a_haystack-1.0.6 lib/needle_in_a_haystack/factories/haystack_factory.rb