Class: Tagtical::Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/tagtical/tag.rb

Defined Under Namespace

Classes: Type

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



155
156
157
158
159
160
161
162
163
164
# File 'lib/tagtical/tag.rb', line 155

def method_missing(method_name, *args, &block)
  if types = tag_types_for_questioner_method(method_name)
    self.class.send(:define_method, method_name) do
      types.any? { |type| is_a?(type.klass) }
    end
    send(method_name)
  else
    super
  end
end

Class Method Details

.define_scope_for_type(tag_type) ⇒ Object



66
67
68
# File 'lib/tagtical/tag.rb', line 66

def define_scope_for_type(tag_type)
  scope(tag_type.scope_name, lambda { |*args| type(tag_type.to_s, *args) }) unless respond_to?(tag_type.scope_name)
end

.detect_comparable(objects, value) ⇒ Object

Checks to see if a tags value is present in a set of tags and returns that tag.



71
72
73
74
# File 'lib/tagtical/tag.rb', line 71

def detect_comparable(objects, value)
  value = comparable_value(value)
  objects.detect { |obj| comparable_value(obj) == value }
end

.detect_possible_value(value) ⇒ Object



76
77
78
# File 'lib/tagtical/tag.rb', line 76

def detect_possible_value(value)
  detect_comparable(possible_values, value) if possible_values
end

.find_or_create_tags(*tag_list) ⇒ Object

Method used to ensure list of tags for the given Tag class. Returns a hash with the key being the value from the tag list and the value being the saved tag.



51
52
53
54
55
56
57
58
59
# File 'lib/tagtical/tag.rb', line 51

def find_or_create_tags(*tag_list)
  tag_list = [tag_list].flatten
  return {} if tag_list.empty?

  existing_tags = where_any_like(tag_list).all
  tag_list.each_with_object({}) do |value, tag_lookup|
    tag_lookup[detect_comparable(existing_tags, value) || create!(:value => value)] = value
  end
end

.find_or_create_with_like_by_value!(value) ⇒ Object

CLASS METHODS:



45
46
47
# File 'lib/tagtical/tag.rb', line 45

def find_or_create_with_like_by_value!(value)
  where_any_like(value).first || create!(:value => value)
end

.sti_nameObject

Save disc space by not having to put in “Tagtical::Tag” repeatedly



62
63
64
# File 'lib/tagtical/tag.rb', line 62

def sti_name
  Tagtical::Tag==self ? nil : super
end

.using_postgresql?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/tagtical/tag.rb', line 34

def using_postgresql?
  connection.adapter_name=='PostgreSQL'
end

.where_any(list, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/tagtical/tag.rb', line 25

def where_any(list, options={})
  char = "%" if options[:wildcard]
  operator = options[:case_insensitive] || options[:wildcard] ?
    (using_postgresql? ? 'ILIKE' : 'LIKE') :
    "="
  conditions = Array(list).map { |tag| ["value #{operator} ?", "#{char}#{tag.to_s}#{char}"] }
  where(conditions.size==1 ? conditions.first : conditions.map { |c| sanitize_sql(c) }.join(" OR "))
end

.where_any_like(list, options = {}) ⇒ Object

Use this for case insensitive



39
40
41
# File 'lib/tagtical/tag.rb', line 39

def where_any_like(list, options={})
  where_any(list, options.update(:case_insensitive => true))
end

Instance Method Details

#<=>(tag) ⇒ Object

Try to sort by the relevance if provided.



118
119
120
121
122
123
124
# File 'lib/tagtical/tag.rb', line 118

def <=>(tag)
  if (r1 = relevance) && (r2 = tag.relevance)
    r1 <=> r2
  else
    value <=> tag.value
  end
end

#==(object) ⇒ Object

INSTANCE METHODS:



104
105
106
# File 'lib/tagtical/tag.rb', line 104

def ==(object)
  super || (object.is_a?(self.class) && value == object.value)
end

#countObject



136
137
138
# File 'lib/tagtical/tag.rb', line 136

def count
  self[:count].to_i
end

#has_tagger?Boolean

Carried over from tagging.

Returns:

  • (Boolean)


145
146
147
# File 'lib/tagtical/tag.rb', line 145

def has_tagger?
  !self[:tagger_id].nil?
end

#inspectObject



130
131
132
133
134
# File 'lib/tagtical/tag.rb', line 130

def inspect
  super.tap do |str|
    str[-1] = ", relevance: #{relevance}>" if relevance
  end
end

#relevanceObject

Relevance is transferred through “taggings” join.



109
110
111
# File 'lib/tagtical/tag.rb', line 109

def relevance
  (v = self[:relevance]) && v.to_f
end

#relevance=(relevance) ⇒ Object



113
114
115
# File 'lib/tagtical/tag.rb', line 113

def relevance=(relevance)
  self[:relevance] = relevance
end

#respond_to?(method_id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/tagtical/tag.rb', line 140

def respond_to?(method_id, include_private = false)
  !!tag_types_for_questioner_method(method_id) || super
end

#to_sObject



126
127
128
# File 'lib/tagtical/tag.rb', line 126

def to_s
  value
end