Sha256: e6de01a3a44529d25008f1d871de0151c4b69f51f91b654126e4c3d949bd386a

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 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)
    @tag_strategy.update_tag(tag, attributes)
    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}")

      if data["children"]
        Rails.logger.info("Processing children for tag: #{tag.name}")
        create_tags(data["children"], tag)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
needle_in_a_haystack-1.0.5 lib/needle_in_a_haystack/factories/haystack_factory.rb