Class: MakeTaggable::Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MakeTaggable::Tag
- Defined in:
- lib/make_taggable/tag.rb
Class Method Summary collapse
- .find_or_create_all_with_like_by_name(*list) ⇒ Object
-
.find_or_create_with_like_by_name(name) ⇒ Object
CLASS METHODS:.
- .for_context(context) ⇒ Object
- .named(name) ⇒ Object
- .named_any(list) ⇒ Object
- .named_like(name) ⇒ Object
- .named_like_any(list) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
INSTANCE METHODS:.
- #count ⇒ Object
- #to_s ⇒ Object
-
#validates_name_uniqueness? ⇒ Boolean
Monkey patch this method if don’t need name uniqueness validation.
Class Method Details
.find_or_create_all_with_like_by_name(*list) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/make_taggable/tag.rb', line 64 def self.find_or_create_all_with_like_by_name(*list) list = Array(list).flatten return [] if list.empty? = named_any(list) list.map do |tag_name| tries ||= 3 comparable_tag_name = comparable_name(tag_name) existing_tag = .find { |tag| comparable_name(tag.name) == comparable_tag_name } existing_tag || create(name: tag_name) rescue ActiveRecord::RecordNotUnique if (tries -= 1).positive? ActiveRecord::Base.connection.execute "ROLLBACK" = named_any(list) retry end raise DuplicateTagError.new("'#{tag_name}' has already been taken") end end |
.find_or_create_with_like_by_name(name) ⇒ Object
CLASS METHODS:
56 57 58 59 60 61 62 |
# File 'lib/make_taggable/tag.rb', line 56 def self.find_or_create_with_like_by_name(name) if MakeTaggable.strict_case_match find_or_create_all_with_like_by_name([name]).first else named_like(name).first || create(name: name) end end |
.for_context(context) ⇒ Object
49 50 51 52 53 |
# File 'lib/make_taggable/tag.rb', line 49 def self.for_context(context) joins(:taggings) .where(["#{MakeTaggable.taggings_table}.context = ?", context]) .select("DISTINCT #{MakeTaggable.}.*") end |
.named(name) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/make_taggable/tag.rb', line 22 def self.named(name) if MakeTaggable.strict_case_match where(["name = #{binary}?", as_8bit_ascii(name)]) else where(["LOWER(name) = LOWER(?)", as_8bit_ascii(unicode_downcase(name))]) end end |
.named_any(list) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/make_taggable/tag.rb', line 30 def self.named_any(list) clause = list.map { |tag| sanitize_sql_for_named_any(tag).force_encoding("BINARY") }.join(" OR ") where(clause) end |
.named_like(name) ⇒ Object
37 38 39 40 |
# File 'lib/make_taggable/tag.rb', line 37 def self.named_like(name) clause = ["name #{MakeTaggable::Utils.like_operator} ? ESCAPE '!'", "%#{MakeTaggable::Utils.escape_like(name)}%"] where(clause) end |
.named_like_any(list) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/make_taggable/tag.rb', line 42 def self.named_like_any(list) clause = list.map { |tag| sanitize_sql(["name #{MakeTaggable::Utils.like_operator} ? ESCAPE '!'", "%#{MakeTaggable::Utils.escape_like(tag.to_s)}%"]) }.join(" OR ") where(clause) end |
Instance Method Details
#==(other) ⇒ Object
INSTANCE METHODS:
87 88 89 |
# File 'lib/make_taggable/tag.rb', line 87 def ==(other) super || (other.is_a?(Tag) && name == other.name) end |
#count ⇒ Object
95 96 97 |
# File 'lib/make_taggable/tag.rb', line 95 def count read_attribute(:count).to_i end |
#to_s ⇒ Object
91 92 93 |
# File 'lib/make_taggable/tag.rb', line 91 def to_s name end |
#validates_name_uniqueness? ⇒ Boolean
Monkey patch this method if don’t need name uniqueness validation
14 15 16 |
# File 'lib/make_taggable/tag.rb', line 14 def validates_name_uniqueness? true end |