gutentag 2.2.0 → 2.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
  SHA256:
3
- metadata.gz: e6935bb3c51e7b225573d2330a1f95b858bed7cd130f0825177aef98c9f0d175
4
- data.tar.gz: 52201a047a523da00bd33cee60fe72ac1901c1273eb8296b4cfa637349991cf2
3
+ metadata.gz: 506e53c384365bbd56d66a31a5a6956955d4098c31dd9bda30c5fe4d9f6822f7
4
+ data.tar.gz: e905db1b576c5ee5cd52a834b776232b7f3339d62f8517d9d9262f7c840151c1
5
5
  SHA512:
6
- metadata.gz: 75251e4214a914b48256c60a97241f0db275fd06441e4eb3def15a8ada9fdebf6150208237b81d6715d63c87eae15d2bbcca208c61cc767961f451be60c8737f
7
- data.tar.gz: 2b8f4b94045a63adfae0bb8e762922f788e49d3b0f37e44d5f54f8a2cd5fb0390f453a532106cc6b4da38b4f9c5cd8bf51211f2ba64713b0fb649a35b5b71e5d
6
+ metadata.gz: a0039255e81ca547cfe1fa5ea9591b4edaedff4982835dbe4faf1788e5661b61d2ed25622242f641b3226b8d494a7c3ff54ca34e21859418b319a7aa701c8d78
7
+ data.tar.gz: 38d36b39b9d935cc32f1e0cdc80267d0d7e347d3c747b02f6270000beae2fe4bb60206f8ef5ccab8f3179c6cdeb6983fc3c3079f3b8452b843373c98b24b704c
data/Appraisals CHANGED
@@ -17,7 +17,7 @@ end if RUBY_VERSION.to_f < 2.4
17
17
 
18
18
  appraise "rails_4_2" do
19
19
  gem "rails", "~> 4.2.8"
20
- end if RUBY_VERSION.to_f < 2.4
20
+ end
21
21
 
22
22
  appraise "rails_5_0" do
23
23
  gem "rails", "~> 5.0.3"
@@ -2,6 +2,12 @@
2
2
 
3
3
  All notable changes to this project (at least, from v0.5.0 onwards) will be documented in this file.
4
4
 
5
+ ## 2.2.1 - 2018-03-06
6
+
7
+ ### Fixed
8
+
9
+ * Fix `tag_names` to return persisted values on a fresh load of a tagged object (which involved distinguishing Rails 4.2 behaviour from more recent approaches).
10
+
5
11
  ## 2.2.0 - 2018-03-04
6
12
 
7
13
  ### Added
data/README.md CHANGED
@@ -80,7 +80,7 @@ These are the versions the test suite runs against. It's possible it may work on
80
80
  Get it into your Gemfile - and don't forget the version constraint!
81
81
 
82
82
  ```Ruby
83
- gem 'gutentag', '~> 2.2.0'
83
+ gem 'gutentag', '~> 2.2.1'
84
84
  ```
85
85
 
86
86
  Next: your tags get persisted to your database, so let's import and run the migrations to get the tables set up:
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "gutentag"
6
- s.version = "2.2.0"
6
+ s.version = "2.2.1"
7
7
  s.authors = ["Pat Allan"]
8
8
  s.email = ["pat@freelancing-gods.com"]
9
9
  s.homepage = "https://github.com/pat/gutentag"
@@ -34,7 +34,8 @@ class Gutentag::ActiveRecord
34
34
  if legacy?
35
35
  model.define_attribute_method "tag_names"
36
36
  else
37
- model.attribute "tag_names", ActiveRecord::Type::Value.new, :default => []
37
+ model.attribute "tag_names", ActiveRecord::Type::Value.new,
38
+ :default => default_tag_names
38
39
  end
39
40
  end
40
41
 
@@ -49,13 +50,21 @@ class Gutentag::ActiveRecord
49
50
  end
50
51
 
51
52
  def add_methods
52
- model.send :extend, Gutentag::ActiveRecord::ClassMethods
53
-
54
- if legacy?
55
- model.send :include, Gutentag::ActiveRecord::LegacyInstanceMethods
53
+ case ActiveRecord::VERSION::STRING.to_f
54
+ when 3.2..4.1
55
+ require "gutentag/active_record/instance_methods_3_2"
56
+ when 4.2
57
+ require "gutentag/active_record/instance_methods_4_2"
56
58
  else
57
- model.send :include, Gutentag::ActiveRecord::ModernInstanceMethods
59
+ require "gutentag/active_record/instance_methods"
58
60
  end
61
+
62
+ model.send :extend, Gutentag::ActiveRecord::ClassMethods
63
+ model.send :include, Gutentag::ActiveRecord::InstanceMethods
64
+ end
65
+
66
+ def default_tag_names
67
+ ActiveRecord::VERSION::STRING.to_f <= 4.2 ? [] : nil
59
68
  end
60
69
 
61
70
  def legacy?
@@ -64,5 +73,3 @@ class Gutentag::ActiveRecord
64
73
  end
65
74
 
66
75
  require "gutentag/active_record/class_methods"
