Skip to content
Browse files

Rails 3.0 beta support

  • Loading branch information...
1 parent fb6ad9a commit 664cd8f4ebc319cc41e49d92f615ed5b30786d73 @mantas committed Feb 6, 2010
Showing with 78 additions and 68 deletions.
  1. +5 −61 lib/active_mongo.rb
  2. +9 −7 lib/active_mongo_attr_accessible.rb
  3. +64 −0 lib/active_mongo_start.rb
View
66 lib/active_mongo.rb
@@ -1,64 +1,8 @@
-require 'mongo'
-require 'active_mongo_uniquenesss'
-include Mongo
-
-config = YAML::load(File.open("#{RAILS_ROOT}/config/mongo.yml"))[Rails.env]
-
-$mongo_conn = Connection.new(config["host"], config["port"], :pool_size => 10, :timeout => 2)
-$mongo_db = $mongo_conn.db(config["database"])
-
-if defined?(PhusionPassenger)
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
- if forked
- $mongo_db.connect_to_master # Call db.connect_to_master to reconnect here
- end
- end
-end
-
-if config["user"]
- if !$mongo_db.authenticate(config["user"], config["password"])
- puts "Wrong MongoDB user and/or password!!!"
- end
-end
-
-require 'active_mongo_has_many'
-require 'active_mongo_attr_accessible'
-require 'active_mongo_indexes'
-require 'active_mongo_named_scopes'
-
module ActiveMongo
-
- class Base
- include ActiveModel::Conversion
- include ActiveModel::Validations
- include ActiveModel::Callbacks
- extend ActiveModel::Callbacks
- extend ActiveMongo::Indexes::ClassMethods
- extend ActiveMongo::AttrAccessible::ClassMethods
- extend ActiveMongo::NamedScopes::ClassMethods
- extend ActiveMongo::HasMany::ClassMethods
- include ActiveMongo::HasMany::InstanceMethods
-
- define_model_callbacks :create, :save, :update
- define_model_callbacks :initializer, :only => :after
-
- class << self; attr_accessor :scope; end
-
- def self.extended(klass)
- class << klass
- alias __old_name name
- def name(*args, &blk)
- return @name || self.__old_name
- end
- end
- end
-
- self.extended(self)
-
+ class Railtie < Rails::Railtie
+ railtie_name :active_mongo
+ initializer "active_mongo.initialize_active_mongo" do |app|
+ require 'active_mongo_start'
+ end
end
end
-
-
-require 'active_mongo_collection'
-require 'active_mongo_instance'
-require 'active_mongo_scope'
View
16 lib/active_mongo_attr_accessible.rb
@@ -2,31 +2,33 @@ module ActiveMongo
module AttrAccessible
module ClassMethods
def attr_accessible(*input)
- @@internal_attr_accessible = []
+ @@internal_attr_accessible ||= {}
+ @@internal_attr_accessible[self.name] ||= []
input.each do |field|
- @@internal_attr_accessible.push(field.to_sym).uniq!
+ @@internal_attr_accessible[self.name].push(field.to_sym).uniq!
end
end
def attr_accessible_get
- @@internal_attr_accessible || []
+ @@internal_attr_accessible[self.name] || []
end
def attr_clear(*input)
- @@internal_attr_clear = []
+ @@internal_attr_clear ||= {}
+ @@internal_attr_clear[self.name] ||= []
input.each do |field|
- @@internal_attr_clear.push(field.to_sym).uniq!
+ @@internal_attr_clear[self.name].push(field.to_sym).uniq!
end
end
def attr_accessible_get
- @@internal_attr_accessible || []
+ @@internal_attr_accessible[self.name] || []
end
def attr_clear_get
- @@internal_attr_clear || []
+ @@internal_attr_clear[self.name] || []
end
end
View
64 lib/active_mongo_start.rb
@@ -0,0 +1,64 @@
+require 'mongo'
+require 'active_mongo_uniquenesss'
+include Mongo
+
+config = YAML::load(File.open("#{RAILS_ROOT}/config/mongo.yml"))[Rails.env]
+
+$mongo_conn = Connection.new(config["host"], config["port"], :pool_size => 10, :timeout => 2)
+$mongo_db = $mongo_conn.db(config["database"])
+
+if defined?(PhusionPassenger)
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
+ if forked
+ $mongo_db.connect_to_master # Call db.connect_to_master to reconnect here
+ end
+ end
+end
+
+if config["user"]
+ if !$mongo_db.authenticate(config["user"], config["password"])
+ puts "Wrong MongoDB user and/or password!!!"
+ end
+end
+
+require 'active_mongo_has_many'
+require 'active_mongo_attr_accessible'
+require 'active_mongo_indexes'
+require 'active_mongo_named_scopes'
+
+module ActiveMongo
+
+ class Base
+ include ActiveModel::Conversion
+ include ActiveModel::Validations
+ include ActiveModel::Callbacks
+ extend ActiveModel::Callbacks
+ extend ActiveMongo::Indexes::ClassMethods
+ extend ActiveMongo::AttrAccessible::ClassMethods
+ extend ActiveMongo::NamedScopes::ClassMethods
+ extend ActiveMongo::HasMany::ClassMethods
+ include ActiveMongo::HasMany::InstanceMethods
+
+ define_model_callbacks :create, :save, :update
+ define_model_callbacks :initializer, :only => :after
+
+ class << self; attr_accessor :scope; end
+
+ def self.extended(klass)
+ class << klass
+ alias __old_name name
+ def name(*args, &blk)
+ return @name || self.__old_name
+ end
+ end
+ end
+
+ self.extended(self)
+
+ end
+end
+
+
+require 'active_mongo_collection'
+require 'active_mongo_instance'
+require 'active_mongo_scope'

0 comments on commit 664cd8f

Please sign in to comment.
Something went wrong with that request. Please try again.