mongoid-simple-tags 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source "http://rubygems.org"
2
- gem "mongoid", ">= 2.0.0"
2
+ gem 'mongoid', '2.2.1'
3
+ gem 'bson_ext', '>= 1.4.0'
4
+
3
5
  group :development do
4
6
  gem "bundler", "~> 1.0.0"
5
7
  gem "jeweler", "~> 1.6.2"
data/Gemfile.lock CHANGED
@@ -1,32 +1,36 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (3.0.7)
5
- activesupport (= 3.0.7)
6
- builder (~> 2.1.2)
7
- i18n (~> 0.5.0)
8
- activesupport (3.0.7)
9
- bson (1.3.1)
10
- builder (2.1.2)
4
+ activemodel (3.1.1)
5
+ activesupport (= 3.1.1)
6
+ builder (~> 3.0.0)
7
+ i18n (~> 0.6)
8
+ activesupport (3.1.1)
9
+ multi_json (~> 1.0)
10
+ bson (1.4.0)
11
+ bson_ext (1.4.0)
12
+ builder (3.0.0)
11
13
  git (1.2.5)
12
- i18n (0.5.0)
14
+ i18n (0.6.0)
13
15
  jeweler (1.6.2)
14
16
  bundler (~> 1.0)
15
17
  git (>= 1.2.5)
16
18
  rake
17
19
  mongo (1.3.1)
18
20
  bson (>= 1.3.1)
19
- mongoid (2.0.2)
21
+ mongoid (2.2.1)
20
22
  activemodel (~> 3.0)
21
- mongo (~> 1.3)
23
+ mongo (>= 1.3, < 1.4)
22
24
  tzinfo (~> 0.3.22)
25
+ multi_json (1.0.3)
23
26
  rake (0.9.2)
24
- tzinfo (0.3.27)
27
+ tzinfo (0.3.30)
25
28
 
26
29
  PLATFORMS
27
30
  ruby
28
31
 
29
32
  DEPENDENCIES
33
+ bson_ext (>= 1.4.0)
30
34
  bundler (~> 1.0.0)
31
35
  jeweler (~> 1.6.2)
32
- mongoid (>= 2.0.0)
36
+ mongoid (= 2.2.1)
data/README.rdoc CHANGED
@@ -5,7 +5,7 @@ mongoid-simple-tags is a basic and simple tagging system for mongoid using map-r
5
5
  == Install
6
6
  Add the following to Gemfile:
7
7
 
8
- gem "mongoid-simple-tags", "0.0.2"
8
+ gem "mongoid-simple-tags", "0.0.5"
9
9
 
10
10
  == Usage
11
11
 
data/Rakefile CHANGED
@@ -26,4 +26,13 @@ Jeweler::Tasks.new do |gem|
26
26
 
27
27
  end
28
28
  Jeweler::RubygemsDotOrgTasks.new
29
+
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new do |t|
33
+ # t.libs.push "lib"
34
+ t.test_files = FileList['test/*_test.rb']
35
+ t.verbose = true
36
+ end
37
+
29
38
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.5
@@ -33,6 +33,7 @@ module Mongoid
33
33
  end
34
34
  end
35
35
 
36
+
36
37
  module ClassMethods
37
38
 
38
39
  def all_tags(opts={})
@@ -40,6 +41,41 @@ module Mongoid
40
41
  opts.merge(:sort => ["_id", :desc]) unless opts[:sort]
41
42
  tags.find({}, opts).to_a.map!{|item| { :name => item['_id'], :count => item['value'].to_i } }
42
43
  end
44
+
45
+ def scoped_tags(options={})
46
+ map = <<-MAP
47
+ function() {
48
+ if(this.tags) {
49
+ this.tags.forEach( function(t) {
50
+ emit(t, 1)
51
+ })
52
+ }
53
+ }
54
+ MAP
55
+
56
+ reduce = <<-REDUCE
57
+ function(key,values) {
58
+ var count = 0
59
+ values.forEach(function(v){
60
+ count += v
61
+ })
62
+ return count
63
+ }
64
+ REDUCE
65
+
66
+ scope = {}
67
+ options.each do |key, value|
68
+ scope[key] = {'$in' => [value]}
69
+ end
70
+
71
+ results = self.collection.map_reduce(
72
+ map,
73
+ reduce,
74
+ :out => "scoped_tags",
75
+ :query => scope
76
+ )
77
+ results.find().to_a.map{ |item| { :name => item['_id'], :count => item['value'].to_i } }
78
+ end
43
79
 
44
80
  def tagged_with(tags)
45
81
  tags = [tags] unless tags.is_a? Array
@@ -49,4 +85,4 @@ module Mongoid
49
85
 
50
86
  end
51
87
  end
52
- end
88
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid-simple-tags}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["chebyte"]
12
- s.date = %q{2011-06-15}
12
+ s.date = %q{2011-11-25}
13
13
  s.description = %q{basic and simple tagging system for mongoid}
