Skip to content

Commit

Permalink
Support for Rails 2 dropped
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Feb 17, 2013
1 parent bf725cb commit 844860b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 51 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ rvm:
- 1.8.7
- 1.9.3
gemfile:
- ci/Gemfile.rails-2.3.x
- ci/Gemfile.rails-3.0.x
- ci/Gemfile.rails-3.1.x
- ci/Gemfile.rails-3.2.x
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source "http://rubygems.org"
source "https://rubygems.org"

# Specify your gem's dependencies in unread.gemspec
gemspec
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Ruby gem to manage read/unread status of ActiveRecord objects - and it's fast.
## Requirements

* Ruby 1.8.7 or 1.9.3
* Rails >= 2.3.6 (including 3.0, 3.1, 3.2)
* Rails 3 (including 3.0, 3.1, 3.2)
* Tested with SQLite and MySQL
* Needs a timestamp field in your models (like created_at or updated_at) with a database index on it

Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.2.0 - WIP

* Support for Rails 2 dropped
* Refactoring

0.1.2 - 2013/01/27

* Scopes: Improved parameter check
Expand Down
7 changes: 0 additions & 7 deletions ci/Gemfile.rails-2.3.x

This file was deleted.

21 changes: 7 additions & 14 deletions lib/app/models/read_mark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@ class ReadMark < ActiveRecord::Base

validates_presence_of :user_id, :readable_type

scope_method = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope

send scope_method, :global, :conditions => { :readable_id => nil }
send scope_method, :single, :conditions => 'readable_id IS NOT NULL'
send scope_method, :readable_type, lambda { |readable_type | { :conditions => { :readable_type => readable_type }}}
send scope_method, :user, lambda { |user| { :conditions => { :user_id => user.id }}}
send scope_method, :older_than, lambda { |timestamp| { :conditions => [ 'timestamp < ?', timestamp] }}
scope :global, where(:readable_id => nil)
scope :single, where('readable_id IS NOT NULL')
scope :readable_type, lambda { |readable_type | where(:readable_type => readable_type) }
scope :user, lambda { |user| where(:user_id => user.id) }
scope :older_than, lambda { |timestamp| where([ 'timestamp < ?', timestamp]) }

# Returns the class defined by ActsAsReadable::acts_as_reader
def self.reader_class
user_association = reflect_on_all_associations(:belongs_to).find { |assoc| assoc.name == :user }
user_association.try(:klass)
reflect_on_all_associations(:belongs_to).find { |assoc| assoc.name == :user }.try(:klass)
end

if respond_to?(:class_attribute)
class_attribute :readable_classes
else
class_inheritable_accessor :readable_classes
end
class_attribute :readable_classes
end
45 changes: 20 additions & 25 deletions lib/unread/acts_as_readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,40 @@ def acts_as_reader
end

def acts_as_readable(options={})
if respond_to?(:class_attribute)
class_attribute :readable_options
else
class_inheritable_accessor :readable_options
end
class_attribute :readable_options

options.reverse_merge!({ :on => :updated_at })
options.reverse_merge!(:on => :updated_at)
self.readable_options = options

has_many :read_marks, :as => :readable, :dependent => :delete_all

ReadMark.readable_classes ||= []
ReadMark.readable_classes << self unless ReadMark.readable_classes.map(&:name).include?(self.name)

scope_method = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope
ReadMark.readable_classes << self unless ReadMark.readable_classes.include?(self)

send scope_method, :unread_by, lambda { |user|
scope :join_read_marks, lambda { |user|
assert_reader(user)

result = { :joins => "LEFT JOIN read_marks ON read_marks.readable_type = '#{self.base_class.name}'
AND read_marks.readable_id = #{self.table_name}.id
AND read_marks.user_id = #{user.id}
AND read_marks.timestamp >= #{self.table_name}.#{readable_options[:on]}",
:conditions => 'read_marks.id IS NULL' }
joins "LEFT JOIN read_marks ON read_marks.readable_type = '#{self.base_class.name}'
AND read_marks.readable_id = #{self.table_name}.id
AND read_marks.user_id = #{user.id}
AND read_marks.timestamp >= #{self.table_name}.#{readable_options[:on]}"
}

scope :unread_by, lambda { |user|
result = join_read_marks(user).
where('read_marks.id IS NULL')

if global_time_stamp = user.read_mark_global(self).try(:timestamp)
result[:conditions] += " AND #{self.table_name}.#{readable_options[:on]} > '#{global_time_stamp.to_s(:db)}'"
result = result.where("#{self.table_name}.#{readable_options[:on]} > '#{global_time_stamp.to_s(:db)}'")
end

result
}

send scope_method, :with_read_marks_for, lambda { |user|
assert_reader(user)

{ :select => "#{self.table_name}.*, read_marks.id AS read_mark_id",
:joins => "LEFT JOIN read_marks ON read_marks.readable_type = '#{self.base_class.name}'
AND read_marks.readable_id = #{self.table_name}.id
AND read_marks.user_id = #{user.id}
AND read_marks.timestamp >= #{self.table_name}.#{readable_options[:on]}" }

scope :with_read_marks_for, lambda { |user|
join_read_marks(user).select("#{self.table_name}.*, read_marks.id AS read_mark_id")
}

extend ReadableClassMethods
include ReadableInstanceMethods
end
Expand Down
2 changes: 1 addition & 1 deletion lib/unread/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Unread
VERSION = '0.1.2'
VERSION = '0.2.0'
end
2 changes: 1 addition & 1 deletion unread.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency 'activerecord', '>= 2.3.6'
s.add_dependency 'activerecord', '>= 3'

s.add_development_dependency 'rake'
s.add_development_dependency 'timecop'
Expand Down

0 comments on commit 844860b

Please sign in to comment.