acts_as_readable 2.0.0 → 2.1.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: 44f09001b617edaeaaeb303302ff7806c787574e
4
- data.tar.gz: aed97cd3809d30621c534da9f3cbe31c40f92336
3
+ metadata.gz: 16e3538346fdcbac00fb9006e4ce67e7e39eafb5
4
+ data.tar.gz: 1516dc9da9ccc137e7224f1c36064a07b7e5f427
5
5
  SHA512:
6
- metadata.gz: ca634e25c362eb653545e2ba6b2213ec636bd038946609e7de37331d67372addbe03c8261df81c88c979c9d17c50439d526b611fea61c739e7d222c95d14d8ca
7
- data.tar.gz: b5ae7f2ad8c44ab52b340613efa8bd1c3d1fe9635e71b491e9c4904e6e2a59629ffb689f3caf2f821d0d6a30dad278264635335c81b2a4f9e880ad557d683892
6
+ metadata.gz: c9df46bdaffac4f1b4c2d1e80e7beb989d2dea722f2f1a57747dfc190d455cd7932eb4426c1d7769bd1a6e2ce4b43295157f116b66fe2bb5ce90eb23460c380e
7
+ data.tar.gz: 844f36b301c376382a10740871538c13b809c418ddd2f68fdadcfb4941765a099111af4a48058db85f6c367386b92cfa41d5ab02c8508a5da18d7fd837eff09f
@@ -6,11 +6,13 @@ module ActsAsReadable
6
6
  class_attribute :acts_as_readable_options
7
7
  self.acts_as_readable_options = options
8
8
 
9
- has_many :readings, :as => :readable
10
- has_many :readers, :through => :readings, :source => :user, :conditions => {:readings => {:state => 'read'}}
9
+ User.has_many :readings, :dependent => :delete_all
11
10
 
12
- scope :read_by, lambda {|user| ActsAsReadable::HelperMethods.outer_join_readings(self, user).where(ActsAsReadable::HelperMethods.read_conditions(self, user))}
13
- scope :unread_by, lambda {|user| ActsAsReadable::HelperMethods.outer_join_readings(self, user).where(ActsAsReadable::HelperMethods.unread_conditions(self, user))}
11
+ has_many :readings, :as => :readable, :dependent => :delete_all
12
+ has_many :readers, lambda { where :readings => {:state => 'read'} }, :through => :readings, :source => :user
13
+
14
+ scope :read_by, lambda {|user| ActsAsReadable::HelperMethods.outer_join_readings(all, user).where(ActsAsReadable::HelperMethods.read_conditions(self, user))}
15
+ scope :unread_by, lambda {|user| ActsAsReadable::HelperMethods.outer_join_readings(all, user).where(ActsAsReadable::HelperMethods.unread_conditions(self, user))}
14
16
 
15
17
  extend ActsAsReadable::ClassMethods
16
18
  include ActsAsReadable::InstanceMethods
@@ -31,8 +33,8 @@ module ActsAsReadable
31
33
  user[readable_class.acts_as_readable_options[:cache]] if readable_class.acts_as_readable_options[:cache]
32
34
  end
33
35
 
34
- def self.outer_join_readings(readable_class, user)
35
- Reading.joins("LEFT OUTER JOIN readings ON readings.readable_type = '#{readable_class.name}' AND readings.readable_id = #{readable_class.table_name}.id AND readings.user_id = #{user.id}")
36
+ def self.outer_join_readings(scope, user)
37
+ 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}")
36
38
  end
37
39
  end
38
40
 
@@ -74,7 +76,7 @@ module ActsAsReadable
74
76
 
75
77
  def read_by!(user)
76
78
  # Find an existing reading and update the record so we can know when the thing was first read, and the last time we read it
77
- reading = Reading.find_or_initialize_by_user_id_and_readable_id_and_readable_type(user.id, self.id, self.class.name)
79
+ reading = Reading.find_or_initialize_by(:user_id => user.id, :readable_id => self.id, :readable_type => self.class.name)
78
80
  reading.updated_at = Time.now # Explicitly set the read time to now in order to force a save in case we haven't changed anything else about the reading
79
81
  reading.state = :read
80
82
  reading.save!
@@ -84,7 +86,7 @@ module ActsAsReadable
84
86
  end
85
87
 
86
88
  def unread_by!(user)
87
- reading = Reading.find_or_initialize_by_user_id_and_readable_id_and_readable_type(user.id, self.id, self.class.name)
89
+ reading = Reading.find_or_initialize_by(:user_id => user.id, :readable_id => self.id, :readable_type => self.class.name)
88
90
  reading.state = :unread
