Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
has_counter now lazy creates counters [#29 state:resolved]
  • Loading branch information
Sven Fuchs committed Jul 24, 2008
1 parent 9df1275 commit 0f29b91
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 25 deletions.
28 changes: 17 additions & 11 deletions spec/models/content_spec.rb
Expand Up @@ -5,6 +5,7 @@
include Matchers::ClassExtensions

before :each do
User.delete_all!
scenario :section_with_published_article
@time_now = Time.zone.now
@author = User.new :name => 'name', :email => 'email@test.org', :homepage => 'http://homepage.com', :login => 'login', :password => 'password', :password_confirmation => 'password'
Expand Down Expand Up @@ -109,27 +110,32 @@
end

describe "validations" do
it "validate presence of a title" do
it "validates presence of a title" do
@content.should validate_presence_of(:title)
end

it "validate presence of a body" do
it "validates presence of a body" do
@content.should validate_presence_of(:body)
end

it "validate presence of an author (through belongs_to_author)" do
it "validates presence of an author (through belongs_to_author)" do
@content.should validate_presence_of(:author)
end

it "validate presence of an author_name (through belongs_to_author)" do
@content.author.stub!(:name).and_return nil
@content.should validate_presence_of(:author_name)
end

it "validate presence of an author_email (through belongs_to_author)" do
@content.author.stub!(:email).and_return nil
@content.should validate_presence_of(:author_email)
it "validates that the author is valid (through belongs_to_author)" do
@content.author.email = nil
@content.valid?.should be_false
end

# it "validate presence of an author_name (through belongs_to_author)" do
# @content.author.stub!(:name).and_return nil
# @content.should validate_presence_of(:author_name)
# end
#
# it "validate presence of an author_email (through belongs_to_author)" do
# @content.author.stub!(:email).and_return nil
# @content.should validate_presence_of(:author_email)
# end

it "validates the uniqueness of the permalink per site" do
@content.should validate_uniqueness_of(:permalink) # :scope => :site_id
Expand Down
4 changes: 2 additions & 2 deletions spec/models/counter_spec.rb
Expand Up @@ -6,9 +6,9 @@
before :each do
@forum = Forum.new :title => 'forum', :site => stub_site
@forum.stub!(:build_path)
@forum.save
@forum.save!

@topic_attributes = {:section => @forum, :title => 'title', :body => 'body', :last_author => stub_user, :last_author_name => 'name', :last_author_email => 'email@email.org'}
@topic_attributes = {:section => @forum, :title => 'title', :body => 'body', :author => stub_user, :last_author => stub_user, :last_author_name => 'name', :last_author_email => 'email@email.org'}
end

it "has_one topics_count" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/topic_spec.rb
Expand Up @@ -85,7 +85,7 @@
end

it "initializes a new Topic with the given attributes" do
Topic.should_receive(:new).with(@attributes).and_return @topic
Topic.should_receive(:new).with(@attributes.merge(:author => @user)).and_return @topic
Topic.post @user, @attributes
end

Expand Down
2 changes: 1 addition & 1 deletion spec/scenarios/forum_with_two_topic_fixtures.rb
Expand Up @@ -3,7 +3,7 @@
@forum.stub!(:build_path)
@forum.save

attributes = {:title => 'title', :body => 'body', :last_author => stub_user, :last_author_name => 'name', :last_author_email => 'email@email.org', :section => @forum}
attributes = {:title => 'title', :body => 'body', :author => stub_user, :last_author => stub_user, :last_author_name => 'name', :last_author_email => 'email@email.org', :section => @forum}
@earlier_topic = Topic.create! attributes.update(:last_updated_at => 1.month.ago)
@latest_topic = Topic.create! attributes.update(:last_updated_at => Time.now)

Expand Down
6 changes: 4 additions & 2 deletions spec/stubs/user.rb
Expand Up @@ -3,7 +3,8 @@
:anonymous? => true,
:registered? => false,
:update_attributes => true,
:destroy => true
:destroy => true,
:valid? => true

instance :user,
:id => 1,
Expand All @@ -23,7 +24,8 @@
:destroy => true,
:verified! => nil,
:assign_token => 'token',
:email= => nil
:email= => nil,
:valid? => true

instance :user,
:id => 1,
Expand Down
1 change: 1 addition & 0 deletions spec/views/admin/user_views_spec.rb
Expand Up @@ -17,6 +17,7 @@

template.stub!(:gravatar_img)
template.stub!(:current_user).and_return @user
template.stub!(:will_paginate).and_return "will_paginate"
end

describe "the :index view" do
Expand Down
4 changes: 2 additions & 2 deletions stories/factories/forum.rb
@@ -1,7 +1,7 @@
factories :user, :sections

factory :comment,
:body => 'the post body',
factory :post,
:body => 'the post body',
:site_id => lambda{ (Site.find(:first) || create_site).id },
:section_id => lambda{ (Forum.find(:first) || create_forum).id },
:author_id => lambda{ (User.find(:first) || create_user).id },
Expand Down
Expand Up @@ -35,8 +35,9 @@ def self.define_author_methods(target, column_names, validate)
belongs_to :#{column}, :polymorphic => true # TODO :with_deleted => true
if #{validate.inspect}
validates_presence_of :#{column}
validates_presence_of :#{column}_name if column_names.include?("#{column}_name")
validates_presence_of :#{column}_email if column_names.include?("#{column}_email")
validates_associated :#{column}
# validates_presence_of :#{column}_name if column_names.include?("#{column}_name")
# validates_presence_of :#{column}_email if column_names.include?("#{column}_email")
end
before_save :cache_#{column}_attributes!
Expand Down
20 changes: 16 additions & 4 deletions vendor/plugins/has_counter/lib/active_record/has_counter.rb
Expand Up @@ -11,7 +11,7 @@ def has_counter(*names)
options = names.extract_options!
options.reverse_merge! :after_create => :increment!,
:after_destroy => :decrement!

names.each do |name|
counter_name = :"#{name}_counter"
owner_name = options[:as] || self.name.demodulize.underscore
Expand All @@ -26,9 +26,21 @@ def has_counter(*names)
:conditions => "name = '#{name}'",
:dependent => :delete

after_create do |owner|
counter = Counter.create! :owner => owner, :name => name.to_s
end
class_eval <<-code, __FILE__, __LINE__
def #{counter_name}_with_lazy_creation(force_reload = false)
result = #{counter_name}_without_lazy_creation force_reload
if result.nil?
Counter.create!(:owner => self, :name => #{name.to_s.inspect})
result = #{counter_name}_without_lazy_creation true
end
result
end
alias_method_chain counter_name, :lazy_creation
code

# after_create do |owner|
# Counter.create! :owner => owner, :name => name.to_s
# end

# Wire up the counted class so that it updates our counter
update = lambda{|record, event|
Expand Down
91 changes: 91 additions & 0 deletions vendor/plugins/has_counter/spec/has_counter.spec.log
Expand Up @@ -4989,3 +4989,94 @@
Counter Update (0.000091) UPDATE "counters" SET "count" = 0 WHERE "id" = 572
CounterSpec::Content Load (0.000367) SELECT * FROM "contents" WHERE ("contents"."id" = 308) 
Counter Load (0.000341) SELECT * FROM "counters" WHERE ("counters".owner_id = 308 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'approved_comments')) LIMIT 1
SQL (0.001680)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (0.000617)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (0.000604)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

Counter Delete all (0.003181) DELETE FROM "counters" WHERE 1=1
CounterSpec::Content Delete all (0.001742) DELETE FROM "contents" WHERE 1=1
CounterSpec::Comment Delete all (0.000752) DELETE FROM "comments" WHERE 1=1
CounterSpec::Content Create (0.000559) INSERT INTO "contents" ("title") VALUES('first content')
CounterSpec::Comment Create (0.000491) INSERT INTO "comments" ("content_id", "text", "approved") VALUES(309, 'first comment', NULL)
CounterSpec::Content Load (0.000255) SELECT * FROM "contents" WHERE ("contents"."id" = 309) 
Counter Load (0.000251) SELECT * FROM "counters" WHERE ("counters".owner_id = 309 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Create (0.000279) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 309, 0, 'CounterSpec::Content')
Counter Load (0.000181) SELECT * FROM "counters" WHERE ("counters".owner_id = 309 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'approved_comments')) LIMIT 1
Counter Create (0.000139) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 309, 0, 'CounterSpec::Content')
Counter Create (0.000123) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 309, 0, 'CounterSpec::Content')
Counter Create (0.000106) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 309, 0, 'CounterSpec::Content')
Counter Load (0.000422) SELECT * FROM "counters" WHERE ("counters".owner_id = 309 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Delete all (0.001965) DELETE FROM "counters" WHERE 1=1
CounterSpec::Content Delete all (0.001775) DELETE FROM "contents" WHERE 1=1
CounterSpec::Comment Delete all (0.001876) DELETE FROM "comments" WHERE 1=1
CounterSpec::Content Create (0.000552) INSERT INTO "contents" ("title") VALUES('first content')
CounterSpec::Comment Create (0.000492) INSERT INTO "comments" ("content_id", "text", "approved") VALUES(310, 'first comment', NULL)
CounterSpec::Content Load (0.000264) SELECT * FROM "contents" WHERE ("contents"."id" = 310) 
Counter Load (0.000146) SELECT * FROM "counters" WHERE ("counters".owner_id = 310 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Create (0.000203) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 310, 0, 'CounterSpec::Content')
Counter Load (0.000138) SELECT * FROM "counters" WHERE ("counters".owner_id = 310 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'approved_comments')) LIMIT 1
Counter Create (0.000118) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 310, 0, 'CounterSpec::Content')
Counter Create (0.000166) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 310, 0, 'CounterSpec::Content')
Counter Create (0.000162) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 310, 0, 'CounterSpec::Content')
Counter Load (0.000499) SELECT * FROM "counters" WHERE ("counters".owner_id = 310 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Delete all (0.002016) DELETE FROM "counters" WHERE 1=1
CounterSpec::Content Delete all (0.001706) DELETE FROM "contents" WHERE 1=1
CounterSpec::Comment Delete all (0.001762) DELETE FROM "comments" WHERE 1=1
CounterSpec::Content Create (0.000488) INSERT INTO "contents" ("title") VALUES('first content')
CounterSpec::Comment Create (0.000486) INSERT INTO "comments" ("content_id", "text", "approved") VALUES(311, 'first comment', NULL)
CounterSpec::Content Load (0.000527) SELECT * FROM "contents" WHERE ("contents"."id" = 311) 
Counter Load (0.000164) SELECT * FROM "counters" WHERE ("counters".owner_id = 311 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Create (0.000250) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 311, 0, 'CounterSpec::Content')
Counter Load (0.000142) SELECT * FROM "counters" WHERE ("counters".owner_id = 311 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'approved_comments')) LIMIT 1
Counter Create (0.000127) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 311, 0, 'CounterSpec::Content')
Counter Create (0.000129) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 311, 0, 'CounterSpec::Content')
Counter Create (0.000151) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 311, 0, 'CounterSpec::Content')
CounterSpec::Comment Destroy (0.000402)  DELETE FROM "comments"
WHERE "id" = 253

