Class: Tagtical::Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tagtical::Tag
show all
- Defined in:
- lib/tagtical/tag.rb
Defined Under Namespace
Classes: Type
Instance Attribute Summary collapse
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
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/tagtical/tag.rb', line 196
def method_missing(method_name, *args, &block)
if self.class.send(:tag_types_for_questioner_method, method_name)
self.class.send(:method_missing, method_name, *args, &block)
self.class.send(:define_method, method_name) do
self.class.send(method_name)
end
send(method_name)
else
super
end
end
|
Instance Attribute Details
#tagger_id ⇒ Object
Returns the value of attribute tagger_id.
4
5
6
|
# File 'lib/tagtical/tag.rb', line 4
def tagger_id
@tagger_id
end
|
Class Method Details
.define_scope_for_type(tag_type) ⇒ Object
80
81
82
|
# File 'lib/tagtical/tag.rb', line 80
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.
85
86
87
88
|
# File 'lib/tagtical/tag.rb', line 85
def detect_comparable(objects, value)
value = comparable_value(value)
objects.detect { |obj| comparable_value(obj) == value }
end
|
.detect_possible_value(value) ⇒ Object
90
91
92
|
# File 'lib/tagtical/tag.rb', line 90
def detect_possible_value(value)
detect_comparable(possible_values, value) if possible_values
end
|
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/tagtical/tag.rb', line 61
def find_or_build_or_create_tags(operation, tag_list, tag_types)
tag_list = [tag_list].flatten
return {} if tag_list.empty?
existing_tags = where_any_like(tag_list)
if tag_types.present?
existing_tags = existing_tags.where(["type IS NULL OR type IN (?)", tag_types.map(&:klass).map(&:to_s)])
end
existing_tags = existing_tags.all
tag_list.each_with_object({}) do |value, tag_lookup|
tag_lookup[detect_comparable(existing_tags, value) || send(operation, :value => value)] = value
end
end
|
57
58
59
|
# File 'lib/tagtical/tag.rb', line 57
def find_or_build_tags(tag_list, tag_types = [])
find_or_build_or_create_tags(:new, tag_list, tag_types)
end
|
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.
53
54
55
|
# File 'lib/tagtical/tag.rb', line 53
def find_or_create_tags(tag_list, tag_types = [])
find_or_build_or_create_tags(:create!, tag_list, tag_types)
end
|
.find_or_create_with_like_by_value!(value) ⇒ Object
47
48
49
|
# File 'lib/tagtical/tag.rb', line 47
def find_or_create_with_like_by_value!(value)
where_any_like(value).first || create!(:value => value)
end
|
.sti_name ⇒ Object
Save disc space by not having to put in “Tagtical::Tag” repeatedly
76
77
78
|
# File 'lib/tagtical/tag.rb', line 76
def sti_name
Tagtical::Tag==self ? nil : super
end
|
.using_postgresql? ⇒ Boolean
36
37
38
|
# File 'lib/tagtical/tag.rb', line 36
def using_postgresql?
connection.adapter_name=='PostgreSQL'
end
|
.where_any(list, options = {}) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/tagtical/tag.rb', line 27
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
41
42
43
|
# File 'lib/tagtical/tag.rb', line 41
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.
163
164
165
166
167
168
169
|
# File 'lib/tagtical/tag.rb', line 163
def <=>(tag)
if (r1 = relevance) && (r2 = tag.relevance)
r1 <=> r2
else
value <=> tag.value
end
end
|
#==(object) ⇒ Object
144
145
146
|
# File 'lib/tagtical/tag.rb', line 144
def ==(object)
super || (object.is_a?(self.class) && value == object.value)
end
|
#count ⇒ Object
181
182
183
|
# File 'lib/tagtical/tag.rb', line 181
def count
self[:count].to_i
end
|
#has_tagger? ⇒ Boolean
Carried over from tagging.
190
191
192
|
# File 'lib/tagtical/tag.rb', line 190
def has_tagger?
!self[:tagger_id].nil?
end
|
#inspect ⇒ Object
175
176
177
178
179
|
# File 'lib/tagtical/tag.rb', line 175
def inspect
super.tap do |str|
str[-1] = ", relevance: #{relevance}>" if relevance
end
end
|
#on_set_target(owner) ⇒ Object
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/tagtical/tag.rb', line 133
def on_set_target(owner)
if owner
self.clone.tap do |target|
target.relevance = owner.relevance
target.tagger_id = owner.tagger_id
end
else
self
end
end
|
#relevance ⇒ Object
Relevance is transferred through “taggings” join.
149
150
151
152
|
# File 'lib/tagtical/tag.rb', line 149
def relevance
v = has_attribute?(:relevance) ? self[:relevance] : @relevance
v && v.to_f
end
|
#relevance=(relevance) ⇒ Object
154
155
156
157
158
159
160
|
# File 'lib/tagtical/tag.rb', line 154
def relevance=(relevance)
if has_attribute?(:relevance)
self[:relevance] = relevance
else
@relevance = relevance
end
end
|
#respond_to?(method_id, include_private = false) ⇒ Boolean
185
186
187
|
# File 'lib/tagtical/tag.rb', line 185
def respond_to?(method_id, include_private = false)
!!self.class.send(:tag_types_for_questioner_method, method_id) || super
end
|
#to_s ⇒ Object
171
172
173
|
# File 'lib/tagtical/tag.rb', line 171
def to_s
value
end
|