class Tagliani::Concerns::Taggable::Tags

Public Class Methods

new(parent = nil) click to toggle source
Calls superclass method
# File lib/tagliani/concerns/taggable/tags.rb, line 5
def initialize(parent = nil)
  @parent = parent
  @index = Tagliani::Search::Index.new
  @tag_kls = @parent.class._tag_kls.constantize
  @async = @parent.class._async
  
  super(search)
end

Public Instance Methods

add(*objects) click to toggle source
# File lib/tagliani/concerns/taggable/tags.rb, line 14
def add(*objects)
  objects.flat_map do |hash|
    options = hash.slice(:name)

    begin
      record = @tag_kls.find_or_initialize_by(options)
      record.save
    rescue ActiveRecord::RecordNotUnique
      record = @tag_kls.find_by(options)
    end

    @index.add!({
      object_kls: parent_kls,
      object_id: @parent.id,
      created_at: @parent.try(:created_at),
      tag_id: record.id,
      tag_kls: record.class.to_s,
      tag_type: record.sticker,
      tag_name: record.name,
      last_updated: Time.now
    }, async: @async)
  end
end
inherit() click to toggle source
# File lib/tagliani/concerns/taggable/tags.rb, line 53
def inherit
  root.each do |object|
    next if object.nil?
    
    self.class.new(object).each do |ref|
      add(name: ref.name)
    end
  end
end
root() click to toggle source
# File lib/tagliani/concerns/taggable/tags.rb, line 63
def root
  klasses = []
  
  if @parent._inherit.respond_to?(:each)
    klasses = @parent._inherit
  else
    klasses << @parent._inherit
  end
  
  objects = klasses.flat_map do |method|
    next if method.nil?
    @parent.send(method)
  end
  
  objects.compact
end

Private Instance Methods

parent_kls() click to toggle source
# File lib/tagliani/concerns/taggable/tags.rb, line 82
def parent_kls
  @parent.class.base_class.to_s 
end