diff --git a/.gitignore b/.gitignore index cc32a34..128f3eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ pkg doc .DS_Store -ajaxful_rating*.gem +*.gem diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..b97d166 --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm 1.9.2@ajaxful_rating diff --git a/CHANGELOG b/CHANGELOG index f0ec137..dcf9730 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +== 3.0.0.beta2 August 8, 2010 +* Fix dom_class and tap methods in rails.rc + == 2.2.9 Sept 30, 2010 * Released as a separate ajaxful_rating_jquery gem. @@ -51,4 +54,4 @@ * Added option for showing current_user's rate instead of global average == 2.1.0 July 25, 2009 -* AjaxfulRating is now available as a gem \ No newline at end of file +* AjaxfulRating is now available as a gem diff --git a/README.textile b/README.textile index 65de561..53e5508 100644 --- a/README.textile +++ b/README.textile @@ -1,12 +1,14 @@ h1. Ajaxful Rating jQuery -Provides a simple way to add rating functionality to your application. This fork uses jQuery instead of prototype and uses unobtrusive javascript. Currently, it only supports Rails 2.3.x. +Provides a simple way to add rating functionality to your application. This fork uses jQuery instead of prototype and uses unobtrusive javascript. + +This fork is a merge of "kamui/ajaxful_rating_jquery":http://github.com/kamui/ajaxful_rating_jquery and the rails3 branch of "edgarjs/ajaxful-rating":http://github.com/edgarjs/ajaxful-rating and works on Rails 3. I've taken some care to update the documentation here but there's probably things that are missing or incorrect. !http://s3.amazonaws.com/ember/v9SvvO3rdSkA40e1sXBf8JgpuaWCC0uB_o.png! h2. Repository -Find it at "http://github.com/kamui/ajaxful_rating_jquery":http://github.com/kamui/ajaxful_rating_jquery +Find it at "http://github.com/danbee/ajaxful_rating_jquery":http://github.com/danbee/ajaxful_rating_jquery The original repository, which uses Prototype, is located at "http://github.com/edgarjs/ajaxful-rating":http://github.com/edgarjs/ajaxful-rating @@ -23,17 +25,13 @@ h2. Instructions h3. Install -To install the gem run the next command: - - @gem install ajaxful_rating_jquery@ - -You can configure it in your environment.rb file also: +To install the gem add it to your Gemfile: - @config.gem "ajaxful_rating_jquery"@ +
gem 'ajaxful_rating_jquery', :git => 'git@github.com:danbee/ajaxful_rating_jquery.git'
h3. Generate - @script/generate ajaxful_rating UserModelName@ + @rails g ajaxful_rating UserModelName@ The generator takes one argument: UserModelName, which is the name of your *current* user model. This is necessary to link both the rate and user models. @@ -44,8 +42,8 @@ Example: _I suppose you have generated already an authenticated model..._
-script/generate authenticated user sessions
-script/generate ajaxful_rating user
+rails g  authenticated user sessions
+rails g  ajaxful_rating user
 
