polytag 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b2a311af9d80377774aaae39a925d1e090a0a4e
4
- data.tar.gz: 1898ea870946ff0e349786a074697642198c7fa5
3
+ metadata.gz: a6d4b2e842f885c1ac09bcf1cc4660712164f336
4
+ data.tar.gz: 52c8c36367c902a736735c68de3fb224cb94a4f4
5
5
  SHA512:
6
- metadata.gz: 76a6da2782ca55f9677283cc988fb3a711faa3f52ccdc5d03d59ae52d8eafd7eb25b1b25c0512c4c0c1aeb0fc9339aa592b108bda41d4654c0fb0f2ec9683173
7
- data.tar.gz: 02247eed75021f18523e7ec4107cb4f268dfe8c369714312f93a52ea8821d3c4928ecfadebd2b4696f689ea07e681f417069c69b834f69e51857d9f778ee1b85
6
+ metadata.gz: 55e8dac4832358fa356f6573e5d67252e547a309826641782cacc224b6dec2645245cd8b7ca9d90d212a9d0bfead1eae88cf1c067def7c705e6bc44cca01c7db
7
+ data.tar.gz: a64fe6106e4c2b6aaaeef87ed9c04f87857e1cab3663dcab4f53fe52312d7200230e37924386a2e21eb671d8aea794f23e7d4f3fd29f7a7291150b95579ec369
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Polytag
2
2
 
3
+ ![CircleCI Build Status](https://circleci.com/gh/JIFFinc/polytag.png)
4
+
3
5
  Adds the ability to easily add tags with optional tag groups to models.
4
6
 
5
7
  ## Installation
@@ -0,0 +1,3 @@
1
+ database:
2
+ override:
3
+ - echo "Using SQLite3 in memory"
@@ -8,24 +8,42 @@ module Polytag
8
8
  base.extend(ClassMethods)
9
9
  base.has_many :polytag_tag_relations, as: :tagged, class_name: '::Polytag::TagRelation'
10
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)
11
+ base.__send__(:alias_method, :tag_relations, :polytag_tag_relations) unless base.method_defined?(:tag_relations)
12
+ base.__send__(:alias_method, :tags, :polytag_tags) unless base.method_defined?(:tags)
13
13
  end
14
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
15
+ def tag_group(_tag_group = nil)
16
+ @__polytag_tag_group_hash__ ||= {}
17
+
18
+ if _tag_group
19
+ @__polytag_tag_group_hash__.merge!(_tag_group)
20
+ @__polytag_tag_group__ = Polytag::TagGroup.search_by_hash(@__polytag_tag_group_hash__).first_or_create
21
+ else
22
+ @__polytag_tag_group_hash__ = {} if _tag_group.is_a?(Hash) && _tag_group.empty?
23
+ @__polytag_tag_group__
24
+ end
17
25
  end
18
26
 
19
27
  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
28
+ polytag_tags << polytag_tags.where(name: tag, polytag_tag_group_id: tag_group(_tag_group).try(&:id)).first_or_initialize
21
29
  end
22
30
 
23
31
  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
32
+ polytag_tags << polytag_tags.where(name: tag, polytag_tag_group_id: tag_group(_tag_group).try(&:id)).first_or_create
33
+ end
34
+
35
+ def add_tags(*_tags)
36
+ _tag_group = _tags.pop if _tags.last.is_a?(Hash)
37
+ _tags.map { |x| add_tag(x, _tag_group || P{}) }
38
+ end
39
+
40
+ def add_tags!(*_tags)
41
+ _tag_group = _tags.pop if _tags.last.is_a?(Hash)
42
+ _tags.map { |x| add_tag!(x, _tag_group || {}) }
25
43
  end
26
44
 
27
45
  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)
46
+ tag_id = polytag_tags.where(name: tag, polytag_tag_group_id: tag_group(_tag_group).try(&:id)).first.try(:id)
29
47
  tag_relations.where("polytag_tag_relations.polytag_tag_id = ?", tag_id).delete_all
30
48
  end
31
49
 
@@ -33,6 +33,10 @@ class Polytag::TagGroup < ActiveRecord::Base
33
33
 
34
34
  # Query by tag group name
35
35
  conditions.merge!(name: "#{hash[:name]}") if hash[:name]
36
+
37
+ # Query by tag group id
38
+ conditions.merge!(id: hash[:id]) if hash[:id]
39
+
36
40
  where(conditions)
37
41
  end
38
42
 
@@ -1,3 +1,3 @@
1
1
  module Polytag
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -3,11 +3,12 @@ require 'spec_helper'
3
3
  describe "Create a a model with tags and query for them" do
4
4
 
5
5
  # Create some random models with tags
6
- let(:sample_classes) do
6
+ let!(:sample_classes) do
7
7
  (0..5).to_a.map do |t|
8
8
  klass = TestTaggableThree.create!(name: "test_#{Time.now}")
9
9
  klass.tag_group(name: 'Apple')
10
- klass.add_tag!(*('A'..'E').shuffle.sample((2..3).sample))
10
+ entropy = ('A'..'E').to_a.shuffle.sample((2..3).to_a.sample)
11
+ klass.add_tags!(*entropy.concat(%w(A)))
11
12
  klass
12
13
  end
13
14
  end
@@ -36,6 +37,8 @@ describe "Create a a model with tags and query for them" do
36
37
 
37
38
  it "Should not find our `test_taggable` model in the `Apple` tag_group" do
38
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
39
42
  end
40
43
 
41
44
  it "Should not find our `test_taggable` model in the `Apple` tag_group even if it shares a tag name" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polytag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Becker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-11 00:00:00.000000000 Z
11
+ date: 2013-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -135,6 +135,7 @@ files:
135
135
  - LICENSE.txt
136
136
  - README.md
137
137
  - Rakefile
138
+ - circle.yml
138
139
  - lib/generators/polytag/install/install_generator.rb
139
140
  - lib/generators/polytag/install/templates/create_polytag_tables.rb
140
141
  - lib/polytag.rb