dm-tags 0.0.2

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/spec/classes.rb ADDED
@@ -0,0 +1,25 @@
1
+ class TaggedModel
2
+ include DataMapper::Resource
3
+ property :id, Integer, :serial => true
4
+
5
+ has_tags_on :skills, :interests, :tags
6
+ end
7
+
8
+ class AnotherTaggedModel
9
+ include DataMapper::Resource
10
+ property :id, Integer, :serial => true
11
+
12
+ has_tags_on :skills, :pets
13
+ end
14
+
15
+ class DefaultTaggedModel
16
+ include DataMapper::Resource
17
+ property :id, Integer, :serial => true
18
+
19
+ has_tags
20
+ end
21
+
22
+ class UntaggedModel
23
+ include DataMapper::Resource
24
+ property :id, Integer, :serial => true
25
+ end
data/spec/dm-setup.rb ADDED
@@ -0,0 +1,2 @@
1
+ DataMapper.setup(:default, 'sqlite3::memory:')
2
+ DataMapper.auto_migrate!
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe DataMapper::Tags do
4
+ it "should add a .has_tags method to models which include DataMapper::Resource" do
5
+ TaggedModel.should respond_to(:has_tags)
6
+ AnotherTaggedModel.should respond_to(:has_tags)
7
+ DefaultTaggedModel.should respond_to(:has_tags)
8
+ UntaggedModel.should respond_to(:has_tags)
9
+ end
10
+
11
+ it "should add a .has_tags_on method to models which include DataMapper::Resource" do
12
+ TaggedModel.should respond_to(:has_tags_on)
13
+ AnotherTaggedModel.should respond_to(:has_tags_on)
14
+ DefaultTaggedModel.should respond_to(:has_tags_on)
15
+ UntaggedModel.should respond_to(:has_tags_on)
16
+ end
17
+
18
+ describe ".has_tags_on" do
19
+ it "should accept an array of context names" do
20
+ class HasTagsOnTestModel
21
+ include DataMapper::Resource
22
+ property :id, Integer, :serial => true
23
+ end
24
+ lambda{HasTagsOnTestModel.has_tags_on(:should, 'not', :raise)}.should_not raise_error(ArgumentError)
25
+ end
26
+
27
+ it "should create taggable functionality for each of the context names passed" do
28
+ class TestModel
29
+ include DataMapper::Resource
30
+ property :id, Integer, :serial => true
31
+
32
+ has_tags_on(:pets, 'skills', :tags)
33
+ end
34
+ TestModel.should be_taggable
35
+ a = TestModel.new
36
+ a.should be_taggable
37
+ a.should respond_to(:pet_list)
38
+ a.should respond_to(:skill_list)
39
+ a.should respond_to(:tag_list)
40
+ a.should respond_to(:pet_list=)
41
+ a.should respond_to(:skill_list=)
42
+ a.should respond_to(:tag_list=)
43
+ end
44
+ end
45
+
46
+ describe ".has_tags" do
47
+ it "should create a taggable with 'tags' context regardless of passed arguments" do
48
+ class TagsOnly
49
+ include DataMapper::Resource
50
+ property :id, Integer, :serial => true
51
+ has_tags :pets, :skills
52
+ end
53
+ TagsOnly.should be_taggable
54
+ TagsOnly.new.should be_taggable
55
+ a = TagsOnly.new
56
+ a.should respond_to(:tag_list)
57
+ a.should respond_to(:tag_list=)
58
+ a.should respond_to(:tags)
59
+ a.should_not respond_to(:pet_list)
60
+ a.should_not respond_to(:pet_list=)
61
+ a.should_not respond_to(:pets)
62
+ a.should_not respond_to(:skill_list)
63
+ a.should_not respond_to(:skill_list=)
64
+ a.should_not respond_to(:skills)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe Tag do
4
+ before(:each) do
5
+ @tag = Tag.new
6
+ end
7
+
8
+ it "should have id and name properties" do
9
+ @tag.attributes.should have_key(:id)
10
+ @tag.attributes.should have_key(:name)
11
+ end
12
+
13
+ it "should have many Taggings" do
14
+ Tag.relationships.should have_key(:taggings)
15
+ end
16
+
17
+ it "should validate the presence of name" do
18
+ @tag.should_not be_valid
19
+ @tag.name = "Meme"
20
+ @tag.should be_valid
21
+ end
22
+ end
@@ -0,0 +1,97 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "Taggable" do
4
+ before(:each) do
5
+ TaggedModel.all.destroy!
6
+ AnotherTaggedModel.all.destroy!
7
+ DefaultTaggedModel.all.destroy!
8
+ UntaggedModel.all.destroy!
9
+ Tag.all.destroy!
10
+ @taggable = DefaultTaggedModel.new
11
+ end
12
+
13
+ it "should have an id property" do
14
+ @taggable.attributes.should have_key(:id)
15
+ end
16
+
17
+ it "should return an alphabetically sorted array of the tag names when sent #tag_list" do
18
+ tag1 = Tag.create!(:name => 'tag1')
19
+ tag2 = Tag.create!(:name => 'tag2')
20
+ tag3 = Tag.create!(:name => 'tag3')
21
+ @taggable.tag_taggings << Tagging.new(:tag => tag1, :taggable_type => DefaultTaggedModel.to_s)
22
+ @taggable.tag_taggings << Tagging.new(:tag => tag2, :taggable_type => DefaultTaggedModel.to_s)
23
+ @taggable.tag_taggings << Tagging.new(:tag => tag3, :taggable_type => DefaultTaggedModel.to_s)
24
+ @taggable.save.should be_true
25
+ @taggable = DefaultTaggedModel.get!(@taggable.id)
26
+ @taggable.tag_list.should == ['tag1', 'tag2', 'tag3']
27
+ end
28
+
29
+ it "should set the tag list to a sanitized, stripped, alphabetized, unique array of tag names" do
30
+ @taggable.tag_list = "tags, !$%^&* !@more-stuff' &, tags , me_again9, et tu " # Must check for redundancy
31
+ valid_array = ["et tu", "me_again9", "more-stuff", "tags"]
32
+ @taggable.tag_list.should == valid_array
33
+ @taggable.instance_variable_get(:@tag_list).should == valid_array
34
+ @taggable.save
35
+ @taggable = DefaultTaggedModel.first
36
+ @taggable.tag_list.should == valid_array
37
+ end
38
+
39
+ it "should set the associated collection of tags to those whose names
40
+ are in the tag list upon saving, creating and deleting as necessary" do
41
+ tag1 = Tag.create!(:name => 'tag1')
42
+ tag2 = Tag.create!(:name => 'tag2')
43
+ tag3 = Tag.create!(:name => 'tag3')
44
+ @taggable = TaggedModel.new
45
+ @taggable.tag_list = 'tag1, tag2, tag3'
46
+ @taggable.save.should be_true
47
+ @taggable.tags.sort_by{|tag| tag.id}.should == [tag1, tag2, tag3]
48
+ @taggable.tag_list = 'tag1, tag2'
49
+ @taggable.save.should be_true # Should dirty the model when changed.
50
+ @taggable.tags.sort_by{|tag| tag.id}.should == [tag1, tag2]
51
+ @taggable.tag_list = 'tag3, tag4'
52
+ @taggable.save.should be_true
53
+ @taggable = TaggedModel.first
54
+ @taggable.tags.sort_by{|tag| tag.id}.should == [tag3, Tag.first(:name => 'tag4')]
55
+ @taggable.skills.sort_by{|skill| skill.id}.should_not == [tag3, Tag.first(:name => 'tag4')]
56
+ end
57
+
58
+ describe ".tagged_with" do
59
+ it "should have a class method .tagged_with" do
60
+ DefaultTaggedModel.should respond_to(:tagged_with)
61
+ UntaggedModel.should_not respond_to(:tagged_with)
62
+ end
63
+
64
+ it "should return taggables tagged with the name given in the first argument" do
65
+ @taggable.tag_list = 'tag1, tag2, tag3'
66
+ @taggable.save
67
+ taggable = DefaultTaggedModel.new
68
+ taggable.tag_list = 'tag1, goat, fish'
69
+ taggable.save
70
+ DefaultTaggedModel.tagged_with('tag1').sort_by{|t| t.id}.to_a.should == [@taggable, taggable]
71
+ end
72
+
73
+ it "should return taggables of the context specified by the second argument" do
74
+ taggable1 = TaggedModel.new
75
+ taggable2 = TaggedModel.new
76
+ taggable1.tag_list = 'tag1, tag2, tag3'
77
+ taggable2.skill_list = 'tag1, skill4'
78
+ taggable1.save
79
+ taggable2.save
80
+ TaggedModel.tagged_with('tag1').should == [taggable1, taggable2]
81
+ TaggedModel.tagged_with('tag1', :on => 'skills').should == [taggable2]
82
+ TaggedModel.tagged_with('tag1', :on => 'tags').should == [taggable1]
83
+ end
84
+ end
85
+
86
+ it "should have a class method .taggable? which returns true if tagging is defined, and false otherwise" do
87
+ UntaggedModel.taggable?.should be_false
88
+ TaggedModel.taggable?.should be_true
89
+ end
90
+
91
+ it "should have an instance method #taggable? which returns the same as the instance's class would" do
92
+ UntaggedModel.new.taggable?.should == UntaggedModel.taggable?
93
+ UntaggedModel.new.taggable?.should be_false
94
+ TaggedModel.new.taggable?.should == TaggedModel.taggable?
95
+ TaggedModel.new.taggable?.should be_true
96
+ end
97
+ end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+ include DataMapper::Tags
3
+
4
+ describe Tagging do
5
+ before(:each) do
6
+ @tagging = Tagging.new
7
+ end
8
+
9
+ it "should be a model which includes DataMapper::Resource" do
10
+ Tagging.should be
11
+ Tagging.should include(DataMapper::Resource)
12
+ end
13
+
14
+ it "should have properties: id, tag_id, taggable_id, taggable_type, tagger_id, tagger_type, and tag_context" do
15
+ @tagging.attributes.should have_key(:id)
16
+ @tagging.attributes.should have_key(:tag_id)
17
+ @tagging.attributes.should have_key(:taggable_id)
18
+ @tagging.attributes.should have_key(:taggable_type)
19
+ # @tagging.attributes.should have_key(:tagger_id)
20
+ # @tagging.attributes.should have_key(:tagger_type)
21
+ @tagging.attributes.should have_key(:tag_context)
22
+ end
23
+
24
+ it "should validate the presence of tag_id, taggable_id, taggable_type and tag_context" do
25
+ @tagging.should_not be_valid
26
+ @tagging.tag_id = 1
27
+ @tagging.should_not be_valid
28
+ @tagging.taggable_id = 1
29
+ @tagging.should_not be_valid
30
+ @tagging.taggable_type = "TaggedModel"
31
+ @tagging.should_not be_valid
32
+ @tagging.tag_context = "skills"
33
+ @tagging.should be_valid
34
+ end
35
+
36
+ it "should belong_to tag" do
37
+ Tagging.relationships[:tag].should be
38
+ Tagging.relationships[:tag].parent_model.should == Tag
39
+ end
40
+
41
+ it "should have a method Tagging#taggable which returns the associated taggable instance" do
42
+ @tagging.should respond_to(:taggable)
43
+ @tagging.taggable.should_not be
44
+ @tagging.taggable_id = 11111
45
+ @tagging.taggable_type = "TaggedModel"
46
+ TaggedModel.should_receive(:get!).with(11111)
47
+ @tagging.taggable
48
+ end
49
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ # require 'merb-core'
4
+ require 'dm-core'
5
+ require 'dm-validations'
6
+ require File.dirname(__FILE__) + '/../lib/dm-tags'
7
+ require File.dirname(__FILE__) + '/classes'
8
+ require File.dirname(__FILE__) + '/dm-setup'
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-tags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Bobby Calderwood
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-31 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.7.0
24
+ version:
25
+ description: This package brings tagging to DataMapper. It is inspired by Acts As Taggable On by Michael Bleigh, github's mbleigh. Props to him for the contextual tagging based on Acts As Taggable on Steroids.
26
+ email:
27
+ - bobby_calderwood@me.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - License.txt
35
+ - Manifest.txt
36
+ - README.txt
37
+ files:
38
+ - History.txt
39
+ - License.txt
40
+ - Manifest.txt
41
+ - README.txt
42
+ - Rakefile
43
+ - lib/dm-tags.rb
44
+ - lib/dm-tags/dm_tags.rb
45
+ - lib/dm-tags/tag.rb
46
+ - lib/dm-tags/tagging.rb
47
+ - lib/dm-tags/version.rb
48
+ - setup.rb
49
+ - spec/classes.rb
50
+ - spec/dm-setup.rb
51
+ - spec/dm-tags/dm_tags_spec.rb
52
+ - spec/dm-tags/tag_spec.rb
53
+ - spec/dm-tags/taggable_spec.rb
54
+ - spec/dm-tags/tagging_spec.rb
55
+ - spec/spec.opts
56
+ - spec/spec_helper.rb
57
+ - tasks/rspec.rake
58
+ has_rdoc: true
59
+ homepage: http://dm-tags.rubyforge.org
60
+ post_install_message: ""
61
+ rdoc_options:
62
+ - --main
63
+ - README.txt
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project: dm-tags
81
+ rubygems_version: 1.2.0
82
+ signing_key:
83
+ specification_version: 2
84
+ summary: This package brings tagging to DataMapper. It is inspired by Acts As Taggable On by Michael Bleigh, github's mbleigh. Props to him for the contextual tagging based on Acts As Taggable on Steroids.
85
+ test_files:
86
+ - spec/dm-tags/dm_tags_spec.rb
87
+ - spec/dm-tags/tag_spec.rb
88
+ - spec/dm-tags/taggable_spec.rb
89
+ - spec/dm-tags/tagging_spec.rb