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:
rolled back to pre-polymorphs

git-svn-id: https://svn.thoughtbot.com/hackfest/recommendable@131 
c973d20d-6cf3-4494-8058-145efcf21f09
(no author) (author)
Tue Apr 01 16:02:51 -0700 2008
commit  c3fe788b38d2fbb32892f6cd4edf3356394a9fd7
tree    d459e35cc0aa548bf4fb9b8f649c5562934366fb
parent  0779a55f2d5f4b1ab24ac09b5a3884b2be6f376f
...
1
2
3
4
5
 
 
 
 
 
 
 
6
7
8
...
1
2
3
 
 
4
5
6
7
8
9
10
11
12
13
0
@@ -1,8 +1,13 @@
0
 class RecommendableGenerator < Rails::Generator::NamedBase
0
   def manifest
0
     record do |m|
0
- m.migration_template 'migration.rb', 'db/migrate'
0
- m.dependency 'model', class_name, :collision => :skip, :skip_migration => true
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.dependency 'model', model_args, :collision => :skip, :skip_migration => true
0
     end
0
   end
0
 end
...
1
 
2
3
4
5
6
 
 
 
7
8
9
10
11
12
13
 
 
 
 
14
15
16
17
 
18
19
...
 
1
2
 
 
 
 
3
4
5
6
7
8
 
 
 
 
9
10
11
12
13
14
15
 
16
17
18
0
@@ -1,20 +1,19 @@
0
-class CreateRecommendations < ActiveRecord::Migration
0
+class <%= migration_name %> < ActiveRecord::Migration
0
   def self.up
0
- create_table :recommendations do |t|
0
- t.integer :recommended_id
0
- t.string :recommeded_type
0
- t.integer :recommender_id
0
+ create_table :<%= table_name %> do |t|
0
+ t.integer :<%= recommendable.singularize %>_one
0
+ t.integer :<%= recommendable.singularize %>_two
0
       t.decimal :score
0
     end
0
     
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
+ 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
   end
0
   
0
   def self.down
0
- drop_table :recommendations
0
+ drop_table :<%= table_name %>
0
   end
0
 end
...
1
2
3
4
5
6
7
8
...
 
 
 
 
 
 
 
 
0
@@ -1,9 +1 @@
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
...
1
2
3
4
5
6
7
8
9
...
 
 
 
 
 
 
 
 
 
0
@@ -1,10 +1 @@
0
-namespace :recommendables do
0
- desc "Update all similar_items tables."
0
- task :update => :environment do
0
-
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
15
 
16
17
18
...
8
9
10
 
 
 
 
 
11
12
13
14
0
@@ -8,11 +8,7 @@
0
     
0
     module ClassMethods
0
       def recommendable
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
+ # ...
0
       end
0
     end
0
   end
0
...
1
2
3
4
5
...
 
 
 
 
 
0
@@ -1,6 +1 @@
0
-mkdir -p ~/.rails/generators
0
-cd generators
0
-ln -sf recommendable ~/.rails/generators
0
-
0
-goals: http://www.markmcgranaghan.com/posts/51

Comments

    No one has commented yet.