|
1 | 1 | module RecommendationsHelper |
2 | | - MAX_USER_RECCOMMENDED_FUNCTIONS = 5 |
| 2 | + MAX_RECOMMENDED_FUNCTIONS = 5 |
3 | 3 |
|
4 | 4 | module RecommendationsPartialPath |
5 | 5 | def to_partial_path |
6 | 6 | 'recommendations/function' |
7 | 7 | end |
8 | 8 | end |
9 | 9 |
|
10 | | - def more_from_user(user) |
11 | | - most_liked = user.functions.order(likes_count: :desc).first |
12 | | - most_commented = user.functions.order(comments_count: :desc).first |
13 | | - most_saved = user.functions.order(saves_count: :desc).first |
| 10 | + def more_from_user_function(function) |
| 11 | + most_liked = user_most_function function.user, likes_count: :desc |
| 12 | + most_commented = user_most_function function.user, comments_count: :desc |
| 13 | + most_saved = user_most_function function.user, saves_count: :desc |
14 | 14 |
|
15 | | - [most_liked, most_saved, most_commented].uniq.map { |obj| obj.extend(RecommendationsPartialPath) } |
| 15 | + recommended_functions = filter_unwanted [most_liked, most_commented, most_saved], function |
| 16 | + override_partial_path recommended_functions, RecommendationsPartialPath |
| 17 | + end |
| 18 | + |
| 19 | + def similar_functions(function) |
| 20 | + popularity_query = generate_popularity_query |
| 21 | + |
| 22 | + popular_functions_from_tags = function.tags.map { |tag| tag.functions.order(popularity_query).first } |
| 23 | + popular_functions = Function.order(Arel.sql(popularity_query)) |
| 24 | + .limit(MAX_RECOMMENDED_FUNCTIONS - popular_functions_from_tags.count) |
| 25 | + |
| 26 | + recommended_functions = filter_unwanted [*popular_functions_from_tags, *popular_functions], function |
| 27 | + override_partial_path recommended_functions, RecommendationsPartialPath |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + def override_partial_path(functions, partial_path_module) |
| 33 | + functions.map { |obj| obj.extend(partial_path_module) } |
| 34 | + end |
| 35 | + |
| 36 | + def generate_popularity_query |
| 37 | + likes_wieght = 1 |
| 38 | + comments_weight = 0.5 |
| 39 | + saves_weight = 0.25 |
| 40 | + |
| 41 | + "( (likes_count * #{likes_wieght}) + (comments_count * #{comments_weight}) + (saves_count * #{saves_weight}) ) desc" |
| 42 | + end |
| 43 | + |
| 44 | + # Returns the most x function for a certain user |
| 45 | + # based on a criteria defined in `parameter`. |
| 46 | + # |
| 47 | + # @param user [User] |
| 48 | + # @param parameter [Hash] |
| 49 | + # @returns [Function] |
| 50 | + def user_most_function(user, parameter) |
| 51 | + user.functions.order(parameter).first |
| 52 | + end |
| 53 | + |
| 54 | + # Filters an array of Functions. It removes an `unwated_function` |
| 55 | + # and any `nil` variables in the `functions` array. |
| 56 | + # |
| 57 | + # @param functions [Array<Function>] |
| 58 | + # |
| 59 | + # @param unwanted_functon [Function] a function to be filtered |
| 60 | + # out of the Functions array. |
| 61 | + # |
| 62 | + # @returns [Array<Function>] |
| 63 | + def filter_unwanted(functions, unwanted_function) |
| 64 | + functions.reject { |func| func.nil? || func == unwanted_function }.uniq |
16 | 65 | end |
17 | 66 | end |
0 commit comments