mongoid-letsrate 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/Gemfile.lock +41 -1
- data/lib/mongoid/letsrate/model.rb +5 -32
- data/lib/mongoid/letsrate/version.rb +1 -1
- data/mongoid-letsrate.gemspec +5 -0
- data/spec/letsrate_spec.rb +60 -0
- data/spec/models/product.rb +6 -0
- data/spec/models/rate.rb +9 -0
- data/spec/models/rating_cache.rb +9 -0
- data/spec/models/user.rb +6 -0
- data/spec/spec_helper.rb +30 -0
- metadata +72 -4
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 804bab80b25f5fce372efbb580c8548e76d04d01
         | 
| 4 | 
            +
              data.tar.gz: 8aa8578ee5d9d53207d81f79049175b4ff2f02b9
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 77ba76f669b3bbc1139fce240a4a5d3fae2f1f6ae7b9cba988c76c01a17297b021cf79929dcfd58c2b8089340c0cf547d4da5031cdcf8e664756667c9cb044c9
         | 
| 7 | 
            +
              data.tar.gz: fcb4d810335911d3806c1ce30ce87db3bc8b659f865bf7b9647e65d89ea793ff19b9b6fc2080c14fb9ddafd4a91581f97f8fafed4f6c464fbdc0539473569d88
         | 
    
        data/Gemfile
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,7 +1,17 @@ | |
| 1 | 
            +
            GIT
         | 
| 2 | 
            +
              remote: git://github.com/mongoid/mongoid.git
         | 
| 3 | 
            +
              revision: c8af8d0836b086cebc4a609037a97747e9f3359b
         | 
| 4 | 
            +
              specs:
         | 
| 5 | 
            +
                mongoid (4.0.0.beta1)
         | 
| 6 | 
            +
                  activemodel (>= 4.0.0)
         | 
| 7 | 
            +
                  moped (~> 2.0.beta6)
         | 
| 8 | 
            +
                  origin (~> 2.1)
         | 
| 9 | 
            +
                  tzinfo (>= 0.3.37)
         | 
| 10 | 
            +
             | 
| 1 11 | 
             
            PATH
         | 
| 2 12 | 
             
              remote: .
         | 
| 3 13 | 
             
              specs:
         | 
