acts_as_readable 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/acts_as_readable/acts_as_readable.rb +10 -4
- data/spec/acts_as_readable_spec.rb +37 -20
- data/spec/spec_helper.rb +10 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d8d2b78dd717f7f730d3421f544bd3ee2364e27
|
4
|
+
data.tar.gz: 09ffc7e7cfd1ef5107a0dc42c36fc9c39ac99c6c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc2801ab538a63cfc9840465eb850b1e894f9692d4a8606fb0f7001c1d557b79208469d90535d98439393dd6c6f3922917a19dedf5d3ce5c00ce387c96373f5b
|
7
|
+
data.tar.gz: 420399273b939da92a3f93375a4ccf7eb57b2a0829928f57903d0d92dd6961a3f0ae70fa873dc99bc5ca463c8e5b0a8704ee098041336d0c7317f73b7cfa9173
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module ActsAsReadable
|
2
|
-
module ActMethod
|
2
|
+
module ActMethod
|
3
3
|
# OPTIONS
|
4
4
|
# :cache => the name under which to cache timestamps for a "mark all as read" action to avoid the need to actually create readings for each record marked as read
|
5
5
|
def acts_as_readable(options = {})
|
@@ -21,12 +21,18 @@ module ActsAsReadable
|
|
21
21
|
|
22
22
|
module HelperMethods
|
23
23
|
def self.read_conditions(readable_class, user)
|
24
|
-
["(readable_type IS NULL
|
24
|
+
["(readable_type IS NULL AND COALESCE(#{readable_class.table_name}.updated_at < :all_read_at, :true))
|
25
|
+
OR (readable_type IS NOT NULL AND COALESCE(readings.updated_at < :all_read_at, :true))
|
26
|
+
OR (readings.state = 'read')",
|
27
|
+
:all_read_at => all_read_at(readable_class, user), :true => true]
|
25
28
|
end
|
26
29
|
|
27
30
|
def self.unread_conditions(readable_class, user)
|
28
31
|
# IF there is no reading and it has been updated since we last read all OR there is an unreading and we haven't read all since then
|
29
|
-
["(
|
32
|
+
["(readings.state IS NULL AND COALESCE(#{readable_class.table_name}.updated_at > :all_read_at, :true))
|
33
|
+
OR (readings.state = 'unread' AND COALESCE(readings.updated_at > :all_read_at, :true))
|
34
|
+
OR (readings.state = 'read' AND COALESCE(#{readable_class.table_name}.updated_at > readings.updated_at, :true))",
|
35
|
+
:all_read_at => all_read_at(readable_class, user), :true => true]
|
30
36
|
end
|
31
37
|
|
32
38
|
def self.all_read_at(readable_class, user)
|
@@ -35,7 +41,7 @@ module ActsAsReadable
|
|
35
41
|
|
36
42
|
def self.outer_join_readings(scope, user)
|
37
43
|
scope.joins("LEFT OUTER JOIN readings ON readings.readable_type = '#{scope.model.name}' AND readings.readable_id = #{scope.model.table_name}.id AND readings.user_id = #{user.id}")
|
38
|
-
end
|
44
|
+
end
|
39
45
|
end
|
40
46
|
|
41
47
|
module ClassMethods
|
@@ -8,117 +8,134 @@ describe 'acts_as_readable' do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "the unread scope" do
|
11
|
+
it "should not return records explicitly marked as read" do
|
12
|
+
@comment.read_by! @user
|
13
|
+
expect( Comment.unread_by(@user) ).not_to include(@comment)
|
14
|
+
end
|
15
|
+
|
11
16
|
it "should return records without readings if the user hasn't 'read all'" do
|
12
|
-
Comment.unread_by(@user).
|
17
|
+
expect( Comment.unread_by(@user) ).to include(@comment)
|
13
18
|
end
|
14
19
|
|
15
20
|
it "should return records explicitly marked as unread if the user hasn't 'read all'" do
|
16
21
|
@comment.unread_by! @user
|
17
|
-
Comment.unread_by(@user).
|
22
|
+
expect( Comment.unread_by(@user) ).to include(@comment)
|
18
23
|
end
|
19
24
|
|
20
25
|
it "should return records explicitly marked as unread if the user has 'read all' before the record was marked unread" do
|
21
26
|
Comment.read_by! @user
|
22
27
|
@comment.unread_by! @user
|
23
|
-
Comment.unread_by(@user).
|
28
|
+
expect( Comment.unread_by(@user) ).to include(@comment)
|
24
29
|
end
|
25
30
|
|
26
31
|
it "should not return records explicitly marked as unread if the user has 'read all' after the record was marked unread" do
|
27
32
|
@comment.unread_by! @user
|
28
33
|
Comment.read_by! @user
|
29
|
-
Comment.unread_by(@user).
|
34
|
+
expect( Comment.unread_by(@user) ).not_to include(@comment)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return records that have been updated since they were last read" do
|
38
|
+
@comment.read_by! @user
|
39
|
+
@comment.touch
|
40
|
+
expect( Comment.unread_by(@user) ).to include(@comment)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return records that have been updated since they were last unread" do
|
44
|
+
@comment.unread_by! @user
|
45
|
+
@comment.touch
|
46
|
+
expect( Comment.unread_by(@user) ).to include(@comment)
|
30
47
|
end
|
31
48
|
end
|
32
49
|
|
33
50
|
describe "the read scope" do
|
34
51
|
it "should return records without readings if the user has 'read all' since the last time the record was updated" do
|
35
52
|
Comment.read_by! @user
|
36
|
-
Comment.read_by(@user).
|
53
|
+
expect( Comment.read_by(@user) ).to include(@comment)
|
37
54
|
end
|
38
55
|
|
39
56
|
it "should return records explicitly marked as read if the user hasn't 'read all'" do
|
40
57
|
@comment.read_by! @user
|
41
|
-
Comment.read_by(@user).
|
58
|
+
expect( Comment.read_by(@user) ).to include(@comment)
|
42
59
|
end
|
43
60
|
|
44
61
|
it "should return records explicitly marked as read if the user has 'read all' before the record was marked unread" do
|
45
62
|
Comment.read_by! @user
|
46
63
|
@comment.read_by! @user
|
47
|
-
Comment.read_by(@user).
|
64
|
+
expect( Comment.read_by(@user) ).to include(@comment)
|
48
65
|
end
|
49
66
|
|
50
67
|
it "should return records explicitly marked as unread if the user has 'read all' after the record was marked unread" do
|
51
68
|
@comment.unread_by! @user
|
52
69
|
Comment.read_by! @user
|
53
|
-
Comment.read_by(@user).
|
70
|
+
expect( Comment.read_by(@user) ).to include(@comment)
|
54
71
|
end
|
55
72
|
end
|
56
73
|
|
57
74
|
describe "when checking a specific record for read_by?" do
|
58
75
|
it "should return true if the record hasn't explicitly been read, but the user has 'read all' since the record was created" do
|
59
76
|
Comment.read_by! @user
|
60
|
-
@comment.read_by?(@user).
|
77
|
+
expect( @comment.read_by?(@user) ).to be_truthy
|
61
78
|
end
|
62
79
|
|
63
80
|
it "should return true if the record hasn't explicitly been read and the user has 'read all' since the record was created but not since it was updated" do
|
64
81
|
Comment.read_by! @user
|
65
82
|
@comment.touch
|
66
|
-
@comment.read_by?(@user).
|
83
|
+
expect( @comment.read_by?(@user) ).to be_truthy
|
67
84
|
end
|
68
85
|
|
69
86
|
it "should return true if the record has been explicitly marked as read and the user hasn't 'read all'" do
|
70
87
|
@comment.read_by! @user
|
71
|
-
@comment.read_by?(@user).
|
88
|
+
expect( @comment.read_by?(@user) ).to be_truthy
|
72
89
|
end
|
73
90
|
|
74
91
|
it "should return false if the user 'read all' before and then marked the record as unread" do
|
75
92
|
Comment.read_by! @user
|
76
93
|
@comment.unread_by! @user
|
77
|
-
@comment.read_by?(@user).
|
94
|
+
expect( @comment.read_by?(@user) ).to be_falsey
|
78
95
|
end
|
79
96
|
|
80
97
|
it "should return true if the user has explicitly marked it as unread and then 'reads all'" do
|
81
98
|
@comment.unread_by! @user
|
82
99
|
Comment.read_by! @user
|
83
|
-
@comment.read_by?(@user).
|
100
|
+
expect( @comment.read_by?(@user) ).to be_truthy
|
84
101
|
end
|
85
102
|
end
|
86
103
|
|
87
104
|
describe "when checking a specific record for latest_update_read_by?" do
|
88
105
|
it "should return true if the record hasn't explicitly been read, but the user has 'read all' since the record was updated" do
|
89
106
|
Comment.read_by! @user
|
90
|
-
@comment.latest_update_read_by?(@user).
|
107
|
+
expect( @comment.latest_update_read_by?(@user) ).to be_truthy
|
91
108
|
end
|
92
109
|
|
93
110
|
it "should return false if the record hasn't explicitly been read and the user has 'read all' since the record was created but not since it was updated" do
|
94
111
|
Comment.read_by! @user
|
95
112
|
@comment.touch
|
96
|
-
@comment.latest_update_read_by?(@user).
|
113
|
+
expect( @comment.latest_update_read_by?(@user) ).to be_falsey
|
97
114
|
end
|
98
115
|
|
99
116
|
it "should return true if the record has been explicitly marked as read and the user hasn't 'read all'" do
|
100
117
|
@comment.read_by! @user
|
101
|
-
@comment.latest_update_read_by?(@user).
|
118
|
+
expect( @comment.latest_update_read_by?(@user) ).to be_truthy
|
102
119
|
end
|
103
120
|
|
104
121
|
it "should return false if the user 'read all' before and then marked the record as unread" do
|
105
122
|
Comment.read_by! @user
|
106
123
|
@comment.unread_by! @user
|
107
|
-
@comment.latest_update_read_by?(@user).
|
124
|
+
expect( @comment.latest_update_read_by?(@user) ).to be_falsey
|
108
125
|
end
|
109
126
|
|
110
127
|
it "should return true if the user has explicitly marked it as unread and then 'reads all'" do
|
111
128
|
@comment.unread_by! @user
|
112
129
|
Comment.read_by! @user
|
113
|
-
@comment.latest_update_read_by?(@user).
|
130
|
+
expect( @comment.latest_update_read_by?(@user) ).to be_truthy
|
114
131
|
end
|
115
132
|
|
116
133
|
it "should return false if the user 'read all' before and then marked the record as unread using cached readings" do
|
117
134
|
Comment.read_by! @user
|
118
135
|
@comment.unread_by! @user
|
119
136
|
Comment.cache_readings_for([@comment], @user)
|
120
|
-
@comment.latest_update_read_by?(@user).
|
137
|
+
expect( @comment.latest_update_read_by?(@user) ).to be_falsey
|
121
138
|
end
|
122
139
|
end
|
123
140
|
end
|
124
|
-
`dropdb acts_as_readable_test`
|
141
|
+
`dropdb acts_as_readable_test`
|
data/spec/spec_helper.rb
CHANGED
@@ -2,7 +2,7 @@ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
|
2
2
|
require 'active_record'
|
3
3
|
require 'acts_as_readable'
|
4
4
|
|
5
|
-
ActiveRecord::Base.establish_connection(:adapter => "postgresql", :database => "acts_as_readable_test")
|
5
|
+
ActiveRecord::Base.establish_connection(:adapter => "postgresql", :database => "acts_as_readable_test") # Not using sqlite3 for testing because of its ridiculous boolean handling
|
6
6
|
|
7
7
|
ActiveRecord::Schema.define(:version => 0) do
|
8
8
|
create_table :users, :force => true do |t|
|
@@ -27,3 +27,12 @@ end
|
|
27
27
|
class Comment < ActiveRecord::Base
|
28
28
|
acts_as_readable :cache => :comments_read_at
|
29
29
|
end
|
30
|
+
|
31
|
+
|
32
|
+
def debug_queries
|
33
|
+
logger = ActiveRecord::Base.logger
|
34
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
35
|
+
yield
|
36
|
+
ensure
|
37
|
+
ActiveRecord::Base.logger = logger
|
38
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_readable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Jakobsen
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|