Skip to content

Commit 486c92d

Browse files
author
mpd
committed
more porting work for activerecord 3. convert special finders to scopes. add specs for them.
1 parent 1fd1400 commit 486c92d

7 files changed

+125
-26
lines changed

Gemfile.lock

+58-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,25 @@ PATH
33
specs:
44
acts_as_commentable_with_threading (0.1.1)
55
activerecord (~> 3.0)
6-
awesome_nested_set
6+
moretea-awesome_nested_set
77

88
GEM
99
remote: http://rubygems.org/
1010
specs:
11+
abstract (1.0.0)
12+
actionmailer (3.0.0)
13+
actionpack (= 3.0.0)
14+
mail (~> 2.2.5)
15+
actionpack (3.0.0)
16+
activemodel (= 3.0.0)
17+
activesupport (= 3.0.0)
18+
builder (~> 2.1.2)
19+
erubis (~> 2.6.6)
20+
i18n (~> 0.4.1)
21+
rack (~> 1.2.1)
22+
rack-mount (~> 0.6.12)
23+
rack-test (~> 0.5.4)
24+
tzinfo (~> 0.3.23)
1125
activemodel (3.0.0)
1226
activesupport (= 3.0.0)
1327
builder (~> 2.1.2)
@@ -17,13 +31,48 @@ GEM
1731
activesupport (= 3.0.0)
1832
arel (~> 1.0.0)
1933
tzinfo (~> 0.3.23)
34+
activeresource (3.0.0)
35+
activemodel (= 3.0.0)
36+
activesupport (= 3.0.0)
2037
activesupport (3.0.0)
2138
arel (1.0.1)
2239
activesupport (~> 3.0.0)
23-
awesome_nested_set (1.4.3)
24-
activerecord (>= 1.1)
2540
builder (2.1.2)
41+
erubis (2.6.6)
42+
abstract (>= 1.0.0)
2643
i18n (0.4.1)
44+
mail (2.2.6.1)
45+
activesupport (>= 2.3.6)
46+
mime-types
47+
treetop (>= 1.4.5)
48+
mime-types (1.16)
49+
moretea-awesome_nested_set (1.4.3.1)
50+
activerecord (~> 3.0.0)
51+
polyglot (0.3.1)
52+
rack (1.2.1)
53+
rack-mount (0.6.13)
54+
rack (>= 1.0.0)
55+
rack-test (0.5.5)
56+
rack (>= 1.0)
57+
rails (3.0.0)
58+
actionmailer (= 3.0.0)
59+
actionpack (= 3.0.0)
60+
activerecord (= 3.0.0)
61+
activeresource (= 3.0.0)
62+
activesupport (= 3.0.0)
63+
bundler (~> 1.0.0)
64+
railties (= 3.0.0)
65+
railties (3.0.0)
66+
actionpack (= 3.0.0)
67+
activesupport (= 3.0.0)
68+
rake (>= 0.8.4)
69+
thor (~> 0.14.0)
70+
rake (0.8.7)
71+
rspec (1.3.0)
72+
sqlite3-ruby (1.3.1)
73+
thor (0.14.2)
74+
treetop (1.4.8)
75+
polyglot (>= 0.3.1)
2776
tzinfo (0.3.23)
2877

2978
PLATFORMS
@@ -32,4 +81,9 @@ PLATFORMS
3281
DEPENDENCIES
3382
activerecord (~> 3.0)
3483
acts_as_commentable_with_threading!
35-
awesome_nested_set
84+
bundler (~> 1.0)
85+
moretea-awesome_nested_set
86+
rails (~> 3.0)
87+
rake
88+
rspec (~> 1.3)
89+
sqlite3-ruby

acts_as_commentable_with_threading.gemspec

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ ACTS_AS_COMMENTABLE_WITH_THREADING = Gem::Specification.new do |s|
2121
s.test_files = ["spec/commentable_spec.rb", "spec/comment_spec.rb", "spec/spec_helper.rb", "spec/db/database.yml", "spec/db/schema.rb"]
2222
s.rdoc_options = ["--main", "README"]
2323

