gutentag 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/app/models/gutentag/tag.rb +2 -0
- data/app/models/gutentag/tagging.rb +1 -1
- data/db/migrate/2_gutentag_cache_counter.rb +15 -0
- data/db/migrate/3_no_null_counters.rb +11 -0
- data/gutentag.gemspec +1 -1
- data/lib/gutentag/active_record.rb +5 -3
- data/lib/gutentag/persistence.rb +13 -6
- data/spec/acceptance/tag_names_spec.rb +4 -0
- data/spec/acceptance/tags_spec.rb +12 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc8dba706d5b7e3075cf6043b26ab140b622fcb3
|
4
|
+
data.tar.gz: ac691b782f055f9f6d204731ba8b64d5ff21eb18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
|
|
data/app/models/gutentag/tag.rb
CHANGED
@@ -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
|
data/gutentag.gemspec
CHANGED
@@ -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
|
data/lib/gutentag/persistence.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
class Gutentag::Persistence
|
2
|
-
|
3
|
-
|
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 <<
|
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|
|
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
|
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
|
@@ -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.
|
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-
|
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
|