spleen 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.markdown +11 -0
- data/config/locales/en.yml +3 -0
- data/lib/generators/spleen/install_generator.rb +39 -0
- data/lib/generators/spleen/templates/README +22 -0
- data/lib/generators/spleen/templates/migration.rb +20 -0
- data/lib/generators/spleen/templates/model.rb +13 -0
- data/lib/spleen.rb +75 -0
- data/spleen.gemspec +16 -0
- metadata +54 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2011 Laurent Arnoud <laurent@spkdev.net>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module Spleen
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
source_root File.expand_path("../templates", __FILE__)
|
9
|
+
|
10
|
+
desc "Copy model and locale files to your application and generate migration."
|
11
|
+
class_option :orm
|
12
|
+
|
13
|
+
def copy_locale
|
14
|
+
copy_file "../../../../config/locales/en.yml", "config/locales/ratings.en.yml"
|
15
|
+
end
|
16
|
+
|
17
|
+
def copy_model
|
18
|
+
template "model.rb", "app/models/rating.rb"
|
19
|
+
end
|
20
|
+
|
21
|
+
def copy_rating_migration
|
22
|
+
migration_template "migration.rb", "db/migrate/create_ratings"
|
23
|
+
end
|
24
|
+
|
25
|
+
def show_readme
|
26
|
+
readme "README" if behavior == :invoke
|
27
|
+
end
|
28
|
+
|
29
|
+
# Taken from ActiveRecord's migration generator
|
30
|
+
def self.next_migration_number(dirname) #:nodoc:
|
31
|
+
if ActiveRecord::Base.timestamped_migrations
|
32
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
33
|
+
else
|
34
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
===============================================================================
|
2
|
+
|
3
|
+
bundle exec rake db:migrate
|
4
|
+
|
5
|
+
In your ratable models:
|
6
|
+
|
7
|
+
extend Spleen
|
8
|
+
spleen(:dependent => :destroy)
|
9
|
+
|
10
|
+
Add your Rateable types in app/models/ratings.rb:
|
11
|
+
RATEABLE_TYPES = %w{SomeRateableClass}
|
12
|
+
|
13
|
+
By default the ratetor is User, you can add some or edit in the same file.
|
14
|
+
|
15
|
+
Examples:
|
16
|
+
|
17
|
+
a = Article.create(:title => 'test')
|
18
|
+
a.rate(1, User.last)
|
19
|
+
a.ratings # => [#<Rating id: 1, ...]
|
20
|
+
a.average_rate # => 1.0
|
21
|
+
a.sum_rate # => 1
|
22
|
+
a.count_rate # => 1
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
class CreateRatings < ActiveRecord::Migration
|
3
|
+
def self.up
|
4
|
+
create_table(:ratings) do |t|
|
5
|
+
t.column :ratetor_id, :integer, :null => false
|
6
|
+
t.column :ratetor_type, :string, :null => false
|
7
|
+
t.column :rate, :float, :null => false
|
8
|
+
t.column :rateable_id, :integer, :null => false
|
9
|
+
t.column :rateable_type, :string
|
10
|
+
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index :ratings, [:rateable_id, :rateable_type]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :ratings
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
class Rating < ActiveRecord::Base
|
3
|
+
RATEABLE_TYPES = %w{}
|
4
|
+
RATETOR_TYPES = %w{User}
|
5
|
+
belongs_to :rateable, :polymorphic => true
|
6
|
+
belongs_to :ratetor, :polymorphic => true
|
7
|
+
|
8
|
+
validates_presence_of :rateable, :ratetor
|
9
|
+
validates_inclusion_of :rateable_type, :in => RATEABLE_TYPES
|
10
|
+
validates_inclusion_of :ratetor_type, :in => RATETOR_TYPES
|
11
|
+
validates_uniqueness_of :ratetor_id, :scope => [:rateable_id, :rateable_type],
|
12
|
+
:message => proc {I18n.t('rateable.already_rated')}
|
13
|
+
end
|
data/lib/spleen.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module Spleen
|
5
|
+
def self.extended(model_class)
|
6
|
+
model_class.instance_eval do
|
7
|
+
extend Base
|
8
|
+
end
|
9
|
+
|
10
|
+
model_class.class_eval do
|
11
|
+
##
|
12
|
+
# @param [Float] value
|
13
|
+
# @param [ActiveRecord::Base] ratetor
|
14
|
+
# @return [Boolean] TrueClass is rate saved else FalseClass
|
15
|
+
#
|
16
|
+
def rate(value, ratetor)
|
17
|
+
r = Rating.new(
|
18
|
+
:ratetor => ratetor,
|
19
|
+
:rateable_type => ratetor.class.to_s,
|
20
|
+
:rate => value,
|
21
|
+
:rateable => self,
|
22
|
+
:rateable_type => self.class.to_s,
|
23
|
+
:rateable_id => self.id
|
24
|
+
)
|
25
|
+
if r.valid?
|
26
|
+
r.save
|
27
|
+
else
|
28
|
+
r.errors.full_messages.each do |e|
|
29
|
+
self.errors.add(:base, e)
|
30
|
+
end
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Helper method that returns the average rating
|
37
|
+
#
|
38
|
+
def average_rate
|
39
|
+
Rating.calculate(:average, :rate,
|
40
|
+
:conditions => {
|
41
|
+
:rateable_type => self.class.to_s,
|
42
|
+
:rateable_id => self.id
|
43
|
+
}).to_f
|
44
|
+
end
|
45
|
+
##
|
46
|
+
# Helper method that returns the sum rate
|
47
|
+
#
|
48
|
+
def sum_rate
|
49
|
+
Rating.calculate(:sum, :rate,
|
50
|
+
:conditions => {
|
51
|
+
:rateable_type => self.class.to_s,
|
52
|
+
:rateable_id => self.id
|
53
|
+
}).to_i
|
54
|
+
end
|
55
|
+
##
|
56
|
+
# Helper method that returns the count rate
|
57
|
+
#
|
58
|
+
def count_rate
|
59
|
+
Rating.calculate(:count, :rate,
|
60
|
+
:conditions => {
|
61
|
+
:rateable_type => self.class.to_s,
|
62
|
+
:rateable_id => self.id
|
63
|
+
}).to_i
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
module Base
|
69
|
+
def spleen(opts={})
|
70
|
+
options = {:as => :rateable}
|
71
|
+
options.merge!(opts)
|
72
|
+
has_many :ratings, options
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spleen.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "spleen"
|
5
|
+
s.version = '0.0.1'
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.summary = "Ratings solution for Rails"
|
8
|
+
s.email = "laurent@spkdev.net"
|
9
|
+
s.homepage = "http://github.com/spk/spleen"
|
10
|
+
s.description = "Ratings model solution for Rails"
|
11
|
+
s.authors = ['Laurent Arnoud']
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
#s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spleen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Laurent Arnoud
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-07 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Ratings model solution for Rails
|
15
|
+
email: laurent@spkdev.net
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- README.markdown
|
22
|
+
- config/locales/en.yml
|
23
|
+
- lib/generators/spleen/install_generator.rb
|
24
|
+
- lib/generators/spleen/templates/README
|
25
|
+
- lib/generators/spleen/templates/migration.rb
|
26
|
+
- lib/generators/spleen/templates/model.rb
|
27
|
+
- lib/spleen.rb
|
28
|
+
- spleen.gemspec
|
29
|
+
homepage: http://github.com/spk/spleen
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.7.2
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Ratings solution for Rails
|
53
|
+
test_files: []
|
54
|
+
has_rdoc:
|