first pass at restoring tag functionality

This commit is contained in:
Reinier Balt 2008-12-22 20:55:32 +01:00
parent c0dbabf226
commit c618d35d3a
8 changed files with 1943 additions and 1942 deletions

View file

@ -175,7 +175,7 @@ class ApplicationController < ActionController::Base
saved = todo.save saved = todo.save
if saved if saved
todo.tag_with(rt.tag_list, current_user) todo.tag_with(rt.tag_list)
todo.tags.reload todo.tags.reload
end end

View file

@ -30,7 +30,7 @@ class RecurringTodosController < ApplicationController
end end
def update def update
@recurring_todo.tag_with(params[:tag_list], current_user) if params[:tag_list] @recurring_todo.tag_with(params[:tag_list]) if params[:tag_list]
@original_item_context_id = @recurring_todo.context_id @original_item_context_id = @recurring_todo.context_id
@original_item_project_id = @recurring_todo.project_id @original_item_project_id = @recurring_todo.project_id
@ -106,7 +106,7 @@ class RecurringTodosController < ApplicationController
@recurring_saved = @recurring_todo.save @recurring_saved = @recurring_todo.save
unless (@recurring_saved == false) || p.tag_list.blank? unless (@recurring_saved == false) || p.tag_list.blank?
@recurring_todo.tag_with(p.tag_list, current_user) @recurring_todo.tag_with(p.tag_list)
@recurring_todo.tags.reload @recurring_todo.tags.reload
end end

View file

@ -67,7 +67,7 @@ class TodosController < ApplicationController
@saved = @todo.save @saved = @todo.save
unless (@saved == false) || p.tag_list.blank? unless (@saved == false) || p.tag_list.blank?
@todo.tag_with(p.tag_list, current_user) @todo.tag_with(p.tag_list)
@todo.tags.reload @todo.tags.reload
end end
@ -175,7 +175,7 @@ class TodosController < ApplicationController
def update def update
@source_view = params['_source_view'] || 'todo' @source_view = params['_source_view'] || 'todo'
init_data_for_sidebar unless mobile? init_data_for_sidebar unless mobile?
@todo.tag_with(params[:tag_list], current_user) if params[:tag_list] @todo.tag_with(params[:tag_list]) if params[:tag_list]
@original_item_context_id = @todo.context_id @original_item_context_id = @todo.context_id
@original_item_project_id = @todo.project_id @original_item_project_id = @todo.project_id
@original_item_was_deferred = @todo.deferred? @original_item_was_deferred = @todo.deferred?

View file

@ -623,10 +623,10 @@ class RecurringTodo < ActiveRecord::Base
def toggle_star! def toggle_star!
if starred? if starred?
delete_tags Todo::STARRED_TAG_NAME _remove_tags Todo::STARRED_TAG_NAME
tags.reload tags.reload
else else
add_tag Todo::STARRED_TAG_NAME _add_tags(Todo::STARRED_TAG_NAME)
tags.reload tags.reload
end end
starred? starred?

View file

@ -118,10 +118,10 @@ class Todo < ActiveRecord::Base
def toggle_star! def toggle_star!
if starred? if starred?
delete_tags STARRED_TAG_NAME _remove_tags STARRED_TAG_NAME
tags.reload tags.reload
else else
add_tag STARRED_TAG_NAME _add_tags(STARRED_TAG_NAME)
tags.reload tags.reload
end end
starred? starred?

View file

@ -7,11 +7,11 @@ class ActiveRecord::Base #:nodoc:
# Add tags to <tt>self</tt>. Accepts a string of tagnames, an array of tagnames, an array of ids, or an array of Tags. # Add tags to <tt>self</tt>. Accepts a string of tagnames, an array of tagnames, an array of ids, or an array of Tags.
# #
# We need to avoid name conflicts with the built-in ActiveRecord association methods, thus the underscores. # We need to avoid name conflicts with the built-in ActiveRecord association methods, thus the underscores.
def _add_tags incoming, user def _add_tags incoming
taggable?(true) taggable?(true)
tag_cast_to_string(incoming).each do |tag_name| tag_cast_to_string(incoming).each do |tag_name|
begin begin
tag = Tag.find_or_create_by_name(tag_name).on(self,user) tag = Tag.find_or_create_by_name(tag_name)
raise Tag::Error, "tag could not be saved: #{tag_name}" if tag.new_record? raise Tag::Error, "tag could not be saved: #{tag_name}" if tag.new_record?
tag.taggables << self tag.taggables << self
rescue ActiveRecord::StatementInvalid => e rescue ActiveRecord::StatementInvalid => e
@ -21,7 +21,7 @@ class ActiveRecord::Base #:nodoc:
end end
# Removes tags from <tt>self</tt>. Accepts a string of tagnames, an array of tagnames, an array of ids, or an array of Tags. # Removes tags from <tt>self</tt>. Accepts a string of tagnames, an array of tagnames, an array of ids, or an array of Tags.
def _remove_tags outgoing, user def _remove_tags outgoing
taggable?(true) taggable?(true)
outgoing = tag_cast_to_string(outgoing) outgoing = tag_cast_to_string(outgoing)
@ -36,7 +36,7 @@ class ActiveRecord::Base #:nodoc:
end end
# Replace the existing tags on <tt>self</tt>. Accepts a string of tagnames, an array of tagnames, an array of ids, or an array of Tags. # Replace the existing tags on <tt>self</tt>. Accepts a string of tagnames, an array of tagnames, an array of ids, or an array of Tags.
def tag_with list, user def tag_with list
#:stopdoc: #:stopdoc:
taggable?(true) taggable?(true)
list = tag_cast_to_string(list) list = tag_cast_to_string(list)
@ -44,8 +44,8 @@ class ActiveRecord::Base #:nodoc:
# Transactions may not be ideal for you here; be aware. # Transactions may not be ideal for you here; be aware.
Tag.transaction do Tag.transaction do
current = tags.map(&:name) current = tags.map(&:name)
_add_tags(list - current, user) _add_tags(list - current)
_remove_tags(current - list, user) _remove_tags(current - list)
end end
self self
@ -78,6 +78,7 @@ class ActiveRecord::Base #:nodoc:
when String when String
obj = obj.split(Tag::DELIMITER).map do |tag_name| obj = obj.split(Tag::DELIMITER).map do |tag_name|
tag_name.strip.squeeze(" ") tag_name.strip.squeeze(" ")
puts "tn=#{tag_name.strip.squeeze(" ")}"
end end
else else
raise "Invalid object of class #{obj.class} as tagging method parameter" raise "Invalid object of class #{obj.class} as tagging method parameter"

View file

@ -251,7 +251,7 @@ class RecurringTodoTest < Test::Rails::TestCase
end end
def test_starred def test_starred
@yearly.tag_with("1, 2, starred", User.find(@yearly.user_id)) @yearly.tag_with("1, 2, starred")
@yearly.tags.reload @yearly.tags.reload
assert_equal true, @yearly.starred? assert_equal true, @yearly.starred?

View file

@ -163,7 +163,7 @@ class TodoTest < Test::Rails::TestCase
end end
def test_todo_is_starred_after_starred_tag_is_added def test_todo_is_starred_after_starred_tag_is_added
@not_completed1.add_tag('starred') @not_completed1._add_tags('starred')
assert @not_completed1.starred? assert @not_completed1.starred?
end end