unread 0.8.1 → 0.8.2

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: '031127638582f162534dd12b0c7634728d8cbaef'
4
- data.tar.gz: 8c142d460544502dfc8c3b6d5be2ab0edb745f49
3
+ metadata.gz: 302efc346d484f76d5346d27288d5e095062df99
4
+ data.tar.gz: 81d1a2fa58f5b4a6a7a78c3bede1d812033a1882
5
5
  SHA512:
6
- metadata.gz: c3eefb5e01849958b959e8982b2f28da7c1264af88a60c8e43a5e28c563b298b7ea5da4440f148a9995d0e0006be7b1485edbfe8cbe2e073ad057931c333b3bf
7
- data.tar.gz: 527a7d87fbc85f7edc88178f8d66d50fc38628722c1fac9faa2e7cdad6cc1f263ae77beac94b5bf30f91ecd99608d33701c398bbbb70117d80812def371b7ce6
6
+ metadata.gz: 7d79c07ac3de9d382349db73507e078156ed7731664cef4d0475b46a147299ab10037b12d874bbccb98b177d379eecc628c9cd4691c23f41bf9a91adff382db9
7
+ data.tar.gz: 5f4863448e71c179e30f61751d7849fee31d7bd6c1419db43c3f57d0d410683dbe9d7d8b069e902f54f4a1c3535c2040526254ecdcad23d3e6f5082ba742a832
@@ -6,7 +6,7 @@ class UnreadMigration < ActiveRecord::Migration
6
6
  t.datetime :timestamp
7
7
  end
8
8
 
9
- add_index ReadMark, [:reader_id, :reader_type, :readable_type, :readable_id], name: 'read_marks_reader_readable_index'
9
+ add_index ReadMark, [:reader_id, :reader_type, :readable_type, :readable_id], name: 'read_marks_reader_readable_index', unique: true
10
10
  end
11
11
 
12
12
  def self.down
@@ -4,7 +4,7 @@ class UnreadPolymorphicReaderMigration < ActiveRecord::Migration
4
4
  rename_column :read_marks, :user_id, :reader_id
5
5
  add_column :read_marks, :reader_type, :string
6
6
  execute "update read_marks set reader_type = 'User'"
7
- add_index :read_marks, [:reader_id, :reader_type, :readable_type, :readable_id], name: 'read_marks_reader_readable_index'
7
+ add_index :read_marks, [:reader_id, :reader_type, :readable_type, :readable_id], name: 'read_marks_reader_readable_index', unique: true
8
8
  end
9
9
 
10
10
  def self.down
data/lib/unread/base.rb CHANGED
@@ -36,5 +36,9 @@ module Unread
36
36
  extend Readable::ClassMethods
37
37
  extend Readable::Scopes
38
38
  end
39
+
40
+ def using_postgresql?
41
+ connection.adapter_name.match(/postgres/i)
42
+ end
39
43
  end
40
44
  end
@@ -27,11 +27,18 @@ module Unread
27
27
  if global_timestamp && global_timestamp >= timestamp
28
28
  # The object is implicitly marked as read, so there is nothing to do
29
29
  else
30
- rm = obj.read_marks.where(:reader_id => reader.id, :reader_type => reader.class.base_class.name).first || obj.read_marks.build
31
- rm.reader_id = reader.id
32
- rm.reader_type = reader.class.base_class.name
33
- rm.timestamp = timestamp
34
- rm.save!
30
+ # This transaction is needed, so that parent transaction won't rollback even there's an error.
31
+ ReadMark.transaction(requires_new: true) do
32
+ begin
33
+ rm = obj.read_marks.where(reader_id: reader.id, reader_type: reader.class.base_class.name).first || obj.read_marks.build
34
+ rm.reader_id = reader.id
35
+ rm.reader_type = reader.class.base_class.name
36
+ rm.timestamp = timestamp
37
+ rm.save!
38
+ rescue ActiveRecord::RecordNotUnique
39
+ raise ActiveRecord::Rollback
40
+ end
41
+ end
35
42
  end
36
43
  end
37
44
  end
@@ -71,7 +78,7 @@ module Unread
71
78
  rm = reader.read_marks.new
72
79
  rm.readable_type = self.readable_parent.name
73
80
  rm.timestamp = Time.current
74
- rm.save!
81
+ rm.save!
75
82
  end
76
83
 
77
84
  reader.forget_memoized_read_mark_global
@@ -35,9 +35,11 @@ module Unread
35
35
  end
36
36
 
37
37
  def with_read_marks_for(reader)
38
+ postgresql_string_cast = using_postgresql? ? '::varchar' : ''
39
+
38
40
  join_read_marks(reader).select("#{quoted_table_name}.*,
39
41
  #{ReadMark.quoted_table_name}.id AS read_mark_id,
40
- #{quote_bound_value(reader.class.base_class.name)} AS read_mark_reader_type,
42
+ #{quote_bound_value(reader.class.base_class.name)}#{postgresql_string_cast} AS read_mark_reader_type,
41
43
  #{quote_bound_value(reader.id)} AS read_mark_reader_id")
42
44
  end
43
45
  end
@@ -27,9 +27,11 @@ module Unread
27
27
  end
28
28
 
29
29
  def with_read_marks_for(readable)
30
+ postgresql_string_cast = using_postgresql? ? '::varchar' : ''
31
+
30
32
  join_read_marks(readable).select("#{quoted_table_name}.*,
31
33
  #{ReadMark.quoted_table_name}.id AS read_mark_id,
32
- '#{readable.class.name}' AS read_mark_readable_type,
34
+ #{quote_bound_value readable.class.name}#{postgresql_string_cast} AS read_mark_readable_type,
33
35
  #{readable.id} AS read_mark_readable_id")
34
36
  end
35
37
  end
@@ -1,3 +1,3 @@
1
1
  module Unread
2
- VERSION = '0.8.1'
2
+ VERSION = '0.8.2'
3
3
  end
@@ -266,6 +266,20 @@ describe Unread::Readable do
266
266
  expect(@email2.unread?(@reader)).to be_falsey
267
267
  end
268
268
 
269
+ it "should mark the rest as read when the first record is not unique" do
270
+ Email.mark_as_read! [ @email1 ], for: @reader
271
+
272
+ allow(@email1).to receive_message_chain("read_marks.build").and_return(@email1.read_marks.build)
273
+ allow(@email1).to receive_message_chain("read_marks.where").and_return([])
274
+
275
+ expect do
276
+ Email.mark_as_read! [ @email1, @email2 ], for: @reader
277
+ end.to change(ReadMark, :count).by(1)
278
+
279
+ expect(@email1.unread?(@reader)).to be_falsey
280
+ expect(@email2.unread?(@reader)).to be_falsey
281
+ end
282
+
269
283
  it "should perform less queries if the objects are already read" do
270
284
  Email.mark_as_read! :all, :for => @reader
271
285
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unread
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georg Ledermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-12 00:00:00.000000000 Z
11
+ date: 2016-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord