acts_as_taggable_on 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/Appraisals +7 -0
- data/Gemfile +5 -0
- data/Guardfile +5 -0
- data/LICENSE.md +20 -0
- data/README.md +309 -0
- data/Rakefile +13 -0
- data/UPGRADING +7 -0
- data/acts_as_taggable_on.gemspec +35 -0
- data/db/migrate/1_acts_as_taggable_on_migration.rb +30 -0
- data/db/migrate/2_add_missing_unique_indices.rb +21 -0
- data/gemfiles/rails_3.gemfile +8 -0
- data/gemfiles/rails_4.gemfile +8 -0
- data/lib/acts_as_taggable_on.rb +61 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb +82 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb +187 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/compatibility.rb +34 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb +394 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/dirty.rb +37 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb +135 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/related.rb +84 -0
- data/lib/acts_as_taggable_on/engine.rb +6 -0
- data/lib/acts_as_taggable_on/tag.rb +119 -0
- data/lib/acts_as_taggable_on/tag_list.rb +101 -0
- data/lib/acts_as_taggable_on/taggable.rb +105 -0
- data/lib/acts_as_taggable_on/tagger.rb +76 -0
- data/lib/acts_as_taggable_on/tagging.rb +34 -0
- data/lib/acts_as_taggable_on/tags_helper.rb +15 -0
- data/lib/acts_as_taggable_on/utils.rb +34 -0
- data/lib/acts_as_taggable_on/version.rb +4 -0
- data/spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb +265 -0
- data/spec/acts_as_taggable_on/acts_as_tagger_spec.rb +114 -0
- data/spec/acts_as_taggable_on/caching_spec.rb +77 -0
- data/spec/acts_as_taggable_on/related_spec.rb +143 -0
- data/spec/acts_as_taggable_on/single_table_inheritance_spec.rb +187 -0
- data/spec/acts_as_taggable_on/tag_list_spec.rb +126 -0
- data/spec/acts_as_taggable_on/tag_spec.rb +211 -0
- data/spec/acts_as_taggable_on/taggable_spec.rb +623 -0
- data/spec/acts_as_taggable_on/tagger_spec.rb +137 -0
- data/spec/acts_as_taggable_on/tagging_spec.rb +28 -0
- data/spec/acts_as_taggable_on/tags_helper_spec.rb +44 -0
- data/spec/acts_as_taggable_on/utils_spec.rb +21 -0
- data/spec/bm.rb +52 -0
- data/spec/database.yml.sample +19 -0
- data/spec/models.rb +58 -0
- data/spec/schema.rb +65 -0
- data/spec/spec_helper.rb +87 -0
- metadata +248 -0
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Tagger" do
|
4
|
+
before(:each) do
|
5
|
+
clean_database!
|
6
|
+
@user = User.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 scope objects returned by tagged_with by owners" do
|
21
|
+
@taggable2 = TaggableModel.create(:name => "Jim Jones")
|
22
|
+
@taggable3 = TaggableModel.create(:name => "Jane Doe")
|
23
|
+
|
24
|
+
@user2 = User.new
|
25
|
+
@user.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
|
26
|
+
@user2.tag(@taggable2, :with => 'ruby, scheme', :on => :tags)
|
27
|
+
@user2.tag(@taggable3, :with => 'ruby, scheme', :on => :tags)
|
28
|
+
|
29
|
+
TaggableModel.tagged_with(%w(ruby scheme), :owned_by => @user).count.should == 1
|
30
|
+
TaggableModel.tagged_with(%w(ruby scheme), :owned_by => @user2).count.should == 2
|
31
|
+
end
|
32
|
+
|
33
|
+
it "only returns objects tagged by owned_by when any is true" do
|
34
|
+
@user2 = User.new
|
35
|
+
@taggable2 = TaggableModel.create(:name => "Jim Jones")
|
36
|
+
@taggable3 = TaggableModel.create(:name => "Jane Doe")
|
37
|
+
|
38
|
+
@user.tag(@taggable, :with => 'ruby', :on => :tags)
|
39
|
+
@user.tag(@taggable2, :with => 'java', :on => :tags)
|
40
|
+
@user2.tag(@taggable3, :with => 'ruby', :on => :tags)
|
41
|
+
|
42
|
+
tags = TaggableModel.tagged_with(%w(ruby java), :owned_by => @user, :any => true)
|
43
|
+
tags.should match_array [@taggable, @taggable2]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "only returns objects tagged by owned_by when exclude is true" do
|
47
|
+
@user2 = User.new
|
48
|
+
@taggable2 = TaggableModel.create(:name => "Jim Jones")
|
49
|
+
@taggable3 = TaggableModel.create(:name => "Jane Doe")
|
50
|
+
|
51
|
+
@user.tag(@taggable, :with => 'ruby', :on => :tags)
|
52
|
+
@user.tag(@taggable2, :with => 'java', :on => :tags)
|
53
|
+
@user2.tag(@taggable3, :with => 'java', :on => :tags)
|
54
|
+
|
55
|
+
tags = TaggableModel.tagged_with(%w(ruby), :owned_by => @user, :exclude => true)
|
56
|
+
tags.should match_array [@taggable2]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should not overlap tags from different taggers" do
|
60
|
+
@user2 = User.new
|
61
|
+
lambda{
|
62
|
+
@user.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
|
63
|
+
@user2.tag(@taggable, :with => 'java, python, lisp, ruby', :on => :tags)
|
64
|
+
}.should change(ActsAsTaggableOn::Tagging, :count).by(6)
|
65
|
+
|
66
|
+
[@user, @user2, @taggable].each(&:reload)
|
67
|
+
|
68
|
+
@user.owned_tags.map(&:name).sort.should == %w(ruby scheme).sort
|
69
|
+
@user2.owned_tags.map(&:name).sort.should == %w(java python lisp ruby).sort
|
70
|
+
|
71
|
+
@taggable.tags_from(@user).sort.should == %w(ruby scheme).sort
|
72
|
+
@taggable.tags_from(@user2).sort.should == %w(java lisp python ruby).sort
|
73
|
+
|
74
|
+
@taggable.all_tags_list.sort.should == %w(ruby scheme java python lisp).sort
|
75
|
+
@taggable.all_tags_on(:tags).size.should == 5
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not lose tags from different taggers" do
|
79
|
+
@user2 = User.create
|
80
|
+
@user2.tag(@taggable, :with => 'java, python, lisp, ruby', :on => :tags)
|
81
|
+
@user.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
|
82
|
+
|
83
|
+
lambda {
|
84
|
+
@user2.tag(@taggable, :with => 'java, python, lisp', :on => :tags)
|
85
|
+
}.should change(ActsAsTaggableOn::Tagging, :count).by(-1)
|
86
|
+
|
87
|
+
[@user, @user2, @taggable].each(&:reload)
|
88
|
+
|
89
|
+
@taggable.tags_from(@user).sort.should == %w(ruby scheme).sort
|
90
|
+
@taggable.tags_from(@user2).sort.should == %w(java python lisp).sort
|
91
|
+
|
92
|
+
@taggable.all_tags_list.sort.should == %w(ruby scheme java python lisp).sort
|
93
|
+
@taggable.all_tags_on(:tags).length.should == 5
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should not lose tags" do
|
97
|
+
@user2 = User.create
|
98
|
+
|
99
|
+
@user.tag(@taggable, :with => 'awesome', :on => :tags)
|
100
|
+
@user2.tag(@taggable, :with => 'awesome, epic', :on => :tags)
|
101
|
+
|
102
|
+
lambda {
|
103
|
+
@user2.tag(@taggable, :with => 'epic', :on => :tags)
|
104
|
+
}.should change(ActsAsTaggableOn::Tagging, :count).by(-1)
|
105
|
+
|
106
|
+
@taggable.reload
|
107
|
+
@taggable.all_tags_list.should include('awesome')
|
108
|
+
@taggable.all_tags_list.should include('epic')
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should not lose tags" do
|
112
|
+
@taggable.update_attributes(:tag_list => 'ruby')
|
113
|
+
@user.tag(@taggable, :with => 'ruby, scheme', :on => :tags)
|
114
|
+
|
115
|
+
[@taggable, @user].each(&:reload)
|
116
|
+
@taggable.tag_list.should == %w(ruby)
|
117
|
+
@taggable.all_tags_list.sort.should == %w(ruby scheme).sort
|
118
|
+
|
119
|
+
lambda {
|
120
|
+
@taggable.update_attributes(:tag_list => "")
|
121
|
+
}.should change(ActsAsTaggableOn::Tagging, :count).by(-1)
|
122
|
+
|
123
|
+
@taggable.tag_list.should == []
|
124
|
+
@taggable.all_tags_list.sort.should == %w(ruby scheme).sort
|
125
|
+
end
|
126
|
+
|
127
|
+
it "is tagger" do
|
128
|
+
@user.is_tagger?.should(be_true)
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should skip save if skip_save is passed as option" do
|
132
|
+
lambda {
|
133
|
+
@user.tag(@taggable, :with => 'epic', :on => :tags, :skip_save => true)
|
134
|
+
}.should_not change(ActsAsTaggableOn::Tagging, :count)
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
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
|
+
@tagging.errors[:tag_id].should == ["can't be blank"]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not create duplicate taggings" do
|
20
|
+
@taggable = TaggableModel.create(:name => "Bob Jones")
|
21
|
+
@tag = ActsAsTaggableOn::Tag.create(:name => "awesome")
|
22
|
+
|
23
|
+
lambda {
|
24
|
+
2.times { ActsAsTaggableOn::Tagging.create(:taggable => @taggable, :tag => @tag, :context => 'tags') }
|
25
|
+
}.should change(ActsAsTaggableOn::Tagging, :count).by(1)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
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
|
+
|
29
|
+
it "should handle tags with zero counts (build for empty)" do
|
30
|
+
bob = ActsAsTaggableOn::Tag.create(:name => "php")
|
31
|
+
tom = ActsAsTaggableOn::Tag.create(:name => "java")
|
32
|
+
eve = ActsAsTaggableOn::Tag.create(:name => "c++")
|
33
|
+
|
34
|
+
tags = { }
|
35
|
+
|
36
|
+
@helper.tag_cloud(ActsAsTaggableOn::Tag.all, ["sucky", "awesome"]) do |tag, css_class|
|
37
|
+
tags[tag.name] = css_class
|
38
|
+
end
|
39
|
+
|
40
|
+
tags["java"].should == "sucky"
|
41
|
+
tags["c++"].should == "sucky"
|
42
|
+
tags["php"].should == "sucky"
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActsAsTaggableOn::Utils do
|
4
|
+
describe "like_operator" do
|
5
|
+
before(:each) do
|
6
|
+
clean_database!
|
7
|
+
TaggableModel.acts_as_taggable_on(:tags, :languages, :skills, :needs, :offerings)
|
8
|
+
@taggable = TaggableModel.new(:name => "Bob Jones")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return 'ILIKE' when the adapter is PostgreSQL" do
|
12
|
+
TaggableModel.connection.stub(:adapter_name).and_return("PostgreSQL")
|
13
|
+
TaggableModel.send(:like_operator).should == "ILIKE"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return 'LIKE' when the adapter is not PostgreSQL" do
|
17
|
+
TaggableModel.connection.stub(:adapter_name).and_return("MySQL")
|
18
|
+
TaggableModel.send(:like_operator).should == "LIKE"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
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,19 @@
|
|
1
|
+
sqlite3:
|
2
|
+
adapter: sqlite3
|
3
|
+
database: acts_as_taggable_on.sqlite3
|
4
|
+
|
5
|
+
mysql:
|
6
|
+
adapter: mysql2
|
7
|
+
hostname: localhost
|
8
|
+
username: root
|
9
|
+
password:
|
10
|
+
database: acts_as_taggable_on
|
11
|
+
charset: utf8
|
12
|
+
|
13
|
+
postgresql:
|
14
|
+
adapter: postgresql
|
15
|
+
hostname: localhost
|
16
|
+
username: postgres
|
17
|
+
password:
|
18
|
+
database: acts_as_taggable_on
|
19
|
+
encoding: utf8
|
data/spec/models.rb
ADDED
@@ -0,0 +1,58 @@
|
|
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
|
+
|
8
|
+
attr_reader :tag_list_submethod_called
|
9
|
+
def tag_list=v
|
10
|
+
@tag_list_submethod_called = true
|
11
|
+
super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class CachedModel < ActiveRecord::Base
|
16
|
+
acts_as_taggable
|
17
|
+
end
|
18
|
+
|
19
|
+
class OtherCachedModel < ActiveRecord::Base
|
20
|
+
acts_as_taggable_on :languages, :statuses, :glasses
|
21
|
+
end
|
22
|
+
|
23
|
+
class OtherTaggableModel < ActiveRecord::Base
|
24
|
+
acts_as_taggable_on :tags, :languages
|
25
|
+
acts_as_taggable_on :needs, :offerings
|
26
|
+
end
|
27
|
+
|
28
|
+
class InheritingTaggableModel < TaggableModel
|
29
|
+
end
|
30
|
+
|
31
|
+
class AlteredInheritingTaggableModel < TaggableModel
|
32
|
+
acts_as_taggable_on :parts
|
33
|
+
end
|
34
|
+
|
35
|
+
class User < ActiveRecord::Base
|
36
|
+
acts_as_tagger
|
37
|
+
end
|
38
|
+
|
39
|
+
class Student < User
|
40
|
+
end
|
41
|
+
|
42
|
+
class UntaggableModel < ActiveRecord::Base
|
43
|
+
belongs_to :taggable_model
|
44
|
+
end
|
45
|
+
|
46
|
+
class NonStandardIdTaggableModel < ActiveRecord::Base
|
47
|
+
primary_key = "an_id"
|
48
|
+
acts_as_taggable
|
49
|
+
acts_as_taggable_on :languages
|
50
|
+
acts_as_taggable_on :skills
|
51
|
+
acts_as_taggable_on :needs, :offerings
|
52
|
+
has_many :untaggable_models
|
53
|
+
end
|
54
|
+
|
55
|
+
class OrderedTaggableModel < ActiveRecord::Base
|
56
|
+
acts_as_ordered_taggable
|
57
|
+
acts_as_ordered_taggable_on :colours
|
58
|
+
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
ActiveRecord::Schema.define :version => 0 do
|
2
|
+
create_table :tags, :force => true do |t|
|
3
|
+
t.string :name
|
4
|
+
end
|
5
|
+
|
6
|
+
create_table :taggings, :force => true do |t|
|
7
|
+
t.references :tag
|
8
|
+
|
9
|
+
# You should make sure that the column created is
|
10
|
+
# long enough to store the required class names.
|
11
|
+
t.references :taggable, :polymorphic => true
|
12
|
+
t.references :tagger, :polymorphic => true
|
13
|
+
|
14
|
+
# Limit is created to prevent MySQL error on index
|
15
|
+
# length for MyISAM table type: http://bit.ly/vgW2Ql
|
16
|
+
t.string :context, :limit => 128
|
17
|
+
|
18
|
+
t.datetime :created_at
|
19
|
+
end
|
20
|
+
# above copied from
|
21
|
+
# generators/acts_as_taggable_on/migration/migration_generator
|
22
|
+
|
23
|
+
create_table :taggable_models, :force => true do |t|
|
24
|
+
t.column :name, :string
|
25
|
+
t.column :type, :string
|
26
|
+
end
|
27
|
+
|
28
|
+
create_table :non_standard_id_taggable_models, :primary_key => "an_id", :force => true do |t|
|
29
|
+
t.column :name, :string
|
30
|
+
t.column :type, :string
|
31
|
+
end
|
32
|
+
|
33
|
+
create_table :untaggable_models, :force => true do |t|
|
34
|
+
t.column :taggable_model_id, :integer
|
35
|
+
t.column :name, :string
|
36
|
+
end
|
37
|
+
|
38
|
+
create_table :cached_models, :force => true do |t|
|
39
|
+
t.column :name, :string
|
40
|
+
t.column :type, :string
|
41
|
+
t.column :cached_tag_list, :string
|
42
|
+
end
|
43
|
+
|
44
|
+
create_table :other_cached_models, :force => true do |t|
|
45
|
+
t.column :name, :string
|
46
|
+
t.column :type, :string
|
47
|
+
t.column :cached_language_list, :string
|
48
|
+
t.column :cached_status_list, :string
|
49
|
+
t.column :cached_glass_list, :string
|
50
|
+
end
|
51
|
+
|
52
|
+
create_table :users, :force => true do |t|
|
53
|
+
t.column :name, :string
|
54
|
+
end
|
55
|
+
|
56
|
+
create_table :other_taggable_models, :force => true do |t|
|
57
|
+
t.column :name, :string
|
58
|
+
t.column :type, :string
|
59
|
+
end
|
60
|
+
|
61
|
+
create_table :ordered_taggable_models, :force => true do |t|
|
62
|
+
t.column :name, :string
|
63
|
+
t.column :type, :string
|
64
|
+
end
|
65
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
$LOAD_PATH << "." unless $LOAD_PATH.include?(".")
|
2
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require "rubygems"
|
7
|
+
require "bundler"
|
8
|
+
|
9
|
+
if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.5")
|
10
|
+
raise RuntimeError, "Your bundler version is too old." +
|
11
|
+
"Run `gem install bundler` to upgrade."
|
12
|
+
end
|
13
|
+
|
14
|
+
# Set up load paths for all bundled gems
|
15
|
+
Bundler.setup
|
16
|
+
rescue Bundler::GemNotFound
|
17
|
+
raise RuntimeError, "Bundler couldn't find some gems." +
|
18
|
+
"Did you run \`bundle install\`?"
|
19
|
+
end
|
20
|
+
|
21
|
+
Bundler.require
|
22
|
+
require File.expand_path('../../lib/acts_as_taggable_on', __FILE__)
|
23
|
+
require 'ammeter/init'
|
24
|
+
|
25
|
+
unless [].respond_to?(:freq)
|
26
|
+
class Array
|
27
|
+
def freq
|
28
|
+
k=Hash.new(0)
|
29
|
+
each {|e| k[e]+=1}
|
30
|
+
k
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# set adapter to use, default is sqlite3
|
36
|
+
# to use an alternative adapter run => rake spec DB='postgresql'
|
37
|
+
db_name = ENV['DB'] || 'sqlite3'
|
38
|
+
database_yml = File.expand_path('../database.yml', __FILE__)
|
39
|
+
|
40
|
+
if File.exists?(database_yml)
|
41
|
+
active_record_configuration = YAML.load_file(database_yml)
|
42
|
+
|
43
|
+
ActiveRecord::Base.configurations = active_record_configuration
|
44
|
+
config = ActiveRecord::Base.configurations[db_name]
|
45
|
+
|
46
|
+
begin
|
47
|
+
ActiveRecord::Base.establish_connection(db_name)
|
48
|
+
ActiveRecord::Base.connection
|
49
|
+
rescue
|
50
|
+
case db_name
|
51
|
+
when /mysql/
|
52
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
|
53
|
+
ActiveRecord::Base.connection.create_database(config['database'], {:charset => 'utf8', :collation => 'utf8_unicode_ci'})
|
54
|
+
when 'postgresql'
|
55
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
56
|
+
ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => 'utf8'))
|
57
|
+
end
|
58
|
+
|
59
|
+
ActiveRecord::Base.establish_connection(config)
|
60
|
+
end
|
61
|
+
|
62
|
+
logger = ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
63
|
+
ActiveRecord::Base.default_timezone = :utc
|
64
|
+
|
65
|
+
begin
|
66
|
+
old_logger_level, logger.level = logger.level, ::Logger::ERROR
|
67
|
+
ActiveRecord::Migration.verbose = false
|
68
|
+
|
69
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
70
|
+
load(File.dirname(__FILE__) + '/models.rb')
|
71
|
+
ensure
|
72
|
+
logger.level = old_logger_level
|
73
|
+
end
|
74
|
+
|
75
|
+
else
|
76
|
+
raise "Please create #{database_yml} first to configure your database. Take a look at: #{database_yml}.sample"
|
77
|
+
end
|
78
|
+
|
79
|
+
def clean_database!
|
80
|
+
models = [ActsAsTaggableOn::Tag, ActsAsTaggableOn::Tagging, TaggableModel, OtherTaggableModel, InheritingTaggableModel,
|
81
|
+
AlteredInheritingTaggableModel, User, UntaggableModel, OrderedTaggableModel]
|
82
|
+
models.each do |model|
|
83
|
+
ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
clean_database!
|