grimen (owner)

Revisions

gist: 181666 Download_button fork
public
Public Clone URL: git://gist.github.com/181666.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# coding: utf-8
 
module IsReviewable
  class Review < ::ActiveRecord::Base
    
    belongs_to :reviewable, :polymorphic => true
    belongs_to :reviewer, :polymorphic => true
    
    # Order.
    named_scope :in_order, :order => 'created_at ASC'
    named_scope :most_recent, :order => 'created_at DESC'
    named_scope :lowest_rating, :order => 'rating ASC'
    named_scope :highest_rating, :order => 'rating DESC'
    
    # Filters.
    named_scope :limit, lambda { |number_of_items| {:limit => number_of_items} }
    named_scope :recent, lambda { |arg| arg.is_a?(DateTime) ? {:conditions => ['created_at >= ?', arg]} : {:limit => arg.to_i} }
    named_scope :between_dates, :conditions => lambda { |from_date, to_date| {:created_at => from_date..to_date} }
    named_scope :with_rating, :conditions => lambda { |rating_value_or_range| {:rating => rating_value_or_range} }
    named_scope :with_a_rating, :conditions => ['rating IS NOT NULL']
    named_scope :with_a_comment, :conditions => ['body IS NOT NULL && LENGTH(body) > 0']
    named_scope :of_reviewable_type, :conditions => lambda { |type| Support.polymorphic_conditions_for(type, :type) }
    named_scope :by_reviewer_type, :conditions => lambda { |type| Support.polymorphic_conditions_for(type, :type) }
    named_scope :on, :conditions => lambda { |reviewable| Support.polymorphic_conditions_for(reviewable) }
    named_scope :by, :conditions => lambda { |reviewer| Support.polymorphic_conditions_for(reviewer) }
    
  end
end
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# coding: utf-8
require 'rubygems'
 
gem 'test-unit', '>= 2.0.0'
gem 'thoughtbot-shoulda', '>= 2.10.2'
gem 'sqlite3-ruby', '>= 1.2.0'
gem 'nakajima-acts_as_fu', '>= 0.0.5'
gem 'jgre-monkeyspecdoc', '>= 0.9.5'
 
require 'test/unit'
require 'shoulda'
require 'acts_as_fu'
require 'monkeyspecdoc'
 
require 'test_helper'
 
require 'is_reviewable'
 
build_model :reviews do
  references :reviewable, :polymorphic => true
  
  references :reviewer, :polymorphic => true
  string :ip, :limit => 24
  
  float :rating
  string :title
  text :body
  
  timestamps
end
 
build_model :guests
build_model :users
build_model :posts
 
build_model :reviewable_posts do
  is_reviewable :by => :users, :scale => 1.0..5.0, :step => 0.5, :average_precision => 2, :accept_ip => true
end
 
build_model :cached_reviewable_posts do
  integer :reviews_count
  integer :average_rating
end
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Part of the is_reviewable_test that fails:
 
context "review" do
    
    should "define named scopes" do
      named_scopes = [
          :between_dates
        ]
      
      assert named_scopes.all? { |named_scope| Review.respond_to?(named_scope, true) }
      assert named_scopes.all? { |named_scope| @reviewable_post.reviews.respond_to?(named_scope) }
    end
    
    should "return reviews by creation date with named scope :in_order" do
      @reviewable_post.review!(:reviewer => @user_1, :rating => 1)
      @reviewable_post.review!(:reviewer => @user_2, :rating => 2)
      
      puts @reviewable_post.reviews.first.class
      
      assert_equal @user_1, @reviewable_post.reviews.in_order.first.reviewer
      assert_equal @user_2, @reviewable_post.reviews.in_order.last.reviewer
    end
    
  end
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Finished in 0.784711 seconds.
 
  1) review should define named scopes FAILED:
    [./test/is_reviewable_test.rb:155:in `__bind_1252217361_543394'
     /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
     /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: review should define named scopes. ']:
    <false> is not true.
 
  2) NoMethodError in review should return reviews by creation date with named scope :in_order:
NoMethodError: undefined method `in_order' for #<Class:0x188c0c4>
    /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:1959:in `method_missing'
    /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_collection.rb:379:in `send'
    /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_collection.rb:379:in `method_missing'
    /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2143:in `with_scope'
    /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_proxy.rb:206:in `send'
    /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_proxy.rb:206:in `with_scope'
    /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_collection.rb:375:in `method_missing'
    ./test/is_reviewable_test.rb:165:in `__bind_1252217361_641641'
    /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
    /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: review should return reviews by creation date with named scope :in_order. '
 
15 tests, 31 assertions, 1 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
rake aborted!
Command failed with status (1): [/opt/local/bin/ruby -I"lib:lib:test" "/opt...]