public
Rubygem
Description: Alpha software / experimental - A Ruby gem which adds simple item-to-item collaborative filtering to ActiveRecord-based apps.
Clone URL: git://github.com/dancroak/recommendable.git
Search Repo:
going the has_many_polymorphs route

git-svn-id: https://svn.thoughtbot.com/hackfest/recommendable@129 
c973d20d-6cf3-4494-8058-145efcf21f09
(no author) (author)
Mon Mar 31 22:41:19 -0700 2008
commit  d24ed8e25cb7ec3514500fbe3e9a3a253e4a8a87
tree    f6925517140a9e9bdd8cf12e90e9b11f6ead1bcb
parent  8fc5a3a7122de0afbcc336907fce6a429a9c47c0
...
1
2
3
4
5
6
7
8
9
 
10
11
12
...
1
2
3
 
 
 
 
 
 
4
5
6
7
0
@@ -1,12 +1,7 @@
0
 class RecommendableGenerator < Rails::Generator::NamedBase
0
   def manifest
0
     record do |m|
0
- m.migration_template 'migration.rb', 'db/migrate', :assigns => {
0
- :recommendable => table_name,
0
- :table_name => "similar_#{table_name}",
0
- :migration_name => "CreateSimilar#{class_name.pluralize.gsub(/::/, '')}"
0
- }, :migration_file_name => "create_similar_#{file_path.gsub(/\//, '_').pluralize}"
0
- model_args = ["Similar#{class_name}", "#{table_name}_one:integer","#{table_name}_two:integer", "score:decimal"]
0
+ m.migration_template 'migration.rb', 'db/migrate'
0
       m.dependency 'model', model_args, :collision => :skip, :skip_migration => true
0
       m.directory 'lib/tasks'
0
       m.template 'task.rake', 'lib/tasks/recommendable.rake'
...
1
 
2
3
4
5
 
 
 
 
6
7
8
9
10
11
12
 
 
 
 
13
14
15
16
 
17
18
...
 
1
2
 
 
 
3
4
5
6
7
8
9
 
 
 
 
10
11
12
13
14
15
16
 
17
18
19
0
@@ -1,19 +1,20 @@
0
-class <%= migration_name %> < ActiveRecord::Migration
0
+class CreateRecommendations < ActiveRecord::Migration
0
   def self.up
0
- create_table :<%= table_name %> do |t|
0
- t.integer :<%= recommendable.singularize %>_one
0
- t.integer :<%= recommendable.singularize %>_two
0
+ create_table :recommendations do |t|
0
+ t.integer :recommended_id
0
+ t.string :recommeded_type
0
+ t.integer :recommender_id
0
       t.decimal :score
0
     end
0
     
0
- add_index :<%= table_name %>, :<%= recommendable.singularize %>_one
0
- add_index :<%= table_name %>, :<%= recommendable.singularize %>_two
0
- add_index :<%= table_name %>, :score
0
- add_index :<%= table_name %>, [:<%= recommendable.singularize %>_one, :<%= recommendable.singularize %>_two]
0
+ add_index :recommendations, :recommended_id
0
+ add_index :recommendations, :recommender_id
0
+ add_index :recommendations, :score
0
+ add_index :recommendations, [:recommended_id, :recommeded_type]
0
   end
0
   
0
   def self.down
0
- drop_table :<%= table_name %>
0
+ drop_table :recommendations
0
   end
0
 end
...
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
0
@@ -1 +1,9 @@
0
+class Recommendation < ActiveRecord::Base
0
+
0
+ belongs_to :recommender, :class_name => "#{class_name}", :foreign_key => :recommender_id
0
+ belongs_to :recommended, :polymorphic => true
0
+
0
+ validates_presence_of :recommender_id, :recommended_type, :recommended_id
0
+
0
+end
...
2
3
4
5
6
7
8
9
 
 
10
11
12
...
2
3
4
 
 
 
 
 
5
6
7
8
9
0
@@ -2,11 +2,8 @@
0
   desc "Update all similar_items tables."
0
   task :update => :environment do
0
     
0
- ActiveRecord::Schema.tables.each do |table|
0
- if table =~ /^similar_/
0
- puts table
0
- end
0
- end
0
+ @all_tables = ActiveRecord::Schema.tables
0
+ @similar_tables = @all_tables.select { |table| table =~ /^similar_/ }
0
     
0
   end
0
 end
...
8
9
10
11
 
 
 
 
 
12
13
14
...
8
9
10
 
11
12
13
14
15
16
17
18
0
@@ -8,7 +8,11 @@
0
     
0
     module ClassMethods
0
       def recommendable
0
- # ...
0
+ has_many_polymorphs :recommended,
0
+ :from => [:"#{self.class_name}s"],
0
+ :through => :"similar_#{self.class_name}s",
0
+ :dependent => :destroy,
0
+ :as => :recommender
0
       end
0
     end
0
   end

Comments

    No one has commented yet.