Skip to content

Commit

Permalink
Merge pull request #52 from lxneng/issue-50
Browse files Browse the repository at this point in the history
let `object.next` and `object.previous` support specify a field
  • Loading branch information
johnnyshields committed May 5, 2015
2 parents 670451f + 151260d commit 42c5583
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/by_star/orm/active_record/by_star.rb
Expand Up @@ -55,13 +55,13 @@ def newest_query(options={})
end

def previous(options={})
field = self.class.by_star_start_field
field = self.class.by_star_start_field(options)
value = self.send(field.split(".").last)
self.class.by_star_scope(options.merge(scope_args: self)).where("#{field} < ?", value).reorder("#{field} DESC").first
end

def next(options={})
field = self.class.by_star_start_field
field = self.class.by_star_start_field(options)
value = self.send(field.split(".").last)
self.class.by_star_scope(options.merge(scope_args: self)).where("#{field} > ?", value).reorder("#{field} ASC").first
end
Expand Down
4 changes: 2 additions & 2 deletions lib/by_star/orm/mongoid/by_star.rb
Expand Up @@ -61,12 +61,12 @@ def newest_query(options={})
end

def previous(options={})
field = self.class.by_star_start_field
field = self.class.by_star_start_field(options)
self.class.by_star_scope(options.merge(scope_args: self)).lt(field => self.send(field)).reorder(field => :desc).first
end

def next(options={})
field = self.class.by_star_start_field
field = self.class.by_star_start_field(options)
self.class.by_star_scope(options.merge(scope_args: self)).gt(field => self.send(field)).reorder(field => :asc).first
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/shared/seeds.rb
Expand Up @@ -19,8 +19,8 @@
2014-03-01
2014-03-15
2014-04-01
2014-04-15).map{|d| Time.zone.parse(d) + 17.hours }.each do |d|
Post.create!(:created_at => d, day_of_month: d.day)
2014-04-15).map{|d| Time.zone.parse(d) + 17.hours }.each_with_index do |d, index|
Post.create!(:created_at => d, :updated_at=> d + index.days, day_of_month: d.day)
Appointment.create!(:created_at => d, day_of_month: d.day)
Event.create!(:created_at => d, :start_time => d - 5.days, :end_time => d + 5.days, day_of_month: d.day)
end
9 changes: 9 additions & 0 deletions spec/integration/shared/by_direction.rb
Expand Up @@ -149,5 +149,14 @@
it{ expect(subject.next({ scope: ->{ where(day_of_month: 1) } }).created_at).to eq Time.zone.parse('2014-02-01 17:00:00') }
it{ expect(subject.next({ scope: ->(record){ where(day_of_month: record.day_of_month - 4) } }).created_at).to eq Time.zone.parse('2014-02-01 17:00:00') }
end

context 'specify a field' do
subject { Post.where(created_at: Time.zone.parse('2014-01-01 17:00:00')).first }
it{ expect(subject.previous.created_at).to eq Time.zone.parse('2013-12-31 17:00:00') }
it{ expect(subject.next.created_at).to eq Time.zone.parse('2014-01-05 17:00:00') }
it{ expect(subject.previous(field: 'updated_at').created_at).to eq Time.zone.parse('2013-12-31 17:00:00') }
it{ expect(subject.next(field: 'updated_at').created_at).to eq Time.zone.parse('2014-01-01 17:00:00') }
end

end
end

0 comments on commit 42c5583

Please sign in to comment.