So this call will create a Rate model and will link it to your User model. @@ -77,7 +75,13 @@ end Finally, as a mere recommendation to make it even easier, modify your routes to map a rate action: - @map.resources :cars, :member => {:rate => :post}@ +
+resources :cars do
+  member do
+    post :rate
+  end
+end
+
h3. Use it diff --git a/Rakefile b/Rakefile index 57e41ec..fadb409 100644 --- a/Rakefile +++ b/Rakefile @@ -1,15 +1,21 @@ require 'rubygems' require 'rake' -require 'echoe' -Echoe.new('ajaxful_rating_jquery', '2.2.9') do |p| - p.summary = "Add star rating functionality to your Rails 2.3.x application. Requires jQuery." - p.description = "Provides a simple way to add rating functionality to your application. This is a fork of ajaxful_ratings that works with jQuery instead of Prototype and uses unobtrusive javascript instead of link_to_remote." - p.url = "http://github.com/kamui/ajaxful_rating_jquery" - p.author = "Jack Chu, Edgar J. Suarez" - p.email = "kamuigt@gmail.com" - p.ignore_pattern = ["tmp/*", "script/*"] - p.development_dependencies = [] -end - -Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext } \ No newline at end of file +begin + require 'jeweler' + + Jeweler::Tasks.new do |spec| + spec.name = "ajaxful_rating" + spec.version = "3.0.0.beta3" + spec.summary = "Provides a simple way to add rating functionality to your application." + spec.description = "Provides a simple way to add rating functionality to your application." + spec.homepage = "http://github.com/edgarjs/ajaxful-rating" + spec.authors = ["Edgar J. Suarez", "Denis Odorcic"] + spec.email = ["edgar.js@gmail.com", "denis.odorcic@gmail.com"] + spec.has_rdoc = true + spec.files.exclude 'init.rb' + end + +rescue LoadError + puts "Jeweler not available. Install it with: gem install jeweler" +end \ No newline at end of file diff --git a/ajaxful_rating_jquery.gemspec b/ajaxful_rating_jquery.gemspec index 2956415..e64f77f 100644 --- a/ajaxful_rating_jquery.gemspec +++ b/ajaxful_rating_jquery.gemspec @@ -1,22 +1,64 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command # -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = %q{ajaxful_rating_jquery} - s.version = "2.2.9" + s.version = "3.0.0.beta3" - s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version= - s.authors = ["Jack Chu, Edgar J. Suarez"] - s.date = %q{2010-09-30} - s.description = %q{Provides a simple way to add rating functionality to your application. This is a fork of ajaxful_ratings that works with jQuery instead of Prototype and uses unobtrusive javascript instead of link_to_remote.} - s.email = %q{kamuigt@gmail.com} - s.extra_rdoc_files = ["CHANGELOG", "README.textile", "lib/ajaxful_rating_jquery.rb", "lib/axr/css_builder.rb", "lib/axr/errors.rb", "lib/axr/helpers.rb", "lib/axr/locale.rb", "lib/axr/model.rb", "lib/axr/stars_builder.rb"] - s.files = ["CHANGELOG", "Manifest", "README.textile", "Rakefile", "ajaxful_rating_jquery.gemspec", "generators/ajaxful_rating/USAGE", "generators/ajaxful_rating/ajaxful_rating_generator.rb", "generators/ajaxful_rating/templates/images/star.png", "generators/ajaxful_rating/templates/images/star_medium.png", "generators/ajaxful_rating/templates/images/star_small.png", "generators/ajaxful_rating/templates/migration.rb", "generators/ajaxful_rating/templates/model.rb", "generators/ajaxful_rating/templates/style.css", "init.rb", "lib/ajaxful_rating_jquery.rb", "lib/axr/css_builder.rb", "lib/axr/errors.rb", "lib/axr/helpers.rb", "lib/axr/locale.rb", "lib/axr/model.rb", "lib/axr/stars_builder.rb"] - s.homepage = %q{http://github.com/kamui/ajaxful_rating_jquery} - s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ajaxful_rating_jquery", "--main", "README.textile"] + s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= + s.authors = ["Edgar J. Suarez", "Denis Odorcic", "Dan Barber"] + s.date = %q{2011-06-24} + s.description = %q{Provides a simple way to add rating functionality to your application.} + s.email = ["edgar.js@gmail.com", "denis.odorcic@gmail.com", "danbee@gmail.com"] + s.extra_rdoc_files = [ + "README.textile" + ] + s.files = [ + ".gitignore", + "CHANGELOG", + "Manifest", + "README.textile", + "Rakefile", + "ajaxful_rating_jquery.gemspec", + "lib/ajaxful_rating_jquery.rb", + "lib/axr/css_builder.rb", + "lib/axr/errors.rb", + "lib/axr/helpers.rb", + "lib/axr/locale.rb", + "lib/axr/model.rb", + "lib/axr/stars_builder.rb", + "lib/generators/ajaxful_rating/USAGE", + "lib/generators/ajaxful_rating/ajaxful_rating_generator.rb", + "lib/generators/ajaxful_rating/templates/images/star.png", + "lib/generators/ajaxful_rating/templates/images/star_small.png", + "lib/generators/ajaxful_rating/templates/migration.rb", + "lib/generators/ajaxful_rating/templates/model.rb", + "lib/generators/ajaxful_rating/templates/style.css", + "test/fixtures/car.rb", + "test/fixtures/cars.yml", + "test/fixtures/rate.rb", + "test/fixtures/rates.yml", + "test/fixtures/schema.rb", + "test/fixtures/user.rb", + "test/fixtures/users.yml", + "test/lib/model_test.rb", + "test/test_helper.rb" + ] + s.homepage = %q{http://github.com/danbee/ajaxful-rating-jquery} + s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] - s.rubyforge_project = %q{ajaxful_rating_jquery} s.rubygems_version = %q{1.3.7} - s.summary = %q{Add star rating functionality to your Rails 2.3.x application. Requires jQuery.} + s.summary = %q{Provides a simple way to add rating functionality to your application.} + s.test_files = [ + "test/fixtures/car.rb", + "test/fixtures/rate.rb", + "test/fixtures/schema.rb", + "test/fixtures/user.rb", + "test/lib/model_test.rb", + "test/test_helper.rb" + ] if s.respond_to? :specification_version then current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION @@ -28,3 +70,4 @@ Gem::Specification.new do |s| else end end + diff --git a/lib/axr/helpers.rb b/lib/axr/helpers.rb index 7b04c6a..450b7af 100644 --- a/lib/axr/helpers.rb +++ b/lib/axr/helpers.rb @@ -11,42 +11,13 @@ def ajaxful_rating_style end def ajaxful_rating_script - if protect_against_forgery? - authenticity_script = %{ - csrf_param = "authenticity_token"; - csrf_token = #{form_authenticity_token.inspect}; - - // Always send the authenticity_token with ajax - $(document).ajaxSend(function(event, request, settings) { - if ( settings.type == 'post' ) { - settings.data = (settings.data ? settings.data + "&" : "") - + encodeURIComponent( csrf_param ) + "=" + encodeURIComponent( csrf_token ); - } - }); - } - end - - %{} end @@ -131,16 +102,18 @@ def ajaxful_rating_script_prototype # # ajaxful_rating: # helper: - # global_average: "Global rating average: {{value}} out of {{max}}" - # user_rating: "Your rating: {{value}} out of {{max}}" - # hover: "Rate {{value}} out of {{max}}" def ratings_for(*args) + # global_average: "Global rating average: %{value} out of %{max}" + # user_rating: "Your rating: %{value} out of %{max}" + # hover: "Rate %{value} out of %{max}" def ratings_for(*args) def ratings_for(*args) @axr_css ||= CSSBuilder.new - options = args.extract_options!.symbolize_keys.slice(:size, :url, :method, + options = args.extract_options!.to_hash.symbolize_keys.slice(:size, :remote_options, :wrap, :show_user_rating, :dimension, :force_static, :current_user) + + remote_options = options.delete(:remote_options) || {} rateable = args.shift - user = args.shift || (respond_to?(:current_user) ? current_user : raise(NoUserSpecified)) - StarsBuilder.new(rateable, user, self, @axr_css, options).render + user = args.shift || (respond_to?(:current_user) ? current_user : raise(Errors::NoUserSpecified)) + StarsBuilder.new(rateable, user, self, @axr_css, options, remote_options).render end end end diff --git a/lib/axr/locale.rb b/lib/axr/locale.rb index 298c441..b689f90 100644 --- a/lib/axr/locale.rb +++ b/lib/axr/locale.rb @@ -4,15 +4,15 @@ module AjaxfulRating # :nodoc: # # ajaxful_rating: # helper: - # global_average: "Global rating average: {{value}} out of {{max}}" - # user_rating: "Your rating: {{value}} out of {{max}}" - # hover: "Rate {{value}} out of {{max}}" + # global_average: "Global rating average: %{value} out of %{max}" + # user_rating: "Your rating: %{value} out of %{max}" + # hover: "Rate %{value} out of %{max}" module Locale DEFAULTS = { - :user_rating => "Your rating: {{value}} out of {{max}}", - :global_average => "Global rating average: {{value}} out of {{max}}", - :hover => "Rate {{value}} out of {{max}}" + :user_rating => "Your rating: %{value} out of %{max}", + :global_average => "Global rating average: %{value} out of %{max}", + :hover => "Rate %{value} out of %{max}" } def i18n(key, value = nil) diff --git a/lib/axr/model.rb b/lib/axr/model.rb index 1d4fb2f..8cb5534 100644 --- a/lib/axr/model.rb +++ b/lib/axr/model.rb @@ -71,12 +71,12 @@ def axr_config # end def rate(stars, user, dimension = nil) return false if (stars.to_i > self.class.max_stars) - raise AlreadyRatedError if (!self.class.axr_config[:allow_update] && rated_by?(user, dimension)) + raise Errors::AlreadyRatedError if (!self.class.axr_config[:allow_update] && rated_by?(user, dimension)) rate = if self.class.axr_config[:allow_update] && rated_by?(user, dimension) rate_by(user, dimension) else - returning rates(dimension).build do |r| + rates(dimension).build.tap do |r| r.rater = user end end @@ -87,16 +87,18 @@ def rate(stars, user, dimension = nil) # Builds the DOM id attribute for the wrapper in view. def wrapper_dom_id(options = {}) - options = options.symbolize_keys.slice(:size, :dimension) - options = options.select { |k, v| v.present? or (v == false) }.map do |k, v| + options = options.to_hash.symbolize_keys.slice(:size, :dimension) + options = options.map do |k, v| if k == :dimension v.to_s else v.to_s == 'true' ? k.to_s : "no-#{k}" end end - options.unshift("ajaxful_rating") - ApplicationController.helpers.dom_id(self, options.sort.join('_')) + options = options.delete_if { |x| x.empty? } + prefix = "ajaxful_rating" + prefix << "_#{options.sort.join('_')}" unless options.empty? + ApplicationController.helpers.dom_id(self, prefix) end # Returns an array with the users that have rated this object for the diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index 395209c..045ac58 100644 --- a/lib/axr/stars_builder.rb +++ b/lib/axr/stars_builder.rb @@ -1,15 +1,15 @@ module AjaxfulRating # :nodoc: class StarsBuilder # :nodoc: include AjaxfulRating::Locale - - attr_reader :rateable, :user, :options - - def initialize(rateable, user_or_static, template, css_builder, options = {}) + + attr_reader :rateable, :user, :options, :remote_options + + def initialize(rateable, user_or_static, template, css_builder, options = {}, remote_options = {}) @user = user_or_static unless user_or_static == :static @rateable, @template, @css_builder = rateable, template, css_builder - apply_stars_builder_options!(options) + apply_stars_builder_options!(options, remote_options) end - + def show_value if options[:show_user_rating] rate = rateable.rate_by(user, options[:dimension]) if user @@ -18,14 +18,14 @@ def show_value rateable.rate_average(true, options[:dimension]) end end - + def render options[:wrap] ? wrapper_tag : ratings_tag end - + private - - def apply_stars_builder_options!(options) + + def apply_stars_builder_options!(options, remote_options) @options = { :url => nil, :method => :post, @@ -34,79 +34,90 @@ def apply_stars_builder_options!(options) :show_user_rating => false, :force_static => false, :current_user => (@template.current_user if @template.respond_to?(:current_user)) - }.merge(options) - - @options[:show_user_rating] = @options[:show_user_rating].to_s == 'true' - @options[:wrap] = @options[:wrap].to_s == 'true' - - if @options[:url].nil? - rateable_name = ActionController::RecordIdentifier.singular_class_name(rateable) - url = "rate_#{rateable_name}_path" - if @template.respond_to?(url) - @options[:url] = @template.send(url, rateable) - else - raise(MissingRateRoute) + }.merge(options) + + @options[:show_user_rating] = @options[:show_user_rating].to_s == 'true' + @options[:wrap] = @options[:wrap].to_s == 'true' + + @remote_options = { + :url => nil, + :method => :post + }.merge(remote_options) + + if @remote_options[:url].nil? + rateable_name = ActionController::RecordIdentifier.dom_class(rateable) + url = "rate_#{rateable_name}_path" + if @template.respond_to?(url) + @remote_options[:url] = @template.send(url, rateable) + else + raise(Errors::MissingRateRoute) + end + end end - end - end - - def ratings_tag - stars = [] - width = (show_value / rateable.class.max_stars.to_f) * 100 - li_class = "axr-#{show_value}-#{rateable.class.max_stars}".gsub('.', '_') - @css_builder.rule('.ajaxful-rating', :width => (rateable.class.max_stars * 25)) - @css_builder.rule('.ajaxful-rating.medium', - :width => (rateable.class.max_stars * 18)) if options[:size] == 'medium' - @css_builder.rule('.ajaxful-rating.small', - :width => (rateable.class.max_stars * 10)) if options[:size] == 'small' - - stars << @template.content_tag(:li, i18n(:current), :class => "show-value", - :style => "width: #{width}%") - stars += (1..rateable.class.max_stars).map do |i| - star_tag(i) - end - if options[:size] == 'small' - size = ' small' - elsif options[:size] == 'medium' - size = ' medium' - end - # When using rails_xss plugin, it needs to render as HTML - @template.content_tag(:ul, stars.join.try(:html_safe), :class => "ajaxful-rating#{size}") - end - - def star_tag(value) - already_rated = rateable.rated_by?(user, options[:dimension]) if user - css_class = "stars-#{value}" - @css_builder.rule(".ajaxful-rating .#{css_class}", { - :width => "#{(value / rateable.class.max_stars.to_f) * 100}%", - :zIndex => (rateable.class.max_stars + 2 - value).to_s - }) - @template.content_tag(:li) do - if !options[:force_static] && (user && options[:current_user] == user && - (!already_rated || rateable.axr_config[:allow_update])) - link_star_tag(value, css_class) - else - @template.content_tag(:span, show_value, :class => css_class, :title => i18n(:current)) + + def ratings_tag + stars = [] + width = (show_value / rateable.class.max_stars.to_f) * 100 + li_class = "axr-#{show_value}-#{rateable.class.max_stars}".gsub('.', '_') + @css_builder.rule('.ajaxful-rating', :width => (rateable.class.max_stars * 25)) + @css_builder.rule('.ajaxful-rating.medium', + :width => (rateable.class.max_stars * 18)) if options[:size] == 'medium' + @css_builder.rule('.ajaxful-rating.small', + :width => (rateable.class.max_stars * 10)) if options[:size] == 'small' + + stars << @template.content_tag(:li, i18n(:current), :class => "show-value", + :style => "width: #{width}%") + stars += (1..rateable.class.max_stars).map do |i| + star_tag(i) + end + if options[:size] == 'small' + size = ' small' + elsif options[:size] == 'medium' + size = ' medium' + end + @template.content_tag(:ul, stars.join.try(:html_safe), :class => "ajaxful-rating#{size}") + end + + def star_tag(value) + already_rated = rateable.rated_by?(user, options[:dimension]) if user + css_class = "stars-#{value}" + @css_builder.rule(".ajaxful-rating .#{css_class}", { + :width => "#{(value / rateable.class.max_stars.to_f) * 100}%", + :zIndex => (rateable.class.max_stars + 2 - value).to_s + }) + + @template.content_tag(:li) do + if !options[:force_static] && (user && options[:current_user] == user && (!already_rated || rateable.axr_config[:allow_update])) + link_star_tag(value, css_class) + else + @template.content_tag(:span, show_value, :class => css_class, :title => i18n(:current)) + end + end + end + + def link_star_tag(value, css_class) + query = { + :stars => value, + :dimension => options[:dimension], + :size => options[:size], + :show_user_rating => options[:show_user_rating] + }.to_query + + options = { + :class => css_class, + :title => i18n(:hover, value), + :method => remote_options[:method] || :post, + :remote => true + } + + href = "#{remote_options[:url]}?#{query}" + + @template.link_to(value, href, options) + end + + def wrapper_tag + @template.content_tag(:div, ratings_tag, :class => "ajaxful-rating-wrapper", + :id => rateable.wrapper_dom_id(options)) + end + end end - end - end - - def link_star_tag(value, css_class) - html = { - :"data-method" => options[:method], - :"data-stars" => value, - :"data-dimension" => options[:dimension], - :"data-size" => options[:size], - :"data-show_user_rating" => options[:show_user_rating], - :class => css_class, - :title => i18n(:hover, value) - } - @template.link_to(value, options[:url], html) - end - - def wrapper_tag - @template.content_tag(:div, ratings_tag, :class => "ajaxful-rating-wrapper", - :id => rateable.wrapper_dom_id(options)) - end - end -end diff --git a/generators/ajaxful_rating/USAGE b/lib/generators/ajaxful_rating/USAGE similarity index 72% rename from generators/ajaxful_rating/USAGE rename to lib/generators/ajaxful_rating/USAGE index bef3330..b601f51 100644 --- a/generators/ajaxful_rating/USAGE +++ b/lib/generators/ajaxful_rating/USAGE @@ -2,4 +2,4 @@ Description: Generates a model called Rate linked to a user model named as the passed parameter. Example: - ./script/generate ajaxful_rating User + rails generate ajaxful_rating User diff --git a/lib/generators/ajaxful_rating/ajaxful_rating_generator.rb b/lib/generators/ajaxful_rating/ajaxful_rating_generator.rb new file mode 100644 index 0000000..3413c5d --- /dev/null +++ b/lib/generators/ajaxful_rating/ajaxful_rating_generator.rb @@ -0,0 +1,38 @@ +require 'rails/generators/migration' +class AjaxfulRatingGenerator < Rails::Generators::NamedBase + include Rails::Generators::Migration + + def self.source_root + @_axr_root ||= File.expand_path("../templates", __FILE__) + end + + def create_model_files + 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', class_path, "rate.rb") + end + + def create_migration + migration_template 'migration.rb', "db/migrate/create_rates.rb" + end + + def create_layout + copy_file 'images/star.png', 'public/images/ajaxful_rating/star.png' + copy_file 'images/star_small.png', 'public/images/ajaxful_rating/star_small.png' + copy_file 'style.css', 'public/stylesheets/ajaxful_rating.css' + end + + private + + # FIXME: Should be proxied to ActiveRecord::Generators::Base + # Implement the required interface for Rails::Generators::Migration. + def self.next_migration_number(dirname) #:nodoc: + if ActiveRecord::Base.timestamped_migrations + Time.now.utc.strftime("%Y%m%d%H%M%S") + else + "%.3d" % (current_migration_number(dirname) + 1) + end + end + +end diff --git a/generators/ajaxful_rating/templates/images/star.png b/lib/generators/ajaxful_rating/templates/images/star.png similarity index 100% rename from generators/ajaxful_rating/templates/images/star.png rename to lib/generators/ajaxful_rating/templates/images/star.png diff --git a/generators/ajaxful_rating/templates/images/star_small.png b/lib/generators/ajaxful_rating/templates/images/star_small.png similarity index 100% rename from generators/ajaxful_rating/templates/images/star_small.png rename to lib/generators/ajaxful_rating/templates/images/star_small.png diff --git a/generators/ajaxful_rating/templates/migration.rb b/lib/generators/ajaxful_rating/templates/migration.rb similarity index 100% rename from generators/ajaxful_rating/templates/migration.rb rename to lib/generators/ajaxful_rating/templates/migration.rb diff --git a/generators/ajaxful_rating/templates/model.rb b/lib/generators/ajaxful_rating/templates/model.rb similarity index 100% rename from generators/ajaxful_rating/templates/model.rb rename to lib/generators/ajaxful_rating/templates/model.rb diff --git a/lib/generators/ajaxful_rating/templates/style.css b/lib/generators/ajaxful_rating/templates/style.css new file mode 100644 index 0000000..d12c52d --- /dev/null +++ b/lib/generators/ajaxful_rating/templates/style.css @@ -0,0 +1,84 @@ +/* +* Style by Rogie http://www.komodomedia.com/blog/2007/01/css-star-rating-redux/ +*/ + +.ajaxful-rating, +.ajaxful-rating a:hover, +.ajaxful-rating a:active, +.ajaxful-rating a:focus, +.ajaxful-rating .show-value{ + background: url(/images/ajaxful_rating/star.png) left -1000px repeat-x; +} +.ajaxful-rating{ + position: relative; + /*width: 125px; this is setted dynamically */ + height: 25px; + overflow: hidden; + list-style: none; + margin: 0; + padding: 0; + background-position: left top; +} +.ajaxful-rating li{ display: inline; } +.ajaxful-rating a, +.ajaxful-rating span, +.ajaxful-rating .show-value{ + position: absolute; + top: 0; + left: 0; + text-indent: -1000em; + height: 25px; + line-height: 25px; + outline: none; + overflow: hidden; + border: none; +} +.ajaxful-rating a:hover, +.ajaxful-rating a:active, +.ajaxful-rating a:focus{ + background-position: left bottom; +} + +/* This section is generated dynamically. +Just add a call to the helper method 'ajaxful_rating_style' within +the head tags in your main layout +.ajaxful-rating .stars-1{ +width: 20%; +z-index: 6; +} +.ajaxful-rating .stars-2{ +width: 40%; +z-index: 5; +} +.ajaxful-rating .stars-3{ +width: 60%; +z-index: 4; +} +.ajaxful-rating .stars-4{ +width: 80%; +z-index: 3; +} +.ajaxful-rating .stars-5{ +width: 100%; +z-index: 2; +} +*/ +.ajaxful-rating .show-value{ + z-index: 1; + background-position: left center; +} + +/* smaller star */ +.ajaxful-rating.small{ + /*width: 50px; this is setted dynamically */ + height: 10px; +} +.ajaxful-rating.small, +.ajaxful-rating.small a:hover, +.ajaxful-rating.small a:active, +.ajaxful-rating.small a:focus, +.ajaxful-rating.small .show-value{ + background-image: url(/images/ajaxful_rating/star_small.png); + line-height: 10px; + height: 10px; +} diff --git a/test/fixtures/car.rb b/test/fixtures/car.rb new file mode 100644 index 0000000..626f61d --- /dev/null +++ b/test/fixtures/car.rb @@ -0,0 +1,3 @@ +class Car < ActiveRecord::Base + ajaxful_rateable :stars => 10, :dimensions => [:speed, :reliability, :price] +end diff --git a/test/fixtures/cars.yml b/test/fixtures/cars.yml new file mode 100644 index 0000000..4f43480 --- /dev/null +++ b/test/fixtures/cars.yml @@ -0,0 +1,7 @@ +audi: + id: 1 + name: Audi + +porsche: + id: 2 + name: Infinity \ No newline at end of file diff --git a/test/fixtures/rate.rb b/test/fixtures/rate.rb new file mode 100644 index 0000000..2170a8e --- /dev/null +++ b/test/fixtures/rate.rb @@ -0,0 +1,6 @@ +class Rate < ActiveRecord::Base + belongs_to :rater, :class_name => "User" + belongs_to :rateable, :polymorphic => true + + attr_accessible :rate, :dimension +end \ No newline at end of file diff --git a/test/fixtures/rates.yml b/test/fixtures/rates.yml new file mode 100644 index 0000000..edb34ad --- /dev/null +++ b/test/fixtures/rates.yml @@ -0,0 +1,41 @@ +rate_audi_denis: + rater_id: 1 # Denis + rateable_id: 1 # Audi + rateable_type: Car + stars: 6 + dimension: + +rate_audi_denis_speed: + rater_id: 1 # Denis + rateable_id: 1 # Audi + rateable_type: Car + stars: 7 + dimension: speed + +rate_audi_denis_reliability: + rater_id: 1 # Denis + rateable_id: 1 # Audi + rateable_type: Car + stars: 5 + dimension: reliability + +rate_audi_john: + rater_id: 2 # John + rateable_id: 1 # Audi + rateable_type: Car + stars: 8 + dimension: + +rate_audi_john_speed: + rater_id: 2 # John + rateable_id: 1 # Audi + rateable_type: Car + stars: 8 + dimension: speed + +rate_audi_john_reliability: + rater_id: 2 # John + rateable_id: 2 # Audi + rateable_type: Car + stars: 5 + dimension: reliability \ No newline at end of file diff --git a/test/fixtures/schema.rb b/test/fixtures/schema.rb new file mode 100644 index 0000000..6335b19 --- /dev/null +++ b/test/fixtures/schema.rb @@ -0,0 +1,22 @@ +ActiveRecord::Schema.define do + create_table "cars", :force => true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "rates", :force => true do |t| + t.integer "rater_id" + t.integer "rateable_id" + t.string "rateable_type" + t.integer "stars", :null => false + t.string "dimension" + end + + add_index "rates", ["rateable_id", "rateable_type"], :name => "index_rates_on_rateable_id_and_rateable_type" + add_index "rates", ["rater_id"], :name => "index_rates_on_rater_id" + + create_table "users", :force => true do |t| + t.string "name" + end +end diff --git a/test/fixtures/user.rb b/test/fixtures/user.rb new file mode 100644 index 0000000..85ce2a2 --- /dev/null +++ b/test/fixtures/user.rb @@ -0,0 +1,3 @@ +class User < ActiveRecord::Base + ajaxful_rater +end \ No newline at end of file diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..5cfcb43 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,7 @@ +denis: + id: 1 + name: Denis Odorcic + +john: + id: 2 + name: John Doe \ No newline at end of file diff --git a/test/lib/model_test.rb b/test/lib/model_test.rb new file mode 100644 index 0000000..9eedd5b --- /dev/null +++ b/test/lib/model_test.rb @@ -0,0 +1,58 @@ +require File.dirname(__FILE__) + "/../test_helper" + +class ModelTest < ActiveSupport::TestCase + include ActiveRecord::TestFixtures + + def setup + @audi = Car.find_by_name("Audi") + @infinity = Car.find_by_name("Infinity") + + @denis = User.find_by_name("Denis Odorcic") + end + + def test_find_statement + assert_equal Car.find_statement(:stars, 7).size, 0 + assert_equal Car.find_statement(:stars, 8).size, 1 + assert_equal Car.find_statement(:stars, 8, :speed).size, 1 + assert_equal Car.find_statement(:stars, 5, :reliability).size, 2 + end + + def test_rate_higher_than_max_stars + assert_equal Car.max_stars, 10 + assert !@audi.rate(15, User.first) + end + + def test_already_rated_error + Car.axr_config[:allow_update] = false + assert @audi.rated_by?(@denis) + assert_raise AjaxfulRating::Errors::AlreadyRatedError do + @audi.rate(4, @denis) + end + end + + def test_already_rated_and_allowed_to_update + assert @audi.rated_by?(@denis) + stars = @audi.rate_by(@denis).stars + + assert_no_difference 'Rate.count' do + @audi.rate(1, @denis) + end + assert_equal @audi.rate_by(@denis).stars, 1 + assert_not_equal @audi.rate_by(@denis).stars, stars + end + + def test_new_rating + assert_difference 'Rate.count', 1 do + @audi.rate(5, @denis, :price) + end + end + + def test_raters + assert_equal @audi.raters.size, 2 + assert_difference 'Rate.count', 1 do + @audi.rate(3, User.create(:name => "Bob")) + end + assert_equal @audi.raters.size, 3 + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..7fe966e --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,25 @@ +$:.unshift File.dirname(__FILE__) + '/../lib' + +require 'rubygems' +require 'test/unit' +require 'active_record' +require 'active_record/fixtures' +require 'action_controller' +require 'ajaxful_rating' + +AXR_FIXTURES_PATH = File.join(File.dirname(__FILE__), 'fixtures') + +# For transactional fixtures to work in tests, configurations in AR::Base has to be set to something +ActiveRecord::Base.configurations = {:epic => 'fail'} +ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:') + +# Add fixtures to load path +dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies +dep.load_paths.unshift AXR_FIXTURES_PATH + +ActiveRecord::Base.silence do + ActiveRecord::Migration.verbose = false + load File.join(AXR_FIXTURES_PATH, 'schema.rb') +end + +Fixtures.create_fixtures(AXR_FIXTURES_PATH, ActiveRecord::Base.connection.tables) \ No newline at end of file