Commit cc60c41d089f3236d07c94aa51fe3ac9a3b281cc

Authored by Rodrigo Souto
1 parent df17e9c0

time-scopes: create scopes for filtering model by created_at

app/models/article.rb
... ... @@ -127,15 +127,7 @@ class Article < ActiveRecord::Base
127 127 {:include => 'categories_including_virtual', :conditions => { 'categories.id' => category.id }}
128 128 }
129 129  
130   - #FIXME make this test
131   - scope :newer_than, lambda { |reference_id|
132   - {:conditions => ["articles.id > #{reference_id}"]}
133   - }
134   -
135   - #FIXME make this test
136   - scope :older_than, lambda { |reference_id|
137   - {:conditions => ["articles.id < #{reference_id}"]}
138   - }
  130 + include TimeScopes
139 131  
140 132 scope :by_range, lambda { |range| {
141 133 :conditions => [
... ...
app/models/comment.rb
... ... @@ -20,16 +20,7 @@ class Comment &lt; ActiveRecord::Base
20 20  
21 21 scope :without_reply, :conditions => ['reply_of_id IS NULL']
22 22  
23   - #FIXME make this test
24   - scope :newer_than, lambda { |reference_id|
25   - {:conditions => ["comments.id > #{reference_id}"]}
26   - }
27   -
28   - #FIXME make this test
29   - scope :older_than, lambda { |reference_id|
30   - {:conditions => ["comments.id < #{reference_id}"]}
31   - }
32   -
  23 + include TimeScopes
33 24  
34 25 # unauthenticated authors:
35 26 validates_presence_of :name, :if => (lambda { |record| !record.email.blank? })
... ...
app/models/profile.rb
... ... @@ -99,15 +99,7 @@ class Profile &lt; ActiveRecord::Base
99 99 }
100 100 scope :no_templates, {:conditions => {:is_template => false}}
101 101  
102   - #FIXME make this test
103   - scope :newer_than, lambda { |reference_id|
104   - {:conditions => ["profiles.id > #{reference_id}"]}
105   - }
106   -
107   - #FIXME make this test
108   - scope :older_than, lambda { |reference_id|
109   - {:conditions => ["profiles.id < #{reference_id}"]}
110   - }
  102 + include TimeScopes
111 103  
112 104 def members
113 105 scopes = plugins.dispatch_scopes(:organization_members, self)
... ...
lib/noosfero/api/helpers.rb
... ... @@ -73,7 +73,8 @@ module Noosfero
73 73 order = make_order_with_parameters(params)
74 74  
75 75 if params[:reference_id]
76   - objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).where(conditions).limit(limit).order(order)
  76 + created_at = object.send(method).find(params[:reference_id]).created_at
  77 + objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at).where(conditions).limit(limit).order(order)
77 78 else
78 79 objects = object.send(method).where(conditions).limit(limit).order(order)
79 80 end
... ...
lib/noosfero/api/v1/comments.rb
... ... @@ -20,7 +20,8 @@ module Noosfero
20 20 article = find_article(environment.articles, params[:id])
21 21  
22 22 if params[:reference_id]
23   - comments = article.comments.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
  23 + created_at = article.comments.find(params[:reference_id]).created_at
  24 + comments = article.comments.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
24 25 else
25 26 comments = article.comments.reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
26 27 end
... ...
lib/time_scopes.rb 0 → 100644
... ... @@ -0,0 +1,21 @@
  1 +module TimeScopes
  2 + def self.included(recipient)
  3 + recipient.extend(ClassMethods)
  4 + end
  5 +
  6 + module ClassMethods
  7 + def self.extended (base)
  8 + if base.respond_to?(:scope) && base.attribute_names.include?('created_at')
  9 + base.class_eval do
  10 + scope :younger_than, lambda { |created_at|
  11 + {:conditions => ["#{table_name}.created_at > ?", created_at]}
  12 + }
  13 +
  14 + scope :older_than, lambda { |created_at|
  15 + {:conditions => ["#{table_name}.created_at < ?", created_at]}
  16 + }
  17 + end
  18 + end
  19 + end
  20 + end
  21 +end
... ...
test/unit/profile_test.rb
... ... @@ -2166,4 +2166,32 @@ class ProfileTest &lt; ActiveSupport::TestCase
2166 2166 assert_includes Profile.enabled, p2
2167 2167 assert_not_includes Profile.enabled, p3
2168 2168 end
  2169 +
  2170 + should 'fetch profiles older than a specific date' do
  2171 + p1 = fast_create(Profile, :created_at => Time.now)
  2172 + p2 = fast_create(Profile, :created_at => Time.now - 1.day)
  2173 + p3 = fast_create(Profile, :created_at => Time.now - 2.days)
  2174 + p4 = fast_create(Profile, :created_at => Time.now - 3.days)
  2175 +
  2176 + profiles = Profile.older_than(p2.created_at)
  2177 +
  2178 + assert_not_includes profiles, p1
  2179 + assert_not_includes profiles, p2
  2180 + assert_includes profiles, p3
  2181 + assert_includes profiles, p4
  2182 + end
  2183 +
  2184 + should 'fetch profiles younger than a specific date' do
  2185 + p1 = fast_create(Profile, :created_at => Time.now)
  2186 + p2 = fast_create(Profile, :created_at => Time.now - 1.day)
  2187 + p3 = fast_create(Profile, :created_at => Time.now - 2.days)
  2188 + p4 = fast_create(Profile, :created_at => Time.now - 3.days)
  2189 +
  2190 + profiles = Profile.younger_than(p3.created_at)
  2191 +
  2192 + assert_includes profiles, p1
  2193 + assert_includes profiles, p2
  2194 + assert_not_includes profiles, p3
  2195 + assert_not_includes profiles, p4
  2196 + end
2169 2197 end
... ...
test/unit/time_scopes.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +# encoding: UTF-8
  2 +require_relative "../test_helper"
  3 +
  4 +#FIXME Find a way to test with a generic example
  5 +
  6 +class TimeScopesTest < ActiveSupport::TestCase
  7 + should 'fetch profiles older than a specific date' do
  8 + p1 = fast_create(Profile, :created_at => Time.now)
  9 + p2 = fast_create(Profile, :created_at => Time.now - 1.day)
  10 + p3 = fast_create(Profile, :created_at => Time.now - 2.days)
  11 + p4 = fast_create(Profile, :created_at => Time.now - 3.days)
  12 +
  13 + profiles = Profile.older_than(p2.created_at)
  14 +
  15 + assert_not_includes profiles, p1
  16 + assert_not_includes profiles, p2
  17 + assert_includes profiles, p3
  18 + assert_includes profiles, p4
  19 + end
  20 +
  21 + should 'fetch profiles younger than a specific date' do
  22 + p1 = fast_create(Profile, :created_at => Time.now)
  23 + p2 = fast_create(Profile, :created_at => Time.now - 1.day)
  24 + p3 = fast_create(Profile, :created_at => Time.now - 2.days)
  25 + p4 = fast_create(Profile, :created_at => Time.now - 3.days)
  26 +
  27 + profiles = Profile.younger_than(p3.created_at)
  28 +
  29 + assert_includes profiles, p1
  30 + assert_includes profiles, p2
  31 + assert_not_includes profiles, p3
  32 + assert_not_includes profiles, p4
  33 + end
  34 +end
... ...