Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Squashing some named scope bugs.

  • Loading branch information...
commit 7ee1924cc81c26bbb6a4ce9f8b411919824bd070 1 parent bc2d3a0
Jonas Grimfelt grimen authored
8 is_reviewable.gemspec
View
@@ -8,10 +8,10 @@ Gem::Specification.new do |s|
s.version = "0.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["grimen@gmail.com"]
- s.date = %q{2009-09-07}
+ s.authors = ["Jonas Grimfelt"]
+ s.date = %q{2009-09-18}
s.description = %q{Rails: Make an ActiveRecord resource ratable/reviewable (rate + text), without the usual extra code-smell.}
- s.email = %q{Jonas Grimfelt}
+ s.email = %q{grimen@gmail.com}
s.extra_rdoc_files = [
"MIT-LICENSE",
"README.textile",
@@ -42,7 +42,7 @@ Gem::Specification.new do |s|
"test/is_reviewable_test.rb",
"test/test_helper.rb"
]
- s.homepage = %q{http://github.com/grimen/is_reviewable/tree/master}
+ s.homepage = %q{http://github.com/grimen/is_reviewable}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.3}
26 lib/is_reviewable/review.rb
View
@@ -13,17 +13,25 @@ class Review < ::ActiveRecord::Base
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 :limit, lambda { |number_of_items| {:limit => number_of_items} }
+ named_scope :recent, lambda { |arg|
+ if [::ActiveSupport::TimeWithZone, ::DateTime].any? { |c| c.is_a?(arg) }
+ {:conditions => ['created_at >= ?', arg]}
+ else
+ {:limit => arg.to_i}
+ end
+ }
+ named_scope :between_dates, lambda { |from_date, to_date| {:conditions => {:created_at => (from_date..to_date)}} }
+ named_scope :with_rating, lambda { |rating_value_or_range| {:conditions => {: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 AND LENGTH(body) > 0']
+ named_scope :without_a_rating, :conditions => ['rating IS NULL']
+ named_scope :with_a_body, :conditions => ['body IS NOT NULL AND LENGTH(body) > 0']
+ named_scope :without_a_body, :conditions => ['body IS NULL OR LENGTH(body) = 0']
named_scope :complete, :conditions => ['rating IS NOT NULL AND body IS NOT NULL AND 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) }
+ named_scope :of_reviewable_type, lambda { |type| {:conditions => Support.polymorphic_conditions_for(type, :reviewable, :type)} }
+ named_scope :by_reviewer_type, lambda { |type| {:conditions => Support.polymorphic_conditions_for(type, :reviewer, :type)} }
+ named_scope :on, lambda { |reviewable| {:conditions => Support.polymorphic_conditions_for(reviewable, :reviewable)} }
+ named_scope :by, lambda { |reviewer| {:conditions => Support.polymorphic_conditions_for(reviewer, :reviewer)} }
end
end
2  lib/is_reviewable/reviewable.rb
View
@@ -72,7 +72,7 @@ def is_reviewable(*args)
options[:review_class] = REVIEW_CLASS_NAME.constantize
rescue
# If not defined...define it!
- ::Object.const_set(REVIEW_CLASS_NAME, ::Class.new(::IsReviewable::Review))
+ ::Object.const_set(REVIEW_CLASS_NAME.to_sym, ::Class.new(::IsReviewable::Review))
options[:review_class] = REVIEW_CLASS_NAME.constantize
end
25 lib/is_reviewable/support.rb
View
@@ -5,19 +5,30 @@ module Support
# Shortcut method for generating conditions hash for polymorphic belongs_to-associations.
#
- def polymorphic_conditions_for(object, *what)
- identifier = object.class.name.underscore
- what = [:id, :type] if what.blank?
- returning Hash.new do |conditions|
- conditions.merge(:"#{identifier}_id" => reviewer.class.name) if what.include?(:id)
- conditions.merge(:"#{identifier}_type" => reviewer.class.name) if what.include?(:type)
+ def polymorphic_conditions_for(object_or_type, field, *match)
+ match = [:id, :type] if match.blank?
+ # Note: {} is equivalent to Hash.new which takes a block, so we must do: ({}) or (Hash.new)
+ returning({}) do |conditions|
+ conditions.merge!(:"#{field}_id" => object_or_type.id) if object_or_type.is_a?(::ActiveRecord::Base && match.include?(:id)
+
+ if match.include?(:type)
+ type = case object_or_type
+ when ::Class
+ object_or_type.name
+ when ::Symbol, ::String
+ object_or_type.to_s.singularize.classify
+ else # Object - or raise NameError as usual
+ object_or_type.class.name
+ end
+ conditions.merge!(:"#{field}_type" => type)
+ end
end
end
# Check if object is a valid activerecord object.
#
def is_active_record?(object)
- object.present? && object.is_a?(::ActiveRecord::Base)
+ object.present? && object.is_a?(::ActiveRecord::Base) # TODO: ::ActiveModel if Rails 3?
end
# Check if input is a valid format of IP, i.e. "#.#.#.#". Note: Just basic validation.
6 test/is_reviewable_test.rb
View
@@ -183,13 +183,17 @@ def setup
context "review" do
- # TODO: Test named scopes á la: http://blog.confabulus.com/2008/11/24/testing-named-scopes, or similar.
+ # TODO: Test named scopes á la:
+ # * http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/
+ # * http://blog.confabulus.com/2008/11/24/testing-named-scopes, or similar.
should "define named scopes" do
named_scopes = [
:between_dates
]
+ # Example: Review.complete.proxy_options # => :conditions=>["rating IS NOT NULL AND body IS NOT NULL AND LENGTH(body) > 0"]}
+
# Old: This won't work...
#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, true) }
Please sign in to comment.
Something went wrong with that request. Please try again.