Skip to content

Instantly share code, notes, and snippets.

@dpmcnevin
Created November 3, 2010 23:52
Show Gist options
  • Save dpmcnevin/661909 to your computer and use it in GitHub Desktop.
Save dpmcnevin/661909 to your computer and use it in GitHub Desktop.
SQL (0.1ms) BEGIN
Issue Load (0.1ms) SELECT `issues`.* FROM `issues` WHERE (`issues`.`resolution` IS NULL) AND (`issues`.`description` = '') AND (`issues`.`name` = 'something') ORDER BY start_at DESC LIMIT 1
SQL (0.2ms) SELECT COUNT(*) AS count_id FROM `comments`
SQL (18.6ms) INSERT INTO `comments` (`body`, `commentable_id`, `commentable_type`, `created_at`, `updated_at`, `user_id`) VALUES ('Duplicate attempted to open', 3370, 'Issue', '2010-11-03 14:36:12', '2010-11-03 14:36:12', 1)
SQL (47.5ms) ROLLBACK
class Issue < ActiveRecord::Base
attr_accessor :duplicate_issue
has_many :comments
validate :check_for_open_duplicate, :on => :create
after_rollback :add_duplicate_comment
scope :open, where(:resolution => nil)
protected
def check_for_open_duplicate
open_issue = Issue.open.where(:name => name, :description => description).first
if open_issue
@duplicate_issue = open_issue
errors.add(:duplicate, "Duplicate Issue")
end
end
def add_duplicate_comment
@duplicate_issue.comments.create(:body => "Duplicate attempted to open") if @duplicate_issue
end
end
class Issue < ActiveRecord::Base
has_many :comments
validate :check_for_open_duplicate, :on => :create
scope :open, where(:resolution => nil)
protected
def check_for_open_duplicate
open_issue = Issue.open.where(:name => name, :description => description).first
if open_issue
open_issue.comments.create(:body => "Duplicate attempted to open")
errors.add(:duplicate, "Duplicate Issue")
end
end
end
context "don't allow duplicate issues" do
setup do
@issue = Issue.create(:name => "something", :start_at => "1/1/2010")
end
should "not create a duplicate issue" do
assert_no_difference('Issue.count') do
another_issue = Issue.create(:name => "something", :start_at => "1/1/2010")
end
end
should "create a new issue if the duplicate issue is closed" do
@issue.update_attributes(:resolution => "something")
assert_difference('Issue.count') do
another_issue = Issue.create(:name => "something", :start_at => "1/1/2010")
end
end
should "add a comment on existing issue" do
assert_difference('@issue.comments.count') do
another_issue = Issue.create(:name => "something", :start_at => "1/1/2010")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment