Module: Recommendable::Rater::Recommender

Defined in:
lib/recommendable/rater/recommender.rb

Instance Method Summary collapse

Instance Method Details

Fetch a list of recommendations for a passed class.

Parameters:

  • klass (String, Symbol, Class)

    the class from which to get recommendations

  • limit (Fixnum) (defaults to: 10)

    the number of recommendations to fetch (defaults to 10)

Returns:

  • (Array)

    a list of things this person’s gonna love



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/recommendable/rater/recommender.rb', line 24

def recommended_for(klass, limit = 10, offset = 0)
  recommended_set = Recommendable::Helpers::RedisKeyMapper.recommended_set_for(klass, self.id)
  return Recommendable.query(klass, []) unless rated_anything? && Recommendable.redis.zcard(recommended_set) > 0

  ids = Recommendable.redis.zrevrange(recommended_set, 0, -1, :with_scores => true)
  ids = ids.select { |id, score| score > 0 }.map { |pair| pair.first }
  ids = sanitize_ids(ids, klass)

  order = ids.map { |id| "#{klass.quoted_table_name}.#{klass.quoted_primary_key} = ? DESC" }.join(', ')
  order = klass.send(:sanitize_sql_for_assignment, [order, *ids])
  Recommendable.query(klass, ids).order(order).limit(limit).offset(offset)
end

#similar_raters(limit = 10, offset = 0) ⇒ Array

Get a list of raters that have been found to be the most similar to self. They are sorted by the calculated similarity value.

Parameters:

  • limit (Fixnum) (defaults to: 10)

    the number of users to return (defaults to 10)

Returns:

  • (Array)

    An array of instances of your user class



9
10
11
12
13
14
15
16
17
# File 'lib/recommendable/rater/recommender.rb', line 9

def similar_raters(limit = 10, offset = 0)
  ids = Recommendable.redis.zrevrange(Recommendable::Helpers::RedisKeyMapper.similarity_set_for(id), 0, -1)
  ids = sanitize_ids(ids, self.class)

  order = ids.map { |id| "#{Recommendable.config.user_class.quoted_table_name}.#{Recommendable.config.user_class.quoted_primary_key} = ? DESC" }.join(', ')
  order = self.class.send(:sanitize_sql_for_assignment, [order, *ids])

  Recommendable.query(self.class, ids).order(order).limit(limit).offset(offset)
end