Module: AjaxfulRating::InstanceMethods

Defined in:
lib/axr/model.rb

Overview

Instance methods for the rateable object.

Instance Method Summary collapse

Instance Method Details

#axr_configObject

Proxy for axr_config singleton method.



59
60
61
# File 'lib/axr/model.rb', line 59

def axr_config
  self.class.axr_config
end

#caching_column_name(dimension = nil) ⇒ Object

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

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

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.

Returns:

  • (Boolean)


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

#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/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

#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.



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

#rate_by(user, dimension = nil) ⇒ Object

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

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

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

Returns:

  • (Boolean)


125
126
127
# File 'lib/axr/model.rb', line 125

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

#raters(dimension = nil) ⇒ Object

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

#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.



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

#rates_sum(dimension = nil) ⇒ Object

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

#total_rates(dimension = nil) ⇒ Object

Instance's total rates.



137
138
139
# File 'lib/axr/model.rb', line 137

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

#update_cached_average(dimension = nil) ⇒ Object

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

#wrapper_dom_id(options = {}) ⇒ Object

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(options = {})
  options = options.symbolize_keys.slice(:size, :dimension)
  options = options.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
  options.unshift("ajaxful_rating")
  ApplicationController.helpers.dom_id(self, options.sort.join('_'))
end