gutentag 0.2.0 → 0.2.1

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: 34f0e855c14d22751ac57bf0ddc8be91effd346c
4
- data.tar.gz: 59c1247fd49e8cc37bb6e22b7fb3903567a8f5af
3
+ metadata.gz: 33f154c781133dc5cc122ae754f11216f6034916
4
+ data.tar.gz: 39d23400e618e07ec79d5ae0c211bedcaf8a78be
5
5
  SHA512:
6
- metadata.gz: 03e82d9335afdbe03d9ac06625da1f2608d8394a6397ff405c293585ce38d3206f4bcc39285e8492ee4a49a8afabe101fe09f9772b557e88e8ab1c91999e9a97
7
- data.tar.gz: ad34f1a97036b19f73d7e8edf48bac7a73bb8e1f25759107897ec2bfd43ad49943b7d61be7370b6f79756997b8bf646dadd9fbce4917d2ec96163b833eab806e
6
+ metadata.gz: ab28289725aecf21bbf6d620e83abb8acf90c579604e54c1bdaa851b888284380e0b3e031003fe621d30af8b31dc6fbe1dd12ee8fd7a118e8c67650c4991253c
7
+ data.tar.gz: ec3a116528231a1b3ddb9574c03df3e35b4592d88249b65d49ce992e2a4058cce9dc8316cdbdd2c613a4be986e6cd88370ab33ef3d6cce1b5c9d586005df8881
data/README.md CHANGED
@@ -11,7 +11,7 @@ This was built partly as a proof-of-concept, and partly to see how a tagging gem
11
11
 
12
12
  Get it into your Gemfile - and don't forget the version constraint!
13
13
 
14
- gem 'gutentag', '~> 0.2.0'
14
+ gem 'gutentag', '~> 0.2.1'
15
15
 
16
16
  Next: your tags get persisted to your database, so let's import and run the migrations to get the tables set up:
17
17
 
@@ -2,10 +2,20 @@ class Gutentag::Tag < ActiveRecord::Base
2
2
  has_many :taggings, :class_name => 'Gutentag::Tagging',
3
3
  :dependent => :destroy
4
4
 
5
+ attr_accessible :name
6
+
5
7
  validates :name, :presence => true, :uniqueness => {:case_sensitive => false}
6
8
 
7
9
  before_validation :normalise_name
8
10
 
11
+ def self.find_by_name(name)
12
+ where(:name => Gutentag::TagName.normalise(name)).first
13
+ end
14
+
15
+ def self.find_or_create(name)
16
+ find_by_name(name) || create(:name => name)
17
+ end
18
+
9
19
  private
10
20
 
11
21
  def normalise_name
data/gutentag.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'gutentag'
4
- s.version = '0.2.0'
4
+ s.version = '0.2.1'
5
5
  s.authors = ['Pat Allan']
6
6
  s.email = ['pat@freelancing-gods.com']
7
7
  s.homepage = 'https://github.com/pat/gutentag'
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.require_paths = ['lib']
15
15
 
16
16
  s.add_runtime_dependency 'activerecord', '>= 3.2.0'
17
- s.add_development_dependency 'combustion', '~> 0.4.0'
17
+ s.add_development_dependency 'combustion', '~> 0.5.0'
18
18
  s.add_development_dependency 'rspec-rails', '~> 2.13'
19
19
  s.add_development_dependency 'sqlite3', '~> 1.3.7'
20
20
  end
@@ -2,10 +2,7 @@ class Gutentag::TagNames
2
2
  include Enumerable
3
3
 
4
4
  def self.new_with_names(taggable, names)
5
- tag_names = new(taggable)
6
- tag_names.clear
7
- names.each { |name| tag_names << Gutentag::TagName.normalise(name) }
8
- tag_names
5
+ new(taggable).replace names
9
6
  end
10
7
 
11
8
  def initialize(taggable)
@@ -17,7 +14,7 @@ class Gutentag::TagNames
17
14
  end
18
15
 
19
16
  def +(array)
20
- (normalised(array) - to_a).each { |name| self.<< name }
17
+ normalised(array).each { |name| self.<< name }
21
18
 
22
19
  self
23
20
  end
@@ -29,9 +26,7 @@ class Gutentag::TagNames
29
26
  end
30
27
 
31
28
  def <<(name)
32
- name = Gutentag::TagName.normalise name
33
- tag = Gutentag::Tag.where(:name => name).first ||
34
- Gutentag::Tag.create(:name => name)
29
+ tag = Gutentag::Tag.find_or_create name
35
30
 
36
31
  taggable.tags << tag unless taggable.tags.include?(tag)
37
32
  end
@@ -49,13 +44,18 @@ class Gutentag::TagNames
49
44
  end
50
45
 
51
46
  def delete(name)
52
- taggable.tags.delete Gutentag::Tag.where(:name => name).first
47
+ taggable.tags.delete Gutentag::Tag.find_by_name(name)
53
48
  end
54
49
 
55
50
  def each(&block)
56
51
  to_a.each &block
57
52
  end
58
53
 
54
+ def replace(names)
55
+ clear
56
+ self.+ names
57
+ end
58
+
59
59
  private
60
60
 
61
61
  attr_reader :taggable
@@ -1,6 +1,42 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Gutentag::Tag do
4
+ describe '.find_by_name' do
5
+ it "returns a tag with the same name" do
6
+ existing = Gutentag::Tag.create! :name => 'pancakes'
7
+
8
+ Gutentag::Tag.find_by_name('pancakes').should == existing
9
+ end
10
+
11
+ it "returns a tag with the same normalised name" do
12
+ existing = Gutentag::Tag.create! :name => 'pancakes'
13
+
14
+ Gutentag::Tag.find_by_name('Pancakes').should == existing
15
+ end
16
+
17
+ it "otherwise returns nil" do
18
+ Gutentag::Tag.find_by_name('pancakes').should be_nil
19
+ end
20
+ end
21
+
22
+ describe '.find_or_create' do
23
+ it "returns a tag with the same name" do
24
+ existing = Gutentag::Tag.create! :name => 'pancakes'
25
+
26
+ Gutentag::Tag.find_or_create('pancakes').should == existing
27
+ end
28
+
29
+ it "returns a tag with the same normalised name" do
30
+ existing = Gutentag::Tag.create! :name => 'pancakes'
31
+
32
+ Gutentag::Tag.find_or_create('Pancakes').should == existing
33
+ end
34
+
35
+ it "creates a new tag if no matches exist" do
36
+ Gutentag::Tag.find_or_create('pancakes').should be_persisted
37
+ end
38
+ end
39
+
4
40
  describe '#name' do
5
41
  before :each do
6
42
  Gutentag::TagName.stub :normalise => 'waffles'
@@ -6,11 +6,15 @@ describe Gutentag::Tagging do
6
6
  let(:taggable) { Article.create! }
7
7
 
8
8
  it "ensures tags are unique for any given taggable" do
9
- Gutentag::Tagging.create! :tag => tag, :taggable => taggable
9
+ tagging = Gutentag::Tagging.new
10
+ tagging.tag = tag
11
+ tagging.taggable = taggable
12
+ tagging.save!
10
13
 
11
- Gutentag::Tagging.create(
12
- :tag => tag, :taggable => taggable
13
- ).should have(1).error_on(:tag_id)
14
+ tagging = Gutentag::Tagging.new
15
+ tagging.tag = tag
16
+ tagging.taggable = taggable
17
+ tagging.should have(1).error_on(:tag_id)
14
18
  end
15
19
  end
16
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gutentag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Allan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-15 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 0.4.0
33
+ version: 0.5.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: 0.4.0
40
+ version: 0.5.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec-rails
43
43
  requirement: !ruby/object:Gem::Requirement