Changeset 1135

Show
Ignore:
Timestamp:
08/21/07 22:15:09 (1 year ago)
Author:
alban
Message:

modification de la recherche de documents par tags, ainsi que dans les podcasts. Fixes #4

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/models/document.rb

    r1130 r1135  
    8686  end 
    8787   
     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   
    8898        protected        
    8999        def destroy_tags 
  • trunk/app/models/podcast.rb

    r1126 r1135  
    1515   
    1616  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
    1818  end 
    1919         
  • trunk/app/models/tag.rb

    r796 r1135  
    1111         
    1212        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           
    1321                tags = Array.new 
    1422                 
     
    3846          name 
    3947        end 
     48         
     49        def self.format(tags) 
     50          tags.join(" ") 
     51        end 
     52         
    4053end 
  • trunk/app/models/user.rb

    r1130 r1135  
    4848           
    4949          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) } 
    5256        end 
    5357         
    5458        if options[:keywords] 
    55           puts options[:keywords] 
    5659          all_subscriptions = all_subscriptions.delete_if do |s|  
    5760            !s.document.match?(options[:keywords]) 
     
    6568          all_subscriptions 
    6669        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         
    6798         
    6899        def find_subscription(id) 
  • trunk/test/fixtures/tags.yml

    r695 r1135  
    22first: 
    33  id: 1 
     4  name: first 
    45another: 
    56  id: 2 
     7  name: another 
     8   
  • trunk/test/unit/document_test.rb

    r562 r1135  
    1616  end 
    1717   
     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   
    1832  def test_truth 
    1933    assert_kind_of Document, Document.find(:first) 
  • trunk/test/unit/tag_test.rb

    r695 r1135  
    33class TagTest < Test::Unit::TestCase 
    44  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 
    527 
    628  # Replace this with your real tests.