Skip to content

Instantly share code, notes, and snippets.

View durran's full-sized avatar

Durran Jordan durran

View GitHub Profile
# lib/multi_parameter_attributes.rb
module MultiParameterAttributes
def time(attributes, name)
attrs = attributes.collect do |key, value|
if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
[$1.to_i, value.send("to_#$2")]
end
end.compact.sort_by(&:first).map(&:last)
Time.zone.local(*attrs) unless attrs.empty?
Given /^the following (.+?)(|\s+only)$/ do |model_name, only, table|
parser = Mongoid::Cucumber::Parser.new(model_name)
model, klass = parser.model, parser.klass
klass.destroy_all if only =~ /only$/
@objects = Mongoid::Cucumber::Factory.create(klass, table)
@objects.each(&:save!)
@object = @objects.last if @objects.size == 1
instance_variable_set("@#{model}", @object)
end
class PetOwner < Mongoid::Document
field :title
has_one :address
end
class Address < Mongoid::Document
field :street
belongs_to :addressable, :inverse_of => :address
end
#<HelpFile _id: test-help-file, title: Test help file, excerpt: Hello world!..., is_published: true, content: Hello world!, publish_date: Sun Jan 03 20:10:35 UTC 2010>
Mon Jan 04 07:10:35 +1100 2010
#<ActiveSupport::TimeZone:0x1a831e8 @tzinfo=nil, @name="Canberra", @utc_offset=36000>
Spec::Runner.configure do |config|
config.mock_with :mocha
config.after :suite do
Mongoid.database.collections.each(&:drop)
end
end
require "spec_helper"
describe Mongoid::Criteria do
describe "#max" do
before do
10.times do |n|
Person.create(:title => "Sir", :age => (n * 10), :aliases => ["D", "Durran"])
end
[ durran@modetojoy-3 | ~/Work/mongoid ] > irb
irb(main):001:0> require "mongoid"
=> true
irb(main):002:0> class Person
irb(main):003:1> include Mongoid::Document
irb(main):004:1> field :age, :type => Integer
irb(main):005:1> end
=> nil
irb(main):006:0> conn = Mongo::Connection.new
=> #<Mongo::Connection:0x18d925c @slave_ok=nil, @queue=#<ConditionVariable:0x18d91a8>, @nodes=[["localhost", 27017]], @safe_mutex=#<Mutex:0x18d91bc>, @options={}, @size=1, @checked_out=[], @connection_mutex=#<Mutex:0x18d91d0>, @port=27017, @logger=nil, @id_lock=#<Mutex:0x18d9220>, @sockets=[], @timeout=5.0, @host="localhost">
context "when document contains a hash field" do
before do
@map = { "first" => 10, "second" => "Blah" }
@person = Person.create(:map => @map)
end
it "properly gets and sets the has attributes" do
@person.map.should == @map
@from_db = Person.find(@person.id)
class Company
include Mongoid::Document
has_many :projects
end
class Project
include Mongoid::Document
has_many :project_zones
has_many :incidents
has_many_related :safety_inspectors
class Campaign
include Mongoid::Document
field :name
index :name, :unique => true
validates_presence_of :name
validates_uniqueness_of :name
end