| 4 | 
            -
                mongoid-letsrate (1.0. | 
| 14 | 
            +
                mongoid-letsrate (1.0.13)
         | 
| 5 15 |  | 
| 6 16 | 
             
            GEM
         | 
| 7 17 | 
             
              remote: http://rubygems.org/
         | 
| @@ -12,6 +22,9 @@ GEM | |
| 12 22 | 
             
                  erubis (~> 2.7.0)
         | 
| 13 23 | 
             
                  rack (~> 1.5.2)
         | 
| 14 24 | 
             
                  rack-test (~> 0.6.2)
         | 
| 25 | 
            +
                activemodel (4.0.2)
         | 
| 26 | 
            +
                  activesupport (= 4.0.2)
         | 
| 27 | 
            +
                  builder (~> 3.1.0)
         | 
| 15 28 | 
             
                activesupport (4.0.2)
         | 
| 16 29 | 
             
                  i18n (~> 0.6, >= 0.6.4)
         | 
| 17 30 | 
             
                  minitest (~> 4.2)
         | 
| @@ -20,14 +33,28 @@ GEM | |
| 20 33 | 
             
                  tzinfo (~> 0.3.37)
         | 
| 21 34 | 
             
                atomic (1.1.14)
         | 
| 22 35 | 
             
                atomic (1.1.14-java)
         | 
| 36 | 
            +
                bson (2.2.1)
         | 
| 37 | 
            +
                bson (2.2.1-java)
         | 
| 23 38 | 
             
                builder (3.1.4)
         | 
| 39 | 
            +
                connection_pool (1.2.0)
         | 
| 40 | 
            +
                diff-lcs (1.2.5)
         | 
| 24 41 | 
             
                erubis (2.7.0)
         | 
| 25 42 | 
             
                i18n (0.6.9)
         | 
| 26 43 | 
             
                jquery-rails (3.0.4)
         | 
| 27 44 | 
             
                  railties (>= 3.0, < 5.0)
         | 
| 28 45 | 
             
                  thor (>= 0.14, < 2.0)
         | 
| 29 46 | 
             
                minitest (4.7.5)
         | 
| 47 | 
            +
                mongoid-rspec (1.10.0)
         | 
| 48 | 
            +
                  mongoid (>= 3.0.1)
         | 
| 49 | 
            +
                  rake
         | 
| 50 | 
            +
                  rspec (>= 2.14)
         | 
| 51 | 
            +
                moped (2.0.0.beta6)
         | 
| 52 | 
            +
                  bson (~> 2.2)
         | 
| 53 | 
            +
                  connection_pool (~> 1.2)
         | 
| 54 | 
            +
                  optionable (~> 0.2.0)
         | 
| 30 55 | 
             
                multi_json (1.8.2)
         | 
| 56 | 
            +
                optionable (0.2.0)
         | 
| 57 | 
            +
                origin (2.1.0)
         | 
| 31 58 | 
             
                rack (1.5.2)
         | 
| 32 59 | 
             
                rack-test (0.6.2)
         | 
| 33 60 | 
             
                  rack (>= 1.0)
         | 
| @@ -37,6 +64,14 @@ GEM | |
| 37 64 | 
             
                  rake (>= 0.8.7)
         | 
| 38 65 | 
             
                  thor (>= 0.18.1, < 2.0)
         | 
| 39 66 | 
             
                rake (10.1.1)
         | 
| 67 | 
            +
                rspec (2.14.1)
         | 
| 68 | 
            +
                  rspec-core (~> 2.14.0)
         | 
| 69 | 
            +
                  rspec-expectations (~> 2.14.0)
         | 
| 70 | 
            +
                  rspec-mocks (~> 2.14.0)
         | 
| 71 | 
            +
                rspec-core (2.14.7)
         | 
| 72 | 
            +
                rspec-expectations (2.14.5)
         | 
| 73 | 
            +
                  diff-lcs (>= 1.1.3, < 2.0)
         | 
| 74 | 
            +
                rspec-mocks (2.14.6)
         | 
| 40 75 | 
             
                thor (0.18.1)
         | 
| 41 76 | 
             
                thread_safe (0.1.3)
         | 
| 42 77 | 
             
                  atomic
         | 
| @@ -49,5 +84,10 @@ PLATFORMS | |
| 49 84 | 
             
              ruby
         | 
| 50 85 |  | 
| 51 86 | 
             
            DEPENDENCIES
         | 
| 87 | 
            +
              activesupport
         | 
| 88 | 
            +
              bson
         | 
| 52 89 | 
             
              jquery-rails
         | 
| 90 | 
            +
              mongoid!
         | 
| 53 91 | 
             
              mongoid-letsrate!
         | 
| 92 | 
            +
              mongoid-rspec
         | 
| 93 | 
            +
              rspec
         | 
| @@ -5,45 +5,18 @@ module Mongoid | |
| 5 5 | 
             
              module Letsrate
         | 
| 6 6 | 
             
                extend ActiveSupport::Concern
         | 
| 7 7 |  | 
| 8 | 
            -
                def rate(stars, user | 
| 8 | 
            +
                def rate(stars, user)
         | 
| 9 9 | 
             
                  if can_rate? user
         | 
| 10 10 | 
             
                    rates.create! do |r|
         | 
| 11 11 | 
             
                      r.stars = stars
         | 
| 12 12 | 
             
                      r.rater = user
         | 
| 13 13 | 
             
                    end
         | 
| 14 | 
            -
                     | 
| 15 | 
            -
                      update_rate_average_dirichlet(stars)
         | 
| 16 | 
            -
                    else
         | 
| 17 | 
            -
                      update_rate_average(stars)
         | 
| 18 | 
            -
                    end
         | 
| 14 | 
            +
                    update_rate_average(stars)
         | 
| 19 15 | 
             
                  else
         | 
| 20 16 | 
             
                    raise 'User has already rated.'
         | 
| 21 17 | 
             
                  end
         | 
| 22 18 | 
             
                end
         | 
| 23 19 |  | 
| 24 | 
            -
                def update_rate_average_dirichlet(stars)
         | 
| 25 | 
            -
                  ## assumes 5 possible vote categories
         | 
| 26 | 
            -
                  dp = {1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1}
         | 
| 27 | 
            -
                  stars_group = Hash[rates.group(:stars).count.map { |k, v| [k.to_i, v] }]
         | 
| 28 | 
            -
                  posterior = dp.merge(stars_group) { |key, a, b| a + b }
         | 
| 29 | 
            -
                  sum = posterior.map { |i, v| v }.inject { |a, b| a + b }
         | 
| 30 | 
            -
                  davg = posterior.map { |i, v| i * v }.inject { |a, b| a + b }.to_f / sum
         | 
| 31 | 
            -
             | 
| 32 | 
            -
                  if average.nil?
         | 
| 33 | 
            -
                    RatingCache.create! do |avg|
         | 
| 34 | 
            -
                      avg.cacheable_id = self.id
         | 
| 35 | 
            -
                      avg.cacheable_type = self.class.name
         | 
| 36 | 
            -
                      avg.qty = 1
         | 
| 37 | 
            -
                      avg.avg = davg
         | 
| 38 | 
            -
                    end
         | 
| 39 | 
            -
                  else
         | 
| 40 | 
            -
                    a = average
         | 
| 41 | 
            -
                    a.qty = rates.count
         | 
| 42 | 
            -
                    a.avg = davg
         | 
| 43 | 
            -
                    a.save!(validate: false)
         | 
| 44 | 
            -
                  end
         | 
| 45 | 
            -
                end
         | 
| 46 | 
            -
             | 
| 47 20 | 
             
                def update_rate_average(stars)
         | 
| 48 21 | 
             
                  if average.nil?
         | 
| 49 22 | 
             
                    RatingCache.create! do |avg|
         | 
| @@ -54,8 +27,9 @@ module Mongoid | |
| 54 27 | 
             
                    end
         | 
| 55 28 | 
             
                  else
         | 
| 56 29 | 
             
                    a = average
         | 
| 30 | 
            +
                    all_stars = rates.map &:stars
         | 
| 31 | 
            +
                    a.avg = all_stars.inject { |sum, value| sum + value } / all_stars.size
         | 
| 57 32 | 
             
                    a.qty = rates.count
         | 
| 58 | 
            -
                    a.avg = average(:stars)
         | 
| 59 33 | 
             
                    a.save!(validate: false)
         | 
| 60 34 | 
             
                  end
         | 
| 61 35 | 
             
                end
         | 
| @@ -68,8 +42,7 @@ module Mongoid | |
| 68 42 |  | 
| 69 43 | 
             
                  def letsrate_rater
         | 
| 70 44 | 
             
                    has_many :ratings_given, :class_name => 'Rate'
         | 
| 71 | 
            -
                    belongs_to : | 
| 72 | 
            -
                    belongs_to :average
         | 
| 45 | 
            +
                    belongs_to :rateable
         | 
| 73 46 | 
             
                  end
         | 
| 74 47 |  | 
| 75 48 | 
             
                  def letsrate_rateable
         | 
    
        data/mongoid-letsrate.gemspec
    CHANGED
    
    | @@ -17,4 +17,9 @@ Gem::Specification.new do |s| | |
| 17 17 | 
             
              s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         | 
| 18 18 | 
             
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         | 
| 19 19 | 
             
              s.require_paths = ['lib']
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              s.add_development_dependency 'rspec'
         | 
| 22 | 
            +
              s.add_development_dependency 'mongoid-rspec'
         | 
| 23 | 
            +
              s.add_development_dependency 'mongoid'
         | 
| 24 | 
            +
              s.add_development_dependency 'activesupport'
         | 
| 20 25 | 
             
            end
         | 
| @@ -0,0 +1,60 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'mongoid-letsrate'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe Mongoid::Letsrate do
         | 
| 6 | 
            +
              let(:first_user) { User.create }
         | 
| 7 | 
            +
              let(:second_user) { User.create }
         | 
| 8 | 
            +
              let(:third_user) { User.create }
         | 
| 9 | 
            +
              let(:product) { Product.create }
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              it 'first_user should not be nil' do
         | 
| 12 | 
            +
                first_user.should_not be_nil
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              it 'second_user should not be nil' do
         | 
| 16 | 
            +
                second_user.should_not be_nil
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              it 'third_user should not be nil' do
         | 
| 20 | 
            +
                third_user.should_not be_nil
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
              it 'user should not have any ratings done' do
         | 
| 24 | 
            +
                first_user.ratings_given.count.should eql 0
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              it 'product should not be nil' do
         | 
| 28 | 
            +
                product.should_not be_nil
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              describe 'User with letsrate' do
         | 
| 32 | 
            +
                it 'should have relation to rateable' do
         | 
| 33 | 
            +
                  first_user.should have_many_related :ratings_given
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
                it 'should have relation to rateable' do
         | 
| 36 | 
            +
                  first_user.should belong_to :rateable
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
              end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              describe 'Product gets rated by user' do
         | 
| 41 | 
            +
                it 'rates should be of type Mongoid::Relations::Targets::Enumerable' do
         | 
| 42 | 
            +
                  product.rates.class.should eql Mongoid::Relations::Targets::Enumerable
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                it 'rates should be empty' do
         | 
| 46 | 
            +
                  product.rates.should be_empty
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                it 'rates should be 1 after rating with 5 stars' do
         | 
| 50 | 
            +
                  product.rate 5, first_user
         | 
| 51 | 
            +
                  product.rates.count.should eql 1
         | 
| 52 | 
            +
                end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                it 'rates should be 2 after rating with 5 stars' do
         | 
| 55 | 
            +
                  product.rate 5, first_user
         | 
| 56 | 
            +
                  product.rate 5, second_user
         | 
| 57 | 
            +
                  product.rates.count.should eql 2
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
               end
         | 
| 60 | 
            +
            end
         | 
    
        data/spec/models/rate.rb
    ADDED
    
    
    
        data/spec/models/user.rb
    ADDED
    
    
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,30 @@ | |
| 1 | 
            +
            MODELS = File.join(File.dirname(__FILE__), 'models')
         | 
| 2 | 
            +
            $LOAD_PATH.unshift(MODELS)
         | 
| 3 | 
            +
             | 
| 4 | 
            +
             | 
| 5 | 
            +
            require 'bundler/setup'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Bundler.setup
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            require 'bson'
         | 
| 10 | 
            +
            require 'mongoid'
         | 
| 11 | 
            +
            require 'active_support'
         | 
| 12 | 
            +
            require 'action_view'
         | 
| 13 | 
            +
            require 'mongoid-letsrate'
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            Mongoid.configure do |config|
         | 
| 16 | 
            +
              config.connect_to('mongoid-letsrate-test')
         | 
| 17 | 
            +
            end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            Dir[ File.join(MODELS, '*.rb') ].sort.each { |file| require File.basename(file) }
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            require 'mongoid-rspec'
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            RSpec.configure do |config|
         | 
| 24 | 
            +
              config.include RSpec::Matchers
         | 
| 25 | 
            +
              config.include Mongoid::Matchers
         | 
| 26 | 
            +
              config.mock_with :rspec
         | 
| 27 | 
            +
              config.after :all do
         | 
| 28 | 
            +
                Mongoid::Config.purge!
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: mongoid-letsrate
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1.0. | 
| 4 | 
            +
              version: 1.0.13
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Michael Johann
         | 
| @@ -9,8 +9,64 @@ authors: | |
| 9 9 | 
             
            autorequire:
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2014- | 
| 13 | 
            -
            dependencies: | 
| 12 | 
            +
            date: 2014-03-05 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies:
         | 
| 14 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            +
              name: rspec
         | 
| 16 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 17 | 
            +
                requirements:
         | 
| 18 | 
            +
                - - '>='
         | 
| 19 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 20 | 
            +
                    version: '0'
         | 
| 21 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 22 | 
            +
                requirements:
         | 
| 23 | 
            +
                - - '>='
         | 
| 24 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 25 | 
            +
                    version: '0'
         | 
| 26 | 
            +
              prerelease: false
         | 
| 27 | 
            +
              type: :development
         | 
| 28 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 29 | 
            +
              name: mongoid-rspec
         | 
| 30 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 31 | 
            +
                requirements:
         | 
| 32 | 
            +
                - - '>='
         | 
| 33 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 34 | 
            +
                    version: '0'
         | 
| 35 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 36 | 
            +
                requirements:
         | 
| 37 | 
            +
                - - '>='
         | 
| 38 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 39 | 
            +
                    version: '0'
         | 
| 40 | 
            +
              prerelease: false
         | 
| 41 | 
            +
              type: :development
         | 
| 42 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 43 | 
            +
              name: mongoid
         | 
| 44 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 45 | 
            +
                requirements:
         | 
| 46 | 
            +
                - - '>='
         | 
| 47 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 48 | 
            +
                    version: '0'
         | 
| 49 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 50 | 
            +
                requirements:
         | 
| 51 | 
            +
                - - '>='
         | 
| 52 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 53 | 
            +
                    version: '0'
         | 
| 54 | 
            +
              prerelease: false
         | 
| 55 | 
            +
              type: :development
         | 
| 56 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 57 | 
            +
              name: activesupport
         | 
| 58 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 59 | 
            +
                requirements:
         | 
| 60 | 
            +
                - - '>='
         | 
| 61 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 62 | 
            +
                    version: '0'
         | 
| 63 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 64 | 
            +
                requirements:
         | 
| 65 | 
            +
                - - '>='
         | 
| 66 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 67 | 
            +
                    version: '0'
         | 
| 68 | 
            +
              prerelease: false
         | 
| 69 | 
            +
              type: :development
         | 
| 14 70 | 
             
            description: Provides the best solution to add rating functionality to your models using mongoid.
         | 
| 15 71 | 
             
            email:
         | 
| 16 72 | 
             
            - mjohann@rails-experts.com
         | 
| @@ -39,6 +95,12 @@ files: | |
| 39 95 | 
             
            - lib/mongoid/letsrate/model.rb
         | 
| 40 96 | 
             
            - lib/mongoid/letsrate/version.rb
         | 
| 41 97 | 
             
            - mongoid-letsrate.gemspec
         | 
| 98 | 
            +
            - spec/letsrate_spec.rb
         | 
| 99 | 
            +
            - spec/models/product.rb
         | 
| 100 | 
            +
            - spec/models/rate.rb
         | 
| 101 | 
            +
            - spec/models/rating_cache.rb
         | 
| 102 | 
            +
            - spec/models/user.rb
         | 
| 103 | 
            +
            - spec/spec_helper.rb
         | 
| 42 104 | 
             
            homepage: http://github.com/malagant/mongoid-letsrate
         | 
| 43 105 | 
             
            licenses: []
         | 
| 44 106 | 
             
            metadata: {}
         | 
| @@ -62,4 +124,10 @@ rubygems_version: 2.1.9 | |
| 62 124 | 
             
            signing_key:
         | 
| 63 125 | 
             
            specification_version: 4
         | 
| 64 126 | 
             
            summary: Provides the best solution to add rating functionality to your models using mongoid.
         | 
| 65 | 
            -
            test_files: | 
| 127 | 
            +
            test_files:
         | 
| 128 | 
            +
            - spec/letsrate_spec.rb
         | 
| 129 | 
            +
            - spec/models/product.rb
         | 
| 130 | 
            +
            - spec/models/rate.rb
         | 
| 131 | 
            +
            - spec/models/rating_cache.rb
         | 
| 132 | 
            +
            - spec/models/user.rb
         | 
| 133 | 
            +
            - spec/spec_helper.rb
         |