yetanothernguyen-acts-as-taggable-on 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/CHANGELOG +25 -0
  2. data/Gemfile +10 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +221 -0
  5. data/Rakefile +59 -0
  6. data/VERSION +1 -0
  7. data/generators/acts_as_taggable_on_migration/acts_as_taggable_on_migration_generator.rb +7 -0
  8. data/generators/acts_as_taggable_on_migration/templates/migration.rb +29 -0
  9. data/lib/acts-as-taggable-on.rb +30 -0
  10. data/lib/acts_as_taggable_on/acts_as_taggable_on.rb +53 -0
  11. data/lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb +53 -0
  12. data/lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb +139 -0
  13. data/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb +262 -0
  14. data/lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb +105 -0
  15. data/lib/acts_as_taggable_on/acts_as_taggable_on/related.rb +69 -0
  16. data/lib/acts_as_taggable_on/acts_as_tagger.rb +67 -0
  17. data/lib/acts_as_taggable_on/compatibility/Gemfile +8 -0
  18. data/lib/acts_as_taggable_on/compatibility/active_record_backports.rb +21 -0
  19. data/lib/acts_as_taggable_on/tag.rb +84 -0
  20. data/lib/acts_as_taggable_on/tag_list.rb +96 -0
  21. data/lib/acts_as_taggable_on/tagging.rb +24 -0
  22. data/lib/acts_as_taggable_on/tags_helper.rb +17 -0
  23. data/lib/generators/acts_as_taggable_on/migration/migration_generator.rb +32 -0
  24. data/lib/generators/acts_as_taggable_on/migration/templates/active_record/migration.rb +28 -0
  25. data/rails/init.rb +1 -0
  26. data/spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb +268 -0
  27. data/spec/acts_as_taggable_on/acts_as_tagger_spec.rb +114 -0
  28. data/spec/acts_as_taggable_on/tag_list_spec.rb +70 -0
  29. data/spec/acts_as_taggable_on/tag_spec.rb +115 -0
  30. data/spec/acts_as_taggable_on/taggable_spec.rb +343 -0
  31. data/spec/acts_as_taggable_on/tagger_spec.rb +91 -0
  32. data/spec/acts_as_taggable_on/tagging_spec.rb +31 -0
  33. data/spec/acts_as_taggable_on/tags_helper_spec.rb +28 -0
  34. data/spec/bm.rb +52 -0
  35. data/spec/database.yml.sample +17 -0
  36. data/spec/models.rb +31 -0
  37. data/spec/schema.rb +43 -0
  38. data/spec/spec_helper.rb +60 -0
  39. metadata +115 -0
