Skip to content

Commit

Permalink
add method for finding or initializing by similar name
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgolick committed Dec 9, 2008
1 parent 1386a9e commit cdc79fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/tag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
class Tag < ActiveRecord::Base
class << self
def find_or_initialize_with_name_like(name)
with_name_like(name).first || new(:name => name)
end
end

has_many :taggings

validates_presence_of :name
validates_uniqueness_of :name

named_scope :with_name_like, lambda { |name| { :conditions => ["name like ?", name] } }
end
4 changes: 4 additions & 0 deletions test/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
t.valid?
t.errors[:name]
end

expect Tag.create!(:name => "iamawesome") do
Tag.find_or_initialize_with_name_like("iaMawesome")
end
end

0 comments on commit cdc79fb

Please sign in to comment.