Skip to content

Commit

Permalink
fix error handling for invalid characters in tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jomz committed May 25, 2009
1 parent 0f218a1 commit da68b9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions app/models/meta_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ class MetaTag < ActiveRecord::Base
:through => :taggings,
:dependent => :destroy,
:skip_duplicates => false

def after_save
# if you allow editable tag names, you might want before_save instead
self.name = name.downcase.strip.squeeze(" ")
end

def self.cloud(args = {})
find(:all, :select => 'meta_tags.*, count(*) as popularity',

class << self
def find_or_create_by_name!(name)
find_by_name(name) || create!(:name => name)
end

def cloud(args = {})
find(:all, :select => 'meta_tags.*, count(*) as popularity',
:limit => args[:limit] || 5,
:joins => "JOIN taggings ON taggings.meta_tag_id = meta_tags.id",
:conditions => args[:conditions],
:group => "meta_tags.id, meta_tags.name",
:order => "popularity DESC" )
end
end

def <=>(other)
Expand Down
6 changes: 4 additions & 2 deletions lib/tagging_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ def tag_with tags

tags.split(MetaTag::DELIMITER).each do |tag|
begin
MetaTag.find_or_create_by_name(tag.strip.squeeze(" ")).taggables << self
rescue ActiveRecord::StatementInvalid => e
MetaTag.find_or_create_by_name!(tag.strip.squeeze(" ")).taggables << self
rescue ActiveRecord::RecordInvalid => e
self.errors.add(:tags, e.record.errors.on(:name))
rescue ActiveRecord::StatementInvalid => e
# With SQLite3 - a duplicate tagging will result in the following message:
# SQLite3::SQLException: SQL logic error or missing database: INSERT INTO taggings ("meta_tag_id", "taggable_type", "taggable_id") VALUES(11, 'Page', 74)
# raise unless e.to_s =~ /duplicate/i
Expand Down

0 comments on commit da68b9d

Please sign in to comment.