Skip to content
Browse files

- added instance methods for the rating model: rater,rater=,rater_id,…

…rater_id=,rateable,...

- refactored out new shared examples for the instance methods
  • Loading branch information...
1 parent aa65ae4 commit b654c20298e56f7d05f178ce0f0d025022087cf1 @Ragmaanir committed Mar 11, 2012
View
23 lib/dm-is-rateable/is/rateable.rb
@@ -220,13 +220,18 @@ def configure_rating_model(model,options,&enhancer)
model.timestamps(:at) if options[:timestamps] # TODO :timestamps => :on
- model.__send__(:define_method,:rater) do
- self.send(rater[:name])
- end
-
rateable_name = self.name.underscore
- model.__send__(:define_method,:rateable) do
- self.send(rateable_name)
+
+ model.instance_eval do
+ alias_method(:rater,rater[:name])
+ alias_method(:rater=,"#{rater[:name]}=")
+ alias_method(:rateable,rateable_name)
+ alias_method(:rateable=,"#{rateable_name}=")
+
+ alias_method(:rater_id,"#{rater[:name]}_id")
+ alias_method(:rater_id=,"#{rater[:name]}_id=")
+ alias_method(:rateable_id,"#{rateable_name}_id")
+ alias_method(:rateable_id=,"#{rateable_name}_id=")
end
model.class_eval(&enhancer) if enhancer
@@ -313,7 +318,8 @@ def enable_rating!
raise TogglableRatingDisabled, "Ratings cannot be toggled for #{self}"
end
end
-
+
+ # FIXME: rater can rate multiple concerns. need to pass concern.
def rate(rating, rater)
unless self.rating_enabled?
raise(RatingDisabled, "Ratings are not enabled for #{self}")
@@ -338,6 +344,7 @@ def rate(rating, rater)
end
+ # FIXME: rater can rate multiple concerns. need to pass concern.
# average_rating_of(:users) => nil
# average_rating_of(Account) => 3.76
def average_rating_of(raters_or_model)
@@ -348,11 +355,13 @@ def average_rating_of(raters_or_model)
c > 0 ? sum.to_f / c : nil
end
+ # FIXME: rater can rate multiple concerns. need to pass concern.
def rating_assoc_for(rater)
config = self.class.rating_config_for(rater.class)
self.send(config[:as])
end
+ # FIXME: rater can rate multiple concerns. need to pass concern.
def rating_of(rater)
raise unless rater.is_a? DataMapper::Resource
config = self.class.rating_config_for(rater.class)
View
5 spec/integration/is_rateable_by_as_spec.rb
@@ -1,4 +1,6 @@
+require 'integration/shared_examples'
+
describe DataMapper::Is::Rateable do
def unload_consts(*consts)
@@ -143,8 +145,7 @@ class Trip
its(:relationships) { should be_named(:account) }
its(:relationships) { should be_named(:trip) }
- its(:instance_methods) { should include(:rater) }
- its(:instance_methods) { should include(:rateable) }
+ it_behaves_like :rating_model
end
describe 'Instance' do
View
5 spec/integration/is_rateable_by_concerning_spec.rb
@@ -1,4 +1,6 @@
+require 'integration/shared_examples'
+
describe DataMapper::Is::Rateable do
def unload_consts(*consts)
@@ -144,8 +146,7 @@ class Trip
its(:relationships) { should be_named(:account) }
its(:relationships) { should be_named(:trip) }
- its(:instance_methods) { should include(:rater) }
- its(:instance_methods) { should include(:rateable) }
+ it_behaves_like :rating_model
end
describe 'Instance' do
View
5 spec/integration/is_rateable_by_spec.rb
@@ -1,4 +1,6 @@
+require 'integration/shared_examples'
+
describe DataMapper::Is::Rateable do
# --------------------------------------------------------------------------------------------------
@@ -136,8 +138,7 @@ class Trip
its(:relationships) { should be_named(:account) }
its(:relationships) { should be_named(:trip) }
- its(:instance_methods) { should include(:rater) }
- its(:instance_methods) { should include(:rateable) }
+ it_behaves_like :rating_model
end
describe 'Instance' do
View
5 spec/integration/is_rateable_by_with_spec.rb
@@ -1,4 +1,6 @@
+require 'integration/shared_examples'
+
describe DataMapper::Is::Rateable do
def unload_consts(*consts)
@@ -143,8 +145,7 @@ class Trip
its(:relationships) { should be_named(:account) }
its(:relationships) { should be_named(:trip) }
- its(:instance_methods) { should include(:rater) }
- its(:instance_methods) { should include(:rateable) }
+ it_behaves_like :rating_model
end
describe 'Instance' do
View
14 spec/integration/shared_examples.rb
@@ -0,0 +1,14 @@
+
+shared_examples_for :rating_model do
+ its(:instance_methods) { should include(:rater) }
+ its(:instance_methods) { should include(:rater=) }
+ its(:instance_methods) { should include(:rater_id) }
+ its(:instance_methods) { should include(:rater_id=) }
+
+ its(:instance_methods) { should include(:rateable) }
+ its(:instance_methods) { should include(:rateable=) }
+ its(:instance_methods) { should include(:rateable_id) }
+ its(:instance_methods) { should include(:rateable_id=) }
+end
+
+

0 comments on commit b654c20

Please sign in to comment.
Something went wrong with that request. Please try again.