Changeset 1135
- Timestamp:
- 08/21/07 22:15:09 (1 year ago)
- Files:
-
- trunk/app/models/document.rb (modified) (1 diff)
- trunk/app/models/podcast.rb (modified) (1 diff)
- trunk/app/models/tag.rb (modified) (2 diffs)
- trunk/app/models/user.rb (modified) (2 diffs)
- trunk/test/fixtures/tags.yml (modified) (1 diff)
- trunk/test/unit/document_test.rb (modified) (1 diff)
- trunk/test/unit/tag_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/app/models/document.rb
r1130 r1135 86 86 end 87 87 88 def match_tags?(tags) 89 tags = [ tags ] unless tags.is_a? Array 90 91 for tag in tags do 92 return false unless self.tags.include?(tag) 93 end 94 95 true 96 end 97 88 98 protected 89 99 def destroy_tags trunk/app/models/podcast.rb
r1126 r1135 15 15 16 16 def documents 17 Document.find_by_sql(["SELECT documents.* FROM documents, tags, podcasts_tags, documents_tags WHERE documents.id = documents_tags.document_id AND tags.id = documents_tags.tag_id AND podcasts_tags.tag_id = tags.id AND documents.id IN (SELECT document_id FROM casts) AND podcasts_tags.podcast_id = ? AND documents.author_id = ? ORDER BY documents.updated_at DESC", self.id, self.author_id])17 self.author.find_documents(:tags => self.tags) 18 18 end 19 19 trunk/app/models/tag.rb
r796 r1135 11 11 12 12 def self.parse(list) 13 if list.is_a? Tag 14 return [ list ] 15 end 16 17 if list.is_a? Array 18 return list.collect { |item| Tag.parse(item) }.flatten 19 end 20 13 21 tags = Array.new 14 22 … … 38 46 name 39 47 end 48 49 def self.format(tags) 50 tags.join(" ") 51 end 52 40 53 end trunk/app/models/user.rb
r1130 r1135 48 48 49 49 if options[:tag] 50 tag = Tag.find_by_name(options[:tag]) 51 all_subscriptions = all_subscriptions.delete_if { |s| ! s.document.tags.include?(tag) } 50 options[:tags] = Tag.parse(options[:tag]) 51 end 52 53 if options[:tags] 54 tags = Tag.parse(options[:tags]) 55 all_subscriptions = all_subscriptions.delete_if { |s| ! s.document.match_tags?(tags) } 52 56 end 53 57 54 58 if options[:keywords] 55 puts options[:keywords]56 59 all_subscriptions = all_subscriptions.delete_if do |s| 57 60 !s.document.match?(options[:keywords]) … … 65 68 all_subscriptions 66 69 end 70 71 def find_documents(options = Hash.new) 72 documents = self.documents.find(:all) 73 74 if options[:tag] 75 options[:tags] = Tag.parse(options[:tag]) 76 end 77 78 if options[:tags] 79 tags = Tag.parse(options[:tags]) 80 documents = documents.delete_if do |document| 81 !document.match_tags?(tags) 82 end 83 end 84 85 if options[:keywords] 86 documents = documents.delete_if do |document| 87 !document.match?(options[:keywords]) 88 end 89 end 90 91 if options[:offset] and options[:limit] 92 documents.slice!(options[:offset], options[:limit]) 93 end 94 95 documents 96 end 97 67 98 68 99 def find_subscription(id) trunk/test/fixtures/tags.yml
r695 r1135 2 2 first: 3 3 id: 1 4 name: first 4 5 another: 5 6 id: 2 7 name: another 8 trunk/test/unit/document_test.rb
r562 r1135 16 16 end 17 17 18 def test_match_tags 19 document = documents(:oscar) 20 document.tag_with("tag1 tag2") 21 22 for tag in document.tags 23 assert document.match_tags?(tag) 24 end 25 assert document.match_tags?(document.tags) 26 27 unknown_tag = Tag.find_or_create_by_name("unknown") 28 assert ! document.match_tags?(unknown_tag) 29 assert ! document.match_tags?(document.tags + [ unknown_tag ]) 30 end 31 18 32 def test_truth 19 33 assert_kind_of Document, Document.find(:first) trunk/test/unit/tag_test.rb
r695 r1135 3 3 class TagTest < Test::Unit::TestCase 4 4 fixtures :tags 5 6 def test_parse 7 first = tags(:first) 8 another = tags(:another) 9 10 tags = [first, another] 11 12 assert_equal [first], Tag.parse(first.to_s) 13 assert_equal tags, Tag.parse(Tag.format(tags)) 14 15 assert_equal [first], Tag.parse(first) 16 assert_equal tags, Tag.parse(tags) 17 assert_equal tags, Tag.parse([ first.to_s, another.to_s ]) 18 end 19 20 def to_s 21 first = tags(:first) 22 another = tags(:another) 23 24 list = Tag.format([first, another]) 25 assert_equal "first another", list 26 end 5 27 6 28 # Replace this with your real tests.
