Class: Hiiro::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/tags.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_tagsObject



19
20
21
# File 'lib/hiiro/tags.rb', line 19

def all_tags
  select(:name).distinct
end

.create_table!(db) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/hiiro/tags.rb', line 8

def create_table!(db)
  db.create_table?(:tags) do
    primary_key :id
    String :name, null: false           # tag value, e.g. "oncall"
    String :taggable_type, null: false  # e.g. "Branch", "PinnedPr", "Task"
    String :taggable_id, null: false    # string id of tagged object
    String :created_at
    unique [:name, :taggable_type, :taggable_id]
  end
end

.everything_tagged(tag_name) ⇒ Object

Returns all tagged objects across all types for a tag name



63
64
65
# File 'lib/hiiro/tags.rb', line 63

def everything_tagged(tag_name)
  named(tag_name).map(&:taggable).compact
end

.filter(terms, tags = all_tags) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hiiro/tags.rb', line 23

def filter(terms, tags=all_tags)
  $stderr.puts("tags class: #{tags.class}")
  $stderr.puts("terms count: #{terms.count}")
  return tags if terms.empty?

  conditions = terms.map{|q| Sequel.like(:name, "%#{q}%") }
  tags.where(Sequel.|(*conditions)).all

  # tags.select{|tag|
  #   terms.any?{|term| tag.start_with?(term) }
  # }
end

.for(obj) ⇒ Object

Returns all Tag rows for a given object



48
49
50
51
# File 'lib/hiiro/tags.rb', line 48

def for(obj)
  t = obj.class.name.split('::').last
  where(taggable_type: t, taggable_id: obj.id.to_s)
end

.named(tag_name) ⇒ Object

Returns all Tag rows with a given name



54
55
56
# File 'lib/hiiro/tags.rb', line 54

def named(tag_name)
  where(name: tag_name.to_s)
end

.search(*terms, type: nil) ⇒ Object



36
37
38
# File 'lib/hiiro/tags.rb', line 36

def search(*terms, type: nil)
    filter(terms.flatten, tags_by_type(type))
end

.tag!(obj, *tag_names) ⇒ Object

Idempotent tag assignment



68
69
70
71
72
73
74
75
76
77
# File 'lib/hiiro/tags.rb', line 68

def tag!(obj, *tag_names)
  t = obj.class.name.split('::').last
  tag_names.each do |name|
    find_or_create(
      name: name.to_s,
      taggable_type: t,
      taggable_id: obj.id.to_s
    ) { |tag| tag.created_at = Time.now.iso8601 }
  end
end

.tagged_by_type(tag, type) ⇒ Object



58
59
60
# File 'lib/hiiro/tags.rb', line 58

def tagged_by_type(tag, type)
  where(name: tag, taggable_type: type).map(&:taggable)
end

.tags_by_type(type = nil) ⇒ Object

Returns all Tag rows for a given object



41
42
43
44
45
# File 'lib/hiiro/tags.rb', line 41

def tags_by_type(type=nil)
  return all_tags if type.nil?

  where(taggable_type: type).select(:name).distinct.map(&:name).sort
end

.untag!(obj, *tag_names) ⇒ Object

Remove tags from an object



80
81
82
83
84
85
86
87
# File 'lib/hiiro/tags.rb', line 80

def untag!(obj, *tag_names)
  t = obj.class.name.split('::').last
  where(
    taggable_type: t,
    taggable_id: obj.id.to_s,
    name: tag_names.map(&:to_s)
  ).delete
end

Instance Method Details

#taggableObject

Polymorphic accessor — returns the tagged object



91
92
93
94
# File 'lib/hiiro/tags.rb', line 91

def taggable
  klass = Hiiro.const_get(taggable_type) rescue nil
  klass&.[](taggable_id)
end