89
91
  reading.save!
90
92
  end
@@ -93,13 +95,13 @@ module ActsAsReadable
93
95
  if cached_reading
94
96
  cached_reading.read?
95
97
  elsif cached_reading == false
96
- user[acts_as_readable_options[:cache]].to_f > self.updated_at.to_f
98
+ user[acts_as_readable_options[:cache]].to_f > self.created_at.to_f
97
99
  elsif readers.loaded?
98
100
  readers.include?(user)
99
101
  elsif reading = readings.find_by_user_id(user.id)
100
102
  reading.read?
101
103
  else
102
- user[acts_as_readable_options[:cache]].to_f > self.updated_at.to_f
104
+ user[acts_as_readable_options[:cache]].to_f > self.created_at.to_f
103
105
  end
104
106
  end
105
107
 
@@ -110,11 +112,11 @@ module ActsAsReadable
110
112
 
111
113
  def latest_update_read_by?(user)
112
114
  if cached_reading
113
- cached_reading.updated_at > self.updated_at
115
+ cached_reading.read? && cached_reading.updated_at > self.updated_at
114
116
  elsif cached_reading == false
115
117
  user[acts_as_readable_options[:cache]].to_f > self.updated_at.to_f
116
- elsif reading = readings.where(:user_id => user.id, :state => :read).first
117
- reading.updated_at > self.updated_at
118
+ elsif reading = readings.find_by_user_id(user.id)
119
+ reading.read? && reading.updated_at > self.updated_at
118
120
  else
119
121
  user[acts_as_readable_options[:cache]].to_f > self.updated_at.to_f
120
122
  end
@@ -54,12 +54,18 @@ describe 'acts_as_readable' do
54
54
  end
55
55
  end
56
56
 
57
- describe "when checking a specific record" do
58
- it "should return true if the record hasn't explicitly been read, but the user has 'read all' since the last time the record was updated" do
57
+ describe "when checking a specific record for read_by?" do
58
+ 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
59
  Comment.read_by! @user
60
60
  @comment.read_by?(@user).should be_true
61
61
  end
62
62
 
63
+ 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
+ Comment.read_by! @user
65
+ @comment.touch
66
+ @comment.read_by?(@user).should be_true
67
+ end
68
+
63
69
  it "should return true if the record has been explicitly marked as read and the user hasn't 'read all'" do
64
70
  @comment.read_by! @user
65
71
  @comment.read_by?(@user).should be_true
@@ -76,6 +82,43 @@ describe 'acts_as_readable' do
76
82
  Comment.read_by! @user
77
83
  @comment.read_by?(@user).should be_true
78
84
  end
79
- end
85
+ end
86
+
87
+ describe "when checking a specific record for latest_update_read_by?" do
88
+ 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
+ Comment.read_by! @user
90
+ @comment.latest_update_read_by?(@user).should be_true
91
+ end
92
+
93
+ 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
+ Comment.read_by! @user
95
+ @comment.touch
96
+ @comment.latest_update_read_by?(@user).should be_false
97
+ end
98
+
99
+ it "should return true if the record has been explicitly marked as read and the user hasn't 'read all'" do
100
+ @comment.read_by! @user
101
+ @comment.latest_update_read_by?(@user).should be_true
102
+ end
103
+
104
+ it "should return false if the user 'read all' before and then marked the record as unread" do
105
+ Comment.read_by! @user
106
+ @comment.unread_by! @user
107
+ @comment.latest_update_read_by?(@user).should be_false
108
+ end
109
+
110
+ it "should return true if the user has explicitly marked it as unread and then 'reads all'" do
111
+ @comment.unread_by! @user
112
+ Comment.read_by! @user
113
+ @comment.latest_update_read_by?(@user).should be_true
114
+ end
115
+
116
+ it "should return false if the user 'read all' before and then marked the record as unread using cached readings" do
117
+ Comment.read_by! @user
118
+ @comment.unread_by! @user
119
+ Comment.cache_readings_for([@comment], @user)
120
+ @comment.latest_update_read_by?(@user).should be_false
121
+ end
122
+ end
80
123
  end
81
124
  `dropdb acts_as_readable_test`
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.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Jakobsen
@@ -10,7 +10,21 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2014-01-28 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.0'
14
28
  description:
15
29
  email: contact@culturecode.ca
16
30
  executables: []