From 2ca90537aed834121341c74bd41d76d5976e8726 Mon Sep 17 00:00:00 2001 From: Matteo Giaccone Date: Fri, 19 Dec 2014 19:09:37 +0000 Subject: [PATCH] Fix #1724 --- app/models/todo.rb | 2 +- test/models/todo_test.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models/todo.rb b/app/models/todo.rb index e40139aa..88178891 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -45,7 +45,7 @@ class Todo < ActiveRecord::Base # other scopes scope :are_due, -> { where 'NOT (todos.due IS NULL)' } scope :due_today, -> { where("todos.due <= ?", Time.zone.now) } - scope :with_tag, lambda { |tag_id| joins("INNER JOIN taggings ON todos.id = taggings.taggable_id").where("taggings.tag_id = ? ", tag_id) } + scope :with_tag, lambda { |tag_id| joins("INNER JOIN taggings ON todos.id = taggings.taggable_id").where("taggings.tag_id = ? AND taggings.taggable_type='Todo'", tag_id) } scope :with_tags, lambda { |tag_ids| where("EXISTS(SELECT * from taggings t WHERE t.tag_id IN (?) AND t.taggable_id=todos.id AND t.taggable_type='Todo')", tag_ids) } scope :completed_after, lambda { |date| where("todos.completed_at > ?", date) } scope :completed_before, lambda { |date| where("todos.completed_at < ?", date) } diff --git a/test/models/todo_test.rb b/test/models/todo_test.rb index 72e19cb0..4662a283 100644 --- a/test/models/todo_test.rb +++ b/test/models/todo_test.rb @@ -471,6 +471,19 @@ class TodoTest < ActiveSupport::TestCase assert_equal 2, todos_with_aORc_and_b.count end + def test_find_tagged_todos_of_correct_taggable_type + recurring = Todo.where(:recurring_todo_id => 1).first.reload + recurring.tag_list = "recurring_tag" + recurring.save! + recurring.recurring_todo.tag_list = "recurring_tag" + recurring.recurring_todo.save! + + tag_id = Tag.where(:name => "recurring_tag").first.id + tagged_todos = Todo.with_tag(tag_id) + assert_equal 1, tagged_todos.count + assert_equal recurring.id, tagged_todos.first.id + end + # test named_scopes def test_find_completed # Given 2 completed todos, one completed now and one completed 2 months ago