67
- require "gutentag/active_record/legacy_instance_methods"
68
- require "gutentag/active_record/modern_instance_methods"
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # For Rails 5.0+
4
+ module Gutentag::ActiveRecord::InstanceMethods
5
+ # If the tag_names attribute was one of the modified values, then let's just
6
+ # use the modifications, rather than overwriting the stored value.
7
+ #
8
+ # The reason we overwrite the stored value is because new tags may be added to
9
+ # the instance directly (e.g. article.tags << tag), which invokes the save
10
+ # callbacks, but the old tag_names value is stored but not updated.
11
+ def reset_tag_names
12
+ return if tag_names_previously_changed?
13
+
14
+ # Update the underlying value rather than going through the setter, to
15
+ # ensure this update doesn't get marked as a 'change'.
16
+ @attributes.write_from_database "tag_names", nil
17
+ end
18
+
19
+ def tag_names
20
+ # If the underlying value is nil, we've not requested this from the
21
+ # database yet.
22
+ self.tag_names = tags.pluck(:name) if super.nil?
23
+
24
+ # Use ActiveRecord's underlying implementation with change tracking.
25
+ super
26
+ end
27
+
28
+ def tag_names=(names)
29
+ # This value is about to be overwritten, but we want to make sure the change
30
+ # tracking doesn't think the original value was nil.
31
+ @attributes.write_from_database "tag_names", []
32
+
33
+ super
34
+ end
35
+
36
+ private
37
+
38
+ def persist_tags
39
+ Gutentag::Persistence.new(Gutentag::ChangeState.new(self)).persist
40
+ end
41
+ end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # For Rails <= 4.1
4
- module Gutentag::ActiveRecord::LegacyInstanceMethods
4
+ module Gutentag::ActiveRecord::InstanceMethods
5
+ # The reason we overwrite the stored value is because new tags may be added to
6
+ # the instance directly (e.g. article.tags << tag), which invokes the save
7
+ # callbacks, but the old tag_names value is stored but not updated.
5
8
  def reset_tag_names
6
9
  @tag_names = nil
7
10
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # For Rails 4.2 only.
4
+ module Gutentag::ActiveRecord::InstanceMethods
5
+ # The reason we overwrite the stored value is because new tags may be added to
6
+ # the instance directly (e.g. article.tags << tag), which invokes the save
7
+ # callbacks, but the old tag_names value is stored but not updated.
8
+ def reset_tag_names
9
+ # Update the underlying value rather than going through the setter, to
10
+ # ensure this update doesn't get marked as a 'change'.
11
+ self.tag_names = nil
12
+ end
13
+
14
+ def tag_names
15
+ # If the underlying value is nil, we've not requested this from the
16
+ # database yet.
17
+ if read_attribute("tag_names") { nil }.nil?
18
+ self.tag_names = tags.pluck(:name)
19
+ end
20
+
21
+ # Use ActiveRecord's underlying implementation with change tracking.
22
+ super
23
+ end
24
+
25
+ private
26
+
27
+ def persist_tags
28
+ Gutentag::Persistence.new(Gutentag::ChangeState.new(self)).persist
29
+ end
30
+ end
@@ -109,4 +109,11 @@ describe "Managing tags via names" do
109
109
  it "allows overriding of tag_names=" do
110
110
  expect(Article.instance_methods(false)).to_not include(:tag_names=)
111
111
  end
112
+
113
+ it "returns known tag names from a freshly loaded object" do
114
+ article.tag_names << "melbourne"
115
+ article.save!
116
+
117
+ expect(Article.find(article.id).tag_names).to eq(["melbourne"])
118
+ end
112
119
  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: 2.2.0
4
+ version: 2.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: 2018-03-04 00:00:00.000000000 Z
11
+ date: 2018-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -147,8 +147,9 @@ files:
147
147
  - lib/gutentag.rb
148
148
  - lib/gutentag/active_record.rb
149
149
  - lib/gutentag/active_record/class_methods.rb
150
- - lib/gutentag/active_record/legacy_instance_methods.rb
151
- - lib/gutentag/active_record/modern_instance_methods.rb
150
+ - lib/gutentag/active_record/instance_methods.rb
151
+ - lib/gutentag/active_record/instance_methods_3_2.rb
152
+ - lib/gutentag/active_record/instance_methods_4_2.rb
152
153
  - lib/gutentag/change_state.rb
153
154
  - lib/gutentag/dirty.rb
154
155
  - lib/gutentag/engine.rb
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # For Rails 4.2+
4
- module Gutentag::ActiveRecord::ModernInstanceMethods
5
- def reset_tag_names
6
- self.tag_names = nil
7
- end
8
-
9
- def tag_names
10
- self.tag_names = tags.pluck(:name) if super.nil?
11
-
12
- super
13
- end
14
-
15
- def tag_names=(names)
16
- Gutentag.dirtier.call self, names if Gutentag.dirtier
17
-
18
- super
19
- end
20
-
21
- private
22
-
23
- def persist_tags
24
- Gutentag::Persistence.new(Gutentag::ChangeState.new(self)).persist
25
- end
26
- end