Counter Create (0.000205) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 311, 0, 'CounterSpec::Content')
Counter Create (0.000117) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 311, 0, 'CounterSpec::Content')
Counter Load (0.000498) SELECT * FROM "counters" WHERE ("counters".owner_id = 311 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Delete all (0.002046) DELETE FROM "counters" WHERE 1=1
CounterSpec::Content Delete all (0.001698) DELETE FROM "contents" WHERE 1=1
CounterSpec::Comment Delete all (0.000820) DELETE FROM "comments" WHERE 1=1
CounterSpec::Content Create (0.000393) INSERT INTO "contents" ("title") VALUES('first content')
CounterSpec::Comment Create (0.000547) INSERT INTO "comments" ("content_id", "text", "approved") VALUES(312, 'first comment', NULL)
CounterSpec::Content Load (0.000289) SELECT * FROM "contents" WHERE ("contents"."id" = 312) 
Counter Load (0.000148) SELECT * FROM "counters" WHERE ("counters".owner_id = 312 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Create (0.000204) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 312, 0, 'CounterSpec::Content')
Counter Load (0.000148) SELECT * FROM "counters" WHERE ("counters".owner_id = 312 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'approved_comments')) LIMIT 1
Counter Create (0.000109) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 312, 0, 'CounterSpec::Content')
Counter Create (0.000146) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 312, 0, 'CounterSpec::Content')
Counter Create (0.000109) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 312, 0, 'CounterSpec::Content')
Counter Load (0.000466) SELECT * FROM "counters" WHERE ("counters".owner_id = 312 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'approved_comments')) LIMIT 1
Counter Delete all (0.002006) DELETE FROM "counters" WHERE 1=1
CounterSpec::Content Delete all (0.001340) DELETE FROM "contents" WHERE 1=1
CounterSpec::Comment Delete all (0.001650) DELETE FROM "comments" WHERE 1=1
CounterSpec::Content Create (0.000685) INSERT INTO "contents" ("title") VALUES('first content')
CounterSpec::Comment Create (0.000538) INSERT INTO "comments" ("content_id", "text", "approved") VALUES(313, 'first comment', 1)
CounterSpec::Content Load (0.000295) SELECT * FROM "contents" WHERE ("contents"."id" = 313) 
Counter Load (0.000166) SELECT * FROM "counters" WHERE ("counters".owner_id = 313 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Create (0.000232) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 313, 0, 'CounterSpec::Content')
Counter Load (0.000144) SELECT * FROM "counters" WHERE ("counters".owner_id = 313 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'approved_comments')) LIMIT 1
Counter Create (0.000117) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 313, 0, 'CounterSpec::Content')
Counter Create (0.000112) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 313, 0, 'CounterSpec::Content')
Counter Delete all (0.001031) DELETE FROM "counters" WHERE 1=1
CounterSpec::Content Delete all (0.001744) DELETE FROM "contents" WHERE 1=1
CounterSpec::Comment Delete all (0.000729) DELETE FROM "comments" WHERE 1=1
CounterSpec::Content Create (0.000374) INSERT INTO "contents" ("title") VALUES('first content')
CounterSpec::Comment Create (0.000413) INSERT INTO "comments" ("content_id", "text", "approved") VALUES(314, 'first comment', 1)
CounterSpec::Content Load (0.000258) SELECT * FROM "contents" WHERE ("contents"."id" = 314) 
Counter Load (0.000142) SELECT * FROM "counters" WHERE ("counters".owner_id = 314 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'comments')) LIMIT 1
Counter Create (0.000241) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('comments', 314, 0, 'CounterSpec::Content')
Counter Load (0.000144) SELECT * FROM "counters" WHERE ("counters".owner_id = 314 AND "counters".owner_type = 'CounterSpec::Content' AND (name = 'approved_comments')) LIMIT 1
Counter Create (0.000115) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 314, 0, 'CounterSpec::Content')
Counter Create (0.000174) INSERT INTO "counters" ("name", "owner_id", "count", "owner_type") VALUES('approved_comments', 314, 0, 'CounterSpec::Content')
Binary file modified vendor/plugins/has_counter/spec/has_counter.sqlite3.db
Binary file not shown.

0 comments on commit 0f29b91

Please sign in to comment.