Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
Changed :force_dynamic for its opposite :force_static.
Browse files Browse the repository at this point in the history
Fixed I18n texts.
Fixed missed quotes for Rate belongs_to association with User class
Detailed wrapper DOM id with passed hash.
  • Loading branch information
edgarjs committed Mar 8, 2010
1 parent b7b1175 commit e0d780b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
== 2.2.1 March 8, 2010
* Changed :force_dynamic for its opposite :force_static.
* Fixed I18n texts.
* Fixed missed quotes for Rate belongs_to association with User class
* Detailed wrapper DOM id with passed hash.

== 2.2.0 March 7, 2010
* Improved model methods and options.
* Works now with inherited models.
Expand Down
14 changes: 5 additions & 9 deletions README.textile
Expand Up @@ -19,17 +19,13 @@ h2. Instructions

h3. Install

To install the plugin:
To install the gem run the next command:

@script/plugin install git://github.com/edgarjs/ajaxful-rating.git@

Or the gem

@sudo gem install edgarjs-ajaxful_rating --source=http://gems.github.com@
@gem install ajaxful_rating@

You can configure it in your environment.rb file also:

@config.gem "edgarjs-ajaxful_rating", :lib => "ajaxful_rating", :source => "http://gems.github.com"@
@config.gem "ajaxful_rating"@

h3. Generate

Expand Down Expand Up @@ -141,8 +137,8 @@ example (if you added the @rate@ route):
@car = Car.find(params[:id])
@car.rate(params[:stars], current_user, params[:dimension])
render :update do |page|
page.replace_html @car.wrapper_dom_id(params[:dimension]), ratings_for(@car, params.merge(:wrap => false))
page.visual_effect :highlight, @car.wrapper_dom_id(params[:dimension])
page.replace_html @car.wrapper_dom_id(params), ratings_for(@car, params.merge(:wrap => false))
page.visual_effect :highlight, @car.wrapper_dom_id(params)
end
end
</pre>
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -2,7 +2,7 @@ require 'rubygems'
require 'rake'
require 'echoe'

Echoe.new('ajaxful_rating', '2.2.0') do |p|
Echoe.new('ajaxful_rating', '2.2.1') 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"
Expand Down
4 changes: 2 additions & 2 deletions ajaxful_rating.gemspec
Expand Up @@ -2,11 +2,11 @@

Gem::Specification.new do |s|
s.name = %q{ajaxful_rating}
s.version = "2.2.0"
s.version = "2.2.1"

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-07}
s.date = %q{2010-03-08}
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"]
Expand Down
2 changes: 1 addition & 1 deletion generators/ajaxful_rating/templates/model.rb
@@ -1,5 +1,5 @@
class Rate < ActiveRecord::Base
belongs_to :rater, :class_name => <%= file_name.classify %>
belongs_to :rater, :class_name => "<%= file_name.classify %>"
belongs_to :rateable, :polymorphic => true

attr_accessible :rate, :dimension
Expand Down
6 changes: 4 additions & 2 deletions lib/axr/helpers.rb
Expand Up @@ -19,6 +19,7 @@ def ajaxful_rating_style
# * <tt>:wrap</tt> Whether the star list is wrapped within a div tag or not. This is useful when page updating. Default is true.
# * <tt>:show_user_rating</tt> Set to true if you want to display only the current user's rating, instead of the global average.
# * <tt>:dimension</tt> The dimension to show the ratings for.
# * <tt>:force_static</tt> Force static stars even when you're passing a user instance.
#
# Example:
# <%= ratings_for @article, :wrap => false %> # => Will produce something like:
Expand Down Expand Up @@ -68,12 +69,13 @@ def ajaxful_rating_style
#
# ajaxful_rating:
# helper:
# global_average: "Current rating average: {{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}}" def ratings_for(*args)
def ratings_for(*args)
@axr_css ||= CSSBuilder.new
options = args.extract_options!.symbolize_keys.slice(:small, :remote_options,
:wrap, :show_user_rating, :dimension)
:wrap, :show_user_rating, :dimension, :force_static)
remote_options = options.delete(:remote_options) || {}
rateable = args.shift
user = args.shift || (respond_to?(:current_user) ? current_user : raise(NoUserSpecified))
Expand Down
29 changes: 19 additions & 10 deletions lib/axr/locale.rb
Expand Up @@ -4,18 +4,27 @@ module AjaxfulRating # :nodoc:
#
# ajaxful_rating:
# helper:
# global_average: "Current rating average: {{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
def i18n(global = true)
key = "ajaxful_rating.helper.#{global ? 'global_average' : 'user_rating'}"
default =
if global
"Current rating average: {{value}} out of {{max}}"
else
"Your rating: {{value}} out of {{max}}"
end
I18n.t(key, :value => show_value, :max => rateable.class.max_stars, :default => default)