14
14
  s.email = %q{maurotorres@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  "lib/mongoid-simple-tags.rb",
28
28
  "mongoid-simple-tags.gemspec",
29
29
  "test/helper.rb",
30
- "test/test_mongoid-simple-tags.rb"
30
+ "test/mongoid-simple-tags_test.rb"
31
31
  ]
32
32
  s.homepage = %q{http://github.com/chebyte/mongoid-simple-tags}
33
33
  s.licenses = ["MIT"]
@@ -39,18 +39,21 @@ Gem::Specification.new do |s|
39
39
  s.specification_version = 3
40
40
 
41
41
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
- s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0"])
42
+ s.add_runtime_dependency(%q<mongoid>, ["= 2.2.1"])
43
+ s.add_runtime_dependency(%q<bson_ext>, [">= 1.4.0"])
43
44
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
44
45
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
45
46
  s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0"])
46
47
  else
47
- s.add_dependency(%q<mongoid>, [">= 2.0.0"])
48
+ s.add_dependency(%q<mongoid>, ["= 2.2.1"])
49
+ s.add_dependency(%q<bson_ext>, [">= 1.4.0"])
48
50
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
49
51
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
50
52
  s.add_dependency(%q<mongoid>, [">= 2.0.0"])
51
53
  end
52
54
  else
53
- s.add_dependency(%q<mongoid>, [">= 2.0.0"])
55
+ s.add_dependency(%q<mongoid>, ["= 2.2.1"])
56
+ s.add_dependency(%q<bson_ext>, [">= 1.4.0"])
54
57
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
58
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
56
59
  s.add_dependency(%q<mongoid>, [">= 2.0.0"])
data/test/helper.rb CHANGED
@@ -7,12 +7,16 @@ rescue Bundler::BundlerError => e
7
7
  $stderr.puts "Run `bundle install` to install missing gems"
8
8
  exit e.status_code
9
9
  end
10
- require 'test/unit'
11
- require 'shoulda'
10
+ require 'minitest/autorun'
11
+ require 'minitest/spec'
12
12
 
13
13
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
14
  $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'mongoid'
15
16
  require 'mongoid-simple-tags'
16
17
 
17
- class Test::Unit::TestCase
18
+ Mongoid.configure do |config|
19
+ config.master = Mongo::Connection.new.db("mongoid_simple_tags_test")
18
20
  end
21
+
22
+
@@ -0,0 +1,115 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'helper'
3
+
4
+ class User
5
+ include Mongoid::Document
6
+ include Mongoid::Document::Taggable
7
+ field :name
8
+ belongs_to :organisation
9
+ end
10
+
11
+ class Organisation
12
+ include Mongoid::Document
13
+ field :name
14
+ has_many :users
15
+ end
16
+
17
+ describe "A Taggable model" do
18
+ before do
19
+ @user = User.new(name: 'Tuquito')
20
+ end
21
+
22
+ it "should have a tag_list method" do
23
+ @user.respond_to?(:tag_list)
24
+ end
25
+
26
+ it "should be able to update a tag list" do
27
+ tag_list = "linux, tucuman, free software"
28
+ tags = tag_list.split(',').map{ |tag| tag.strip}.flatten
29
+ @user.tag_list = tag_list
30
+ assert_equal tags, @user.tags
31
+ end
32
+ end
33
+
34
+ describe "A Taggable model with tags assigned" do
35
+ before do
36
+ @tag_list = "linux, tucuman, free software"
37
+ @user = User.new(name: 'Tuquito')
38
+ @user.tag_list = @tag_list
39
+ @user.save
40
+ end
41
+
42
+ it "should be able to find tagged_with objects" do
43
+ assert_equal @user, User.tagged_with('linux').first
44
+ assert_equal @user, User.tagged_with(['tucuman', 'free software']).first
45
+ end
46
+
47
+ it "should be able to find tagged_with objects if more than one object is present" do
48
+ user2 = User.new(name: 'ubuntu')
49
+ user2.tag_list = "linux"
50
+ user2.save
51
+ tagged_with_list = User.tagged_with('linux')
52
+ assert tagged_with_list.include? @user
53
+ assert tagged_with_list.include? user2
54
+ end
55
+
56
+ it "should return all_tags per Model class" do
57
+ user2 = User.create(name: 'ubuntu', tag_list: 'linux')
58
+ all_tags_for_users = User.all_tags
59
+ expected_tag_list = [ {:name=>"free software", :count=>1},
60
+ {:name=>"linux", :count=>2},
61
+ {:name=>"tucuman", :count=>1}
62
+ ]
63
+ assert_equal expected_tag_list, all_tags_for_users
64
+ end
65
+
66
+ after do
67
+ cleanup_database
68
+ end
69
+ end
70
+
71
+ describe "A Taggable model with scope" do
72
+ before do
73
+ @organisation_1 = Organisation.create(name: 'Qualica')
74
+ @user_1 = @organisation_1.users.create(name: 'User1', tag_list: "ubuntu, linux, tucuman")
75
+ @user_2 = @organisation_1.users.create(name: 'User2', tag_list: "ubuntu, linux, tucuman")
76
+ @organisation_2 = Organisation.create(name: 'Microsoft')
77
+ @user_3 = @organisation_2.users.create(name: 'User3', tag_list: 'microsoft, windows, tucuman')
78
+ @user_4 = @organisation_2.users.create(name: 'User4', tag_list: 'ubuntu, linux, tucuman')
79
+ end
80
+
81
+ it "should return scoped tags when passing one option" do
82
+ results = User.scoped_tags(organisation_id: @organisation_1.id)
83
+ assert !results.empty?
84
+
85
+ assert results.include?( {:name => "linux", :count => 2 } )
86
+ assert results.include?( {:name => "ubuntu", :count => 2 } )
87
+ assert results.include?( {:name => "tucuman", :count => 2 } )
88
+
89
+ results = User.scoped_tags(organisation_id: @organisation_2.id)
90
+ assert !results.empty?
91
+ assert results.include?( {:name =>"linux", :count => 1 } )
92
+ assert results.include?( {:name => "microsoft", :count => 1 } )
93
+ assert results.include?( {:name => "windows", :count => 1 } )
94
+ assert results.include?( {:name => "tucuman", :count => 2 } )
95
+ end
96
+
97
+ it "should return scoped tags when passing more than one option" do
98
+ results = User.scoped_tags(organisation_id: @organisation_1.id, name: @user_1.name)
99
+ assert !results.empty?
100
+ assert results.include?( {:name => "linux", :count => 1 } )
101
+ assert results.include?( {:name => "ubuntu", :count => 1 } )
102
+ assert results.include?( {:name => "tucuman", :count => 1 } )
103
+ end
104
+
105
+ after do
106
+ cleanup_database
107
+ end
108
+
109
+ end
110
+
111
+ def cleanup_database
112
+ Mongoid.database.collections.each do |collection|
113
+ collection.remove if collection.name !=~ /^system\./
114
+ end
115
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid-simple-tags
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - chebyte
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-15 00:00:00 -03:00
13
+ date: 2011-11-25 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -18,15 +18,26 @@ dependencies:
18
18
  requirement: &id001 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
- - - ">="
21
+ - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 2.0.0
23
+ version: 2.2.1
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
28
- name: bundler
28
+ name: bson_ext
29
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.4.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
30
41
  none: false
31
42
  requirements:
32
43
  - - ~>
@@ -34,10 +45,10 @@ dependencies:
34
45
  version: 1.0.0
35
46
  type: :development
36
47
  prerelease: false
37
- version_requirements: *id002
48
+ version_requirements: *id003
38
49
  - !ruby/object:Gem::Dependency
39
50
  name: jeweler
40
- requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirement: &id004 !ruby/object:Gem::Requirement
41
52
  none: false
42
53
  requirements:
43
54
  - - ~>
@@ -45,10 +56,10 @@ dependencies:
45
56
  version: 1.6.2
46
57
  type: :development
47
58
  prerelease: false
48
- version_requirements: *id003
59
+ version_requirements: *id004
49
60
  - !ruby/object:Gem::Dependency
50
61
  name: mongoid
51
- requirement: &id004 !ruby/object:Gem::Requirement
62
+ requirement: &id005 !ruby/object:Gem::Requirement
52
63
  none: false
53
64
  requirements:
54
65
  - - ">="
@@ -56,7 +67,7 @@ dependencies:
56
67
  version: 2.0.0
57
68
  type: :runtime
58
69
  prerelease: false
59
- version_requirements: *id004
70
+ version_requirements: *id005
60
71
  description: basic and simple tagging system for mongoid
61
72
  email: maurotorres@gmail.com
62
73
  executables: []
@@ -77,7 +88,7 @@ files:
77
88
  - lib/mongoid-simple-tags.rb
78
89
  - mongoid-simple-tags.gemspec
79
90
  - test/helper.rb
80
- - test/test_mongoid-simple-tags.rb
91
+ - test/mongoid-simple-tags_test.rb
81
92
  has_rdoc: true
82
93
  homepage: http://github.com/chebyte/mongoid-simple-tags
83
94
  licenses:
@@ -92,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
103
  requirements:
93
104
  - - ">="
94
105
  - !ruby/object:Gem::Version
95
- hash: -4519926753338870437
106
+ hash: 100154636351803078
96
107
  segments:
97
108
  - 0
98
109
  version: "0"
@@ -1,7 +0,0 @@
1
- require 'helper'
2
-
3
- class TestMongoidSimpleTags < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end