Commit f3cdff16 by Rocky Duan

update to mongoid 3.0

parent 104308fd
rvm use 1.9.3 rvm use 1.9.3@cs_comments_service
...@@ -16,23 +16,20 @@ gem 'sqlite3' ...@@ -16,23 +16,20 @@ gem 'sqlite3'
gem 'ampex' gem 'ampex'
# ruby-mongo-driver gem 'moped'
gem 'mongo', "1.6.2" gem 'mongoid', "~> 3.0"
gem 'bson'
gem 'bson_ext' gem 'bson_ext'
gem 'mongoid', "~> 2.4.0"
gem 'delayed_job' gem 'delayed_job'
gem 'delayed_job_mongoid' gem 'delayed_job_mongoid'
gem 'mongoid-tree', :git => 'git@github.com:dementrock/mongoid-tree.git', :branch => "mongoid-2.0" gem 'mongoid-tree', :git => 'git@github.com:dementrock/mongoid-tree.git'
gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git' gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git'
gem 'mongoid_taggable', :git => 'git@github.com:dementrock/mongoid_taggable.git' gem 'mongoid_taggable', :git => 'git@github.com:dementrock/mongoid_taggable.git'
gem 'sunspot' gem 'sunspot'
gem 'sunspot_solr' gem 'sunspot_solr'
gem 'sunspot_mongoid' gem 'sunspot_mongoid', :git => 'git@github.com:dementrock/sunspot_mongoid.git'
group :test do group :test do
......
...@@ -109,7 +109,6 @@ put '/api/v1/comments/:comment_id' do |comment_id| ...@@ -109,7 +109,6 @@ put '/api/v1/comments/:comment_id' do |comment_id|
end end
post '/api/v1/comments/:comment_id' do |comment_id| post '/api/v1/comments/:comment_id' do |comment_id|
puts params
sub_comment = comment.children.new(params.slice(*%w[body course_id])) sub_comment = comment.children.new(params.slice(*%w[body course_id]))
sub_comment.anonymous = value_to_boolean(params["anonymous"]) || false sub_comment.anonymous = value_to_boolean(params["anonymous"]) || false
sub_comment.author = user sub_comment.author = user
...@@ -171,7 +170,7 @@ if environment.to_s == "development" ...@@ -171,7 +170,7 @@ if environment.to_s == "development"
end end
end end
error BSON::InvalidObjectId do error Moped::Errors::InvalidObjectId do
error 400, ["requested object not found"].to_json error 400, ["requested object not found"].to_json
end end
......
development: development:
host: localhost sessions:
database: cs_comments_service_development default:
database: cs_comments_service_development
hosts:
- localhost:27017
test: test:
host: localhost sessions:
database: cs_comments_service_test default:
database: cs_comments_service_test
hosts:
- localhost:27017
defaults: &defaults defaults: &defaults
use_utc: false use_utc: false
......
...@@ -4,6 +4,7 @@ helpers do ...@@ -4,6 +4,7 @@ helpers do
end end
def user # TODO handle 404 if integrated user service def user # TODO handle 404 if integrated user service
raise ArgumentError, "User id is required" unless @user || params[:user_id]
@user ||= User.find_or_create_by(external_id: params[:user_id]) @user ||= User.find_or_create_by(external_id: params[:user_id])
end end
......
...@@ -9,8 +9,8 @@ class CommentThread < Content ...@@ -9,8 +9,8 @@ class CommentThread < Content
field :title, type: String field :title, type: String
field :body, type: String field :body, type: String
field :course_id, type: String, index: true field :course_id, type: String
field :commentable_id, type: String, index: true field :commentable_id, type: String
field :anonymous, type: Boolean, default: false field :anonymous, type: Boolean, default: false
include Sunspot::Mongoid include Sunspot::Mongoid
......
...@@ -6,7 +6,7 @@ class Subscription ...@@ -6,7 +6,7 @@ class Subscription
field :source_id, type: String field :source_id, type: String
field :source_type, type: String field :source_type, type: String
index [[:subscriber_id, Mongo::ASCENDING], [:source_id, Mongo::ASCENDING], [:source_type, Mongo::ASCENDING]] index({subscriber_id: 1, source_id: 1, source_type: 1})
def to_hash def to_hash
as_document.slice(*%w[subscriber_id source_id source_type]) as_document.slice(*%w[subscriber_id source_id source_type])
......
...@@ -2,9 +2,10 @@ class User ...@@ -2,9 +2,10 @@ class User
include Mongoid::Document include Mongoid::Document
include Mongo::Voter include Mongo::Voter
key :external_id, type: String, index: true field :_id, type: String, default: -> { external_id }
field :external_id, type: String
has_many :comments has_many :comments, inverse_of: :author
has_many :comment_threads, inverse_of: :author has_many :comment_threads, inverse_of: :author
has_many :activities, class_name: "Notification", inverse_of: :actor has_many :activities, class_name: "Notification", inverse_of: :actor
has_and_belongs_to_many :notifications, inverse_of: :receivers has_and_belongs_to_many :notifications, inverse_of: :receivers
...@@ -12,6 +13,8 @@ class User ...@@ -12,6 +13,8 @@ class User
validates_presence_of :external_id validates_presence_of :external_id
validates_uniqueness_of :external_id validates_uniqueness_of :external_id
index external_id: 1
def subscriptions_as_source def subscriptions_as_source
Subscription.where(source_id: id.to_s, source_type: self.class.to_s) Subscription.where(source_id: id.to_s, source_type: self.class.to_s)
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment