taggable 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,9 @@
1
+ = taggable
2
+
3
+ Tagging for MongoMapper
4
+
5
+ = installation
6
+
7
+ The versioned gem is hosted on gemcutter.org:
8
+
9
+ * gem install taggable
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |g|
9
+ g.name = 'taggable'
10
+ g.summary = %(Tagging for MongoMapper)
11
+ g.description = %(Tagging for MongoMapper)
12
+ g.email = 'signalstatic@gmail.com'
13
+ g.homepage = 'http://github.com/twoism/taggable'
14
+ g.authors = %w(twoism)
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts 'Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com'
19
+ end
20
+
21
+ Rake::TestTask.new do |t|
22
+ t.libs = %w(test)
23
+ t.pattern = 'test/**/*_test.rb'
24
+ end
25
+
26
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/tag.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Tag
2
+ include MongoMapper::Document
3
+ belongs_to :taggable, :polymorphic => true
4
+ key :taggable_type, String
5
+ key :taggable_id, ObjectId
6
+ key :name, String, :required => true
7
+ end
data/lib/taggable.rb ADDED
@@ -0,0 +1,25 @@
1
+ module Taggable
2
+ module InstanceMethods
3
+ def tag_list
4
+ tags.collect(&:name).join(",")
5
+ end
6
+
7
+ def tag_list=(tag_str)
8
+ self.tags = []
9
+ split_tags.collect {|n| self.tags << Tag.find_or_create_by_name(n)}
10
+ end
11
+ end
12
+
13
+ protected
14
+ def split_tags tag_str
15
+ return [] if tag_str.empty?
16
+ tag_str.split(",")
17
+ end
18
+
19
+ def self.included klass
20
+ klass.class_eval do
21
+ many :tags, :as => :taggable
22
+ include InstanceMethods
23
+ end
24
+ end
25
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,12 @@
1
+ MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
2
+ MongoMapper.database = "testing_versioned"
3
+
4
+ class Doc
5
+ include MongoMapper::Document
6
+ include Taggable
7
+ key :name, String, :required => true
8
+ timestamps!
9
+ end
10
+
11
+ Doc.destroy_all
12
+ Tag.destroy_all
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class TagAssignmentTest < Test::Unit::TestCase
4
+ context 'A taggable document' do
5
+
6
+ setup do
7
+ @tag = Tag.create(:name=>"Hoge")
8
+ @tag2 = Tag.create(:name=>"Fuga")
9
+ @doc = Doc.create(:name=>"Piyo")
10
+ @doc.tags << @tag
11
+ end
12
+
13
+ should "contain the tag" do
14
+ assert_contains @doc.tags, @tag
15
+ end
16
+
17
+ should "have the proper tag list" do
18
+ assert_equal("Hoge,Fuga", @doc.tag_list)
19
+ end
20
+
21
+ should "find doc by tag" do
22
+ assert_contains @doc, Doc.find(:conditions=>{"tags.name"=>/hoge/i})
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class TagCreationTest < Test::Unit::TestCase
4
+ context 'A tag instance' do
5
+ setup do
6
+ @tag = Tag.create(:name=>"Hoge")
7
+ @bad_tag = Tag.create()
8
+ end
9
+
10
+ should "be a tag" do
11
+ assert @tag.is_a?(Tag)
12
+ end
13
+
14
+ should "be valid" do
15
+ assert @tag.valid?
16
+ end
17
+
18
+ should "require name" do
19
+ assert_not_nil @bad_tag.errors[:name]
20
+ end
21
+
22
+ should "belong to taggable" do
23
+ assert_not_nil Tag.associations["taggable"]
24
+ assert_equal :belongs_to, Tag.associations["taggable"].type
25
+ end
26
+
27
+ should "be polymorph" do
28
+ assert Tag.associations["taggable"].options[:polymorphic]
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class TaggableTest < Test::Unit::TestCase
4
+ context 'A taggable instance' do
5
+ setup do
6
+ @doc = Doc.create(:name=>"Hoge")
7
+ @tag_str = "hoge, fuga, piyo"
8
+ end
9
+
10
+ should "have many tags" do
11
+ assert_not_nil Tag.associations["tags"]
12
+ assert_equal :many, Tag.associations["tags"].type
13
+ end
14
+
15
+ should "respond to" do
16
+ [:tag_list,:tag_list=].each {|m| assert @doc.respond_to?(m) }
17
+ end
18
+
19
+ should "return []" do
20
+ assert_equal [], @doc.split_tags("")
21
+ end
22
+
23
+ should "return proper array" do
24
+ assert_equal ["hoge","fuga","piyo"], @doc.split_tags(@tag_str)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.dirname(__FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ require 'mongo_mapper'
8
+ require 'taggable'
9
+ require 'tag'
10
+ require 'schema'
11
+ begin; require 'redgreen'; rescue LoadError; end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taggable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - twoism
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-18 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Tagging for MongoMapper
17
+ email: signalstatic@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - Rakefile
27
+ - VERSION
28
+ - lib/tag.rb
29
+ - lib/taggable.rb
30
+ - test/schema.rb
31
+ - test/tag_assignment_test.rb
32
+ - test/tag_creation_test.rb
33
+ - test/taggable_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/twoism/taggable
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Tagging for MongoMapper
63
+ test_files:
64
+ - test/schema.rb
65
+ - test/tag_assignment_test.rb
66
+ - test/tag_creation_test.rb
67
+ - test/taggable_test.rb
68
+ - test/test_helper.rb