Skip to content

Commit

Permalink
Handle primary keys other than "id" (fixes #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Dec 4, 2013
1 parent 2ab2d4c commit 3cc9224
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 0.3.1 - WIP

* Use Time.current instead of Time.now to avoid time zone trouble. Issue #18 (thanks to @henrythe9th)
* Handle primary keys other than "id". Issue #29 (thanks to @bcavileer)


## 0.3.0 - 2013-03-17
Expand Down
2 changes: 1 addition & 1 deletion lib/unread/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def reset_read_marks_for_all
ReadMark.delete_all :readable_type => self.base_class.name
ReadMark.connection.execute <<-EOT
INSERT INTO read_marks (user_id, readable_type, timestamp)
SELECT id, '#{self.base_class.name}', '#{Time.current.to_s(:db)}'
SELECT #{ReadMark.reader_class.primary_key}, '#{self.base_class.name}', '#{Time.current.to_s(:db)}'
FROM #{ReadMark.reader_class.table_name}
EOT
end
Expand Down
4 changes: 2 additions & 2 deletions lib/unread/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def join_read_marks(user)
assert_reader(user)

joins "LEFT JOIN read_marks ON read_marks.readable_type = '#{base_class.name}'
AND read_marks.readable_id = #{table_name}.id
AND read_marks.readable_id = #{table_name}.#{primary_key}
AND read_marks.user_id = #{user.id}
AND read_marks.timestamp >= #{table_name}.#{readable_options[:on]}"
end
Expand All @@ -26,4 +26,4 @@ def with_read_marks_for(user)
end
end
end
end
end
4 changes: 2 additions & 2 deletions test/schema.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ActiveRecord::Schema.define(:version => 0) do
create_table :readers, :force => true do |t|
create_table :readers, :primary_key => 'number', :force => true do |t|
t.string :name
end

create_table :emails, :force => true do |t|
create_table :emails, :primary_key => 'messageid', :force => true do |t|
t.string :subject
t.text :content
t.datetime :created_at
Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
require 'unread'

class Reader < ActiveRecord::Base
self.primary_key = 'number'
acts_as_reader
end

class Email < ActiveRecord::Base
self.primary_key = 'messageid'

acts_as_readable :on => :updated_at
end

Expand Down

0 comments on commit 3cc9224

Please sign in to comment.