Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Few fixes

  • Loading branch information...
commit e2968ebf5422c14491e75f748e5cf40045774943 1 parent 5c6d7b2
@mantas authored
View
59 README.textile
@@ -8,30 +8,11 @@ Feel free to it if you're adventurous.
h2. Installation
-1. Add to your Rails application Gemfile
-
-<pre><code> sudo gem install active_mongo</code></pre>
-
-2. Run gem bundle
-
-<pre><code> gem bundle</code></pre>
-
-3. Load gem. For example:
-
-<pre><code> #config/initalizers/active_mongo.rb
- require 'active_mongo'
-</code></pre>
-
-4. Create config/mongo.yml. Format is the same as database.yml. Database, host and port are required. User and password are optional
-
-5. Create a model
-
-<pre><code> class Model < ActiveMongo::Base
- validates_presence_of :attribute
-
- has_many :submodels
- end
-</code></pre>
+# Add to your Rails application Gemfile
+# Run gem bundle
+# Load gem. For example, create initializer that says "require 'active_mongo'"
+# Create config/mongo.yml. Format is the same as database.yml. Database, host and port are required. User and password are optional
+# Create a model, which is a subclass of ActiveMongo::Base
h2. Usage
@@ -41,7 +22,8 @@ All ActiveModel validations are supported. validates_uniqueness_of with optional
h3. Collection methods
-<pre><code> Model.find(ID) # get by ID
+<pre><code>
+ Model.find(ID) # get by ID
Model.find #get whole collection
Model.find(args) #run a query, args are the same as for mongo_ruby_driver
@@ -51,7 +33,8 @@ h3. Collection methods
h3. Create an object
-<pre><code> Model.new()
+<pre><code>
+ Model.new()
Model.new(:attribute => "value")
Model.save
@@ -59,7 +42,8 @@ h3. Create an object
h3. Update an object
-<pre><code> object.attribute = "value" #No need to declare anywhere before setting, see?
+<pre><code>
+ object.attribute = "value" #No need to declare anywhere before setting, see?
object.update_attributes(:attribute => "value") #does not save!
object.unset(:attribute) #removes :attribute from the object and saves ONLY this change
@@ -70,12 +54,14 @@ h3. Update an object
h3. Destroy object
-<pre><code> object.destroy
+<pre><code>
+ object.destroy
</code></pre>
h3. HasMany Associations
-<pre><code> #in model
+<pre><code>
+ #in model
has_many :items#, :class_name => "Item", :foreign_key => "item_id"
#in code
@@ -88,17 +74,20 @@ h3. Mass assignment
You can limit what may be assigned by passing a hash to #new or #update_attributes with this option.
-<pre><code> attr_accessible :attribute
+<pre><code>
+ attr_accessible :attribute
</code></pre>
h3. Do not save specific field to database
-<pre><code> attr_clear :attribute
+<pre><code>
+ attr_clear :attribute
</code></pre>
h3. Indexes
-<pre><code> ensure_index :attribute
+<pre><code>
+ ensure_index :attribute
ensure_index :attribute, :unique => true
ensure_index [ [:attribute, Mongo::ASCENDING] ], :unique => true # takes mongo-driver syntax
</code></pre>
@@ -107,12 +96,14 @@ h3. Callbacks
after initialize and before/after/around create, update and save callbacks are supported
-<pre><code> after_save :method
+<pre><code>
+ after_save :method
</code></pre>
h4. Named Scopes
-<pre><code> #in model
+<pre><code>
+ #in model
named_scope :with_value, :attribute => :value #all mongo_driver find() parameters accepted
#in code
View
10 lib/active_mongo.rb
@@ -4,9 +4,17 @@
config = YAML::load(File.open("#{RAILS_ROOT}/config/mongo.yml"))[Rails.env]
-$mongo_conn = Connection.new(config["host"], config["port"], :pool_size => 5, :timeout => 5)
+$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!!!"
View
21 lib/active_mongo_collection.rb
@@ -28,11 +28,12 @@ def self.destroy_all(attrs = {})
end
end
- def self.find(attrs = {})
- if attrs.class == String || attrs.class == Mongo::ObjectID
- id = Mongo::ObjectID.from_string(attrs) if attrs.class == String
+ # def self.find(attrs = {})
+ def self.find(selector = nil, *attrs)
+ if selector.class == String || selector.class == Mongo::ObjectID
+ id = Mongo::ObjectID.from_string(selector) if selector.class == String
- id ||= attrs
+ id ||= selector
obj = self.collection.find_one(id)
@@ -42,11 +43,19 @@ def self.find(attrs = {})
return model
else
- attrs = self.scope.merge(attrs) if self.scope
+ selector = self.scope.merge(selector || {}) if self.scope
ret = []
- self.collection.find(attrs).to_a.map do |obj|
+ selector ||= {}
+
+ selector.each do |key, value|
+ if key.to_s.match(/\_id$/) && value.class == String
+ selector[key] = Mongo::ObjectID.from_string(value)
+ end
+ end
+
+ self.collection.find(selector, attrs[0] || {}).to_a.map do |obj|
model = eval(self.name || @name).new
obj.each {|key, value| model.set_var(key, value) }
View
8 lib/active_mongo_has_many.rb
@@ -8,12 +8,16 @@ def has_many(name, attrs = {})
def internal_has_manies_set(name, attrs)
@@internal_has_manies ||= {}
- @@internal_has_manies[name.to_sym] = attrs if @@internal_has_manies[name].nil?
+ name = self.name.to_s+'___'+name.to_s
+
+ @@internal_has_manies[name.to_sym] = attrs if @@internal_has_manies[name.to_sym].nil?
end
def internal_has_manies_get(name)
@@internal_has_manies ||= {}
+ name = self.name.to_s+'___'+name.to_s
+
@@internal_has_manies[name.to_sym]
end
@@ -24,7 +28,7 @@ def has_many_hit(name, attrs)
return false if self.new_record?
attrs[:class_name] ||= name.to_s.classify
- attrs[:foreign_key] ||= name.to_s.singularize.underscore+"_id"
+ attrs[:foreign_key] ||= self.class.name.to_s.singularize.underscore+"_id"
return eval(attrs[:class_name]).with_scope(attrs[:foreign_key] => self._id )
end
View
13 lib/active_mongo_instance.rb
@@ -2,7 +2,7 @@
def initialize(*attr)
@vars = []
- attrs = attr[0] || {}
+ attrs = (attr[0] || {})
if self.class.attr_accessible_get.any?
attrs.delete_if {|key, value| !self.class.attr_accessible_get.include?(key.to_sym) }
@@ -28,6 +28,13 @@ def to_hash
h[var] = instance_variable_get("@#{var}")
end
+ h.each do |key, value|
+ if key.to_s.match(/\_id$/) && value.class == String
+ h[key] = Mongo::ObjectID.from_string(value)
+ end
+ end
+
+
return h
end
@@ -117,12 +124,12 @@ def update_attributes(*attrs)
return self
end
- def unset(var)
+ def unset(var, do_not_save = false)
self.set_var(var, nil)
@vars.delete var.to_sym
- return false if self.new_record?
+ return false if self.new_record? || do_not_save
hash = self.class.collection.find_one self._id
Please sign in to comment.
Something went wrong with that request. Please try again.