DEFAULTS = {
: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)
key = if key == :current
options[:show_user_rating] ? :user_rating : :global_average
else
key.to_sym
end
default = DEFAULTS[key]
key = "ajaxful_rating.helper.#{key}"
I18n.t(key, :value => (value || show_value),
:max => rateable.class.max_stars, :default => default)
end
end
end
12 changes: 10 additions & 2 deletions lib/axr/model.rb
Expand Up @@ -86,9 +86,17 @@ def rate(stars, user, dimension = nil)
end

# Builds the DOM id attribute for the wrapper in view.
def wrapper_dom_id(dimension = nil)
def wrapper_dom_id(options = {})
options = options.slice(:small, :wrap, :show_user_rating, :dimension, :force_static)
options = options.map do |k, v|
if k == :dimension
v
else
v.to_s == 'true' ? k.to_s : "no-#{k}"
end
end
prefix = "ajaxful_rating"
prefix << "_#{dimension}" unless dimension.blank?
prefix << "__#{options.join('_')}" unless options.empty?
ApplicationController.helpers.dom_id(self, prefix)
end

Expand Down
15 changes: 8 additions & 7 deletions lib/axr/stars_builder.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(rateable, user_or_static, template, css_builder, options = {}, re

def show_value
if options[:show_user_rating]
rate = rateable.rate_by(user, options[:dimension])
rate = rateable.rate_by(user, options[:dimension]) if user
rate ? rate.stars : 0
else
rateable.rate_average(true, options[:dimension])
Expand All @@ -29,7 +29,8 @@ def apply_stars_builder_options!(options, remote_options)
@options = {
:wrap => true,
:small => false,
:show_user_rating => false
:show_user_rating => false,
:force_static => false
}.merge(options)

@options[:small] = @options[:small].to_s == 'true'
Expand Down Expand Up @@ -60,7 +61,7 @@ def ratings_tag
@css_builder.rule('.ajaxful-rating.small',
:width => (rateable.class.max_stars * 10)) if options[:small]

stars << @template.content_tag(:li, i18n, :class => "show-value",
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)
Expand All @@ -77,10 +78,10 @@ def star_tag(value)
})

@template.content_tag(:li) do
if user && (!already_rated || rateable.axr_config[:allow_update])
if !options[:force_static] && (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)
@template.content_tag(:span, show_value, :class => css_class, :title => i18n(:current))
end
end
end
Expand All @@ -95,7 +96,7 @@ def link_star_tag(value, css_class)
config = {
:html => {
:class => css_class,
:title => i18n(false)
:title => i18n(:hover, value)
},
:url => "#{remote_options[:url]}",
:with => "'#{query}'"
Expand All @@ -105,7 +106,7 @@ def link_star_tag(value, css_class)

def wrapper_tag
@template.content_tag(:div, ratings_tag, :class => "ajaxful-rating-wrapper",
:id => rateable.wrapper_dom_id(options[:dimension]))
:id => rateable.wrapper_dom_id(options))
end
end
end

0 comments on commit e0d780b

Please sign in to comment.