Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 6 commits
  • 20 files changed
  • 0 commit comments
  • 1 contributor
View
1  .vagrant
@@ -0,0 +1 @@
+{"active":{"default":"1afb7771-2825-4c28-b4d4-87e11ab92be8"}}
View
3  Gemfile
@@ -7,5 +7,6 @@ group :development do
gem "bundler", "~> 1.0.14"
gem "jeweler", "~> 1.6.2"
gem "turn"
- gem "sqlite3"
+ gem "mongoid", ">= 2.0.0"
+ gem "bson_ext"
end
View
37 README.rdoc
@@ -1,23 +1,40 @@
-= coletivo
+= coletivo-mongoid
+
+A simple Rails 3 recommendations engine for Mongoid models.
-A simple Rails 3 recommendations engine.
Coletivo uses {Euclidean Distance}[http://en.wikipedia.org/wiki/Euclidean_distance] or {Pearson's Correlation Coefficient}[http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient] to calculate the similarity between persons and their preferences.
+This is a fork of {coletivo}[https://github.com/diogenes/coletivo], if you need ActiveRecord support, check coletivo.
+
== Installation:
- sudo gem install coletivo
- rails g coletivo
- rake db:migrate
+ sudo gem install coletivo-mongoid
+
+or in your rails app, add following line to Gemfile:
+
+ gem "coletivo-mongoid"
== Usage:
At your Rails model that represents a person (can be an _User_, _Member_, or something like that):
- class User < ActiveRecord::Base
+ class User
+ include Mongoid::Document
+ include Coletivo::Models::Person
+ include Coletivo::Models::Recommendable
has_own_preferences
# ...
end
+
+At your models that represent something that can be recommend (such as _Movie_):
+
+ class Movie
+ include Mongoid::Document
+ include Coletivo::Models::Recommendable
+
+ # ...
+ end
So, a person can rate things:
@@ -34,7 +51,7 @@ By default, the similarity strategy used is Euclidean Distance, but you can chan
Movie.find_recommendations_for(current_user, :strategy => :pearson)
-== Contributing to coletivo
+== Contributing to coletivo-mongoid
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
@@ -46,5 +63,7 @@ By default, the similarity strategy used is Euclidean Distance, but you can chan
== Copyright
-Copyright (c) 2011 Diógenes Falcão. See LICENSE.txt for
-further details.
+Copyright (c) 2011 Diógenes Falcão.
+Copyright (c) 2012 Francis Chong.
+
+See LICENSE.txt for further details.
View
12 Rakefile
@@ -14,13 +14,13 @@ require 'rake'
require 'jeweler'
Jeweler::Tasks.new do |gem|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
- gem.name = "coletivo"
- gem.homepage = "http://github.com/diogenes/coletivo"
+ gem.name = "coletivo-mongoid"
+ gem.homepage = "http://github.com/siuying/coletivo-mongoid"
gem.license = "MIT"
- gem.summary = %Q{A simple Rails 3 recommendations engine}
- gem.description = %Q{A simple Rails 3 recommendations engine}
- gem.email = "diogenes.araujo@gmail.com"
- gem.authors = ["Diógenes Falcão"]
+ gem.summary = %Q{A simple Rails 3 recommendations engine using Mongoid models}
+ gem.description = %Q{A simple Rails 3 recommendations engine using Mongoid models}
+ gem.email = "francis@ignition.hk"
+ gem.authors = ["Francis Chong", "Diógenes Falcão"]
gem.files = Dir["{lib}/**/*"]
# dependencies defined in Gemfile
end
View
2  VERSION
@@ -1 +1 @@
-0.0.3
+0.0.4
View
24 Vagrantfile
@@ -0,0 +1,24 @@
+Vagrant::Config.run do |config|
+ # Every Vagrant virtual environment requires a box to build off of.
+ config.vm.box = "mongo"
+
+ # The url from where the 'config.vm.box' box will be fetched if it
+ # doesn't already exist on the user's system.
+ config.vm.box_url = "http://box.ignition.hk/mongo.box"
+
+ # Assign this VM to a host only network IP, allowing you to access it
+ # via the IP.
+ # config.vm.network "33.33.33.10"
+ # config.vm.share_folder "v-root", "/vagrant", ".", :nfs => true
+
+ # Forward a port from the guest to the host, which allows for outside
+ # computers to access the VM, whereas host only networking does not.
+ config.vm.forward_port "mongo", 27017, 27017
+ config.vm.forward_port "mongo-http", 28017, 28017
+ config.vm.forward_port "redis", 6379, 6379
+ config.vm.forward_port "memcached", 11211, 11211
+
+ # config.vm.boot_mode = :gui
+ config.ssh.max_tries = 30
+
+end
View
66 coletivo-mongoid.gemspec
@@ -0,0 +1,66 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = "coletivo-mongoid"
+ s.version = "0.0.4"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Francis Chong", "Di\u{f3}genes Falc\u{e3}o"]
+ s.date = "2012-01-21"
+ s.description = "A simple Rails 3 recommendations engine using Mongoid models"
+ s.email = "francis@ignition.hk"
+ s.extra_rdoc_files = [
+ "LICENSE.txt",
+ "README.rdoc"
+ ]
+ s.files = [
+ "lib/coletivo.rb",
+ "lib/coletivo/models/person.rb",
+ "lib/coletivo/models/person_rating.rb",
+ "lib/coletivo/models/recommendable.rb",
+ "lib/coletivo/rails/engine.rb",
+ "lib/coletivo/similarity/base_strategy.rb",
+ "lib/coletivo/similarity/engine.rb",
+ "lib/coletivo/similarity/euclidean_distance_strategy.rb",
+ "lib/coletivo/similarity/pearson_correlation_strategy.rb"
+ ]
+ s.homepage = "http://github.com/siuying/coletivo-mongoid"
+ s.licenses = ["MIT"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = "1.8.10"
+ s.summary = "A simple Rails 3 recommendations engine using Mongoid models"
+
+ if s.respond_to? :specification_version then
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.7"])
+ s.add_development_dependency(%q<shoulda>, ["~> 3.0.0.beta2"])
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.14"])
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
+ s.add_development_dependency(%q<turn>, [">= 0"])
+ s.add_development_dependency(%q<mongoid>, [">= 2.0.0"])
+ s.add_development_dependency(%q<bson_ext>, [">= 0"])
+ else
+ s.add_dependency(%q<rails>, [">= 3.0.7"])
+ s.add_dependency(%q<shoulda>, ["~> 3.0.0.beta2"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.14"])
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
+ s.add_dependency(%q<turn>, [">= 0"])
+ s.add_dependency(%q<mongoid>, [">= 2.0.0"])
+ s.add_dependency(%q<bson_ext>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<rails>, [">= 3.0.7"])
+ s.add_dependency(%q<shoulda>, ["~> 3.0.0.beta2"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.14"])
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
+ s.add_dependency(%q<turn>, [">= 0"])
+ s.add_dependency(%q<mongoid>, [">= 2.0.0"])
+ s.add_dependency(%q<bson_ext>, [">= 0"])
+ end
+end
+
View
31 coletivo.gemspec
@@ -4,14 +4,14 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = %q{coletivo}
+ s.name = "coletivo-mongoid"
s.version = "0.0.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Di\303\263genes Falc\303\243o"]
- s.date = %q{2011-10-31}
- s.description = %q{A simple Rails 3 recommendations engine}
- s.email = %q{diogenes.araujo@gmail.com}
+ s.authors = ["Di\u{f3}genes Falc\u{e3}o"]
+ s.date = "2012-01-21"
+ s.description = "A simple Rails 3 recommendations engine"
+ s.email = "diogenes.araujo@gmail.com"
s.extra_rdoc_files = [
"LICENSE.txt",
"README.rdoc"
@@ -21,23 +21,19 @@ Gem::Specification.new do |s|
"lib/coletivo/models/person.rb",
"lib/coletivo/models/person_rating.rb",
"lib/coletivo/models/recommendable.rb",
- "lib/coletivo/rails/active_record.rb",
"lib/coletivo/rails/engine.rb",
"lib/coletivo/similarity/base_strategy.rb",
"lib/coletivo/similarity/engine.rb",
"lib/coletivo/similarity/euclidean_distance_strategy.rb",
- "lib/coletivo/similarity/pearson_correlation_strategy.rb",
- "lib/generators/coletivo/coletivo_generator.rb",
- "lib/generators/coletivo/templates/person_ratings_migration.rb"
+ "lib/coletivo/similarity/pearson_correlation_strategy.rb"
]
- s.homepage = %q{http://github.com/diogenes/coletivo}
+ s.homepage = "http://github.com/diogenes/coletivo"
s.licenses = ["MIT"]
s.require_paths = ["lib"]
- s.rubygems_version = %q{1.3.7}
- s.summary = %q{A simple Rails 3 recommendations engine}
+ s.rubygems_version = "1.8.10"
+ s.summary = "A simple Rails 3 recommendations engine"
if s.respond_to? :specification_version then
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -46,14 +42,16 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<bundler>, ["~> 1.0.14"])
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
s.add_development_dependency(%q<turn>, [">= 0"])
- s.add_development_dependency(%q<sqlite3>, [">= 0"])
+ s.add_development_dependency(%q<mongoid>, [">= 2.0.0"])
+ s.add_development_dependency(%q<bson_ext>, [">= 0"])
else
s.add_dependency(%q<rails>, [">= 3.0.7"])
s.add_dependency(%q<shoulda>, ["~> 3.0.0.beta2"])
s.add_dependency(%q<bundler>, ["~> 1.0.14"])
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
s.add_dependency(%q<turn>, [">= 0"])
- s.add_dependency(%q<sqlite3>, [">= 0"])
+ s.add_dependency(%q<mongoid>, [">= 2.0.0"])
+ s.add_dependency(%q<bson_ext>, [">= 0"])
end
else
s.add_dependency(%q<rails>, [">= 3.0.7"])
@@ -61,7 +59,8 @@ Gem::Specification.new do |s|
s.add_dependency(%q<bundler>, ["~> 1.0.14"])
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
s.add_dependency(%q<turn>, [">= 0"])
- s.add_dependency(%q<sqlite3>, [">= 0"])
+ s.add_dependency(%q<mongoid>, [">= 2.0.0"])
+ s.add_dependency(%q<bson_ext>, [">= 0"])
end
end
View
14 config/mongoid.yml
@@ -0,0 +1,14 @@
+defaults: &defaults
+ host: '127.0.0.1'
+
+development:
+ <<: *defaults
+ database: coletivo_development
+
+test:
+ <<: *defaults
+ database: coletivo_test
+
+# set these environment variables on your prod server
+production:
+ database: coletivo_production
View
2  lib/coletivo.rb
@@ -1,4 +1,3 @@
-require 'rails'
require 'active_model'
require 'active_record'
require 'active_support'
@@ -30,6 +29,5 @@ module Config
if defined?(Rails)
require 'coletivo/rails/engine'
- require 'coletivo/rails/active_record'
end
end
View
15 lib/coletivo/models/person_rating.rb
@@ -1,6 +1,19 @@
+require 'mongoid'
+
module Coletivo
module Models
- class PersonRating < ActiveRecord::Base
+ class PersonRating
+ include Mongoid::Document
+ include Mongoid::Timestamps
+
+ field :person_id, type: String
+ field :person_type, type: String
+
+ field :rateable_id, type: String
+ field :rateable_type, type: String
+
+ field :weight, type: Float
+
belongs_to :person, :polymorphic => true
belongs_to :rateable, :polymorphic => true
View
3  lib/coletivo/models/recommendable.rb
@@ -12,8 +12,7 @@ def find_recommendations_for(person, options = {})
load_preferences_for_recommendation(person)
top = predict_highest_ratings(person, preferences, options)
ids = top.collect(&:last)
-
- where(:id => ids).limit(options[:limit]).all
+ where(:_id.in => ids).limit(options[:limit]).all
end
def map_ratings_to_preferences(ratings)
View
2  lib/coletivo/rails/active_record.rb
@@ -1,2 +0,0 @@
-ActiveRecord::Base.send :include, Coletivo::Models::Person
-ActiveRecord::Base.send :include, Coletivo::Models::Recommendable
View
24 lib/generators/coletivo/coletivo_generator.rb
@@ -1,24 +0,0 @@
-require 'rails/generators'
-require 'rails/generators/migration'
-
-class ColetivoGenerator < Rails::Generators::Base
- include Rails::Generators::Migration
-
- def self.source_root
- File.join(File.dirname(__FILE__), 'templates')
- end
-
- # Implement the required interface for Rails::Generators::Migration.
- def self.next_migration_number(dirname) #:nodoc:
- if ActiveRecord::Base.timestamped_migrations
- Time.now.utc.strftime("%Y%m%d%H%M%S")
- else
- "%.3d" % (current_migration_number(dirname) + 1)
- end
- end
-
- def create_migration_file
- migration_template 'person_ratings_migration.rb',
- 'db/migrate/create_person_ratings.rb'
- end
-end
View
21 lib/generators/coletivo/templates/person_ratings_migration.rb
@@ -1,21 +0,0 @@
-class CreatePersonRatings < ActiveRecord::Migration
- def self.up
- create_table :person_ratings do |t|
- t.integer :person_id
- t.string :person_type
-
- t.integer :rateable_id
- t.string :rateable_type
-
- t.decimal :weight, :precision => 5, :scale => 2
-
- t.timestamps
- end
-
- add_index :person_ratings, :rateable_type, :unique => false
- end
-
- def self.down
- drop_table :person_ratings
- end
-end
View
24 test/db/schema.rb
@@ -1,24 +0,0 @@
-require 'generators/coletivo/templates/person_ratings_migration'
-
-ActiveRecord::Schema.define(:version => 1) do
- CreatePersonRatings.up
-
- create_table :users do |t|
- t.string :name
- t.string :email
-
- t.timestamps
- end
-
- create_table :movies do |t|
- t.string :name
-
- t.timestamps
- end
-
- create_table :actors do |t|
- t.string :name
-
- t.timestamps
- end
-end
View
BIN  test/db/test.db
Binary file not shown
View
17 test/helper.rb
@@ -8,21 +8,22 @@
exit e.status_code
end
+require 'mongoid'
+Mongoid.configure do |config|
+ config.master = Mongo::Connection.new.db("coletivo")
+end
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'coletivo'
+Mongoid.logger = Logger.new($stdout, :debug)
+
require 'test/unit'
require 'turn'
require 'shoulda'
-ActiveRecord::Base.establish_connection({
- :adapter => 'sqlite3',
- :database => ':memory:'
-})
-ActiveRecord::Migration.verbose = false
-
-require 'db/schema'
+Mongoid.database.collections.each do |coll|
+ coll.remove if coll.name !~ /system/
+end
class Test::Unit::TestCase
end
View
8 test/models/recommendable_test.rb
@@ -69,8 +69,10 @@ def setup
}
recommendations = Movie.find_recommendations_for(@person1,
- :preferences => p, :strategy => strategy)
-
+ :preferences => p, :strategy => strategy).to_a
+
+ assert_not_nil recommendations.index(m2)
+ assert_not_nil recommendations.index(m4)
assert recommendations.index(m2) < recommendations.index(m4)
end
@@ -89,7 +91,7 @@ def setup
}
assert_equal 1, Movie.find_recommendations_for(@person1,
- :preferences => p, :strategy => strategy, :limit => 1).size
+ :preferences => p, :strategy => strategy, :limit => 1).to_a.count
end
end
end
View
34 test/models_helper.rb
@@ -1,29 +1,41 @@
require 'coletivo'
-class User < ActiveRecord::Base
+class User
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ include Coletivo::Models::Person
+ include Coletivo::Models::Recommendable
has_own_preferences
+
+ field :name, type: String
+ field :email, type: String
end
-class Movie < ActiveRecord::Base
+class Movie
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ include Coletivo::Models::Recommendable
+
+ field :name, type: String
end
-class Actor < ActiveRecord::Base
+class Actor
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ include Coletivo::Models::Recommendable
+
+ field :name, type: String
end
+
def ratings_container
Coletivo::Config.ratings_container
end
class Test::Unit::TestCase
def setup
- truncate! :person_ratings, :users, :movies, :actors
- end
-
- private
-
- def truncate!(*tables)
- [*tables].each do |t|
- ActiveRecord::Base.connection.execute("DELETE FROM #{t}")
+ Mongoid.database.collections.each do |coll|
+ coll.remove if coll.name !~ /system/
end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.