simple_recommender 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_recommender/recommendable.rb +33 -42
- data/lib/simple_recommender/version.rb +1 -1
- data/test/dummy/app/models/book.rb +3 -1
- data/test/dummy/log/test.log +1037 -0
- data/test/dummy/test/models/book_test.rb +2 -51
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2cb3b1f905c25ef005cea8efb18ac33fd8247f6
|
4
|
+
data.tar.gz: 9bd804d22da171f530d108cd5f8f38ebe5c219b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 373fd505ef35d570b7b36db8bbfdae06b96462a411ee8721fcf763adf528d2983f5e411b93babb987cdffa71627368166d20e3a8e841aaae61c44313acbc426c
|
7
|
+
data.tar.gz: 0d537aadd70ad398f1e28d61cc282f74d202750f10f07e9b6f965fe5aaf3e7a8126c7c2ff7f8330836cfa32632c938eabd64909bf9f1cbbfe7a877511fe5057a
|
@@ -1,32 +1,46 @@
|
|
1
1
|
module SimpleRecommender
|
2
2
|
module Recommendable
|
3
3
|
extend ActiveSupport::Concern
|
4
|
+
DEFAULT_N_RESULTS = 10
|
5
|
+
SIMILARITY_KEY = "similarity" # todo: allow renaming to avoid conflicts
|
6
|
+
AssociationMetadata = Struct.new(:join_table, :foreign_key, :association_foreign_key)
|
4
7
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
self.class.find_by_sql(query)
|
8
|
+
module ClassMethods
|
9
|
+
def similar_by(association_name)
|
10
|
+
define_method :similar_items do |n_results: DEFAULT_N_RESULTS|
|
11
|
+
query = similar_query(
|
12
|
+
association_name: association_name,
|
13
|
+
n_results: n_results
|
14
|
+
)
|
15
|
+
self.class.find_by_sql(query)
|
16
|
+
end
|
16
17
|
end
|
18
|
+
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
self.reflect_on_all_associations.each do |association|
|
21
|
-
association_name = association.name
|
20
|
+
included do
|
21
|
+
private
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
# Returns database metadata about an association based on its type,
|
24
|
+
# used in constructing a similarity query based on that association
|
25
|
+
def association_metadata(reflection)
|
26
|
+
case reflection
|
27
|
+
when ActiveRecord::Reflection::HasAndBelongsToManyReflection
|
28
|
+
AssociationMetadata.new(
|
29
|
+
reflection.join_table,
|
30
|
+
reflection.foreign_key,
|
31
|
+
reflection.association_foreign_key
|
32
|
+
)
|
33
|
+
when ActiveRecord::Reflection::ThroughReflection
|
34
|
+
AssociationMetadata.new(
|
35
|
+
reflection.through_reflection.table_name,
|
36
|
+
reflection.through_reflection.foreign_key,
|
37
|
+
reflection.association_foreign_key
|
38
|
+
)
|
39
|
+
else
|
40
|
+
raise ArgumentError, "Association '#{reflection.name}' is not a supported type"
|
25
41
|
end
|
26
42
|
end
|
27
43
|
|
28
|
-
private
|
29
|
-
|
30
44
|
# Returns a Postgres query that can be executed to return similar items.
|
31
45
|
# Reflects on the association to get relevant table names, and then
|
32
46
|
# uses Postgres's integer array intersection/union operators to
|
@@ -63,29 +77,6 @@ module SimpleRecommender
|
|
63
77
|
ORDER BY similarity DESC;
|
64
78
|
SQL
|
65
79
|
end
|
66
|
-
|
67
|
-
# Returns database metadata about an association based on its type,
|
68
|
-
# used in constructing a similarity query based on that association
|
69
|
-
def association_metadata(reflection)
|
70
|
-
case reflection
|
71
|
-
when ActiveRecord::Reflection::HasAndBelongsToManyReflection
|
72
|
-
AssociationMetadata.new(
|
73
|
-
reflection.join_table,
|
74
|
-
reflection.foreign_key,
|
75
|
-
reflection.association_foreign_key
|
76
|
-
)
|
77
|
-
when ActiveRecord::Reflection::ThroughReflection
|
78
|
-
AssociationMetadata.new(
|
79
|
-
reflection.through_reflection.table_name,
|
80
|
-
reflection.through_reflection.foreign_key,
|
81
|
-
reflection.association_foreign_key
|
82
|
-
)
|
83
|
-
else
|
84
|
-
raise ArgumentError, "Association '#{reflection.name}' is not a supported type"
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
AssociationMetadata = Struct.new(:join_table, :foreign_key, :association_foreign_key)
|
89
80
|
end
|
90
81
|
end
|
91
82
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
class Book < ActiveRecord::Base
|
2
|
+
include SimpleRecommender::Recommendable
|
3
|
+
|
2
4
|
has_and_belongs_to_many :tags
|
3
5
|
|
4
6
|
has_many :likes
|
@@ -6,5 +8,5 @@ class Book < ActiveRecord::Base
|
|
6
8
|
|
7
9
|
belongs_to :author, class_name: "User"
|
8
10
|
|
9
|
-
|
11
|
+
similar_by :users
|
10
12
|
end
|