Module: AjaxfulRating::InstanceMethods
- Defined in:
- lib/axr/model.rb
Overview
Instance methods for the rateable object.
Instance Method Summary (collapse)
-
- (Object) axr_config
Proxy for axr_config singleton method.
-
- (Object) caching_column_name(dimension = nil)
Returns the name of the cache column for the passed dimension.
-
- (Boolean) can_rate_by?(user, dimension = nil)
Returns whether or not the user can rate this object.
-
- (Object) rate(stars, user, dimension = nil)
Submits a new rate.
-
- (Object) rate_average(cached = true, dimension = nil)
Rating average for the object.
-
- (Object) rate_by(user, dimension = nil)
Finds the rate made by the user if he/she has already voted.
-
- (Boolean) rated_by?(user, dimension = nil)
Return true if the user has rated the object, otherwise false.
-
- (Object) raters(dimension = nil)
Returns an array with the users that have rated this object for the passed dimension.
-
- (Object) rates(dimension = nil)
Overrides the default rates method and returns the propper array for the dimension passed.
-
- (Object) rates_sum(dimension = nil)
Total sum of the rates.
-
- (Object) total_rates(dimension = nil)
Instance's total rates.
-
- (Object) update_cached_average(dimension = nil)
Updates the cached average column in the rateable model.
-
- (Object) wrapper_dom_id(options = {})
Builds the DOM id attribute for the wrapper in view.
Instance Method Details
- (Object) axr_config
Proxy for axr_config singleton method.
59 60 61 |
# File 'lib/axr/model.rb', line 59 def axr_config self.class.axr_config end |
- (Object) caching_column_name(dimension = nil)
Returns the name of the cache column for the passed dimension.
171 172 173 |
# File 'lib/axr/model.rb', line 171 def caching_column_name(dimension = nil) self.class.caching_column_name(dimension) end |
- (Boolean) can_rate_by?(user, dimension = nil)
Returns whether or not the user can rate this object. Based on if the user has already rated the object or the :allow_update option is enabled.
132 133 134 |
# File 'lib/axr/model.rb', line 132 def can_rate_by?(user, dimension = nil) !rated_by?(user, dimension) || self.class.axr_config[:allow_update] end |
- (Object) rate(stars, user, dimension = nil)
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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/axr/model.rb', line 72 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)) rate = if self.class.axr_config[:allow_update] && rated_by?(user, dimension) rate_by(user, dimension) else returning rates(dimension).build do |r| r.rater = user end end rate.stars = stars rate.save! self.update_cached_average(dimension) end |
- (Object) rate_average(cached = true, dimension = nil)
Rating average for the object.
Pass false as param to force the calculation if you are caching it.
149 150 151 152 153 154 155 156 |
# File 'lib/axr/model.rb', line 149 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 |
- (Object) rate_by(user, dimension = nil)
Finds the rate made by the user if he/she has already voted.
120 121 122 |
# File 'lib/axr/model.rb', line 120 def rate_by(user, dimension = nil) rates(dimension).find_by_rater_id(user.id) end |
- (Boolean) rated_by?(user, dimension = nil)
Return true if the user has rated the object, otherwise false
125 126 127 |
# File 'lib/axr/model.rb', line 125 def rated_by?(user, dimension = nil) !rate_by(user, dimension).nil? end |
- (Object) raters(dimension = nil)
Returns an array with the users that have rated this object for the passed dimension.
It may works as an alias for dimension_raters methods.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/axr/model.rb', line 106 def raters(dimension = nil) sql = "SELECT DISTINCT u.* FROM #{self.class.user_class.table_name} u "\ "INNER JOIN rates r ON u.id = r.rater_id WHERE " sql << self.class.send(:sanitize_sql_for_conditions, { :rateable_id => id, :rateable_type => self.class.base_class.name, :dimension => (dimension.to_s if dimension) }, 'r') self.class.user_class.find_by_sql(sql) end |
- (Object) rates(dimension = nil)
Overrides the default rates method and returns the propper array for the dimension passed.
It may works as an alias for dimension_rates methods.
162 163 164 165 166 167 168 |
# File 'lib/axr/model.rb', line 162 def rates(dimension = nil) unless dimension.blank? send("#{dimension}_rates") else rates_without_dimension end end |
- (Object) rates_sum(dimension = nil)
Total sum of the rates.
142 143 144 |
# File 'lib/axr/model.rb', line 142 def rates_sum(dimension = nil) rates(dimension).sum(:stars) end |
- (Object) total_rates(dimension = nil)
Instance's total rates.
137 138 139 |
# File 'lib/axr/model.rb', line 137 def total_rates(dimension = nil) rates(dimension).size end |
- (Object) update_cached_average(dimension = nil)
Updates the cached average column in the rateable model.
176 177 178 179 180 |
# File 'lib/axr/model.rb', line 176 def update_cached_average(dimension = nil) if self.class.caching_average?(dimension) update_attribute caching_column_name(dimension), self.rate_average(false, dimension) end end |
- (Object) wrapper_dom_id(options = {})
Builds the DOM id attribute for the wrapper in view.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/axr/model.rb', line 89 def wrapper_dom_id( = {}) = .symbolize_keys.slice(:size, :dimension) = .select { |k, v| v.present? or (v == false) }.map do |k, v| if k == :dimension v.to_s else v.to_s == 'true' ? k.to_s : "no-#{k}" end end .unshift("ajaxful_rating") ApplicationController.helpers.dom_id(self, .sort.join('_')) end |