diff --git a/app/models/article.rb b/app/models/article.rb index 23dcdc5..7d7c9ff 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -127,15 +127,7 @@ class Article < ActiveRecord::Base {:include => 'categories_including_virtual', :conditions => { 'categories.id' => category.id }} } - #FIXME make this test - scope :newer_than, lambda { |reference_id| - {:conditions => ["articles.id > #{reference_id}"]} - } - - #FIXME make this test - scope :older_than, lambda { |reference_id| - {:conditions => ["articles.id < #{reference_id}"]} - } + include TimeScopes scope :by_range, lambda { |range| { :conditions => [ diff --git a/app/models/comment.rb b/app/models/comment.rb index 02c1fe3..575e96f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -20,16 +20,7 @@ class Comment < ActiveRecord::Base scope :without_reply, :conditions => ['reply_of_id IS NULL'] - #FIXME make this test - scope :newer_than, lambda { |reference_id| - {:conditions => ["comments.id > #{reference_id}"]} - } - - #FIXME make this test - scope :older_than, lambda { |reference_id| - {:conditions => ["comments.id < #{reference_id}"]} - } - + include TimeScopes # unauthenticated authors: validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) diff --git a/app/models/profile.rb b/app/models/profile.rb index 93cb98d..5257aac 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -99,15 +99,7 @@ class Profile < ActiveRecord::Base } scope :no_templates, {:conditions => {:is_template => false}} - #FIXME make this test - scope :newer_than, lambda { |reference_id| - {:conditions => ["profiles.id > #{reference_id}"]} - } - - #FIXME make this test - scope :older_than, lambda { |reference_id| - {:conditions => ["profiles.id < #{reference_id}"]} - } + include TimeScopes def members scopes = plugins.dispatch_scopes(:organization_members, self) diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 489ff1c..b6ef5f4 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -73,7 +73,8 @@ module Noosfero order = make_order_with_parameters(params) if params[:reference_id] - objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).where(conditions).limit(limit).order(order) + created_at = object.send(method).find(params[:reference_id]).created_at + objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at).where(conditions).limit(limit).order(order) else objects = object.send(method).where(conditions).limit(limit).order(order) end diff --git a/lib/noosfero/api/v1/comments.rb b/lib/noosfero/api/v1/comments.rb index 6b44f4c..1af0404 100644 --- a/lib/noosfero/api/v1/comments.rb +++ b/lib/noosfero/api/v1/comments.rb @@ -20,7 +20,8 @@ module Noosfero article = find_article(environment.articles, params[:id]) if params[:reference_id] - comments = article.comments.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) + created_at = article.comments.find(params[:reference_id]).created_at + comments = article.comments.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) else comments = article.comments.reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) end diff --git a/lib/time_scopes.rb b/lib/time_scopes.rb new file mode 100644 index 0000000..6642c7e --- /dev/null +++ b/lib/time_scopes.rb @@ -0,0 +1,21 @@ +module TimeScopes + def self.included(recipient) + recipient.extend(ClassMethods) + end + + module ClassMethods + def self.extended (base) + if base.respond_to?(:scope) && base.attribute_names.include?('created_at') + base.class_eval do + scope :younger_than, lambda { |created_at| + {:conditions => ["#{table_name}.created_at > ?", created_at]} + } + + scope :older_than, lambda { |created_at| + {:conditions => ["#{table_name}.created_at < ?", created_at]} + } + end + end + end + end +end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 5b6a9ec..5d4b0a4 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -2166,4 +2166,32 @@ class ProfileTest < ActiveSupport::TestCase assert_includes Profile.enabled, p2 assert_not_includes Profile.enabled, p3 end + + should 'fetch profiles older than a specific date' do + p1 = fast_create(Profile, :created_at => Time.now) + p2 = fast_create(Profile, :created_at => Time.now - 1.day) + p3 = fast_create(Profile, :created_at => Time.now - 2.days) + p4 = fast_create(Profile, :created_at => Time.now - 3.days) + + profiles = Profile.older_than(p2.created_at) + + assert_not_includes profiles, p1 + assert_not_includes profiles, p2 + assert_includes profiles, p3 + assert_includes profiles, p4 + end + + should 'fetch profiles younger than a specific date' do + p1 = fast_create(Profile, :created_at => Time.now) + p2 = fast_create(Profile, :created_at => Time.now - 1.day) + p3 = fast_create(Profile, :created_at => Time.now - 2.days) + p4 = fast_create(Profile, :created_at => Time.now - 3.days) + + profiles = Profile.younger_than(p3.created_at) + + assert_includes profiles, p1 + assert_includes profiles, p2 + assert_not_includes profiles, p3 + assert_not_includes profiles, p4 + end end diff --git a/test/unit/time_scopes.rb b/test/unit/time_scopes.rb new file mode 100644 index 0000000..d67eb15 --- /dev/null +++ b/test/unit/time_scopes.rb @@ -0,0 +1,34 @@ +# encoding: UTF-8 +require_relative "../test_helper" + +#FIXME Find a way to test with a generic example + +class TimeScopesTest < ActiveSupport::TestCase + should 'fetch profiles older than a specific date' do + p1 = fast_create(Profile, :created_at => Time.now) + p2 = fast_create(Profile, :created_at => Time.now - 1.day) + p3 = fast_create(Profile, :created_at => Time.now - 2.days) + p4 = fast_create(Profile, :created_at => Time.now - 3.days) + + profiles = Profile.older_than(p2.created_at) + + assert_not_includes profiles, p1 + assert_not_includes profiles, p2 + assert_includes profiles, p3 + assert_includes profiles, p4 + end + + should 'fetch profiles younger than a specific date' do + p1 = fast_create(Profile, :created_at => Time.now) + p2 = fast_create(Profile, :created_at => Time.now - 1.day) + p3 = fast_create(Profile, :created_at => Time.now - 2.days) + p4 = fast_create(Profile, :created_at => Time.now - 3.days) + + profiles = Profile.younger_than(p3.created_at) + + assert_includes profiles, p1 + assert_includes profiles, p2 + assert_not_includes profiles, p3 + assert_not_includes profiles, p4 + end +end -- libgit2 0.21.2