Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored code:

- Moved a bunch of class_eval code in ratings_extension.rb into a module that gets included in the Page class.
- Converted the tests to specs.
  • Loading branch information...
commit 78191941bace7aa62a914561f9e741bff028603c 1 parent d503e40
@myronmarston authored
View
32 lib/ratings_page_extension.rb
@@ -0,0 +1,32 @@
+module RatingsPageExtension
+ def self.included(base)
+ base.send(:has_many, :ratings)
+ end
+
+ def add_rating(rating, rating_user_token)
+ r = Rating.find_or_initialize_by_page_id_and_user_token(self.id, rating_user_token)
+ r.rating = rating
+ r.save!
+
+ update_average_rating
+ Radiant::Cache.clear
+ end
+
+ def update_average_rating
+ set_average_rating
+ begin
+ self.save!
+ rescue ActiveRecord::StaleObjectError => e
+ self.reload
+ set_average_rating
+ self.save!
+ end
+ end
+
+ protected
+
+ def set_average_rating
+ avg = Rating.average(:rating, :conditions => "page_id = #{id}")
+ self.average_rating = avg ? avg.round(2) : BigDecimal('0')
+ end
+end
View
32 ratings_extension.rb
@@ -8,37 +8,9 @@ class RatingsExtension < Radiant::Extension
end
def activate
- Page.send :include, RatingTags
-
Page.class_eval do
- has_many :ratings
-
- def add_rating(rating, rating_user_token)
- r = Rating.find_or_initialize_by_page_id_and_user_token(self.id, rating_user_token)
- r.rating = rating
- r.save!
-
- update_average_rating
- Radiant::Cache.clear
- end
-
- def update_average_rating
- set_average_rating
- begin
- self.save!
- rescue ActiveRecord::StaleObjectError => e
- self.reload
- set_average_rating
- self.save!
- end
- end
-
- protected
-
- def set_average_rating
- avg = Rating.average(:rating, :conditions => "page_id = #{id}")
- self.average_rating = avg ? avg.round(2) : BigDecimal('0')
- end
+ include RatingTags
+ include RatingsPageExtension
end
end
View
19 spec/models/page_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Page do
+ dataset :pages
+
+ it 'should handle a stale object error' do
+ p1 = pages(:home)
+ p2 = Page.find(p1.id)
+
+ p1.add_rating("1", "token")
+ p1.update_average_rating
+
+ p2.add_rating("2", "token2")
+ p2.update_average_rating
+ p1.reload
+
+ p1.average_rating.should == 1.5
+ end
+end
View
24 test/test_helper.rb
@@ -1,24 +0,0 @@
-require 'test/unit'
-# Load the environment
-unless defined? RADIANT_ROOT
- ENV["RAILS_ENV"] = "test"
- case
- when ENV["RADIANT_ENV_FILE"]
- require ENV["RADIANT_ENV_FILE"]
- when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
- require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../../")}/config/environment"
- else
- require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/environment"
- end
-end
-require "#{RADIANT_ROOT}/test/test_helper"
-
-class Test::Unit::TestCase
- self.use_transactional_fixtures = true
- self.use_instantiated_fixtures = false
- self.fixture_path << File.expand_path(File.dirname(__FILE__)) + '/fixtures'
-end
-
-class ActionController::IntegrationTest < Test::Unit::TestCase
- self.fixture_path = File.expand_path(File.dirname(__FILE__)) + '/fixtures'
-end
View
22 test/unit/page_test.rb
@@ -1,22 +0,0 @@
-require File.dirname(__FILE__) + '/../test_helper'
-require File.dirname(__FILE__) + '/../../ratings_extension'
-
-class PageTest < Test::Unit::TestCase
- def test_handles_stale_exception
- Page.create!(:title => 'Home',
- :slug => '/',
- :breadcrumb => 'home',
- :status => Status[:published])
- RatingsExtension.activate
- p1 = Page.find(1)
- p2 = Page.find(1)
-
- p1.add_rating("1", "token")
- p1.update_average_rating
-
- p2.add_rating("2", "token2")
- p2.update_average_rating
- p1.reload
- assert_equal "1.5", p1.average_rating.to_s
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.