is_rateable 0.1.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5cacf75857fb169b5a451217c907216a376c22b9
4
- data.tar.gz: ed563dfbe246777073cb28c79c62e468021f31a9
3
+ metadata.gz: 4f3bcffc749baa5893e0114cff2181930112b5b9
4
+ data.tar.gz: 561b773c80ef4fd89724ab48930c798f971761a8
5
5
  SHA512:
6
- metadata.gz: 0707cd528f170a9d8d44afe01e1f18a39bf912ebc534851b7676d200b451594d41b0c565a56f5106543fd377c23075cf54fbd8266436c81ccfcda745ea2dfd84
7
- data.tar.gz: 8fd01f8552e7186d08240e563ca9b405ea1db4456903538eb1181a66c1abf19698385a53a4206b77b85117f5df02cab6006bc0952f27e1bc6664723ec235223d
6
+ metadata.gz: bc052d062be289510a13d0185d0eabd40caae01fa1f7682033719e87d75c80b926475403733bc5bb148dd1590d90ab26105cd8ec0e31fe3f5820be515c4fc789
7
+ data.tar.gz: 892be645487d653acd682347893e686249e7d12a0b677c11c4d20091288e3255d4bb0e8d74b8b3226fbfe7d68be57247bc944958d84b15b58ea8c1be67ef914d
data/.gitignore CHANGED
@@ -7,3 +7,8 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ spec/dummy/db/*.sqlite3
11
+ spec/dummy/db/*.sqlite3-journal
12
+ spec/dummy/log/*.log
13
+ spec/dummy/tmp/
14
+ spec/dummy/.sass-cache
data/README.md CHANGED
@@ -22,13 +22,11 @@ Or install it yourself as:
22
22
 
23
23
  ## Getting Started
24
24
 
25
- ### Adding the ratings table.
25
+ ### Adding the ratings table and initializer.
26
26
 
27
27
  $ rails generate is_rateable:install
28
28
  $ rake db:migrate
29
29
 
30
- This will create a migration adding a ratings table for you.
31
-
32
30
  **If you are using UUIDS:**
33
31
 
34
32
  $ rails generate is_rateable:install --id_column_type uuid
@@ -36,6 +34,14 @@ This will create a migration adding a ratings table for you.
36
34
 
37
35
  This makes sure that the references to your `rater` and `ratee` uses a `:uuid` column rather than an `:integer` one
38
36
 
37
+ ### Configuring the initializer
38
+
39
+ in `config/initializers/is_rateable.rb`:
40
+
41
+ `minimum_ratings_for_average` sets the amount of ratings an object must have before the average is calculated. This stops the ratings swaying wildly when an object is new.
42
+
43
+ `default_rating` sets the rating that is returned if the object does not have enough ratings for the average, and you ask for it's `average_rating`
44
+
39
45
  ### Configuring your models
40
46
 
41
47
  For the object that you wish to be rateable:
@@ -118,13 +124,22 @@ This shows the average of all ratings applied to an object. Rounded to the neare
118
124
 
119
125
  **All Ratings that an object as given to another object:**
120
126
 
121
- Ratings that an object has applied to another object are stored as `rater_ratings`.
127
+ Ratings that an object has applied to another object are stored as `rated_ratings`.
122
128
 
123
129
  ```ruby
124
- @user.rater_ratings
130
+ @user.rated_ratings
125
131
  => ActiveRecord::Association: []
126
132
  ```
127
133
 
134
+ **Rating that a rater has given to a particular object:**
135
+
136
+ If the object has rated something more than once, then it will return the average rating they have provided for that object (eg. Uber if you have had the same driver multiple times).
137
+
138
+ ```ruby
139
+ @user.rating_for(@movie)
140
+ => 5.0
141
+ ```
142
+
128
143
  ## Development
129
144
 
130
145
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -1,2 +1,21 @@
1
- require "bundler/gem_tasks"
1
+ begin
2
+ require 'bundler/setup'
3
+ require "bundler/gem_tasks"
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
2
7
 
8
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
9
+ load 'rails/tasks/engine.rake'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each { |f| load f }
14
+
15
+ require 'rspec/core'
16
+ require 'rspec/core/rake_task'
17
+
18
+ desc 'Run all specs in spec directory (excluding plugin specs)'
19
+ RSpec::Core::RakeTask.new(spec: 'app:db:test:prepare')
20
+
21
+ task default: :spec
data/bin/console CHANGED
File without changes
data/bin/rails ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/is_rateable/engine', __FILE__)
6
+
7
+ # Set up gems listed in the Gemfile.
8
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
9
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
10
+
11
+ require 'rails/all'
12
+ require 'rails/engine/commands'
data/bin/setup CHANGED
File without changes
data/is_rateable.gemspec CHANGED
@@ -2,6 +2,7 @@
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'is_rateable/version'
5
+ require 'rails/all'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
8
  spec.name = "is_rateable"
@@ -26,4 +27,8 @@ Gem::Specification.new do |spec|
26
27
 
27
28
  spec.add_development_dependency "bundler", "~> 1.8"
28
29
  spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency 'sqlite3'
31
+ spec.add_development_dependency 'rspec-rails', '~> 3.0'
32
+ spec.add_development_dependency 'factory_girl_rails'
33
+ spec.add_development_dependency 'pry'
29
34
  end
@@ -6,6 +6,10 @@ module IsRateable
6
6
 
7
7
  source_root File.expand_path('../templates', __FILE__)
8
8
 
9
+ def create_initializer
10
+ template 'initializer.rb', 'config/initializers/is_rateable.rb'
11
+ end
12
+
9
13
  def copy_migrations
10
14
  migration_template 'migration_ratings.rb', "db/migrate/create_is_rateable_ratings.rb"
11
15
  end
@@ -0,0 +1,6 @@
1
+ IsRateable.setup do |config|
2
+ # Return the default raitng for ratee, when they have not received any ratings
3
+ config.default_rating = 5.0
4
+ # Limit the amount of ratings an object must have received before it returns the average
5
+ config.minimum_ratings_for_average = 0
6
+ end
@@ -10,6 +10,7 @@ module IsRateable
10
10
  include IsRateable::ActsAsRateable::LocalInstanceMethods
11
11
 
12
12
  has_many :ratee_ratings, as: :ratee, class_name: 'IsRateable::Rating'
13
+
13
14
  end
14
15
  end
15
16
 
@@ -17,6 +18,7 @@ module IsRateable
17
18
  # Find the Average rating for the ratee.
18
19
  # Return 0.0 if they have not been rated yet.
19
20
  def average_rating
21
+ return IsRateable.default_rating unless enough_ratings_for_average?
20
22
  any_ratings? ? ((ratee_ratings.average(:score) * 10).round / 10.0) : 0.0
21
23
  end
22
24
 
@@ -25,6 +27,11 @@ module IsRateable
25
27
  ratee_ratings.any?
26
28
  end
27
29
 
30
+ # Has the object received enough ratings to show the average?
31
+ def enough_ratings_for_average?
32
+ any_ratings? && ratee_ratings.length > IsRateable.minimum_ratings_for_average
33
+ end
34
+
28
35
  # Easily add a new rating to the object by calling Object.add_rating(score: 5, rater: user)
29
36
  # Pass in the entire rater object in, rather than the id.
30
37
  def add_rating(options = {})
@@ -19,6 +19,12 @@ module IsRateable
19
19
  def rated_any?
20
20
  rated_ratings.any?
21
21
  end
22
+
23
+ # Return the objects rating for another object
24
+ def rating_for(ratee)
25
+ return nil unless rated_ratings.where(ratee: ratee).first
26
+ ((rated_ratings.where(ratee: ratee).average(:score) * 10).round / 10.0)
27
+ end
22
28
  end
23
29
  end
24
30
  end
@@ -1,3 +1,3 @@
1
1
  module IsRateable
2
- VERSION = "0.1.3"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/is_rateable.rb CHANGED
@@ -7,6 +7,14 @@ module IsRateable
7
7
  autoload :ActsAsRateable,'is_rateable/acts_as_rateable'
8
8
  autoload :ActsAsRater, 'is_rateable/acts_as_rater'
9
9
 
10
+ # Method retrieve the default rating:
11
+ mattr_accessor :default_rating
12
+ @@default_rating = nil
13
+
14
+ # Method to retrieve minimum ratings before we show the average:
15
+ mattr_accessor :minimum_ratings_for_average
16
+ @@minimum_ratings_for_average = nil
17
+
10
18
  # Default way to setup IsRateable:
11
19
  def self.setup
12
20
  yield self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: is_rateable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Isaac Norman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-26 00:00:00.000000000 Z
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -58,6 +58,62 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: sqlite3
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec-rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: factory_girl_rails
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: pry
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
61
117
  description: Allow any model to become rateable by any other model in your rails app.
62
118
  email:
63
119
  - idn@papercloud.com.au
@@ -75,9 +131,11 @@ files:
75
131
  - Rakefile
76
132
  - app/models/is_rateable/rating.rb
77
133
  - bin/console
134
+ - bin/rails
78
135
  - bin/setup
79
136
  - is_rateable.gemspec
80
137
  - lib/generators/is_rateable/install/install_generator.rb
138
+ - lib/generators/is_rateable/install/templates/initializer.rb
81
139
  - lib/generators/is_rateable/install/templates/migration_ratings.rb
82
140
  - lib/is_rateable.rb
83
141
  - lib/is_rateable/acts_as_rateable.rb
@@ -109,4 +167,3 @@ signing_key:
109
167
  specification_version: 4
110
168
  summary: 5 star ratings for your Rails models
111
169
  test_files: []
112
- has_rdoc: