polytag 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b2a311af9d80377774aaae39a925d1e090a0a4e
4
+ data.tar.gz: 1898ea870946ff0e349786a074697642198c7fa5
5
+ SHA512:
6
+ metadata.gz: 76a6da2782ca55f9677283cc988fb3a711faa3f52ccdc5d03d59ae52d8eafd7eb25b1b25c0512c4c0c1aeb0fc9339aa592b108bda41d4654c0fb0f2ec9683173
7
+ data.tar.gz: 02247eed75021f18523e7ec4107cb4f268dfe8c369714312f93a52ea8821d3c4928ecfadebd2b4696f689ea07e681f417069c69b834f69e51857d9f778ee1b85
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format documentation
3
+ --backtrace
4
+ --profile
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in polytag.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kelly Becker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Polytag
2
+
3
+ Adds the ability to easily add tags with optional tag groups to models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'polytag'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install polytag
18
+
19
+ ## Usage
20
+
21
+ Please see specs in the meantime.
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module Polytag
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ source_root File.expand_path('../templates', __FILE__)
8
+ desc "Add the migrations for Polytags"
9
+
10
+ def self.next_migration_number(path)
11
+ unless @prev_migration_nr
12
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
13
+ else
14
+ @prev_migration_nr += 1
15
+ end
16
+ @prev_migration_nr.to_s
17
+ end
18
+
19
+ def copy_migrations
20
+ migration_template "create_polytag_tables.rb", "db/migrate/create_polytag_tables.rb"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ class CreatePolytagTables < ActiveRecord::Migration
2
+ def self.up
3
+ # Create the tags table
4
+ create_table :polytag_tags do |t|
5
+ t.belongs_to :polytag_tag_group, index:true, null: true, default: nil
6
+ t.string :name, index: true
7
+ t.timestamps
8
+ end
9
+
10
+ # Create the relations table
11
+ create_table :polytag_tag_relations do |t|
12
+ t.belongs_to :tagged, polymorphic: true, index: true
13
+ t.belongs_to :polytag_tag, index: true
14
+ t.timestamps
15
+ end
16
+
17
+ # Create the tag groups table
18
+ create_table :polytag_tag_groups do |t|
19
+ t.belongs_to :owner, polymorphic: true, index: true
20
+ t.string :name, index: true, null: true, default: nil
21
+ t.timestamps
22
+ end
23
+
24
+ # Index for the category and name
25
+ add_index :polytag_tags, [:polytag_tag_group_id, :name], unique: true
26
+ add_index :polytag_tag_groups, [:owner_type, :owner_id, :name], unique: true
27
+ end
28
+
29
+ def self.down
30
+ drop_table :polytag_tags
31
+ drop_table :polytag_tag_relations
32
+ end
33
+ end
@@ -0,0 +1,60 @@
1
+ require "polytag/version"
2
+ require "polytag/tag"
3
+ require "polytag/tag_group"
4
+ require "polytag/tag_relation"
5
+
6
+ module Polytag
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ base.has_many :polytag_tag_relations, as: :tagged, class_name: '::Polytag::TagRelation'
10
+ base.has_many :polytag_tags, through: :polytag_tag_relations, class_name: '::Polytag::Tag'
11
+ base.__send__(:alias_method, :tag_relations, :polytag_tag_relations)
12
+ base.__send__(:alias_method, :tags, :polytag_tags)
13
+ end
14
+
15
+ def tag_group(tag_group = {})
16
+ tag_group.empty? ? @__polytag_tag_group__ : @__polytag_tag_group__ = Polytag::TagGroup.search_by_hash(tag_group).first_or_create
17
+ end
18
+
19
+ def add_tag(tag, _tag_group = {})
20
+ tags << tags.where(name: tag, polytag_tag_group_id: tag_group(_tag_group).try(&:id)).first_or_initialize
21
+ end
22
+
23
+ def add_tag!(tag, _tag_group = {})
24
+ tags << tags.where(name: tag, polytag_tag_group_id: tag_group(_tag_group).try(&:id)).first_or_create
25
+ end
26
+
27
+ def remove_tag!(tag, _tag_group = {})
28
+ tag_id = tags.where(name: tag, polytag_tag_group_id: tag_group(_tag_group).try(&:id)).first.try(:id)
29
+ tag_relations.where("polytag_tag_relations.polytag_tag_id = ?", tag_id).delete_all
30
+ end
31
+
32
+ module ClassMethods
33
+
34
+ # Get records with tags
35
+ def has_tags(*tags)
36
+ includes(polytag_tag_relations: :polytag_tag).references(:polytag_tags).where("polytag_tags.name IN (?)", tags).group("#{table_name}.id")
37
+ end
38
+
39
+ alias_method :has_tag, :has_tags
40
+
41
+ def in_tag_group(_tag_group = {})
42
+ if _tag_group[:group_ids]
43
+ tag_groups = _tag_group[:group_ids]
44
+ else
45
+ tag_groups = Polytag::TagGroup.select('polytag_tag_groups.id').search_by_hash(_tag_group).map{ |tg| tg.try(:id) }.flatten
46
+ end
47
+
48
+ return tag_groups if _tag_group[:multi]
49
+
50
+ includes(polytag_tag_relations: :polytag_tag).references(:polytag_tags).where('polytag_tags.polytag_tag_group_id IN (?)', tag_groups)
51
+ end
52
+
53
+ def in_tag_groups(*tag_groups)
54
+ tag_groups = tag_groups.map{ |tg| in_tag_group(tg.merge(multi: true)) }.flatten.compact
55
+ in_tag_group(group_ids: tag_groups)
56
+ end
57
+
58
+ # @TODO: Implement results must be connected to all tags
59
+ end
60
+ end
@@ -0,0 +1,25 @@
1
+ class Polytag::Tag < ActiveRecord::Base
2
+ self.table_name = :polytag_tags
3
+ belongs_to :polytag_tag_group, class_name: '::Polytag::TagGroup'
4
+ has_many :polytag_tag_relations, class_name: '::Polytag::TagRelation',
5
+ foreign_key: :polytag_tag_id,
6
+ dependent: :destroy
7
+
8
+ alias_method :tag_group, :polytag_tag_group
9
+ alias_method :relations, :polytag_tag_relations
10
+
11
+ # Indiscrimanetly get
12
+ # the tagged models
13
+ def tagged
14
+ relations.tagged
15
+ end
16
+
17
+ # Cleanup group if there are
18
+ # no more tags left on the group
19
+ after_destroy do
20
+ tag_group.reload
21
+ if tag_group.tags.count < 0
22
+ tag_group.destroy
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,43 @@
1
+ class Polytag::TagGroup < ActiveRecord::Base
2
+ self.table_name = :polytag_tag_groups
3
+ has_many :polytag_tag, class_name: '::Polytag::Tag',
4
+ dependent: :destroy
5
+
6
+ belongs_to :owner, polymorphic: true
7
+ alias_method :tag, :polytag_tag
8
+
9
+ # Cleanup tag if there are
10
+ # no more relations left on the tag
11
+ after_destroy do
12
+ tag.reload
13
+ if tag.relations.count < 0
14
+ tag.destroy
15
+ end
16
+ end
17
+
18
+ class << self
19
+ def search_by_hash(hash = {})
20
+ return self if hash.empty?
21
+ conditions = {}
22
+
23
+ # Query by owner information
24
+ if hash[:owner]
25
+ conditions.merge!(owner_type: "#{hash[:owner].class}")
26
+ conditions.merge!(owner_id: hash[:owner].id)
27
+ else
28
+ if hash[:owner_type]
29
+ conditions.merge!(owner_type: "#{hash[:owner_type]}")
30
+ conditions.merge!(owner_id: hash[:owner_id]) if hash[:owner_id]
31
+ end
32
+ end
33
+
34
+ # Query by tag group name
35
+ conditions.merge!(name: "#{hash[:name]}") if hash[:name]
36
+ where(conditions)
37
+ end
38
+
39
+ def find_by_hash(hash = {})
40
+ search_by_hash(hash).first
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
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 :tagged, polymorphic: true
5
+ alias_method :tag, :polytag_tag
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
@@ -0,0 +1,3 @@
1
+ module Polytag
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'polytag/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "polytag"
8
+ spec.version = Polytag::VERSION
9
+ spec.authors = ["Kelly Becker"]
10
+ spec.email = ["kellylsbkr@gmail.com"]
11
+ spec.description = %q{Provides really easy tagging for rails and ActiveRecord models}
12
+ spec.summary = %q{Provides really easy tagging for rails and ActiveRecord models}
13
+ spec.homepage = "http://github.com/JIFFinc/polytag"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rspec-rails"
25
+ spec.add_development_dependency "sqlite3"
26
+ spec.add_development_dependency "awesome_print"
27
+ spec.add_dependency "activerecord", ">= 3.2.13"
28
+ spec.add_dependency "activesupport", ">= 3.2.13"
29
+ end
@@ -0,0 +1,47 @@
1
+ SPEC_DIR = File.dirname(__FILE__)
2
+ GEM_DIR = File.dirname(SPEC_DIR)
3
+
4
+ # Load dependencies
5
+ require 'rspec/rails/extensions/active_record/base'
6
+ require 'active_record'
7
+ require 'awesome_print'
8
+
9
+ # Load in the support for AR
10
+ require 'support/active_record'
11
+
12
+ # Load in the gem we are testing
13
+ require 'polytag'
14
+
15
+ # The test models
16
+ require 'support/owner'
17
+ require 'support/test_taggable_one'
18
+ require 'support/test_taggable_two'
19
+ require 'support/test_taggable_three'
20
+ require 'support/test_taggable_four'
21
+
22
+ # This file was generated by the `rspec --init` command. Conventionally, all
23
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
24
+ # Require this file using `require "spec_helper"` to ensure that it is only
25
+ # loaded once.
26
+ #
27
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
28
+
29
+ RSpec.configure do |config|
30
+ config.treat_symbols_as_metadata_keys_with_true_values = true
31
+ config.run_all_when_everything_filtered = true
32
+ config.filter_run :focus
33
+
34
+ # Handle ActiveRecord transactions
35
+ config.around do |example|
36
+ ActiveRecord::Base.transaction do
37
+ example.run
38
+ raise ActiveRecord::Rollback
39
+ end
40
+ end
41
+
42
+ # Run specs in random order to surface order dependencies. If you find an
43
+ # order dependency and want to debug it, you can fix the order by providing
44
+ # the seed, which is printed after each run.
45
+ # --seed 1234
46
+ config.order = 'random'
47
+ end
@@ -0,0 +1,53 @@
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
+ klass.add_tag!(*('A'..'E').shuffle.sample((2..3).sample))
11
+ klass
12
+ end
13
+ end
14
+
15
+ # Create the model I will be testing regularly
16
+ let!(:time) { Time.now.to_s }
17
+ let(:test_taggable) do
18
+ TestTaggableThree.create!(name: "test_#{time}")
19
+ end
20
+
21
+ it "Should have a tag group attached" do
22
+ test_taggable.name.should == "test_#{time}"
23
+ test_taggable.should be_a(TestTaggableThree)
24
+ test_taggable.tag_group.should be_a(Polytag::TagGroup)
25
+ end
26
+
27
+ context "Query for the tags in question on the default tag_group " do
28
+ before(:each) do
29
+ test_taggable.add_tag!('A')
30
+ end
31
+
32
+ it "Test searching for our `test_taggable` model with the A tag" do
33
+ TestTaggableThree.has_tag('A').should include(test_taggable)
34
+ TestTaggableThree.has_tag('B').should_not include(test_taggable)
35
+ end
36
+
37
+ it "Should not find our `test_taggable` model in the `Apple` tag_group" do
38
+ TestTaggableThree.in_tag_group(name: 'Apple').should_not include(test_taggable)
39
+ end
40
+
41
+ it "Should not find our `test_taggable` model in the `Apple` tag_group even if it shares a tag name" do
42
+ TestTaggableThree.in_tag_group(name: 'Apple').has_tag('A').should_not include(test_taggable)
43
+ end
44
+
45
+ it "Should find our `test_taggable` model when we specify the right tag group and tag" do
46
+ TestTaggableThree.in_tag_group(owner: test_taggable.tag_group.owner).has_tag('A').should include(test_taggable)
47
+ end
48
+
49
+ it "Should find our `test_taggable` model when we specify the right tag group" do
50
+ TestTaggableThree.in_tag_group(owner: test_taggable.tag_group.owner).should include(test_taggable)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,54 @@
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
@@ -0,0 +1,47 @@
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
@@ -0,0 +1,51 @@
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
@@ -0,0 +1,51 @@
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
@@ -0,0 +1,23 @@
1
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
2
+ # ActiveRecord::Base.connection.disable_query_cache!
3
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
4
+
5
+ # Where is the migration file
6
+ migration_path = 'lib/generators/polytag/install/templates'
7
+ migration_file = File.join(GEM_DIR, migration_path, 'create_polytag_tables.rb')
8
+ require(migration_file)
9
+
10
+ # Run the migration file
11
+ CreatePolytagTables.up
12
+
13
+ [:ones, :twos, :threes, :fours].each do |table|
14
+ ActiveRecord::Migration.create_table "test_taggable_#{table}" do |t|
15
+ t.string :name
16
+ t.timestamps
17
+ end
18
+ end
19
+
20
+ ActiveRecord::Migration.create_table :owners do |t|
21
+ t.string :name
22
+ t.timestamps
23
+ end
@@ -0,0 +1,2 @@
1
+ class Owner < ActiveRecord::Base
2
+ end
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,3 @@
1
+ class TestTaggableOne < ActiveRecord::Base
2
+ include Polytag
3
+ end
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,7 @@
1
+ class TestTaggableTwo < ActiveRecord::Base
2
+ include Polytag
3
+
4
+ after_initialize do
5
+ tag_group owner: Owner.create!
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polytag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kelly Becker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: 3.2.13
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 3.2.13
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 3.2.13
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: 3.2.13
125
+ description: Provides really easy tagging for rails and ActiveRecord models
126
+ email:
127
+ - kellylsbkr@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .rspec
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - lib/generators/polytag/install/install_generator.rb
139
+ - lib/generators/polytag/install/templates/create_polytag_tables.rb
140
+ - lib/polytag.rb
141
+ - lib/polytag/tag.rb
142
+ - lib/polytag/tag_group.rb
143
+ - lib/polytag/tag_relation.rb
144
+ - lib/polytag/version.rb
145
+ - polytag.gemspec
146
+ - spec/spec_helper.rb
147
+ - spec/specs/polytag_querying_spec.rb
148
+ - spec/specs/polytag_test_taggable_four_spec.rb
149
+ - spec/specs/polytag_test_taggable_one_spec.rb
150
+ - spec/specs/polytag_test_taggable_three_spec.rb
151
+ - spec/specs/polytag_test_taggable_two_spec.rb
152
+ - spec/support/active_record.rb
153
+ - spec/support/owner.rb
154
+ - spec/support/test_taggable_four.rb
155
+ - spec/support/test_taggable_one.rb
156
+ - spec/support/test_taggable_three.rb
157
+ - spec/support/test_taggable_two.rb
158
+ homepage: http://github.com/JIFFinc/polytag
159
+ licenses:
160
+ - MIT
161
+ metadata: {}
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - '>='
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubyforge_project:
178
+ rubygems_version: 2.0.3
179
+ signing_key:
180
+ specification_version: 4
181
+ summary: Provides really easy tagging for rails and ActiveRecord models
182
+ test_files:
183
+ - spec/spec_helper.rb
184
+ - spec/specs/polytag_querying_spec.rb
185
+ - spec/specs/polytag_test_taggable_four_spec.rb
186
+ - spec/specs/polytag_test_taggable_one_spec.rb
187
+ - spec/specs/polytag_test_taggable_three_spec.rb
188
+ - spec/specs/polytag_test_taggable_two_spec.rb
189
+ - spec/support/active_record.rb
190
+ - spec/support/owner.rb
191
+ - spec/support/test_taggable_four.rb
192
+ - spec/support/test_taggable_one.rb
193
+ - spec/support/test_taggable_three.rb
194
+ - spec/support/test_taggable_two.rb
195
+ has_rdoc: