polytag 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.DS_Store +0 -0
  2. data/.pryrc +27 -0
  3. data/.rspec +3 -1
  4. data/README.md +26 -4
  5. data/lib/.DS_Store +0 -0
  6. data/lib/generators/polytag/.DS_Store +0 -0
  7. data/lib/generators/polytag/install/.DS_Store +0 -0
  8. data/lib/generators/polytag/install/templates/create_polytag_tables.rb +25 -11
  9. data/lib/polytag.rb +221 -58
  10. data/lib/polytag/concerns/tag_owner.rb +31 -0
  11. data/lib/polytag/concerns/tag_owner/association_extensions.rb +15 -0
  12. data/lib/polytag/concerns/tag_owner/association_extensions/owned_tags.rb +16 -0
  13. data/lib/polytag/concerns/tag_owner/class_helpers.rb +25 -0
  14. data/lib/polytag/concerns/tag_owner/model_helpers.rb +78 -0
  15. data/lib/polytag/concerns/taggable.rb +24 -0
  16. data/lib/polytag/concerns/taggable/association_extensions.rb +15 -0
  17. data/lib/polytag/concerns/taggable/class_helpers.rb +16 -0
  18. data/lib/polytag/concerns/taggable/model_helpers.rb +60 -0
  19. data/lib/polytag/connection.rb +27 -0
  20. data/lib/polytag/exceptions.rb +7 -0
  21. data/lib/polytag/tag.rb +6 -24
  22. data/lib/polytag/tag_group.rb +8 -46
  23. data/lib/polytag/version.rb +1 -1
  24. data/polytag.gemspec +7 -3
  25. data/spec/spec_helper.rb +15 -10
  26. data/spec/specs/owner_spec.rb +104 -0
  27. data/spec/specs/taggable_with_owner_spec.rb +120 -0
  28. data/spec/specs/taggable_without_owner_spec.rb +59 -0
  29. data/spec/support/active_record.rb +6 -6
  30. data/spec/support/owner.rb +1 -1
  31. data/spec/support/taggable.rb +3 -0
  32. metadata +115 -46
  33. checksums.yaml +0 -7
  34. data/circle.yml +0 -3
  35. data/lib/polytag/tag_group/owner.rb +0 -17
  36. data/lib/polytag/tag_relation.rb +0 -15
  37. data/spec/specs/polytag_querying_spec.rb +0 -56
  38. data/spec/specs/polytag_test_taggable_four_spec.rb +0 -54
  39. data/spec/specs/polytag_test_taggable_one_spec.rb +0 -47
  40. data/spec/specs/polytag_test_taggable_three_spec.rb +0 -51
  41. data/spec/specs/polytag_test_taggable_two_spec.rb +0 -51
  42. data/spec/support/test_taggable_four.rb +0 -7
  43. data/spec/support/test_taggable_one.rb +0 -3
  44. data/spec/support/test_taggable_three.rb +0 -8
  45. data/spec/support/test_taggable_two.rb +0 -7
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: b081aadd687235fc80ef0e6fe2ac99bdccf8edff
4
- data.tar.gz: 559397b671a71e791ad423a0221260ca4112bb0c
5
- SHA512:
6
- metadata.gz: a316be1f475ad261c166c43e3689091f70f4e98b881c195147400f71a62569eb7b3cd10add70f83965831d8a539a3075dedcd77d8f6e18d4e9613304384ae922
7
- data.tar.gz: 8b37be6c30b19c9bf7ff2809bef05398f287fa36be499955c9765ceddea4b7669166932e84439a91f52faa44ca77274023e3fdf33361f09a8076d00c2839183a
data/circle.yml DELETED
@@ -1,3 +0,0 @@
1
- database:
2
- override:
3
- - echo "Using SQLite3 in memory"
@@ -1,17 +0,0 @@
1
- module Polytag::TagGroup::Owner
2
- def self.included(base)
3
- base.has_many :polytag_tag_groups, class_name: '::Polytag::TagGroup',
4
- as: :owner
5
-
6
- base.has_many :tag_groups, class_name: '::Polytag::TagGroup',
7
- as: :owner
8
-
9
- base.has_many :polytag_owned_tags, class_name: '::Polytag::Tag',
10
- through: :polytag_tag_groups,
11
- source: :polytag_tag
12
-
13
- base.has_many :owned_tags, class_name: '::Polytag::Tag',
14
- through: :polytag_tag_groups,
15
- source: :polytag_tag
16
- end
17
- end
@@ -1,15 +0,0 @@
1
- class Polytag::TagRelation < ActiveRecord::Base
2
- self.table_name = :polytag_tag_relations
3
- belongs_to :polytag_tag, class_name: '::Polytag::Tag'
4
- belongs_to :tag, class_name: '::Polytag::Tag', foreign_key: :polytag_tag_id
5
- belongs_to :tagged, polymorphic: true
6
-
7
- # Cleanup tag if there are
8
- # no more relations left on the tag
9
- after_destroy do
10
- tag.reload
11
- if tag.relations.count < 0
12
- tag.destroy
13
- end
14
- end
15
- end
@@ -1,56 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Create a a model with tags and query for them" do
4
-
5
- # Create some random models with tags
6
- let!(:sample_classes) do
7
- (0..5).to_a.map do |t|
8
- klass = TestTaggableThree.create!(name: "test_#{Time.now}")
9
- klass.tag_group(name: 'Apple')
10
- entropy = ('A'..'E').to_a.shuffle.sample((2..3).to_a.sample)
11
- klass.add_tags!(*entropy.concat(%w(A)))
12
- klass
13
- end
14
- end
15
-
16
- # Create the model I will be testing regularly
17
- let!(:time) { Time.now.to_s }
18
- let(:test_taggable) do
19
- TestTaggableThree.create!(name: "test_#{time}")
20
- end
21
-
22
- it "Should have a tag group attached" do
23
- test_taggable.name.should == "test_#{time}"
24
- test_taggable.should be_a(TestTaggableThree)
25
- test_taggable.tag_group.should be_a(Polytag::TagGroup)
26
- end
27
-
28
- context "Query for the tags in question on the default tag_group " do
29
- before(:each) do
30
- test_taggable.add_tag!('A')
31
- end
32
-
33
- it "Test searching for our `test_taggable` model with the A tag" do
34
- TestTaggableThree.has_tag('A').should include(test_taggable)
35
- TestTaggableThree.has_tag('B').should_not include(test_taggable)
36
- end
37
-
38
- it "Should not find our `test_taggable` model in the `Apple` tag_group" do
39
- TestTaggableThree.in_tag_group(name: 'Apple').should_not include(test_taggable)
40
- TestTaggableThree.in_tag_group(name: 'Apple').should_not be_empty
41
- TestTaggableThree.in_tag_group(name: 'Apple').count.should == 6
42
- end
43
-
44
- it "Should not find our `test_taggable` model in the `Apple` tag_group even if it shares a tag name" do
45
- TestTaggableThree.in_tag_group(name: 'Apple').has_tag('A').should_not include(test_taggable)
46
- end
47
-
48
- it "Should find our `test_taggable` model when we specify the right tag group and tag" do
49
- TestTaggableThree.in_tag_group(owner: test_taggable.tag_group.owner).has_tag('A').should include(test_taggable)
50
- end
51
-
52
- it "Should find our `test_taggable` model when we specify the right tag group" do
53
- TestTaggableThree.in_tag_group(owner: test_taggable.tag_group.owner).should include(test_taggable)
54
- end
55
- end
56
- end
@@ -1,54 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Create a tag with a tag group with owner passed as a object and tag group name" do
4
- let!(:time) { Time.now.to_s }
5
- let(:test_taggable) do
6
- TestTaggableFour.create!(name: "test_#{time}")
7
- end
8
-
9
- it "Should have a tag group attached" do
10
- test_taggable.name.should == "test_#{time}"
11
- test_taggable.should be_a(TestTaggableFour)
12
- test_taggable.tag_group.should be_a(Polytag::TagGroup)
13
- test_taggable.tag_group.should_not be_nil
14
- end
15
-
16
- context "Add a tag " do
17
- before(:each) do
18
- test_taggable.add_tag!(:pie)
19
- end
20
-
21
- it "Should have a tag on the TestTaggableTwo model with the right tag group" do
22
- tags = test_taggable.tags(true)
23
- tags.size.should == 1
24
- tags.first.name.should == 'pie'
25
- tags.first.tag_group.should == test_taggable.tag_group
26
- tags.first.tag_group.name.should == test_taggable.tag_group.name
27
- end
28
-
29
- it "Should delete the tag on the TestTaggable model" do
30
- test_taggable.remove_tag!(:pie)
31
- test_taggable.tags(true).should be_empty
32
- end
33
-
34
- it "Should add an another tag and have two" do
35
- test_taggable.add_tag!(:pizza)
36
- tags = test_taggable.tags(true)
37
- tags.size.should == 2
38
- tags.map(&:name).should == ['pie', 'pizza']
39
-
40
- tags.each do |tag|
41
- tag.tag_group.should == test_taggable.tag_group
42
- tag.tag_group.name.should == test_taggable.tag_group.name
43
- end
44
- end
45
-
46
- it "Should add an another tag and delete the original" do
47
- test_taggable.add_tag!(:pizza)
48
- test_taggable.remove_tag!(:pie)
49
- tags = test_taggable.tags(true)
50
- tags.size.should == 1
51
- tags.first.name.should == 'pizza'
52
- end
53
- end
54
- end
@@ -1,47 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Create a tag without a tag group" do
4
- let!(:time) { Time.now.to_s }
5
- let(:test_taggable) { TestTaggableOne.create!(name: "test_#{time}") }
6
-
7
- it "Should create a model of TestTaggable with test_timestamp" do
8
- test_taggable.name.should == "test_#{time}"
9
- test_taggable.should be_a(TestTaggableOne)
10
- end
11
-
12
- it "Should not have a tag group" do
13
- test_taggable.tag_group.should be_nil
14
- end
15
-
16
- context "Add a tag" do
17
- before(:each) do
18
- test_taggable.add_tag!(:pie)
19
- end
20
-
21
- it "Should have a tag on the TestTaggable model" do
22
- tags = test_taggable.tags(true)
23
- tags.size.should == 1
24
- tags.first.name.should == 'pie'
25
- end
26
-
27
- it "Should delete the tag on the TestTaggable model" do
28
- test_taggable.remove_tag!(:pie)
29
- test_taggable.tags(true).should be_empty
30
- end
31
-
32
- it "Should add an another tag and have two" do
33
- test_taggable.add_tag!(:pizza)
34
- tags = test_taggable.tags(true)
35
- tags.size.should == 2
36
- tags.map(&:name).should == ['pie', 'pizza']
37
- end
38
-
39
- it "Should add an another tag and delete the original" do
40
- test_taggable.add_tag!(:pizza)
41
- test_taggable.remove_tag!(:pie)
42
- tags = test_taggable.tags(true)
43
- tags.size.should == 1
44
- tags.first.name.should == 'pizza'
45
- end
46
- end
47
- end
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Create a tag with a tag group with owner passed with type and id" do
4
- let!(:time) { Time.now.to_s }
5
- let(:test_taggable) do
6
- TestTaggableThree.create!(name: "test_#{time}")
7
- end
8
-
9
- it "Should have a tag group attached" do
10
- test_taggable.name.should == "test_#{time}"
11
- test_taggable.should be_a(TestTaggableThree)
12
- test_taggable.tag_group.should be_a(Polytag::TagGroup)
13
- end
14
-
15
- context "Add a tag " do
16
- before(:each) do
17
- test_taggable.add_tag!(:pie)
18
- end
19
-
20
- it "Should have a tag on the TestTaggableTwo model with the right tag group" do
21
- tags = test_taggable.tags(true)
22
- tags.size.should == 1
23
- tags.first.name.should == 'pie'
24
- tags.first.tag_group.should == test_taggable.tag_group
25
- end
26
-
27
- it "Should delete the tag on the TestTaggable model" do
28
- test_taggable.remove_tag!(:pie)
29
- test_taggable.tags(true).should be_empty
30
- end
31
-
32
- it "Should add an another tag and have two" do
33
- test_taggable.add_tag!(:pizza)
34
- tags = test_taggable.tags(true)
35
- tags.size.should == 2
36
- tags.map(&:name).should == ['pie', 'pizza']
37
-
38
- tags.each do |tag|
39
- tag.tag_group.should == test_taggable.tag_group
40
- end
41
- end
42
-
43
- it "Should add an another tag and delete the original" do
44
- test_taggable.add_tag!(:pizza)
45
- test_taggable.remove_tag!(:pie)
46
- tags = test_taggable.tags(true)
47
- tags.size.should == 1
48
- tags.first.name.should == 'pizza'
49
- end
50
- end
51
- end
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Create a tag with a tag group with owner passed as a object" do
4
- let!(:time) { Time.now.to_s }
5
- let(:test_taggable) do
6
- TestTaggableTwo.create!(name: "test_#{time}")
7
- end
8
-
9
- it "Should have a tag group attached" do
10
- test_taggable.name.should == "test_#{time}"
11
- test_taggable.should be_a(TestTaggableTwo)
12
- test_taggable.tag_group.should be_a(Polytag::TagGroup)
13
- end
14
-
15
- context "Add a tag " do
16
- before(:each) do
17
- test_taggable.add_tag!(:pie)
18
- end
19
-
20
- it "Should have a tag on the TestTaggableTwo model with the right tag group" do
21
- tags = test_taggable.tags(true)
22
- tags.size.should == 1
23
- tags.first.name.should == 'pie'
24
- tags.first.tag_group.should == test_taggable.tag_group
25
- end
26
-
27
- it "Should delete the tag on the TestTaggable model" do
28
- test_taggable.remove_tag!(:pie)
29
- test_taggable.tags(true).should be_empty
30
- end
31
-
32
- it "Should add an another tag and have two" do
33
- test_taggable.add_tag!(:pizza)
34
- tags = test_taggable.tags(true)
35
- tags.size.should == 2
36
- tags.map(&:name).should == ['pie', 'pizza']
37
-
38
- tags.each do |tag|
39
- tag.tag_group.should == test_taggable.tag_group
40
- end
41
- end
42
-
43
- it "Should add an another tag and delete the original" do
44
- test_taggable.add_tag!(:pizza)
45
- test_taggable.remove_tag!(:pie)
46
- tags = test_taggable.tags(true)
47
- tags.size.should == 1
48
- tags.first.name.should == 'pizza'
49
- end
50
- end
51
- end
@@ -1,7 +0,0 @@
1
- class TestTaggableFour < ActiveRecord::Base
2
- include Polytag
3
-
4
- after_initialize do
5
- tag_group owner: Owner.create!, name: 'Employees'
6
- end
7
- end
@@ -1,3 +0,0 @@
1
- class TestTaggableOne < ActiveRecord::Base
2
- include Polytag
3
- end
@@ -1,8 +0,0 @@
1
- class TestTaggableThree < ActiveRecord::Base
2
- include Polytag
3
-
4
- after_initialize do
5
- tag_group_owner = Owner.create!
6
- tag_group owner_type: tag_group_owner.class.name, owner_id: tag_group_owner.id
7
- end
8
- end
@@ -1,7 +0,0 @@
1
- class TestTaggableTwo < ActiveRecord::Base
2
- include Polytag
3
-
4
- after_initialize do
5
- tag_group owner: Owner.create!
6
- end
7
- end