Module: AjaxfulRating::SingletonMethods

Defined in:
lib/ajaxful_rating_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

Returns:

  • (Boolean)


208
209
210
# File 'lib/ajaxful_rating_model.rb', line 208

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.



213
214
215
216
217
# File 'lib/ajaxful_rating_model.rb', line 213

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

Finds the rateable object with the lowest rate average.



183
184
185
# File 'lib/ajaxful_rating_model.rb', line 183

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.



178
179
180
# File 'lib/ajaxful_rating_model.rb', line 178

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

#find_rated_by(user) ⇒ Object

Finds all rateable objects rated by the user.



168
169
170
# File 'lib/ajaxful_rating_model.rb', line 168

def find_rated_by(user)
  find_statement(:user_id, user.id)
end

#find_rated_with(stars) ⇒ Object

Finds all rateable objects rated with stars.



173
174
175
# File 'lib/ajaxful_rating_model.rb', line 173

def find_rated_with(stars)
  find_statement(:stars, stars)
end

#find_statement(attr_name, attr_value) ⇒ Object

Finds rateable objects by Rate’s attribute.



188
189
190
191
192
193
194
195
# File 'lib/ajaxful_rating_model.rb', line 188

def find_statement(attr_name, attr_value)
  rateable = self.base_class.name
  sql = sanitize_sql(["SELECT DISTINCT r2.* FROM rates r1 INNER JOIN " +
        "#{rateable.constantize.table_name} r2 ON r1.rateable_id = r2.id " +
        "WHERE (r1.[rateable_type] = ? AND r1.[#{attr_name}] = ?)",
      rateable, attr_value])
  find_by_sql(sql)
end

#user_class_nameObject

Name of the class for the user model.



160
161
162
163
164
165
# File 'lib/ajaxful_rating_model.rb', line 160

def user_class_name
  @@user_class_name ||= Rate.column_names.find do |c|
    u = c.scan(/(\w+)_id$/).flatten.first
    break u if u && u != 'rateable'
  end
end