gutentag 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dda4e9077e5d19af15ad0ae9bab60a64147f4c11
4
- data.tar.gz: dcd0271e7e842c8edf3fc25ba1f4254c4853369a
3
+ metadata.gz: fc8dba706d5b7e3075cf6043b26ab140b622fcb3
4
+ data.tar.gz: ac691b782f055f9f6d204731ba8b64d5ff21eb18
5
5
  SHA512:
6
- metadata.gz: fc49c2eaa8236fd2991c5213fb53bce4077890135185f0cf5730331603a384813fe0ecf3427d370719e11931044095ba65297c38052a43a4343e396311e77d68
7
- data.tar.gz: b734e64f5731e2458af240c09a26e650a2d103d5d9bdb8649c52a0cf9d868cb1d06f1fd0fd61271c93b4764c3a7ea0dfa79b3f61ae5e6e932e042bd6321977ff
6
+ metadata.gz: 245dc0526ff19021c898615a041e8938056ff459cd863e1a896b0990a0130826c4af08c5362e1e13806014d09c45939e7066af7b7a3fe8feda8f8475be3eb27d
7
+ data.tar.gz: cc4364399ddd4ec9d7dc1964f3359a02e32f1cf5af13baf2da3bee8b88f7a721a5e162f978c6f961c95401e201b9a26df581545e15b23d22134356545ffa553e
data/README.md CHANGED
@@ -12,7 +12,7 @@ This was built partly as a proof-of-concept, and partly to see how a tagging gem
12
12
 
13
13
  Get it into your Gemfile - and don't forget the version constraint!
14
14
 
15
- gem 'gutentag', '~> 0.3.0'
15
+ gem 'gutentag', '~> 0.4.0'
16
16
 
17
17
  Next: your tags get persisted to your database, so let's import and run the migrations to get the tables set up:
18
18
 
@@ -4,6 +4,8 @@ class Gutentag::Tag < ActiveRecord::Base
4
4
 
5
5
  attr_accessible :name if Rails.version.to_s < '4.0.0'
6
6
 
7
+ scope :by_weight, ->{ order('tags.taggings_count DESC') }
8
+
7
9
  validates :name, :presence => true, :uniqueness => {:case_sensitive => false}
8
10
 
9
11
  before_validation :normalise_name
@@ -1,6 +1,6 @@
1
1
  class Gutentag::Tagging < ActiveRecord::Base
2
2
  belongs_to :taggable, :polymorphic => true
3
- belongs_to :tag, :class_name => 'Gutentag::Tag'
3
+ belongs_to :tag, :class_name => 'Gutentag::Tag', :counter_cache => true
4
4
 
5
5
  validates :taggable, :presence => true
6
6
  validates :tag, :presence => true
@@ -0,0 +1,15 @@
1
+ class GutentagCacheCounter < ActiveRecord::Migration
2
+ def up
3
+ add_column :tags, :taggings_count, :integer, :default => 0
4
+ add_index :tags, :taggings_count
5
+
6
+ Gutentag::Tag.reset_column_information
7
+ Gutentag::Tag.pluck(:id).each do |tag_id|
8
+ Gutentag::Tag.reset_counters tag_id, :taggings
9
+ end
10
+ end
11
+
12
+ def down
13
+ remove_column :tags, :taggings_count
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ class NoNullCounters < ActiveRecord::Migration
2
+ def up
3
+ change_column :tags, :taggings_count, :integer, :default => 0,
4
+ :null => false
5
+ end
6
+
7
+ def down
8
+ change_column :tags, :taggings_count, :integer, :default => 0,
9
+ :null => true
10
+ end
11
+ end
@@ -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.3.0'
4
+ s.version = '0.4.0'
5
5
  s.authors = ['Pat Allan']
6
6
  s.email = ['pat@freelancing-gods.com']
7
7
  s.homepage = 'https://github.com/pat/gutentag'
@@ -10,9 +10,7 @@ module Gutentag::ActiveRecord
10
10
  has_many :tags, :class_name => 'Gutentag::Tag',
11
11
  :through => :taggings
12
12
 
13
- after_save Gutentag::Persistence
14
-
15
- attr_writer :tag_names
13
+ after_save { |instance| Gutentag::Persistence.new(instance).persist }
16
14
  end
17
15
  end
18
16
 
@@ -23,4 +21,8 @@ module Gutentag::ActiveRecord
23
21
  def tag_names
24
22
  @tag_names ||= tags.collect(&:name)
25
23
  end
24
+
25
+ def tag_names=(names)
26
+ @tag_names = names
27
+ end
26
28
  end
@@ -1,7 +1,6 @@
1
1
  class Gutentag::Persistence
2
- def self.after_save(taggable)
3
- new(taggable).persist
4
- end
2
+
3
+ attr_writer :tagger, :normaliser
5
4
 
6
5
  def initialize(taggable)
7
6
  @taggable = taggable
@@ -22,17 +21,25 @@ class Gutentag::Persistence
22
21
 
23
22
  def add_new
24
23
  (changes - existing).each do |name|
25
- taggable.tags << Gutentag::Tag.find_or_create(name)
24
+ taggable.tags << tagger.find_or_create(name)
26
25
  end
27
26
  end
28
27
 
29
28
  def normalised(names)
30
- names.collect { |name| Gutentag::TagName.normalise(name) }.uniq
29
+ names.collect { |name| normaliser.call(name) }.uniq
31
30
  end
32
31
 
33
32
  def remove_old
34
33
  (existing - changes).each do |name|
35
- taggable.tags.delete Gutentag::Tag.find_by_name(name)
34
+ taggable.tags.delete tagger.find_by_name(name)
36
35
  end
37
36
  end
37
+
38
+ def tagger
39
+ @tagger ||= Gutentag::Tag
40
+ end
41
+
42
+ def normaliser
43
+ @normaliser ||= Proc.new { |name| Gutentag::TagName.normalise(name) }
44
+ end
38
45
  end
@@ -94,4 +94,8 @@ describe "Managing tags via names" do
94
94
 
95
95
  article.tag_names.should == ['melbourne', 'pancakes']
96
96
  end
97
+
98
+ it "allows overriding of tag_names=" do
99
+ Article.instance_methods(false).should_not include(:tag_names=)
100
+ end
97
101
  end
@@ -35,4 +35,16 @@ describe 'Adding and removing tags' do
35
35
 
36
36
  Gutentag::Tagging.where(:tag_id => pancakes.id).count.should be_zero
37
37
  end
38
+
39
+ it 'should have a mean tag cloud' do
40
+ gorillas = Gutentag::Tag.create(:name => 'gorillas')
41
+ another_article = Article.create
42
+
43
+ article.tags << pancakes
44
+ Gutentag::Tag.by_weight.first.should == pancakes
45
+
46
+ article.tags << gorillas
47
+ another_article.tags << gorillas
48
+ Gutentag::Tag.by_weight.first.should == gorillas
49
+ end
38
50
  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.3.0
4
+ version: 0.4.0
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-08-07 00:00:00.000000000 Z
11
+ date: 2013-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -82,6 +82,8 @@ files:
82
82
  - app/models/gutentag/tag.rb
83
83
  - app/models/gutentag/tagging.rb
84
84
  - db/migrate/1_gutentag_tables.rb
85
+ - db/migrate/2_gutentag_cache_counter.rb
86
+ - db/migrate/3_no_null_counters.rb
85
87
  - gutentag.gemspec
86
88
  - lib/gutentag.rb
87
89
  - lib/gutentag/active_record.rb