is_rateable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f5e220972a5694adf478b2053074fc90411e855
4
+ data.tar.gz: e82e8da2e5e0bdd73f8d3d4434c516fb5478dcd9
5
+ SHA512:
6
+ metadata.gz: 0bb6f90f10352c14b70fbed38f54f7d0a1b7f72e5c04fa8f9509ed8e00201999c76b4e9ed9d82ca51fab99a1a676db2c902844610a4938cf0c3826cfed899212
7
+ data.tar.gz: 30f163df295fb93cdc27dd7dd914cbd096eeb2e25584b38bc6bf2283a6bc4bdb41e06911001a8f80460d05c54aa7ffd4fb73c2bcbfdf5ef437bc40473e4b7115
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in is_rateable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Isaac Norman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,131 @@
1
+ # IsRateable
2
+
3
+ ### Easily drop a rating system into your Rails project.
4
+
5
+ IsRateable allows any object to be rateable by any other object with very little setup.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'is_rateable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install is_rateable
22
+
23
+ ## Getting Started
24
+
25
+ ### Adding the ratings table.
26
+
27
+ $ rails generate is_rateable:install
28
+ $ rake db:migrate
29
+
30
+ This will create a migration adding a ratings table for you.
31
+
32
+ **If you are using UUIDS:**
33
+
34
+ $ rails generate is_rateable:install --id_column_type uuid
35
+ $ rake db:migrate
36
+
37
+ This makes sure that the references to your `rater` and `ratee` uses a `:uuid` column rather than an `:integer` one
38
+
39
+ ### Configuring your models
40
+
41
+ For the object that you wish to be rateable:
42
+
43
+ ```ruby
44
+ class MyModel < ActiveRecord::Base
45
+ acts_as_rateable
46
+
47
+ # your code here...
48
+ end
49
+ ```
50
+
51
+ For the object you wish to be able to submit ratings:
52
+
53
+ ```ruby
54
+ class MyModel < ActiveRecord::Base
55
+ acts_as_rater
56
+
57
+ # your code here...
58
+ end
59
+ ```
60
+
61
+ This will give you access to all the methods describe below.
62
+
63
+ ## Usage
64
+
65
+ ### Adding a new rating
66
+
67
+ The `Rating` is created from the object that `acts_as_rateable`. It takes 2 options:
68
+
69
+ * `rater:` The object setting the rating.
70
+ * `score:` The score of the rating.
71
+
72
+ As an example lets say that we have a `Movie` model, that can be rated by `Users`.
73
+
74
+ ```ruby
75
+ @user = User.first
76
+ @movie = Movie.find_by(name: 'Toy Story 2')
77
+
78
+ @movie.add_rating(score: 5, rater: user)
79
+ ```
80
+
81
+ **NOTE:** The `add_rating` method expects the object of the rater to be passed in, not just the `id`.
82
+
83
+ ### Viewing Ratings
84
+
85
+ **All ratings that have been added to the object:**
86
+
87
+ Ratings applied to an object are called `ratee_ratings`
88
+
89
+ ```ruby
90
+ @movie.ratee_ratings
91
+ => ActiveRecord::Association: []
92
+ ```
93
+
94
+ **Average rating of an object:**
95
+
96
+ This shows the average of all ratings applied to an object. Rounded to the nearest `0.1`
97
+
98
+ ```ruby
99
+ @movie.average_rating
100
+ => 4.8
101
+ ```
102
+
103
+ **Has the object been rated at all?:**
104
+
105
+ ```ruby
106
+ @movie.any_ratings?
107
+ => true
108
+ ```
109
+
110
+ ### Viewing Raters
111
+
112
+ **Has the object rated anything?:
113
+
114
+ ```ruby
115
+ @user.rated_any?
116
+ => true
117
+ ```
118
+
119
+ ## Development
120
+
121
+ 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.
122
+
123
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
124
+
125
+ ## Contributing
126
+
127
+ 1. Fork it ( https://github.com/Papercloud/is_rateable/fork )
128
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
129
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
130
+ 4. Push to the branch (`git push origin my-new-feature`)
131
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,12 @@
1
+ module IsRateable
2
+ class Rating < ActiveRecord::Base
3
+ # Associations
4
+ belongs_to :rater, polymorphic: true
5
+ belongs_to :ratee, polymorphic: true
6
+
7
+ # Validations
8
+ validates :rater, presence: true
9
+ validates :ratee, presence: true
10
+ validates :score, presence: true
11
+ end
12
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "is_rateable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'is_rateable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "is_rateable"
8
+ spec.version = IsRateable::VERSION
9
+ spec.authors = ["Isaac Norman"]
10
+ spec.email = ["idn@papercloud.com.au"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ end
14
+
15
+ spec.summary = '5 star ratings for your Rails models'
16
+ spec.description = 'Allow any model to become rateable by any other model in your rails app.'
17
+ spec.homepage = "https://github.com/Papercloud/is_rateable."
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,23 @@
1
+ module IsRateable
2
+ class InstallGenerator < Rails::Generators::Base
3
+ include Rails::Generators::Migration
4
+ class_option :id_column_type, type: :string, aliases: '-id', default: 'integer', desc: 'Column type used for ids. Either integer(default) or uuid'
5
+ desc 'Add the ratings table, allowing models to be rated.'
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def copy_migrations
10
+ migration_template 'migration_ratings.rb', "db/migrate/create_is_rateable_ratings.rb"
11
+ end
12
+
13
+ # Implement the required interface for Rails::Generators::Migration.
14
+ def self.next_migration_number(dirname)
15
+ next_migration_number = current_migration_number(dirname) + 1
16
+ if ActiveRecord::Base.timestamped_migrations
17
+ [Time.now.utc.strftime('%Y%m%d%H%M%S'), '%.14d' % next_migration_number].max
18
+ else
19
+ '%.3d' % next_migration_number
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ class CreateIsRateableRatings < ActiveRecord::Migration
2
+ def change
3
+ create_table :is_rateable_ratings do |t|
4
+ t.<%= options['id_column_type'] -%> :rater_id, nil: false
5
+ t.<%= options['id_column_type'] -%> :ratee_id, nil: false
6
+ t.string :rater_type
7
+ t.string :ratee_type
8
+ t.integer :score, nil: false, default: 0
9
+ t.timestamps
10
+ end
11
+ add_index :is_rateable_ratings, :rater_id
12
+ add_index :is_rateable_ratings, :ratee_id
13
+ add_index :is_rateable_ratings, :score
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ module IsRateable
2
+ module ActsAsRateable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_rateable(options = {})
10
+ include IsRateable::ActsAsRateable::LocalInstanceMethods
11
+
12
+ has_many :ratee_ratings, as: :ratee, class_name: 'IsRateable::Rating'
13
+ end
14
+ end
15
+
16
+ module LocalInstanceMethods
17
+ # Find the Average rating for the ratee.
18
+ # Return 0.0 if they have not been rated yet.
19
+ def average_rating
20
+ any_ratings? ? ((ratee_ratings.average(:score) * 10).round / 10.0) : 0.0
21
+ end
22
+
23
+ # Has this object been rated before?
24
+ def any_ratings?
25
+ ratee_ratings.any?
26
+ end
27
+
28
+ # Easily add a new rating to the object by calling Object.add_rating(score: 5, rater: user)
29
+ # Pass in the entire rater object in, rather than the id.
30
+ def add_rating(options = {})
31
+ Rating.create(score: options[:score], rater: options[:rater], ratee: self)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ module IsRateable
2
+ module ActsAsRater
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_rater(options = {})
10
+ include IsRateable::ActsAsRater::LocalInstanceMethods
11
+
12
+ has_many :rated_ratings, as: :rater, class_name: 'IsRateable::Rating'
13
+ end
14
+ end
15
+
16
+ module LocalInstanceMethods
17
+
18
+ # Has this object rated anything yet?
19
+ def rated_any?
20
+ rated_ratings.any?
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module IsRateable
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace IsRateable
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, :fixture => false
7
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
8
+ g.assets false
9
+ g.helper false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module IsRateable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ require "is_rateable/engine"
2
+ require "is_rateable/version"
3
+
4
+ module IsRateable
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :ActsAsRateable,'is_rateable/acts_as_rateable'
8
+ autoload :ActsAsRater, 'is_rateable/acts_as_rater'
9
+
10
+ # Default way to setup IsRateable:
11
+ def self.setup
12
+ yield self
13
+ end
14
+ end
15
+
16
+ module ActiveRecord
17
+ class Base
18
+ include IsRateable::ActsAsRateable
19
+ include IsRateable::ActsAsRater
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: is_rateable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Isaac Norman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Allow any model to become rateable by any other model in your rails app.
42
+ email:
43
+ - idn@papercloud.com.au
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - app/models/is_rateable/rating.rb
57
+ - bin/console
58
+ - bin/setup
59
+ - is_rateable.gemspec
60
+ - lib/generators/is_rateable/install/install_generator.rb
61
+ - lib/generators/is_rateable/install/templates/migration_ratings.rb
62
+ - lib/is_rateable.rb
63
+ - lib/is_rateable/acts_as_rateable.rb
64
+ - lib/is_rateable/acts_as_rater.rb
65
+ - lib/is_rateable/engine.rb
66
+ - lib/is_rateable/version.rb
67
+ homepage: https://github.com/Papercloud/is_rateable.
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.4.6
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: 5 star ratings for your Rails models
91
+ test_files: []
92
+ has_rdoc: