Module: AjaxfulRating::SingletonMethods

Defined in:
lib/axr/model.rb

Instance Method Summary collapse

Instance Method Details

#caching_average?(dimension = nil) ⇒ Boolean

Indicates if the rateable model is able to cache the rate average.

Include a column named rating_average in your rateable model with default null, as decimal:

t.decimal :rating_average, :precision => 3, :scale => 1, :default => 0

To customize the name of the column specify the option :cache_column to ajaxful_rateable

ajaxful_rateable :cache_column => :my_custom_column


249
250
251
# File 'lib/axr/model.rb', line 249

def caching_average?(dimension = nil)
  column_names.include?(caching_column_name(dimension))
end

#caching_column_name(dimension = nil) ⇒ Object

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



254
255
256
257
258
# File 'lib/axr/model.rb', line 254

def caching_column_name(dimension = nil)
  name = axr_config[:cache_column].to_s
  name += "_#{dimension.to_s.underscore}" unless dimension.blank?
  name
end

Finds the rateable object with the lowest rate average.



220
221
222
# File 'lib/axr/model.rb', line 220

def find_less_popular(dimension = nil)
  all.sort_by { |o| o.rate_average(true, dimension) }.first
end

Finds the rateable object with the highest rate average.



215
216
217
# File 'lib/axr/model.rb', line 215

def find_most_popular(dimension = nil)
  all.sort_by { |o| o.rate_average(true, dimension) }.last
end

#find_rated_by(user, dimension = nil) ⇒ Object

Finds all rateable objects rated by the user.



205
206
207
# File 'lib/axr/model.rb', line 205

def find_rated_by(user, dimension = nil)
  find_statement(:rater_id, user.id, dimension)
end

#find_rated_with(stars, dimension = nil) ⇒ Object

Finds all rateable objects rated with stars.



210
211
212
# File 'lib/axr/model.rb', line 210

def find_rated_with(stars, dimension = nil)
  find_statement(:stars, stars, dimension)
end

#find_statement(attr_name, attr_value, dimension = nil) ⇒ Object

Finds rateable objects by Rate's attribute.



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/axr/model.rb', line 225

def find_statement(attr_name, attr_value, dimension = nil)
  sql = "SELECT DISTINCT r2.* FROM rates r1 INNER JOIN "\
    "#{self.base_class.table_name} r2 ON r1.rateable_id = r2.id WHERE "
  
  sql << sanitize_sql_for_conditions({
    :rateable_type => self.base_class.name,
    attr_name => attr_value,
    :dimension => (dimension.to_s if dimension)
  }, 'r1')
  
  find_by_sql(sql)
end

#max_starsObject

Maximum value accepted when rating the model. Default is 5.

Change it by passing the :stars option to ajaxful_rateable

ajaxful_rateable :stars => 10


190
191
192
# File 'lib/axr/model.rb', line 190

def max_stars
  axr_config[:stars]
end

#user_classObject

Gets the user's class



200
201
202
# File 'lib/axr/model.rb', line 200

def user_class
  user_class_name.constantize
end

#user_class_nameObject

Name of the class for the user model.



195
196
197
# File 'lib/axr/model.rb', line 195

def user_class_name
  Rate.reflect_on_association(:rater).options[:class_name]
end