Module: AjaxfulRating::InstanceMethods

Defined in:
lib/ajaxful_rating_model.rb

Overview

Instance methods for the rateable object.

Instance Method Summary collapse

Instance Method Details

#caching_column_name(dimension = nil) ⇒ Object

Returns the name of the cache column for the passed dimension.



143
144
145
# File 'lib/ajaxful_rating_model.rb', line 143

def caching_column_name(dimension = nil)
  self.class.caching_column_name(dimension)
end

#rate(stars, user, dimension = nil) ⇒ Object

Submits a new rate. Accepts a hash of tipical Ajax request.

Example:

# Articles Controller
def rate
  @article = Article.find(params[:id])
  @article.rate(params[:stars], current_user, params[:dimension])
  # some page update here ...
end

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ajaxful_rating_model.rb', line 72

def rate(stars, user, dimension = nil)
  return false if (stars.to_i > self.class.max_rate_value)
  raise AlreadyRatedError if (!self.class.options[:allow_update] && rated_by?(user, dimension))

  rate = (self.class.options[:allow_update] && rated_by?(user, dimension)) ?
    rate_by(user, dimension) : rates(dimension).build
  rate.stars = stars
  if user.respond_to?(:rates)
    user.rates << rate
  else
    rate.send "#{self.class.user_class_name}_id=", user.id
  end if rate.new_record?
  rate.save!
  self.update_cached_average(dimension)
end

#rate_average(cached = true, dimension = nil) ⇒ Object

Rating average for the object.

Pass false as param to force the calculation if you are caching it.



121
122
123
124
125
126
127
128
# File 'lib/ajaxful_rating_model.rb', line 121

def rate_average(cached = true, dimension = nil)
  avg = if cached && self.class.caching_average?(dimension)
    send(caching_column_name(dimension)).to_f
  else
    self.rates_sum(dimension).to_f / self.total_rates(dimension).to_f
  end
  avg.nan? ? 0.0 : avg
end

#rate_by(user, dimension = nil) ⇒ Object

Finds the rate made by the user if he/she has already voted.



98
99
100
101
# File 'lib/ajaxful_rating_model.rb', line 98

def rate_by(user, dimension = nil)
  filter = "find_by_#{self.class.user_class_name}_id"
  rates(dimension).send filter, user
end

#rated_by?(user, dimension = nil) ⇒ Boolean

Return true if the user has rated the object, otherwise false

Returns:

  • (Boolean)


104
105
106
# File 'lib/ajaxful_rating_model.rb', line 104

def rated_by?(user, dimension = nil)
  !rate_by(user, dimension).nil?
end

#ratersObject

Returns an array with all users that have rated this object.



89
90
91
92
93
94
95
# File 'lib/ajaxful_rating_model.rb', line 89

def raters
  eval(self.class.user_class_name.classify).find_by_sql(
    ["SELECT DISTINCT u.* FROM #{self.class.user_class_name.pluralize} u INNER JOIN rates r ON " +
        "u.[id] = r.[#{self.class.user_class_name}_id] WHERE r.[rateable_id] = ? AND r.[rateable_type] = ?",
      id, self.class.name]
  )
end

#rates(dimension = nil) ⇒ Object

Overrides the default rates method and returns the propper array for the dimension passed.

It may works as an alias for dimension_rates methods.



134
135
136
137
138
139
140
# File 'lib/ajaxful_rating_model.rb', line 134

def rates(dimension = nil)
  unless dimension.blank?
    send("#{dimension}_rates")
  else
    rates_without_dimension
  end
end

#rates_sum(dimension = nil) ⇒ Object

Total sum of the rates.



114
115
116
# File 'lib/ajaxful_rating_model.rb', line 114

def rates_sum(dimension = nil)
  rates(dimension).sum(:stars)
end

#total_rates(dimension = nil) ⇒ Object

Instance’s total rates.



109
110
111
# File 'lib/ajaxful_rating_model.rb', line 109

def total_rates(dimension = nil)
  rates(dimension).size
end

#update_cached_average(dimension = nil) ⇒ Object

Updates the cached average column in the rateable model.



148
149
150
151
152
153
154
# File 'lib/ajaxful_rating_model.rb', line 148

def update_cached_average(dimension = nil)
  if self.class.caching_average?(dimension)
    rates(:refresh).size if self.respond_to?(:rates_count)
    send("#{caching_column_name(dimension)}=", self.rate_average(false, dimension))
    save!
  end
end