Permalink
Browse files

first init

  • Loading branch information...
0 parents commit eb1ba56b4236634b8f4bf6094a680db96758cacc @muratguzel committed Aug 28, 2012
BIN .DS_Store
Binary file not shown.
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+gem 'jquery-rails'
+
+# Specify your gem's dependencies in ka-rate.gemspec
+gemspec
@@ -0,0 +1,64 @@
+PATH
+ remote: .
+ specs:
+ rateme (1.0.3)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionpack (3.2.2)
+ activemodel (= 3.2.2)
+ activesupport (= 3.2.2)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.1)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.2)
+ activesupport (= 3.2.2)
+ builder (~> 3.0.0)
+ activesupport (3.2.2)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ builder (3.0.0)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ journey (1.0.3)
+ jquery-rails (2.0.1)
+ railties (< 5.0, >= 3.2.0)
+ thor (~> 0.14)
+ json (1.6.5)
+ multi_json (1.1.0)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ railties (3.2.2)
+ actionpack (= 3.2.2)
+ activesupport (= 3.2.2)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ sprockets (2.1.2)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (!= 1.3.0, ~> 1.1)
+ thor (0.14.6)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ jquery-rails
+ rateme!
@@ -0,0 +1,89 @@
+# Rateit Rating Gem
+
+Provides the best way to add rating capabilites to your Rails application with jQuery Raty plugin.
+
+## Repository
+
+Find it at [github.com/muratguzel/rateit](github.com/muratguzel/rateit)
+
+## Instructions
+
+### Install
+
+You can add the rateit gem into your Gemfile
+
+ gem 'rateit'
+
+### Generate
+
+ rails g rateit User
+
+The generator takes one argument which is the name of your existing UserModelName. This is necessary to bind the user and rating datas.
+Also the generator copies necessary files (jquery raty plugin files, star icons and javascripts)
+
+Example:
+
+Suppose you will have a devise user model which name is User. The generator should be like below
+
+ rails g devise:install
+ rails g devise user
+ rails g rateit user # => This is rateit generator.
+
+This generator will create Rate and RatingCache models and link to your user model.
+
+### Prepare
+
+I suppose you have a car model
+
+ rails g model car name:string
+
+You should add the rateit_rateable function with its dimension option.
+
+ class Car < ActiveRecord::Base
+ rateit_rateable :dimensions => [:speed, :engine, :price]
+ end
+
+Then you need to add a call rateit_rater in the user model.
+
+ class User < ActiveRecord::Base
+ ratme_rater
+ end
+
+
+### Using
+
+There is a helper method which name is rating_for to add the star links. By default rating_for will display the average rating and accept the
+new rating value from authenticated user.
+
+ #show.html.erb -> /cars/1
+
+ Speed : <%= rating_for @car, "speed" %>
+ Engine : <%= rating_for @car, "engine" %>
+ Price : <%= rating_for @car, "price" %>
+
+### Important
+
+By default rating_for tries to call current_user method as the rater instance in the rater_controller.rb file. You can change the current_user method
+as you will.
+
+ #rater_controller.rb
+
+ def create
+ if current_user.present?
+ obj = eval "#{params[:klass]}.find(#{params[:id]})"
+ if params[:dimension].present?
+ obj.rate params[:score].to_i, current_user.id, "#{params[:dimension]}"
+ else
+ obj.rate params[:score].to_i, current_user.id
+ end
+
+ render :json => true
+ else
+ render :json => false
+ end
+ end
+
+## Feedback
+If you find bugs please open a ticket at [github.com/muratguzel/rateit/issues](github.com/muratguzel/rateit/issues)
+
+
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
Binary file not shown.
No changes.
@@ -0,0 +1,49 @@
+require 'rails/generators/migration'
+class RateitGenerator < Rails::Generators::NamedBase
+ include Rails::Generators::Migration
+
+ source_root File.expand_path('../templates', __FILE__)
+
+ desc "copying jquery.raty files to assets directory ..."
+ def copying
+ copy_file 'jquery.raty.min.js', 'app/assets/javascripts/jquery.raty.min.js'
+ copy_file 'star-on.png', 'app/assets/images/star-on.png'
+ copy_file 'star-off.png', 'app/assets/images/star-off.png'
+ copy_file 'star-half.png', 'app/assets/images/star-half.png'
+ copy_file 'rateit.js', 'app/assets/javascripts/rateit.js.erb'
+ copy_file 'rater_controller.rb', 'app/controllers/rater_controller.rb'
+ end
+
+ desc "model is creating..."
+ def create_model
+ model_file = File.join('app/models', "#{file_path}.rb")
+ raise "User model (#{model_file}) must exits." unless File.exists?(model_file)
+ class_collisions 'Rate'
+ template 'model.rb', File.join('app/models', "rate.rb")
+ template 'cache_model.rb', File.join('app/models', "rating_cache.rb")
+ end
+
+ def add_rate_path_to_route
+ route "match '/rate' => 'rater#create', :as => 'rate'"
+ end
+
+ desc "cacheable rating average migration is creating ..."
+ def create_cacheable_migration
+ migration_template "cache_migration.rb", "db/migrate/create_rating_caches.rb"
+ end
+
+ desc "migration is creating ..."
+ def create_migration
+ migration_template "migration.rb", "db/migrate/create_rates.rb"
+ end
+
+
+ private
+ def self.next_migration_number(dirname)
+ if ActiveRecord::Base.timestamped_migrations
+ Time.now.utc.strftime("%Y%m%d%H%M%S%L")
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
+ end
+ end
+end
@@ -0,0 +1,19 @@
+class CreateRatingCaches < ActiveRecord::Migration
+
+ def self.up
+ create_table :rating_caches do |t|
+ t.belongs_to :cacheable, :polymorphic => true
+ t.float :avg, :null => false
+ t.integer :qty, :null => false
+ t.string :dimension
+ t.timestamps
+ end
+
+ add_index :rating_caches, [:cacheable_id, :cacheable_type]
+ end
+
+ def self.down
+ drop_table :rating_caches
+ end
+
+end
@@ -0,0 +1,3 @@
+class RatingCache < ActiveRecord::Base
+ belongs_to :cacheable, :polymorphic => true
+end

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,20 @@
+class CreateRates < ActiveRecord::Migration
+
+ def self.up
+ create_table :rates do |t|
+ t.belongs_to :rater
+ t.belongs_to :rateable, :polymorphic => true
+ t.float :stars, :null => false
+ t.string :dimension
+ t.timestamps
+ end
+
+ add_index :rates, :rater_id
+ add_index :rates, [:rateable_id, :rateable_type]
+ end
+
+ def self.down
+ drop_table :rates
+ end
+
+end
@@ -0,0 +1,7 @@
+class Rate < ActiveRecord::Base
+ belongs_to :rater, :class_name => "<%= file_name.classify %>"
+ belongs_to :rateable, :polymorphic => true
+
+ attr_accessible :rate, :dimension
+
+end
@@ -0,0 +1,24 @@
+$.fn.raty.defaults.path = "/assets";
+$.fn.raty.defaults.half_show = true;
+
+$(function(){
+ $(".star").raty({
+ start: function(){
+ return $(this).attr('data-rating')
+ },
+ click: function(score, evt) {
+ $.post('<%= Rails.application.class.routes.url_helpers.rate_path %>',
+ {
+ score: score,
+ dimension: $(this).attr('data-dimension'),
+ id: $(this).attr('data-id'),
+ klass: $(this).attr('data-classname')
+ },
+ function(data) {
+ if(data) {
+ // success code goes here ...
+ }
+ });
+ }
+ });
+});
@@ -0,0 +1,19 @@
+class RaterController < ApplicationController
+
+ def create
+ if current_user.present?
+ obj = eval "#{params[:klass]}.find(#{params[:id]})"
+ if params[:dimension].present?
+ obj.rate params[:score].to_i, current_user.id, "#{params[:dimension]}"
+ else
+ obj.rate params[:score].to_i, current_user.id
+ end
+
+ render :json => true
+ else
+ render :json => false
+ end
+ end
+
+
+end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,7 @@
+require "rateit/version"
+require "rateit/model"
+require "rateit/helpers"
+
+module Rateit
+
+end
@@ -0,0 +1,24 @@
+module Helpers
+ def rating_for(rateable_obj, dimension=nil, options={})
+
+ if dimension.nil?
+ klass = rateable_obj.average
+ else
+ klass = rateable_obj.average "#{dimension}"
+ end
+
+ if klass.nil?
+ avg = 0
+ else
+ avg = klass.avg
+ end
+
+ content_tag :div, "", "data-dimension" => dimension, :class => "star", "data-rating" => avg,
+ "data-id" => rateable_obj.id, "data-classname" => rateable_obj.class.name
+ end
+
+end
+
+class ActionView::Base
+ include Helpers
+end
Oops, something went wrong.

0 comments on commit eb1ba56

Please sign in to comment.