24-
s.add_dependency 'awesome_nested_set'
24+
s.add_development_dependency 'rake'
25+
s.add_development_dependency 'bundler', '~> 1.0'
26+
s.add_development_dependency 'rspec', '~> 1.3'
27+
s.add_development_dependency 'sqlite3-ruby'
28+
s.add_development_dependency 'rails', '~> 3.0'
29+
2530
s.add_dependency 'activerecord', '~> 3.0'
31+
s.add_dependency 'moretea-awesome_nested_set'
2632
end

init.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
# Include hook code here
2-
require 'acts_as_commentable_with_threading'
3-
1+
require File.expand_path('./rails/init', File.dirname(__FILE__))

lib/comment.rb

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# WHY IS THIS REQUIRED?
2+
ActiveRecord::Base.send(:include, CollectiveIdea::Acts::NestedSet::Base) unless ActiveRecord::Base.respond_to?(:acts_as_nested_set)
3+
14
class Comment < ActiveRecord::Base
25
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
36

@@ -30,25 +33,19 @@ def has_children?
3033

3134
# Helper class method to lookup all comments assigned
3235
# to all commentable types for a given user.
33-
def self.find_comments_by_user(user)
34-
find(:all,
35-
:conditions => ["user_id = ?", user.id],
36-
:order => "created_at DESC"
37-
)
38-
end
39-
36+
scope :find_comments_by_user, lambda { |user|
37+
where(:user_id => user.id).order('created_at DESC')
38+
}
39+
4040
# Helper class method to look up all comments for
4141
# commentable class name and commentable id.
42-
def self.find_comments_for_commentable(commentable_str, commentable_id)
43-
find(:all,
44-
:conditions => ["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id],
45-
:order => "created_at DESC"
46-
)
47-
end
42+
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
43+
where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
44+
}
4845

4946
# Helper class method to look up a commentable object
5047
# given the commentable class name and id
5148
def self.find_commentable(commentable_str, commentable_id)
5249
commentable_str.constantize.find(commentable_id)
5350
end
54-
end
51+
end

rails/init.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# encoding: utf-8
2+
require 'acts_as_commentable_with_threading'
3+
4+
# Why isn't this being done in the railtie require?
5+
ActiveRecord::Base.send(:include, CollectiveIdea::Acts::NestedSet::Base)
6+

spec/comment_spec.rb

+39-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,42 @@
4646
@comment.children.first.should == @child
4747
end
4848
end
49-
end
49+
50+
describe "finders" do
51+
describe "#find_comments_by_user" do
52+
before :each do
53+
@other_user = User.create!
54+
@user_comment = Comment.create!(:body => "Child comment", :user => @user)
55+
@non_user_comment = Comment.create!(:body => "Child comment", :user => @other_user)
56+
@comments = Comment.find_comments_by_user(@user)
57+
end
58+
59+
it "should return all the comments created by the passed user" do
60+
@comments.should include(@user_comment)
61+
end
62+
63+
it "should not return comments created by non-passed users" do
64+
@comments.should_not include(@non_user_comment)
65+
end
66+
end
67+
68+
describe "#find_comments_for_commentable" do
69+
before :each do
70+
@other_user = User.create!
71+
@user_comment = Comment.create!(:body => 'from user', :commentable_type => @other_user.class.to_s, :commentable_id => @other_user.id, :user => @user)
72+
73+
@other_comment = Comment.create!(:body => 'from other user', :commentable_type => @user.class.to_s, :commentable_id => @user.id, :user => @other_user)
74+
75+
@comments = Comment.find_comments_for_commentable(@other_user.class, @other_user.id)
76+
end
77+
78+
it "should return the comments for the passed commentable" do
79+
@comments.should include(@user_comment)
80+
end
81+
82+
it "should not return the comments for non-passed commentables" do
83+
@comments.should_not include(@other_comment)
84+
end
85+
end
86+
end
87+
end

spec/spec_helper.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
$:.unshift(File.dirname(__FILE__) + '/../lib')
1+
require 'bundler/setup'
2+
3+
require 'active_record'
24

3-
require 'rubygems'
4-
require 'active_record'
55
require 'logger'
66

77
plugin_test_dir = File.dirname(__FILE__)

0 commit comments

Comments
 (0)