jqrating 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ Jqrating
2
+ ========
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2010 [name of plugin creator], released under the MIT license
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :test
9
+
10
+ PKG_FILES = FileList[
11
+ '[a-zA-Z]*',
12
+ 'generators/**/*',
13
+ 'lib/**/*',
14
+ 'test/**/*' ]
15
+
16
+ spec = Gem::Specification.new do |s|
17
+ s.name = "jqrating"
18
+ s.version = "0.1"
19
+ s.author = "Balachandar Muruganantham"
20
+ s.email = "mbchandar@gmail.com"
21
+ s.homepage = "http://www.balachandar.net/"
22
+ s.platform = Gem::Platform::RUBY
23
+ s.summary = "Sharing jQuery based rating system - ported from ajax-rating"
24
+ s.files = PKG_FILES.to_a
25
+ s.require_path = "lib"
26
+ s.has_rdoc = false
27
+ s.extra_rdoc_files = ["README"]
28
+ end
29
+
30
+ desc 'Turn this plugin into a gem.'
31
+ Rake::GemPackageTask.new(spec) do |pkg|
32
+ pkg.gem_spec = spec
33
+ end
34
+
35
+ desc 'Test the jqrating plugin.'
36
+ Rake::TestTask.new(:test) do |t|
37
+ t.libs << 'lib'
38
+ t.libs << 'test'
39
+ t.pattern = 'test/**/*_test.rb'
40
+ t.verbose = true
41
+ end
42
+
43
+ desc 'Generate documentation for the jqrating plugin.'
44
+ Rake::RDocTask.new(:rdoc) do |rdoc|
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = 'Jqrating'
47
+ rdoc.options << '--line-numbers' << '--inline-source'
48
+ rdoc.rdoc_files.include('README')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
@@ -0,0 +1,33 @@
1
+ class JqratingGenerator < Rails::Generator::Base
2
+ def manifest
3
+ recorded_session = record do |m|
4
+ m.file "controllers/ratings_controller.rb", "app/controllers/ratings_controller.rb"
5
+ m.file "models/rating.rb", "app/models/rating.rb"
6
+ m.file "stylesheets/rating.css", "public/stylesheets/rating.css"
7
+ m.file "images/star1.png", "public/images/star1.png"
8
+ m.file "images/star2.png", "public/images/star2.png"
9
+ m.file "images/star3.png", "public/images/star3.png"
10
+
11
+ m.migration_template 'migrations/create_ratings.rb', 'db/migrate', :assigns => {
12
+ :migration_name => "CreateRatings"
13
+ }, :migration_file_name => "create_ratings"
14
+
15
+ m.route_resources 'ratings'
16
+ end
17
+
18
+ action = nil
19
+ action = $0.split("/")[1]
20
+ case action
21
+ when "generate"
22
+ puts
23
+ puts ("-" * 70)
24
+ puts "Thanks for using JqRating, Bug report to mbchandar@gmail.com"
25
+ puts ("-" * 70)
26
+ puts
27
+ else
28
+ puts
29
+ end
30
+
31
+ recorded_session
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ class RatingsController < ApplicationController
2
+ def index
3
+ @rater = respond_to?(:current_rater) ? current_rater : params[:rating][:rater_type].constantize.find_by_id(params[:rating][:rater_id])
4
+ @rating = @rater.rate(params[:rating])
5
+ respond_to do |format|
6
+ if (@rating.new_record? ? @rating.save : @rating.update_attribute(:score, params[:rating][:score]))
7
+ format.js { render :inline => "#{20 * @rating.score}"}
8
+ else
9
+ format.js { render :inline => "null"}
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,2 @@
1
+ module RatingsHelper
2
+ end
@@ -0,0 +1,17 @@
1
+ class CreateRatings < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :ratings do |t|
4
+ t.integer :rater_id
5
+ t.string :rater_type
6
+ t.integer :ratable_id
7
+ t.string :ratable_type
8
+ t.integer :score
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :ratings
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ class Rating < ActiveRecord::Base
2
+ validates_presence_of :rater_id, :rater_type, :ratable_type, :ratable_id, :score
3
+ validates_numericality_of :score, :greater_than => 0, :less_than => 6
4
+ validates_uniqueness_of :rater_id, :scope => [:rater_type, :ratable_type, :ratable_id]
5
+
6
+ belongs_to :rater, :polymorphic => true
7
+ belongs_to :ratable, :polymorphic => true
8
+ end
@@ -0,0 +1,56 @@
1
+ .star-rating {
2
+ list-style:none;
3
+ margin: 0 25px 10px 21px;
4
+ padding: 0px;
5
+ width: 100px;
6
+ height: 20px;
7
+ position: relative;
8
+ background: url(/images/star1.png) top left repeat-x;
9
+ }
10
+
11
+ .star-rating li {
12
+ padding:0px;
13
+ margin:0px;
14
+ float: left;
15
+ }
16
+
17
+ .star-rating li a {
18
+ display: block;
19
+ width: 20px;
20
+ height: 20px;
21
+ text-decoration: none;
22
+ text-indent: -9000px;
23
+ z-index: 20;
24
+ position: absolute;
25
+ padding: 0px;
26
+ float: left;
27
+ }
28
+
29
+ .star-rating li a:hover {
30
+ background: url(/images/star2.png) left center;
31
+ border :0px;
32
+ z-index: 2;
33
+ left: 0px;
34
+ }
35
+
36
+ .star-rating a.one-star{left: 0px;}
37
+ .star-rating a.one-star:hover{width:20px;}
38
+ .star-rating a.two-stars{left:20px;}
39
+ .star-rating a.two-stars:hover{width: 40px;}
40
+ .star-rating a.three-stars{left: 40px;}
41
+ .star-rating a.three-stars:hover{width: 60px;}
42
+ .star-rating a.four-stars{left: 60px;}
43
+ .star-rating a.four-stars:hover{width: 80px;}
44
+ .star-rating a.five-stars{left: 80px;}
45
+ .star-rating a.five-stars:hover{width: 100px;}
46
+
47
+ .star-rating li.current-rating {
48
+ background: url(/images/star3.png) left bottom;
49
+ position: absolute;
50
+ height: 20px;
51
+ width: 60px;
52
+ display: block;
53
+ text-indent: -9000px;
54
+ z-index: 1;
55
+ float: left;
56
+ }
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'jqrating'
2
+ ActionView::Base.send(:include, Jqrating::RatingHelper)
3
+ ActiveRecord::Base.send(:include, Jqrating::ActiveRecord)
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,77 @@
1
+ module Jqrating
2
+ module ActiveRecord
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def acts_as_ratable
9
+ has_many :ratings, :as => 'ratable'
10
+ include Jqrating::Ratable
11
+ end
12
+
13
+ def acts_as_rater
14
+ has_many :ratings, :as => 'rater'
15
+ include Jqrating::Rater
16
+ end
17
+ end
18
+ end
19
+
20
+ module Ratable
21
+ def rating
22
+ @rating ||= ratings.count.zero? ? 0 : ratings.sum('score') / ratings.count
23
+ end
24
+
25
+ def rating_count
26
+ ratings.count
27
+ end
28
+ end
29
+
30
+ module Rater
31
+ def rate(rating)
32
+ ratings.find_by_ratable_id_and_ratable_type(rating[:ratable_id], rating[:ratable_type]) ||
33
+ ratings.build(rating)
34
+ end
35
+
36
+ def rating_on(ratable)
37
+ rating = ratings.find_by_ratable_id_and_ratable_type(rating[:ratable_id], rating[:ratable_type])
38
+ rating.nil? ? 0 : rating.score
39
+ end
40
+ end
41
+
42
+ module RatingHelper
43
+ def rating_link(title, score, style, rater, ratable, update_tag)
44
+ if rater
45
+ # link_to_remote(score, :update => update_tag,
46
+ # :url => ratings_path(:rating => {:ratable_type => ratable.class.to_s,
47
+ # :ratable_id => ratable.id,
48
+ # :score => score},
49
+ # :rater_id => rater.id,
50
+ # :rater_type => rater.class.to_s),
51
+ # :method => 'post',
52
+ # :html => { :class => style, :title => title})
53
+
54
+ link_to score, ratings_path(:rating => {:ratable_type => ratable.class.to_s,
55
+ :ratable_id => ratable.id,
56
+ :score => score,:rater_id => rater.id,:rater_type => rater.class.to_s}), :class => style, :title => title
57
+ else
58
+ link_to score, '', :class => style
59
+ end
60
+ end
61
+
62
+ def rating_tag(rater, ratable)
63
+ update_tag = "rating#{ratable.id}"
64
+
65
+ content_tag(:div, :id => update_tag, :class => 'rating') do
66
+ content_tag(:ul, :class => "star-rating") do
67
+ content_tag(:li, '', :class => "current-rating", :style => "width:#{20 * ratable.rating}px;") +
68
+ content_tag(:li, rating_link('1 star', '1', 'rate one-star', rater, ratable, update_tag)) +
69
+ content_tag(:li, rating_link('2 stars', '2', 'rate two-stars', rater, ratable, update_tag)) +
70
+ content_tag(:li, rating_link('3 stars', '3', 'rate three-stars', rater, ratable, update_tag)) +
71
+ content_tag(:li, rating_link('4 stars', '4', 'rate four-stars', rater, ratable, update_tag)) +
72
+ content_tag(:li, rating_link('5 stars', '5', "rate five-stars", rater, ratable, update_tag))
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :jqrating do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class JqratingTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jqrating
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Balachandar Muruganantham
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-02 00:00:00 +05:30
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: mbchandar@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ files:
30
+ - MIT-LICENSE
31
+ - README
32
+ - install.rb
33
+ - uninstall.rb
34
+ - Rakefile
35
+ - init.rb
36
+ - generators/jqrating/jqrating_generator.rb
37
+ - generators/jqrating/templates/helpers/ratings_helper.rb
38
+ - generators/jqrating/templates/models/rating.rb
39
+ - generators/jqrating/templates/controllers/ratings_controller.rb
40
+ - generators/jqrating/templates/stylesheets/rating.css
41
+ - generators/jqrating/templates/migrations/create_ratings.rb
42
+ - generators/jqrating/templates/images/star3.png
43
+ - generators/jqrating/templates/images/star1.png
44
+ - generators/jqrating/templates/images/star2.png
45
+ - lib/jqrating.rb
46
+ - lib/tasks/jqrating.rake
47
+ - test/test_helper.rb
48
+ - test/jqrating_test.rb
49
+ has_rdoc: true
50
+ homepage: http://www.balachandar.net/
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.7
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Sharing jQuery based rating system - ported from ajax-rating
83
+ test_files: []
84
+