Class: Tagtical::Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tagtical::Tag
- Defined in:
- lib/tagtical/tag.rb
Defined Under Namespace
Classes: Type
Class Method Summary collapse
- .define_scope_for_type(tag_type) ⇒ Object
-
.detect_comparable(objects, value) ⇒ Object
Checks to see if a tags value is present in a set of tags and returns that tag.
- .detect_possible_value(value) ⇒ Object
-
.find_or_create_tags(*tag_list) ⇒ Object
Method used to ensure list of tags for the given Tag class.
-
.find_or_create_with_like_by_value!(value) ⇒ Object
CLASS METHODS:.
-
.sti_name ⇒ Object
Save disc space by not having to put in “Tagtical::Tag” repeatedly.
- .using_postgresql? ⇒ Boolean
- .where_any(list, options = {}) ⇒ Object
-
.where_any_like(list, options = {}) ⇒ Object
Use this for case insensitive.
Instance Method Summary collapse
-
#<=>(tag) ⇒ Object
Try to sort by the relevance if provided.
-
#==(object) ⇒ Object
INSTANCE METHODS:.
- #count ⇒ Object
- #inspect ⇒ Object
- #relevance ⇒ Object
- #to_s ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/tagtical/tag.rb', line 137 def method_missing(method_name, *args, &block) if method_name[-1]=="?" && (types = Type.cache[method_name[0..-2]]) 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 (*tag_list) tag_list = [tag_list].flatten return {} if tag_list.empty? = where_any_like(tag_list).all tag_list.each_with_object({}) do |value, tag_lookup| tag_lookup[detect_comparable(, 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_name ⇒ Object
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
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, ={}) char = "%" if [:wildcard] operator = [:case_insensitive] || [: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, ={}) where_any(list, .update(:case_insensitive => true)) end |
Instance Method Details
#<=>(tag) ⇒ Object
Try to sort by the relevance if provided.
113 114 115 116 117 118 119 |
# File 'lib/tagtical/tag.rb', line 113 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 |
#count ⇒ Object
131 132 133 |
# File 'lib/tagtical/tag.rb', line 131 def count self[:count].to_i end |
#inspect ⇒ Object
125 126 127 128 129 |
# File 'lib/tagtical/tag.rb', line 125 def inspect super.tap do |str| str[-1] = ", relevance: #{relevance}>" if relevance end end |
#relevance ⇒ Object
108 109 110 |
# File 'lib/tagtical/tag.rb', line 108 def relevance (v = self[:relevance]) && v.to_f end |
#to_s ⇒ Object
121 122 123 |
# File 'lib/tagtical/tag.rb', line 121 def to_s value end |