@@ -0,0 +1,91 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Tagger" do
4
+ before(:each) do
5
+ clean_database!
6
+ @user = TaggableUser.create
7
+ @taggable = TaggableModel.create(:name => "Bob Jones")
8
+ end
9
+
10
+ it "should have taggings" do
11
+ @user.tag(@taggable, :with=>'ruby,scheme', :on=>:tags)
12
+ @user.owned_taggings.size == 2
13
+ end
14
+
15
+ it "should have tags" do
16
+ @user.tag(@taggable, :with=>'ruby,scheme', :on=>:tags)
17
+ @user.owned_tags.size == 2
18
+ end
19
+
20
+ it "should not overlap tags from different taggers" do
21
+ @user2 = TaggableUser.new
22
+ lambda{
23
+ @user.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
24
+ @user2.tag(@taggable, :with => 'java, python, lisp, ruby', :on => :tags)
25
+ }.should change(ActsAsTaggableOn::Tagging, :count).by(6)
26
+
27
+ [@user, @user2, @taggable].each(&:reload)
28
+
29
+ @user.owned_tags.map(&:name).sort.should == %w(ruby scheme).sort
30
+ @user2.owned_tags.map(&:name).sort.should == %w(java python lisp ruby).sort
31
+
32
+ @taggable.tags_from(@user).sort.should == %w(ruby scheme).sort
33
+ @taggable.tags_from(@user2).sort.should == %w(java lisp python ruby).sort
34
+
35
+ @taggable.all_tags_list.sort.should == %w(ruby scheme java python lisp).sort
36
+ @taggable.all_tags_on(:tags).size.should == 5
37
+ end
38
+
39
+ it "should not lose tags from different taggers" do
40
+ @user2 = TaggableUser.create
41
+ @user2.tag(@taggable, :with => 'java, python, lisp, ruby', :on => :tags)
42
+ @user.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
43
+
44
+ lambda {
45
+ @user2.tag(@taggable, :with => 'java, python, lisp', :on => :tags)
46
+ }.should change(ActsAsTaggableOn::Tagging, :count).by(-1)
47
+
48
+ [@user, @user2, @taggable].each(&:reload)
49
+
50
+ @taggable.tags_from(@user).sort.should == %w(ruby scheme).sort
51
+ @taggable.tags_from(@user2).sort.should == %w(java python lisp).sort
52
+
53
+ @taggable.all_tags_list.sort.should == %w(ruby scheme java python lisp).sort
54
+ @taggable.all_tags_on(:tags).length.should == 5
55
+ end
56
+
57
+ it "should not lose tags" do
58
+ @user2 = TaggableUser.create
59
+
60
+ @user.tag(@taggable, :with => 'awesome', :on => :tags)
61
+ @user2.tag(@taggable, :with => 'awesome, epic', :on => :tags)
62
+
63
+ lambda {
64
+ @user2.tag(@taggable, :with => 'epic', :on => :tags)
65
+ }.should change(ActsAsTaggableOn::Tagging, :count).by(-1)
66
+
67
+ @taggable.reload
68
+ @taggable.all_tags_list.should include('awesome')
69
+ @taggable.all_tags_list.should include('epic')
70
+ end
71
+
72
+ it "should not lose tags" do
73
+ @taggable.update_attributes(:tag_list => 'ruby')
74
+ @user.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
75
+
76
+ [@taggable, @user].each(&:reload)
77
+ @taggable.tag_list.should == %w(ruby)
78
+ @taggable.all_tags_list.sort.should == %w(ruby scheme).sort
79
+
80
+ lambda {
81
+ @taggable.update_attributes(:tag_list => "")
82
+ }.should change(ActsAsTaggableOn::Tagging, :count).by(-1)
83
+
84
+ @taggable.tag_list.should == []
85
+ @taggable.all_tags_list.sort.should == %w(ruby scheme).sort
86
+ end
87
+
88
+ it "is tagger" do
89
+ @user.is_tagger?.should(be_true)
90
+ end
91
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe ActsAsTaggableOn::Tagging do
4
+ before(:each) do
5
+ clean_database!
6
+ @tagging = ActsAsTaggableOn::Tagging.new
7
+ end
8
+
9
+ it "should not be valid with a invalid tag" do
10
+ @tagging.taggable = TaggableModel.create(:name => "Bob Jones")
11
+ @tagging.tag = ActsAsTaggableOn::Tag.new(:name => "")
12
+ @tagging.context = "tags"
13
+
14
+ @tagging.should_not be_valid
15
+
16
+ if ActiveRecord::VERSION::MAJOR >= 3
17
+ @tagging.errors[:tag_id].should == ["can't be blank"]
18
+ else
19
+ @tagging.errors[:tag_id].should == "can't be blank"
20
+ end
21
+ end
22
+
23
+ it "should not create duplicate taggings" do
24
+ @taggable = TaggableModel.create(:name => "Bob Jones")
25
+ @tag = ActsAsTaggableOn::Tag.create(:name => "awesome")
26
+
27
+ lambda {
28
+ 2.times { ActsAsTaggableOn::Tagging.create(:taggable => @taggable, :tag => @tag, :context => 'tags') }
29
+ }.should change(ActsAsTaggableOn::Tagging, :count).by(1)
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe ActsAsTaggableOn::TagsHelper do
4
+ before(:each) do
5
+ clean_database!
6
+
7
+ @bob = TaggableModel.create(:name => "Bob Jones", :language_list => "ruby, php")
8
+ @tom = TaggableModel.create(:name => "Tom Marley", :language_list => "ruby, java")
9
+ @eve = TaggableModel.create(:name => "Eve Nodd", :language_list => "ruby, c++")
10
+
11
+ @helper = class Helper
12
+ include ActsAsTaggableOn::TagsHelper
13
+ end.new
14
+ end
15
+
16
+ it "should yield the proper css classes" do
17
+ tags = { }
18
+
19
+ @helper.tag_cloud(TaggableModel.tag_counts_on(:languages), ["sucky", "awesome"]) do |tag, css_class|
20
+ tags[tag.name] = css_class
21
+ end
22
+
23
+ tags["ruby"].should == "awesome"
24
+ tags["java"].should == "sucky"
25
+ tags["c++"].should == "sucky"
26
+ tags["php"].should == "sucky"
27
+ end
28
+ end
data/spec/bm.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'active_record'
2
+ require 'action_view'
3
+ require File.expand_path('../../lib/acts-as-taggable-on', __FILE__)
4
+
5
+ if defined?(ActiveRecord::Acts::TaggableOn)
6
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::TaggableOn
7
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::Tagger
8
+ ActionView::Base.send :include, TagsHelper if defined?(ActionView::Base)
9
+ end
10
+
11
+ TEST_DATABASE_FILE = File.join(File.dirname(__FILE__), '..', 'test.sqlite3')
12
+ File.unlink(TEST_DATABASE_FILE) if File.exist?(TEST_DATABASE_FILE)
13
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => TEST_DATABASE_FILE
14
+
15
+ ActiveRecord::Base.silence do
16
+ ActiveRecord::Migration.verbose = false
17
+ ActiveRecord::Schema.define :version => 0 do
18
+ create_table "taggings", :force => true do |t|
19
+ t.integer "tag_id", :limit => 11
20
+ t.integer "taggable_id", :limit => 11
21
+ t.string "taggable_type"
22
+ t.string "context"
23
+ t.datetime "created_at"
24
+ t.integer "tagger_id", :limit => 11
25
+ t.string "tagger_type"
26
+ end
27
+
28
+ add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
29
+ add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
30
+
31
+ create_table "tags", :force => true do |t|
32
+ t.string "name"
33
+ end
34
+
35
+ create_table :taggable_models, :force => true do |t|
36
+ t.column :name, :string
37
+ t.column :type, :string
38
+ t.column :cached_tag_list, :string
39
+ end
40
+ end
41
+
42
+ class TaggableModel < ActiveRecord::Base
43
+ acts_as_taggable
44
+ acts_as_taggable_on :languages
45
+ acts_as_taggable_on :skills
46
+ acts_as_taggable_on :needs, :offerings
47
+ end
48
+ end
49
+
50
+ puts Benchmark.measure {
51
+ 1000.times { TaggableModel.create :tag_list => "awesome, epic, neat" }
52
+ }
@@ -0,0 +1,17 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: acts_as_taggable_on.sqlite3
4
+
5
+ mysql:
6
+ adapter: mysql
7
+ hostname: localhost
8
+ username: root
9
+ password:
10
+ database: acts_as_taggable_on
11
+
12
+ postgresql:
13
+ adapter: postgresql
14
+ hostname: localhost
15
+ username: postgres
16
+ password:
17
+ database: acts_as_taggable_on
data/spec/models.rb ADDED
@@ -0,0 +1,31 @@
1
+ class TaggableModel < ActiveRecord::Base
2
+ acts_as_taggable
3
+ acts_as_taggable_on :languages
4
+ acts_as_taggable_on :skills
5
+ acts_as_taggable_on :needs, :offerings
6
+ has_many :untaggable_models
7
+ end
8
+
9
+ class CachedModel < ActiveRecord::Base
10
+ acts_as_taggable
11
+ end
12
+
13
+ class OtherTaggableModel < ActiveRecord::Base
14
+ acts_as_taggable_on :tags, :languages
15
+ acts_as_taggable_on :needs, :offerings
16
+ end
17
+
18
+ class InheritingTaggableModel < TaggableModel
19
+ end
20
+
21
+ class AlteredInheritingTaggableModel < TaggableModel
22
+ acts_as_taggable_on :parts
23
+ end
24
+
25
+ class TaggableUser < ActiveRecord::Base
26
+ acts_as_tagger
27
+ end
28
+
29
+ class UntaggableModel < ActiveRecord::Base
30
+ belongs_to :taggable_model
31
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,43 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table "taggings", :force => true do |t|
3
+ t.integer "tag_id", :limit => 11
4
+ t.integer "taggable_id", :limit => 11
5
+ t.string "taggable_type"
6
+ t.string "context"
7
+ t.datetime "created_at"
8
+ t.integer "tagger_id", :limit => 11
9
+ t.string "tagger_type"
10
+ end
11
+
12
+ add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
13
+ add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
14
+
15
+ create_table "tags", :force => true do |t|
16
+ t.string "name"
17
+ end
18
+
19
+ create_table :taggable_models, :force => true do |t|
20
+ t.column :name, :string
21
+ t.column :type, :string
22
+ end
23
+
24
+ create_table :untaggable_models, :force => true do |t|
25
+ t.column :taggable_model_id, :integer
26
+ t.column :name, :string
27
+ end
28
+
29
+ create_table :cached_models, :force => true do |t|
30
+ t.column :name, :string
31
+ t.column :type, :string
32
+ t.column :cached_tag_list, :string
33
+ end
34
+
35
+ create_table :taggable_users, :force => true do |t|
36
+ t.column :name, :string
37
+ end
38
+
39
+ create_table :other_taggable_models, :force => true do |t|
40
+ t.column :name, :string
41
+ t.column :type, :string
42
+ end
43
+ end
@@ -0,0 +1,60 @@
1
+ $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
2
+
3
+ begin
4
+ require "rubygems"
5
+ require "bundler"
6
+
7
+ if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.5")
8
+ raise RuntimeError, "Your bundler version is too old." +
9
+ "Run `gem install bundler` to upgrade."
10
+ end
11
+
12
+ # Set up load paths for all bundled gems
13
+ Bundler.setup
14
+ rescue Bundler::GemNotFound
15
+ raise RuntimeError, "Bundler couldn't find some gems." +
16
+ "Did you run \`bundlee install\`?"
17
+ end
18
+
19
+ Bundler.require
20
+ require File.expand_path('../../lib/acts-as-taggable-on', __FILE__)
21
+
22
+ unless [].respond_to?(:freq)
23
+ class Array
24
+ def freq
25
+ k=Hash.new(0)
26
+ each {|e| k[e]+=1}
27
+ k
28
+ end
29
+ end
30
+ end
31
+
32
+ ENV['DB'] ||= 'sqlite3'
33
+
34
+ database_yml = File.expand_path('../database.yml', __FILE__)
35
+ if File.exists?(database_yml)
36
+ active_record_configuration = YAML.load_file(database_yml)[ENV['DB']]
37
+
38
+ ActiveRecord::Base.establish_connection(active_record_configuration)
39
+ ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
40
+
41
+ ActiveRecord::Base.silence do
42
+ ActiveRecord::Migration.verbose = false
43
+
44
+ load(File.dirname(__FILE__) + '/schema.rb')
45
+ load(File.dirname(__FILE__) + '/models.rb')
46
+ end
47
+
48
+ else
49
+ raise "Please create #{database_yml} first to configure your database. Take a look at: #{database_yml}.sample"
50
+ end
51
+
52
+ def clean_database!
53
+ models = [ActsAsTaggableOn::Tag, ActsAsTaggableOn::Tagging, TaggableModel, OtherTaggableModel, InheritingTaggableModel,
54
+ AlteredInheritingTaggableModel, TaggableUser, UntaggableModel]
55
+ models.each do |model|
56
+ ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
57
+ end
58
+ end
59
+
60
+ clean_database!
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yetanothernguyen-acts-as-taggable-on
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - Michael Bleigh
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-15 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: With ActsAsTaggableOn, you could tag a single model on several contexts, such as skills, interests, and awards. It also provides other advanced functionality. This fork changes the behavior of tagged_with. It will return empty scope on empty or nil tag list.
23
+ email: michael@intridea.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - CHANGELOG
32
+ - Gemfile
33
+ - MIT-LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - generators/acts_as_taggable_on_migration/acts_as_taggable_on_migration_generator.rb
38
+ - generators/acts_as_taggable_on_migration/templates/migration.rb
39
+ - lib/acts-as-taggable-on.rb
40
+ - lib/acts_as_taggable_on/acts_as_taggable_on.rb
41
+ - lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb
42
+ - lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb
43
+ - lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
44
+ - lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb
45
+ - lib/acts_as_taggable_on/acts_as_taggable_on/related.rb
46
+ - lib/acts_as_taggable_on/acts_as_tagger.rb
47
+ - lib/acts_as_taggable_on/compatibility/Gemfile
48
+ - lib/acts_as_taggable_on/compatibility/active_record_backports.rb
49
+ - lib/acts_as_taggable_on/tag.rb
50
+ - lib/acts_as_taggable_on/tag_list.rb
51
+ - lib/acts_as_taggable_on/tagging.rb
52
+ - lib/acts_as_taggable_on/tags_helper.rb
53
+ - lib/generators/acts_as_taggable_on/migration/migration_generator.rb
54
+ - lib/generators/acts_as_taggable_on/migration/templates/active_record/migration.rb
55
+ - rails/init.rb
56
+ - spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb
57
+ - spec/acts_as_taggable_on/acts_as_tagger_spec.rb
58
+ - spec/acts_as_taggable_on/tag_list_spec.rb
59
+ - spec/acts_as_taggable_on/tag_spec.rb
60
+ - spec/acts_as_taggable_on/taggable_spec.rb
61
+ - spec/acts_as_taggable_on/tagger_spec.rb
62
+ - spec/acts_as_taggable_on/tagging_spec.rb
63
+ - spec/acts_as_taggable_on/tags_helper_spec.rb
64
+ - spec/bm.rb
65
+ - spec/database.yml.sample
66
+ - spec/models.rb
67
+ - spec/schema.rb
68
+ - spec/spec_helper.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/yetanothernguyen/acts-as-taggable-on
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.5.2
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: ActsAsTaggableOn is a tagging plugin for Rails that provides multiple tagging contexts on a single model.
103
+ test_files:
104
+ - spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb
105
+ - spec/acts_as_taggable_on/acts_as_tagger_spec.rb
106
+ - spec/acts_as_taggable_on/tag_list_spec.rb
107
+ - spec/acts_as_taggable_on/tag_spec.rb
108
+ - spec/acts_as_taggable_on/taggable_spec.rb
109
+ - spec/acts_as_taggable_on/tagger_spec.rb
110
+ - spec/acts_as_taggable_on/tagging_spec.rb
111
+ - spec/acts_as_taggable_on/tags_helper_spec.rb
112
+ - spec/bm.rb
113
+ - spec/models.rb
114
+ - spec/schema.rb
115
+ - spec/spec_helper.rb