Permalink
Browse files

- gem dependencies updated

- added active support dependency for class_attribute
- removed deprecated class_inheritable_reader
- replaced Extlib::Inflection with DataMapper::Inflector
- replaced :nullable with :required
- changed default rater-options to :required => false, so the test pass
  • Loading branch information...
Ragmaanir committed Feb 11, 2012
1 parent d510ba5 commit ca7ac0b13d96139a310c589002d713eb10890b60
Showing with 261 additions and 73 deletions.
  1. +27 −0 Gemfile
  2. +68 −0 Gemfile.lock
  3. +22 −8 Rakefile
  4. +6 −1 lib/dm-is-rateable.rb
  5. +52 −34 lib/dm-is-rateable/is/rateable.rb
  6. +77 −26 spec/integration/rateable_spec.rb
  7. +9 −4 spec/spec_helper.rb
View
27 Gemfile
@@ -1,5 +1,31 @@
source 'http://rubygems.org'
+DM_VERSION = '~> 1.2.0'
+
+gem 'dm-core', DM_VERSION
+
+gem 'dm-validations', DM_VERSION
+gem 'dm-aggregates', DM_VERSION
+gem 'dm-timestamps', DM_VERSION
+gem 'dm-types', DM_VERSION
+gem 'dm-is-remixable', DM_VERSION
+gem 'dm-migrations', DM_VERSION
+
+gem 'activesupport', '~> 3.1.1'
+
+group(:test) do
+ gem 'rspec'
+end
+
+group(:development) do
+ gem 'rake'
+ gem 'jeweler'
+ gem 'yard'
+end
+
+=begin
+source 'http://rubygems.org'
+
gem 'dm-core', '~> 0.10.2', :git => 'git://github.com/snusnu/dm-core.git', :branch => 'active_support'
git 'git://github.com/snusnu/dm-more.git', :branch => 'active_support' do
@@ -21,3 +47,4 @@ group(:development) do
gem 'jeweler', '~> 1.4'
gem 'yard', '~> 0.5'
end
+=end
View
@@ -0,0 +1,68 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.1.1)
+ multi_json (~> 1.0)
+ addressable (2.2.6)
+ bcrypt-ruby (3.0.1)
+ diff-lcs (1.1.3)
+ dm-aggregates (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-core (1.2.0)
+ addressable (~> 2.2.6)
+ dm-is-remixable (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-migrations (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-timestamps (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-types (1.2.1)
+ bcrypt-ruby (~> 3.0.0)
+ dm-core (~> 1.2.0)
+ fastercsv (~> 1.5.4)
+ json (~> 1.6.1)
+ multi_json (~> 1.0.3)
+ stringex (~> 1.3.0)
+ uuidtools (~> 2.1.2)
+ dm-validations (1.2.0)
+ dm-core (~> 1.2.0)
+ fastercsv (1.5.4)
+ git (1.2.5)
+ jeweler (1.8.3)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ rdoc
+ json (1.6.5)
+ multi_json (1.0.4)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rspec (2.8.0)
+ rspec-core (~> 2.8.0)
+ rspec-expectations (~> 2.8.0)
+ rspec-mocks (~> 2.8.0)
+ rspec-core (2.8.0)
+ rspec-expectations (2.8.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.8.0)
+ stringex (1.3.0)
+ uuidtools (2.1.2)
+ yard (0.7.5)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activesupport (~> 3.1.1)
+ dm-aggregates (~> 1.2.0)
+ dm-core (~> 1.2.0)
+ dm-is-remixable (~> 1.2.0)
+ dm-migrations (~> 1.2.0)
+ dm-timestamps (~> 1.2.0)
+ dm-types (~> 1.2.0)
+ dm-validations (~> 1.2.0)
+ jeweler
+ rake
+ rspec
+ yard
View
@@ -25,14 +25,28 @@ begin
gem.homepage = 'http://github.com/snusnu/dm-is-rateable'
gem.authors = [ 'Martin Gamsjaeger (snusnu)' ]
- gem.add_dependency 'dm-core', '~> 0.10.2'
- gem.add_dependency 'dm-validations', '~> 0.10.2'
- gem.add_dependency 'dm-aggregates', '~> 0.10.2'
- gem.add_dependency 'dm-timestamps', '~> 0.10.2'
- gem.add_dependency 'dm-types', '~> 0.10.2'
- gem.add_dependency 'dm-is-remixable', '~> 0.10.2'
-
- gem.add_development_dependency 'rspec', '~> 1.3'
+ #gem.add_dependency 'dm-core', '~> 0.10.2'
+ #gem.add_dependency 'dm-validations', '~> 0.10.2'
+ #gem.add_dependency 'dm-aggregates', '~> 0.10.2'
+ #gem.add_dependency 'dm-timestamps', '~> 0.10.2'
+ #gem.add_dependency 'dm-types', '~> 0.10.2'
+ #gem.add_dependency 'dm-is-remixable', '~> 0.10.2'
+
+
+ DM_VERSION = '~> 1.2.0'
+
+ gem.add_dependency 'dm-core', DM_VERSION
+
+ gem.add_dependency 'dm-validations', DM_VERSION
+ gem.add_dependency 'dm-aggregates', DM_VERSION
+ gem.add_dependency 'dm-timestamps', DM_VERSION
+ gem.add_dependency 'dm-types', DM_VERSION
+ gem.add_dependency 'dm-is-remixable', DM_VERSION
+ gem.add_dependency 'dm-migrations', DM_VERSION
+
+ gem.add_dependency 'activesupport', '~> 3.1.1'
+
+ gem.add_development_dependency 'rspec', '~> 2.8'
gem.add_development_dependency 'yard', '~> 0.5'
end
View
@@ -6,9 +6,14 @@
require 'dm-validations'
require 'dm-aggregates'
require 'dm-is-remixable'
+require 'dm-migrations'
+
+require 'active_support/core_ext/class'
+require 'active_support/inflector'
# Require plugin-files
-require Pathname(__FILE__).dirname.expand_path / 'dm-is-rateable' / 'is' / 'rateable.rb'
+#require Pathname(__FILE__).dirname.expand_path / 'dm-is-rateable' / 'is' / 'rateable.rb'
+require File.join('dm-is-rateable','is','rateable.rb')
# Include the plugin in Resource
DataMapper::Model.append_extensions DataMapper::Is::Rateable
@@ -47,31 +47,43 @@ def is_rateable(options = {})
:model => "#{self}Rating"
}.merge(options)
- @allowed_ratings = options[:allowed_ratings]
- class_inheritable_accessor :allowed_ratings
+ #@allowed_ratings = options[:allowed_ratings]
+ #class_inheritable_accessor :allowed_ratings
- @rateable_model = options[:model]
- class_inheritable_accessor :rateable_model
+ #@rateable_model = options[:model]
+ #class_inheritable_accessor :rateable_model
- @rateable_key = @rateable_model.snake_case.to_sym
- class_inheritable_accessor :rateable_key
+ #@rateable_key = @rateable_model.snake_case.to_sym
+ #class_inheritable_accessor :rateable_key
+
+ class_attribute :allowed_ratings
+ self.allowed_ratings = options[:allowed_ratings]
+
+ class_attribute :rateable_model
+ self.rateable_model = options[:model]
+
+ class_attribute :rateable_key
+ self.rateable_key = rateable_model.underscore.to_sym
remix n, Rating, :as => options[:as], :model => options[:model]
- @remixed_rating = remixables[:rating]
- class_inheritable_reader :remixed_rating
+ #@remixed_rating = remixables[:rating]
+ #class_inheritable_reader :remixed_rating
+
+ class_attribute :remixed_rating
+ self.remixed_rating = remixables[:rating]
- if @remixed_rating[:reader] != :ratings
+ if remixed_rating[:reader] != :ratings
self.class_eval(<<-EOS, __FILE__, __LINE__ + 1)
- alias :ratings :#{@remixed_rating[rateable_key][:reader]}
+ alias :ratings :#{remixed_rating[rateable_key][:reader]}
EOS
end
# determine property type based on supplied values
rating_type = case options[:allowed_ratings]
- when Range then
+ when Range then
options[:allowed_ratings].first.is_a?(Integer) ? Integer : String
- when Enum then
+ when Enum then
require 'dm-types'
DataMapper::Types::Enum
else
@@ -83,48 +95,48 @@ def is_rateable(options = {})
# prepare rating enhancements
def rater_fk(name)
- name ? Extlib::Inflection.foreign_key(name.to_s.singular).to_sym : :user_id
+ name ? DataMapper::Inflector.foreign_key(name.to_s.singularize).to_sym : :user_id
end
- if options[:rater]
-
+ tmp_rater_fk = if options[:rater]
rater_opts = options[:rater]
rater_name = rater_opts.is_a?(Hash) ? (rater_opts.delete(:name) || :user_id) : rater_fk(rater_opts)
rater_type = rater_opts.is_a?(Hash) ? (rater_opts.delete(:type) || Integer) : Integer
- rater_property_opts = rater_opts.is_a?(Hash) ? rater_opts : { :required => true }
+ #rater_property_opts = rater_opts.is_a?(Hash) ? rater_opts : { :required => true }
+ rater_property_opts = rater_opts.is_a?(Hash) ? rater_opts : { :required => false }
rater_property_opts.merge!(:min => 0) if rater_type == Integer # Match referenced column type
rater_association = rater_name.to_s.gsub(/_id/, '').to_sym
- @rater_fk = rater_name
-
+ rater_name
else
- @rater_fk = nil # no rater association established
+ nil # no rater association established
end
- class_inheritable_reader :rater_fk
+ #class_inheritable_reader :rater_fk
+ class_attribute :rater_fk
+ self.rater_fk = tmp_rater_fk
# close on this because enhance will class_eval in remixable model scope
parent_key = self.rateable_fk
- enhance :rating, @rateable_model do
+ enhance :rating, rateable_model do
- property :rating, rating_type, :nullable => false
+ property :rating, rating_type, :required => true #, :nullable => false
property :rating, rating_type, :required => true
if options[:rater]
-
property rater_name, rater_type, rater_property_opts # rater
belongs_to rater_association
parent_assocation = parent_key.to_s.gsub(/_id/, '').to_sym
- validates_is_unique rater_name, :when => :testing_association, :scope => [parent_assocation]
- validates_is_unique rater_name, :when => :testing_property, :scope => [parent_key]
-
+ validates_uniqueness_of rater_name, :when => :testing_association, :scope => [parent_assocation]
+ validates_uniqueness_of rater_name, :when => :testing_property, :scope => [parent_key]
end
if options[:timestamps]
- property :created_at, DateTime
- property :updated_at, DateTime
+ #property :created_at, DateTime
+ #property :updated_at, DateTime
+ timestamps :at
end
end
@@ -146,8 +158,8 @@ def total_rating
end
def rateable_fk
- demodulized_name = Extlib::Inflection.demodulize(self.name)
- Extlib::Inflection.foreign_key(demodulized_name).to_sym
+ demodulized_name = DataMapper::Inflector.demodulize(self.name)
+ DataMapper::Inflector.foreign_key(demodulized_name).to_sym
end
end
@@ -244,14 +256,20 @@ def rate(rating, user = nil)
if user
if r = self.user_rating(user)
if r.rating != rating
- r.update(:rating => rating)
+ r.update(:rating => rating) or raise
end
else
- self.ratings.create(self.rater => user, :rating => rating)
+ # FIXME: save or raise
+ res = self.ratings.create(self.rater => user, :rating => rating)
+ raise(res.errors.inspect) unless res.saved?
+ res
end
else
if self.anonymous_rating_enabled?
- self.ratings.create(:rating => rating)
+ # FIXME: save or raise
+ res = self.ratings.create(:rating => rating)
+ raise(res.errors.inspect) unless res.saved?
+ res
else
msg = "Anonymous ratings are not enabled for #{self}"
raise AnonymousRatingDisabled, msg
@@ -275,4 +293,4 @@ def user_rating(user, conditions = {})
end
end
-end
+end
Oops, something went wrong.

0 comments on commit ca7ac0b

Please sign in to comment.