From a21e4286138cf005305bf426d606a1ef15858a0a Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Thu, 11 Mar 2010 12:11:34 -0600 Subject: [PATCH 01/22] Add Rails 3 generator and code changes (cherry picked from commit 9f15d11658260601d67cdfa70ce6bb8156f91095) Conflicts: lib/axr/helpers.rb lib/axr/stars_builder.rb --- lib/axr/helpers.rb | 3 +- lib/axr/model.rb | 2 +- lib/axr/stars_builder.rb | 33 +++---- lib/generators/ajaxful_rating/USAGE | 5 ++ .../ajaxful_rating_generator.rb | 38 ++++++++ .../ajaxful_rating/templates/images/star.png | Bin 0 -> 1688 bytes .../templates/images/star_small.png | Bin 0 -> 302 bytes .../ajaxful_rating/templates/migration.rb | 18 ++++ .../ajaxful_rating/templates/model.rb | 6 ++ .../ajaxful_rating/templates/style.css | 84 ++++++++++++++++++ 10 files changed, 171 insertions(+), 18 deletions(-) create mode 100644 lib/generators/ajaxful_rating/USAGE create mode 100644 lib/generators/ajaxful_rating/ajaxful_rating_generator.rb create mode 100644 lib/generators/ajaxful_rating/templates/images/star.png create mode 100644 lib/generators/ajaxful_rating/templates/images/star_small.png create mode 100644 lib/generators/ajaxful_rating/templates/migration.rb create mode 100644 lib/generators/ajaxful_rating/templates/model.rb create mode 100644 lib/generators/ajaxful_rating/templates/style.css diff --git a/lib/axr/helpers.rb b/lib/axr/helpers.rb index 67521ee..d5ed885 100644 --- a/lib/axr/helpers.rb +++ b/lib/axr/helpers.rb @@ -74,8 +74,9 @@ def ajaxful_rating_style # 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(:small, :remote_options, + options = args.extract_options!.to_hash.symbolize_keys.slice(:small, :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)) diff --git a/lib/axr/model.rb b/lib/axr/model.rb index ab28ac1..57e50ec 100644 --- a/lib/axr/model.rb +++ b/lib/axr/model.rb @@ -87,7 +87,7 @@ 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(:small, :dimension) + options = options.to_hash.symbolize_keys.slice(:small, :dimension) options = options.map do |k, v| if k == :dimension v.to_s diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index b1fb61d..ea27673 100644 --- a/lib/axr/stars_builder.rb +++ b/lib/axr/stars_builder.rb @@ -77,16 +77,15 @@ def star_tag(value) :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 + + if !options[:force_static] && (user && options[:current_user] == user && + (!already_rated || rateable.axr_config[:allow_update])) + @template.content_tag(:li, link_star_tag(value, css_class)) + else + @template.content_tag(:li, @template.content_tag(:span, show_value, :class => css_class, :title => i18n(:current))) end end - + def link_star_tag(value, css_class) query = { :stars => value, @@ -95,16 +94,18 @@ def link_star_tag(value, css_class) :show_user_rating => options[:show_user_rating] }.to_query config = { - :html => { - :class => css_class, - :title => i18n(:hover, value) - }, - :url => "#{remote_options[:url]}", - :with => "'#{query}'" + :url => "#{remote_options[:url]}?#{query}" + } + html_options = { + :class => css_class, + :title => i18n(:hover, value), + :method => :post, + :remote => true } - @template.link_to_remote(value, remote_options.merge(config)) + + @template.link_to(value, remote_options.merge(config), html_options) end - + def wrapper_tag @template.content_tag(:div, ratings_tag, :class => "ajaxful-rating-wrapper", :id => rateable.wrapper_dom_id(options)) diff --git a/lib/generators/ajaxful_rating/USAGE b/lib/generators/ajaxful_rating/USAGE new file mode 100644 index 0000000..b601f51 --- /dev/null +++ b/lib/generators/ajaxful_rating/USAGE @@ -0,0 +1,5 @@ +Description: + Generates a model called Rate linked to a user model named as the passed parameter. + +Example: + 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/lib/generators/ajaxful_rating/templates/images/star.png b/lib/generators/ajaxful_rating/templates/images/star.png new file mode 100644 index 0000000000000000000000000000000000000000..1e58c5d8cbca0c2cb5bc64d57b4fb5203876b16f GIT binary patch literal 1688 zcmV;J250$+P)P002t}1^@s6g|9510000PbVXQnQ*UN; zcVTj606}DLVr3vnZDD6+Qe|Oed2z{QJOBU&I!Q!9RCwC#m}_WTWf;f*C%2O(X_KyP zTH4yqDLS2OL)}1deo&BIWJ7e@mwgaHa18cA6voDe7u<%MABK)8Q&1S zv2JeF>FmX<-P)$P=cGx_)&EIqmu*dxv}r$#9(Xu!(l_t_JeT)*e?5X>7?|rz=-iR^J!JNx_lAF8y3J+#fV zM`|!Z`4tN7n@Xgoy}f;z!{M0Kp3pQ6fj}Tck*0awjPovXY-nhxUs049 zFQ)4{1_yIFKUPfS=!8%%k(FL%0hj_5I#8JOsls)bllPcHW&$e};BO97V0Sq=+0Uo> zr4nzFUPV~rGvcmtaxyR6HMxt;Dl_f>&xOYC|KS;KWRn++bm)77-PYiD zQCjm<^RKs()x|JQa#yndB_sz9R+qadFv(q+@OKcj7_1fnQsAA+xa+#a7v-+8Q)}V$ zcp<=wWH5l1HC;%JXUpTR0+FKJB@Mm{IrJGscOwiv%lWbwJP!Xqi!H<5yrPDvYcG(bWcrU4;}Qg`9Y!VqHF+Ld`ihB%43|c4!cZb-pu~EZBMUSo3q>)|vEgL- z+|{u111RGH(&1qU_FG^O6bwzz3FPi-er)yJg^UzKc07&F$4+u?UHROlX#$$={}5Tp z42MFG{9JfKz%<>&C3{XNol2sE0v55Ma_-V0!ri(VnfMrI)nE07V&Gk{H_=-OHU2fQ zx>_pZE>4={E=3yP?&cHA3D%*DPus~5-AT{tnU@&L+rmm5D9K&6>9|Wt2Do21#Uip) zJCRMqnB*iJ&Q@4$I<%~eZ1hS=?y^nCU219+PNxUb=w)OQak$#o!EW~=(sKefyA4ja z7fK>rlDnpD^=c-9=%o;9Z{3R4wYw$}scF?-gu0JoAz#DBNkmKMcL^+qs21OV=(4KfE}1aEE@L-{~Z?$dLVZbZ+1d?>z|AHs{lLxd+&B z7E-#3aEDXuix)oOvDC^QtNrXw?hJBOeG1K;x5$WLQ`v&ZuxMLOthVLRq| zFU9F^8-)VEIFDtwpJ&2SVDSUMvh~nvTS~(nzIqDKxsa=l;o;)7TtKHc@ez{W5I}kX zfev*PBB6j3iA&wUMMmnI{lLq;WhDNIj~NmxkcrqG`;9N>A6-Q5+uRNL|;D~XGZG9-2FIdX(PA)sh^F`Z-ovb={$dcLO{dX2Y&mud#X zv+Zlfp9f-wp{Zfq$-sF}Cd`bq`MN3*qQGewo~9YRR3ipqIS6=d(rIKzhvA&nOEuDAu8j;`1pYe34Y(3mx}M{+K!D%L8M1PGmM_)Jr1r8* z?|z(+GzMeaaTre?0^UwSUbh#KfgsXSW|lA2HonHAYD-@4I=J(#-GN0Lb|TgF5&!#I ifV%}Gn&SFTfB^tvNSP&xDbDl&0000!2jd}d{GTMM4lbfQ@GdVda+U=&*2*nyTIp!K#os)6^cG|INU9g?ht4EXs+Pb zQpLCA2d~e=aD{VhkG?wm5z{hbmU4OTAaiWXmMs@&NKP^QT(5SR;V93EtS>VJn@-iT xJ@VetEtwLZ&y*CeW0=f!q@LX%je&`WL27s4Ez20!xj?@$c)I$ztaD0e0sxUNY#smr literal 0 HcmV?d00001 diff --git a/lib/generators/ajaxful_rating/templates/migration.rb b/lib/generators/ajaxful_rating/templates/migration.rb new file mode 100644 index 0000000..d7a5277 --- /dev/null +++ b/lib/generators/ajaxful_rating/templates/migration.rb @@ -0,0 +1,18 @@ +class CreateRates < ActiveRecord::Migration + def self.up + create_table :rates do |t| + t.belongs_to :rater + t.belongs_to :rateable, :polymorphic => true + t.integer :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 diff --git a/lib/generators/ajaxful_rating/templates/model.rb b/lib/generators/ajaxful_rating/templates/model.rb new file mode 100644 index 0000000..885edc7 --- /dev/null +++ b/lib/generators/ajaxful_rating/templates/model.rb @@ -0,0 +1,6 @@ +class Rate < ActiveRecord::Base + belongs_to :rater, :class_name => "<%= file_name.classify %>" + belongs_to :rateable, :polymorphic => true + + attr_accessible :rate, :dimension +end 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; +} From 0b2512490267b60f4f3b77000a35b16be5a87805 Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Fri, 12 Mar 2010 10:10:18 +0800 Subject: [PATCH 02/22] nested content_tags fixed in Rails commit 4464b8e - reverting back to original code style --- lib/axr/stars_builder.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index ea27673..367f82e 100644 --- a/lib/axr/stars_builder.rb +++ b/lib/axr/stars_builder.rb @@ -78,11 +78,12 @@ def star_tag(value) :zIndex => (rateable.class.max_stars + 2 - value).to_s }) - if !options[:force_static] && (user && options[:current_user] == user && - (!already_rated || rateable.axr_config[:allow_update])) - @template.content_tag(:li, link_star_tag(value, css_class)) - else - @template.content_tag(:li, @template.content_tag(:span, show_value, :class => css_class, :title => i18n(:current))) + @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 From a4ac23a01e052986586fd7b61879a2eaa58b0fc4 Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Fri, 12 Mar 2010 10:11:15 +0800 Subject: [PATCH 03/22] Add html_safe to star_tags to mark the string as safe --- lib/axr/stars_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index 367f82e..77378be 100644 --- a/lib/axr/stars_builder.rb +++ b/lib/axr/stars_builder.rb @@ -67,7 +67,7 @@ def ratings_tag stars += (1..rateable.class.max_stars).map do |i| star_tag(i) end - @template.content_tag(:ul, stars.join, :class => "ajaxful-rating#{' small' if options[:small]}") + @template.content_tag(:ul, stars.join.html_safe, :class => "ajaxful-rating#{' small' if options[:small]}") end def star_tag(value) From 6708c2daff8b40297396d7f0b2a4494a171bb1bb Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Sat, 13 Mar 2010 12:22:21 +0800 Subject: [PATCH 04/22] Remove updated to v2.2.x message --- init.rb | 5 ----- lib/ajaxful_rating.rb | 5 ----- 2 files changed, 10 deletions(-) diff --git a/init.rb b/init.rb index b536d1a..9b76f24 100644 --- a/init.rb +++ b/init.rb @@ -1,6 +1 @@ -puts "------------------------------------------" -puts "IMPORTANT: AjaxfulRating has been updated to v2.2.x and some options changed."\ - "Please read the changelog at http://github.com/edgarjs/ajaxful-rating/blob/master/CHANGELOG" -puts "------------------------------------------" - require 'ajaxful_rating' diff --git a/lib/ajaxful_rating.rb b/lib/ajaxful_rating.rb index c95af02..58e6b3a 100644 --- a/lib/ajaxful_rating.rb +++ b/lib/ajaxful_rating.rb @@ -1,8 +1,3 @@ -puts "------------------------------------------" -puts "IMPORTANT: AjaxfulRating has been updated to v2.2.x and some options changed."\ - "Please read the changelog at http://github.com/edgarjs/ajaxful-rating/blob/master/CHANGELOG" -puts "------------------------------------------" - require 'axr/locale' require 'axr/errors' require 'axr/model' From c2fa203b2bccf4b56f6526aea743de876cdcaf3d Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Sat, 13 Mar 2010 15:19:53 +0800 Subject: [PATCH 05/22] Fix Error classes not being called correctly --- lib/axr/helpers.rb | 2 +- lib/axr/model.rb | 2 +- lib/axr/stars_builder.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/axr/helpers.rb b/lib/axr/helpers.rb index d5ed885..9382e5c 100644 --- a/lib/axr/helpers.rb +++ b/lib/axr/helpers.rb @@ -79,7 +79,7 @@ def ratings_for(*args) remote_options = options.delete(:remote_options) || {} rateable = args.shift - user = args.shift || (respond_to?(:current_user) ? current_user : raise(NoUserSpecified)) + 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 diff --git a/lib/axr/model.rb b/lib/axr/model.rb index 57e50ec..2a97af4 100644 --- a/lib/axr/model.rb +++ b/lib/axr/model.rb @@ -71,7 +71,7 @@ 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) diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index 77378be..c4821e6 100644 --- a/lib/axr/stars_builder.rb +++ b/lib/axr/stars_builder.rb @@ -49,7 +49,7 @@ def apply_stars_builder_options!(options, remote_options) if @template.respond_to?(url) @remote_options[:url] = @template.send(url, rateable) else - raise(MissingRateRoute) + raise(Errors::MissingRateRoute) end end end From 5888fc47d964940abca14251cfc03539cc6e009a Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Sat, 13 Mar 2010 15:20:45 +0800 Subject: [PATCH 06/22] Add initial testing because a plugin like this needs some :) --- test/fixtures/car.rb | 3 +++ test/fixtures/cars.yml | 7 +++++ test/fixtures/rate.rb | 6 +++++ test/fixtures/rates.yml | 41 +++++++++++++++++++++++++++++ test/fixtures/schema.rb | 22 ++++++++++++++++ test/fixtures/user.rb | 3 +++ test/fixtures/users.yml | 7 +++++ test/lib/model_test.rb | 58 +++++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 25 ++++++++++++++++++ 9 files changed, 172 insertions(+) create mode 100644 test/fixtures/car.rb create mode 100644 test/fixtures/cars.yml create mode 100644 test/fixtures/rate.rb create mode 100644 test/fixtures/rates.yml create mode 100644 test/fixtures/schema.rb create mode 100644 test/fixtures/user.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/lib/model_test.rb create mode 100644 test/test_helper.rb 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 From f78aea715c89f98b51ff2595c3380c7ec4bed3ae Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Sat, 13 Mar 2010 15:53:07 +0800 Subject: [PATCH 07/22] Remove empty strings from wrapper_dom_id, otherwise you get back __ when no dimension is set --- lib/axr/model.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/axr/model.rb b/lib/axr/model.rb index 2a97af4..a7ed61f 100644 --- a/lib/axr/model.rb +++ b/lib/axr/model.rb @@ -95,6 +95,7 @@ def wrapper_dom_id(options = {}) v.to_s == 'true' ? k.to_s : "no-#{k}" end end + options = options.delete_if { |x| x.empty? } prefix = "ajaxful_rating" prefix << "_#{options.sort.join('_')}" unless options.empty? ApplicationController.helpers.dom_id(self, prefix) From 4cd6950a8d8e2207a973f958c268f7591675e098 Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Sat, 20 Mar 2010 07:16:04 +0800 Subject: [PATCH 08/22] Do away with data-url when building the star links as the support is going away, and use href instead --- lib/axr/stars_builder.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index c4821e6..c049be3 100644 --- a/lib/axr/stars_builder.rb +++ b/lib/axr/stars_builder.rb @@ -94,17 +94,17 @@ def link_star_tag(value, css_class) :small => options[:small], :show_user_rating => options[:show_user_rating] }.to_query - config = { - :url => "#{remote_options[:url]}?#{query}" - } - html_options = { + + options = { :class => css_class, :title => i18n(:hover, value), - :method => :post, + :method => remote_options[:method] || :post, :remote => true } + + href = "#{remote_options[:url]}?#{query}" - @template.link_to(value, remote_options.merge(config), html_options) + @template.link_to(value, href, options) end def wrapper_tag From 47e7b0de05837067fa893fd02d74f7a8af56bde3 Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Fri, 12 Mar 2010 23:13:27 -0500 Subject: [PATCH 09/22] Remove Rails 2.x style generator --- generators/ajaxful_rating/USAGE | 5 -- .../ajaxful_rating_generator.rb | 30 ------- .../ajaxful_rating/templates/images/star.png | Bin 1688 -> 0 bytes .../templates/images/star_small.png | Bin 302 -> 0 bytes .../ajaxful_rating/templates/migration.rb | 18 ---- generators/ajaxful_rating/templates/model.rb | 6 -- generators/ajaxful_rating/templates/style.css | 84 ------------------ 7 files changed, 143 deletions(-) delete mode 100644 generators/ajaxful_rating/USAGE delete mode 100644 generators/ajaxful_rating/ajaxful_rating_generator.rb delete mode 100644 generators/ajaxful_rating/templates/images/star.png delete mode 100644 generators/ajaxful_rating/templates/images/star_small.png delete mode 100644 generators/ajaxful_rating/templates/migration.rb delete mode 100644 generators/ajaxful_rating/templates/model.rb delete mode 100644 generators/ajaxful_rating/templates/style.css diff --git a/generators/ajaxful_rating/USAGE b/generators/ajaxful_rating/USAGE deleted file mode 100644 index bef3330..0000000 --- a/generators/ajaxful_rating/USAGE +++ /dev/null @@ -1,5 +0,0 @@ -Description: - Generates a model called Rate linked to a user model named as the passed parameter. - -Example: - ./script/generate ajaxful_rating User diff --git a/generators/ajaxful_rating/ajaxful_rating_generator.rb b/generators/ajaxful_rating/ajaxful_rating_generator.rb deleted file mode 100644 index 11d1421..0000000 --- a/generators/ajaxful_rating/ajaxful_rating_generator.rb +++ /dev/null @@ -1,30 +0,0 @@ -class AjaxfulRatingGenerator < Rails::Generator::NamedBase - def initialize(runtime_args, runtime_options = {}) - super - - # if there's no user model - model_file = File.join('app/models', "#{file_path}.rb") - raise "User model (#{model_file}) must exits." unless File.exists?(model_file) - end - - def manifest - record do |m| - m.class_collisions 'Rate' - m.template 'model.rb', File.join('app/models', 'rate.rb') - m.migration_template 'migration.rb', 'db/migrate', - :migration_file_name => 'create_rates' - - # style - m.directory 'public/images/ajaxful_rating' - m.file 'images/star.png', 'public/images/ajaxful_rating/star.png' - m.file 'images/star_small.png', 'public/images/ajaxful_rating/star_small.png' - m.file 'style.css', 'public/stylesheets/ajaxful_rating.css' - end - end - - protected - - def banner - "Usage: #{$0} ajaxful_rating UserModelName" - end -end diff --git a/generators/ajaxful_rating/templates/images/star.png b/generators/ajaxful_rating/templates/images/star.png deleted file mode 100644 index 1e58c5d8cbca0c2cb5bc64d57b4fb5203876b16f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1688 zcmV;J250$+P)P002t}1^@s6g|9510000PbVXQnQ*UN; zcVTj606}DLVr3vnZDD6+Qe|Oed2z{QJOBU&I!Q!9RCwC#m}_WTWf;f*C%2O(X_KyP zTH4yqDLS2OL)}1deo&BIWJ7e@mwgaHa18cA6voDe7u<%MABK)8Q&1S zv2JeF>FmX<-P)$P=cGx_)&EIqmu*dxv}r$#9(Xu!(l_t_JeT)*e?5X>7?|rz=-iR^J!JNx_lAF8y3J+#fV zM`|!Z`4tN7n@Xgoy}f;z!{M0Kp3pQ6fj}Tck*0awjPovXY-nhxUs049 zFQ)4{1_yIFKUPfS=!8%%k(FL%0hj_5I#8JOsls)bllPcHW&$e};BO97V0Sq=+0Uo> zr4nzFUPV~rGvcmtaxyR6HMxt;Dl_f>&xOYC|KS;KWRn++bm)77-PYiD zQCjm<^RKs()x|JQa#yndB_sz9R+qadFv(q+@OKcj7_1fnQsAA+xa+#a7v-+8Q)}V$ zcp<=wWH5l1HC;%JXUpTR0+FKJB@Mm{IrJGscOwiv%lWbwJP!Xqi!H<5yrPDvYcG(bWcrU4;}Qg`9Y!VqHF+Ld`ihB%43|c4!cZb-pu~EZBMUSo3q>)|vEgL- z+|{u111RGH(&1qU_FG^O6bwzz3FPi-er)yJg^UzKc07&F$4+u?UHROlX#$$={}5Tp z42MFG{9JfKz%<>&C3{XNol2sE0v55Ma_-V0!ri(VnfMrI)nE07V&Gk{H_=-OHU2fQ zx>_pZE>4={E=3yP?&cHA3D%*DPus~5-AT{tnU@&L+rmm5D9K&6>9|Wt2Do21#Uip) zJCRMqnB*iJ&Q@4$I<%~eZ1hS=?y^nCU219+PNxUb=w)OQak$#o!EW~=(sKefyA4ja z7fK>rlDnpD^=c-9=%o;9Z{3R4wYw$}scF?-gu0JoAz#DBNkmKMcL^+qs21OV=(4KfE}1aEE@L-{~Z?$dLVZbZ+1d?>z|AHs{lLxd+&B z7E-#3aEDXuix)oOvDC^QtNrXw?hJBOeG1K;x5$WLQ`v&ZuxMLOthVLRq| zFU9F^8-)VEIFDtwpJ&2SVDSUMvh~nvTS~(nzIqDKxsa=l;o;)7TtKHc@ez{W5I}kX zfev*PBB6j3iA&wUMMmnI{lLq;WhDNIj~NmxkcrqG`;9N>A6-Q5+uRNL|;D~XGZG9-2FIdX(PA)sh^F`Z-ovb={$dcLO{dX2Y&mud#X zv+Zlfp9f-wp{Zfq$-sF}Cd`bq`MN3*qQGewo~9YRR3ipqIS6=d(rIKzhvA&nOEuDAu8j;`1pYe34Y(3mx}M{+K!D%L8M1PGmM_)Jr1r8* z?|z(+GzMeaaTre?0^UwSUbh#KfgsXSW|lA2HonHAYD-@4I=J(#-GN0Lb|TgF5&!#I ifV%}Gn&SFTfB^tvNSP&xDbDl&0000!2jd}d{GTMM4lbfQ@GdVda+U=&*2*nyTIp!K#os)6^cG|INU9g?ht4EXs+Pb zQpLCA2d~e=aD{VhkG?wm5z{hbmU4OTAaiWXmMs@&NKP^QT(5SR;V93EtS>VJn@-iT xJ@VetEtwLZ&y*CeW0=f!q@LX%je&`WL27s4Ez20!xj?@$c)I$ztaD0e0sxUNY#smr diff --git a/generators/ajaxful_rating/templates/migration.rb b/generators/ajaxful_rating/templates/migration.rb deleted file mode 100644 index d7a5277..0000000 --- a/generators/ajaxful_rating/templates/migration.rb +++ /dev/null @@ -1,18 +0,0 @@ -class CreateRates < ActiveRecord::Migration - def self.up - create_table :rates do |t| - t.belongs_to :rater - t.belongs_to :rateable, :polymorphic => true - t.integer :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 diff --git a/generators/ajaxful_rating/templates/model.rb b/generators/ajaxful_rating/templates/model.rb deleted file mode 100644 index 885edc7..0000000 --- a/generators/ajaxful_rating/templates/model.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Rate < ActiveRecord::Base - belongs_to :rater, :class_name => "<%= file_name.classify %>" - belongs_to :rateable, :polymorphic => true - - attr_accessible :rate, :dimension -end diff --git a/generators/ajaxful_rating/templates/style.css b/generators/ajaxful_rating/templates/style.css deleted file mode 100644 index d12c52d..0000000 --- a/generators/ajaxful_rating/templates/style.css +++ /dev/null @@ -1,84 +0,0 @@ -/* -* 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; -} From f67a233f9c77cb22b6af95618067a7eaed0d3c4e Mon Sep 17 00:00:00 2001 From: edgarjs Date: Mon, 3 May 2010 10:52:19 -0500 Subject: [PATCH 10/22] Change gem manager to jeweler --- .gitignore | 2 +- Rakefile | 27 +++++++++++------- ajaxful_rating.gemspec | 62 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 71 insertions(+), 20 deletions(-) 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/Rakefile b/Rakefile index 22d20e5..00383b4 100644 --- a/Rakefile +++ b/Rakefile @@ -1,14 +1,21 @@ require 'rubygems' require 'rake' -require 'echoe' -Echoe.new('ajaxful_rating', '2.2.3') do |p| - p.description = "Provides a simple way to add rating functionality to your application." - p.url = "http://github.com/edgarjs/ajaxful-rating" - p.author = "Edgar J. Suarez" - p.email = "edgar.js@gmail.com" - p.ignore_pattern = ["tmp/*", "script/*"] - p.development_dependencies = [] +begin + require 'jeweler' + + Jeweler::Tasks.new do |spec| + spec.name = "ajaxful_rating" + spec.version = "3.0.0.beta1" + 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 - -Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext } diff --git a/ajaxful_rating.gemspec b/ajaxful_rating.gemspec index be82015..1e78a93 100644 --- a/ajaxful_rating.gemspec +++ b/ajaxful_rating.gemspec @@ -1,22 +1,65 @@ +# 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} - s.version = "2.2.3" + s.version = "3.0.0.beta1" - s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version= - s.authors = ["Edgar J. Suarez"] - s.date = %q{2010-03-10} + s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= + s.authors = ["Edgar J. Suarez", "Denis Odorcic"] + s.date = %q{2010-05-03} s.description = %q{Provides a simple way to add rating functionality to your application.} - s.email = %q{edgar.js@gmail.com} - s.extra_rdoc_files = ["CHANGELOG", "README.textile", "lib/ajaxful_rating.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", "generators/ajaxful_rating/USAGE", "generators/ajaxful_rating/ajaxful_rating_generator.rb", "generators/ajaxful_rating/templates/images/star.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.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", "ajaxful_rating.gemspec"] + s.email = ["edgar.js@gmail.com", "denis.odorcic@gmail.com"] + s.extra_rdoc_files = [ + "ChangeLog", + "README.textile" + ] + s.files = [ + ".gitignore", + "CHANGELOG", + "Manifest", + "README.textile", + "Rakefile", + "ajaxful_rating.gemspec", + "lib/ajaxful_rating.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/edgarjs/ajaxful-rating} - s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ajaxful_rating", "--main", "README.textile"] + s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] - s.rubyforge_project = %q{ajaxful_rating} s.rubygems_version = %q{1.3.6} 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 +71,4 @@ Gem::Specification.new do |s| else end end + From fa5a50ee3900f427379964350763011d2a07af49 Mon Sep 17 00:00:00 2001 From: msaffitz Date: Sun, 8 Aug 2010 23:08:20 +0800 Subject: [PATCH 11/22] fixing singular_class_name to dom_class for rails3.0.0.rc --- lib/axr/stars_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index c049be3..f909d3d 100644 --- a/lib/axr/stars_builder.rb +++ b/lib/axr/stars_builder.rb @@ -44,7 +44,7 @@ def apply_stars_builder_options!(options, remote_options) }.merge(remote_options) if @remote_options[:url].nil? - rateable_name = ActionController::RecordIdentifier.singular_class_name(rateable) + rateable_name = ActionController::RecordIdentifier.dom_class(rateable) url = "rate_#{rateable_name}_path" if @template.respond_to?(url) @remote_options[:url] = @template.send(url, rateable) From e2a6408c645c30d48b3e27ed28fa6cc449aee152 Mon Sep 17 00:00:00 2001 From: msaffitz Date: Mon, 9 Aug 2010 05:50:23 +0800 Subject: [PATCH 12/22] replacing returning with tap --- lib/axr/model.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/axr/model.rb b/lib/axr/model.rb index a7ed61f..d28177d 100644 --- a/lib/axr/model.rb +++ b/lib/axr/model.rb @@ -76,7 +76,7 @@ def rate(stars, user, dimension = nil) 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 From 85221d1e3aa6f6991b07c692c736c1f2877dd95e Mon Sep 17 00:00:00 2001 From: "@edgarjs" Date: Sun, 8 Aug 2010 21:37:03 -0500 Subject: [PATCH 13/22] bumping beta2 --- CHANGELOG | 5 ++++- Rakefile | 2 +- ajaxful_rating.gemspec | 11 +++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4333538..33127b8 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.3 March 10, 2010 * Fix validation of current_user to show linked stars @@ -30,4 +33,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/Rakefile b/Rakefile index 00383b4..9a32399 100644 --- a/Rakefile +++ b/Rakefile @@ -6,7 +6,7 @@ begin Jeweler::Tasks.new do |spec| spec.name = "ajaxful_rating" - spec.version = "3.0.0.beta1" + spec.version = "3.0.0.beta2" 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" diff --git a/ajaxful_rating.gemspec b/ajaxful_rating.gemspec index 1e78a93..cf32df1 100644 --- a/ajaxful_rating.gemspec +++ b/ajaxful_rating.gemspec @@ -5,16 +5,15 @@ Gem::Specification.new do |s| s.name = %q{ajaxful_rating} - s.version = "3.0.0.beta1" + s.version = "3.0.0.beta2" s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= s.authors = ["Edgar J. Suarez", "Denis Odorcic"] - s.date = %q{2010-05-03} + s.date = %q{2010-08-08} s.description = %q{Provides a simple way to add rating functionality to your application.} s.email = ["edgar.js@gmail.com", "denis.odorcic@gmail.com"] s.extra_rdoc_files = [ - "ChangeLog", - "README.textile" + "README.textile" ] s.files = [ ".gitignore", @@ -50,7 +49,7 @@ Gem::Specification.new do |s| s.homepage = %q{http://github.com/edgarjs/ajaxful-rating} s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.6} + s.rubygems_version = %q{1.3.7} s.summary = %q{Provides a simple way to add rating functionality to your application.} s.test_files = [ "test/fixtures/car.rb", @@ -65,7 +64,7 @@ Gem::Specification.new do |s| current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 3 - if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then else end else From 81b563ef4e4f7937b769ea64b589d990dc833b43 Mon Sep 17 00:00:00 2001 From: "@edgarjs" Date: Thu, 14 Oct 2010 16:45:11 -0500 Subject: [PATCH 14/22] Changing I18n keys --- Rakefile | 2 +- ajaxful_rating.gemspec | 4 ++-- lib/axr/helpers.rb | 6 +++--- lib/axr/locale.rb | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Rakefile b/Rakefile index 9a32399..e32d1f9 100644 --- a/Rakefile +++ b/Rakefile @@ -6,7 +6,7 @@ begin Jeweler::Tasks.new do |spec| spec.name = "ajaxful_rating" - spec.version = "3.0.0.beta2" + 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" diff --git a/ajaxful_rating.gemspec b/ajaxful_rating.gemspec index cf32df1..b4bd166 100644 --- a/ajaxful_rating.gemspec +++ b/ajaxful_rating.gemspec @@ -5,11 +5,11 @@ Gem::Specification.new do |s| s.name = %q{ajaxful_rating} - s.version = "3.0.0.beta2" + s.version = "3.0.0.beta3" s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= s.authors = ["Edgar J. Suarez", "Denis Odorcic"] - s.date = %q{2010-08-08} + s.date = %q{2010-10-14} s.description = %q{Provides a simple way to add rating functionality to your application.} s.email = ["edgar.js@gmail.com", "denis.odorcic@gmail.com"] s.extra_rdoc_files = [ diff --git a/lib/axr/helpers.rb b/lib/axr/helpers.rb index 9382e5c..b78b68a 100644 --- a/lib/axr/helpers.rb +++ b/lib/axr/helpers.rb @@ -69,9 +69,9 @@ def ajaxful_rating_style # # 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!.to_hash.symbolize_keys.slice(:small, :remote_options, 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) From 0715118ba6db96abda8dd3e76b1064c5679991f3 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Thu, 23 Jun 2011 18:52:07 +0100 Subject: [PATCH 15/22] Fixed the rest of the bugs with the merge. --- lib/axr/stars_builder.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index cafc7a7..6011b03 100644 --- a/lib/axr/stars_builder.rb +++ b/lib/axr/stars_builder.rb @@ -2,12 +2,12 @@ module AjaxfulRating # :nodoc: class StarsBuilder # :nodoc: include AjaxfulRating::Locale - attr_reader :rateable, :user, :options + attr_reader :rateable, :user, :options, :remote_options - def initialize(rateable, user_or_static, template, css_builder, 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 @@ -25,7 +25,7 @@ def render private - def apply_stars_builder_options!(options) + def apply_stars_builder_options!(options, remote_options) @options = { :url => nil, :method => :post, @@ -48,7 +48,7 @@ def apply_stars_builder_options!(options) rateable_name = ActionController::RecordIdentifier.dom_class(rateable) url = "rate_#{rateable_name}_path" if @template.respond_to?(url) - @options[:url] = @template.send(url, rateable) + @remote_options[:url] = @template.send(url, rateable) else raise(Errors::MissingRateRoute) end From b033a006fa8b23454ca5f3321dbd68f6975d8bfa Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 24 Jun 2011 15:10:56 +0100 Subject: [PATCH 16/22] Updated README file. --- README.textile | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/README.textile b/README.textile index 65de561..2805008 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 From 446407d237c73e1e1ebafd84d1f15c8e1685ed47 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 24 Jun 2011 15:12:19 +0100 Subject: [PATCH 17/22] Minor fix to README. --- README.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.textile b/README.textile index 2805008..506d646 100644 --- a/README.textile +++ b/README.textile @@ -27,11 +27,11 @@ h3. Install To install the gem add it to your Gemfile: - gem 'ajaxful_rating_jquery', :git => 'git@github.com:danbee/ajaxful_rating_jquery.git' + @gem 'ajaxful_rating_jquery', :git => 'git@github.com:danbee/ajaxful_rating_jquery.git' h3. Generate - rails g 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. From 6301549a81d39a08d51a1d1a5342257e28e0e43b Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 24 Jun 2011 15:13:10 +0100 Subject: [PATCH 18/22] Minor fix to README. --- README.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.textile b/README.textile index 506d646..53e5508 100644 --- a/README.textile +++ b/README.textile @@ -27,7 +27,7 @@ h3. Install To install the gem add it to your Gemfile: - @gem 'ajaxful_rating_jquery', :git => 'git@github.com:danbee/ajaxful_rating_jquery.git' +
gem 'ajaxful_rating_jquery', :git => 'git@github.com:danbee/ajaxful_rating_jquery.git'
h3. Generate From 89ecdabecbea2bb955bd68bd594c0dc4442b6ee1 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 24 Jun 2011 15:19:08 +0100 Subject: [PATCH 19/22] Updated gemspec file. --- ajaxful_rating_jquery.gemspec | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ajaxful_rating_jquery.gemspec b/ajaxful_rating_jquery.gemspec index 084787e..e64f77f 100644 --- a/ajaxful_rating_jquery.gemspec +++ b/ajaxful_rating_jquery.gemspec @@ -8,10 +8,10 @@ Gem::Specification.new do |s| s.version = "3.0.0.beta3" s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= - s.authors = ["Edgar J. Suarez", "Denis Odorcic"] - s.date = %q{2010-10-14} + 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"] + s.email = ["edgar.js@gmail.com", "denis.odorcic@gmail.com", "danbee@gmail.com"] s.extra_rdoc_files = [ "README.textile" ] @@ -21,8 +21,8 @@ Gem::Specification.new do |s| "Manifest", "README.textile", "Rakefile", - "ajaxful_rating.gemspec", - "lib/ajaxful_rating.rb", + "ajaxful_rating_jquery.gemspec", + "lib/ajaxful_rating_jquery.rb", "lib/axr/css_builder.rb", "lib/axr/errors.rb", "lib/axr/helpers.rb", From db437210863c126fdbc220ef518381665656f592 Mon Sep 17 00:00:00 2001 From: Russell Osborne Date: Mon, 27 Jun 2011 23:09:21 -0400 Subject: [PATCH 20/22] Use rails.js to submit action, fix in the get paramets to ensure size is passed, Works in 3.07, without dimensons --- lib/axr/helpers.rb | 43 ++-------- lib/axr/model.rb | 2 +- lib/axr/stars_builder.rb | 173 ++++++++++++++++++++------------------- 3 files changed, 97 insertions(+), 121 deletions(-) diff --git a/lib/axr/helpers.rb b/lib/axr/helpers.rb index 2de039a..ce10498 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 diff --git a/lib/axr/model.rb b/lib/axr/model.rb index d28177d..8cb5534 100644 --- a/lib/axr/model.rb +++ b/lib/axr/model.rb @@ -87,7 +87,7 @@ def rate(stars, user, dimension = nil) # Builds the DOM id attribute for the wrapper in view. def wrapper_dom_id(options = {}) - options = options.to_hash.symbolize_keys.slice(:small, :dimension) + options = options.to_hash.symbolize_keys.slice(:size, :dimension) options = options.map do |k, v| if k == :dimension v.to_s diff --git a/lib/axr/stars_builder.rb b/lib/axr/stars_builder.rb index 6011b03..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, :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, remote_options) end - + def show_value if options[:show_user_rating] rate = rateable.rate_by(user, options[:dimension]) if user @@ -18,13 +18,13 @@ 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, remote_options) @options = { :url => nil, @@ -34,85 +34,90 @@ def apply_stars_builder_options!(options, remote_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' - - @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) + }.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 - @template.content_tag(:ul, stars.join.html_safe, :class => "ajaxful-rating#{' small' if options[:small]}") - 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 - end - end - def link_star_tag(value, css_class) - query = { - :stars => value, - :dimension => options[:dimension], - :small => options[:small], - :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 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 + }) - def wrapper_tag - @template.content_tag(:div, ratings_tag, :class => "ajaxful-rating-wrapper", - :id => rateable.wrapper_dom_id(options)) - end - end -end + @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 From 33889d9b491842f74f510e942fce908122eb7428 Mon Sep 17 00:00:00 2001 From: Sylvain Desbureaux Date: Fri, 1 Jul 2011 14:05:59 +0200 Subject: [PATCH 21/22] add of .rvmrc --- .rvmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .rvmrc 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 From 1462c328ae898865da0412c2419638d1b8a0e75b Mon Sep 17 00:00:00 2001 From: Sylvain Desbureaux Date: Fri, 1 Jul 2011 22:25:26 +0200 Subject: [PATCH 22/22] issue with size options in the helper --- lib/axr/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/axr/helpers.rb b/lib/axr/helpers.rb index ce10498..450b7af 100644 --- a/lib/axr/helpers.rb +++ b/lib/axr/helpers.rb @@ -107,7 +107,7 @@ def ajaxful_rating_script_prototype # hover: "Rate %{value} out of %{max}" def ratings_for(*args) def ratings_for(*args) @axr_css ||= CSSBuilder.new - options = args.extract_options!.to_hash.symbolize_keys.slice(:small, :remote_options, + 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) || {}