Skip to content

Instantly share code, notes, and snippets.

View durran's full-sized avatar

Durran Jordan durran

View GitHub Profile
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
class Post
include Mongoid::Document
field :title, :type => String
has_many :comments do
def update(selector, value)
@target.each { |c| c.body = value if c.matches?(selector) }
end
end
end
module MultiParameterAttributes
def filter_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?
end
class BasalPlan
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::DeepVersioning
field :name, :type => String
belongs_to :user, :inverse_of => :basal_plan
has_many :basal_entries
accepts_nested_attributes_for :basal_entries, :reject_if => lambda { |attrs| attrs[:_delete].present? }
class InterventionsController < ApplicationController
inherit_resources
actions :create, :update, :edit, :new
nested_belongs_to :patient, :drug_therapy_problem
create! do |success, failure|
success.html do
@intervention.activate!("Manual communication")
end
failure.html { render :action => 'new', :status => :unprocessable_entity }
First we create a document with a pre-epoch Time in Ruby:
>> time = Date.new(1950, 1, 1).to_time
=> Sun Jan 01 00:00:00 -0400 1950
>> time.class
=> Time
>> time.to_i
=> -631137600
>> collection = Mongoid.master.collection("driver_test");
>> collection.insert({ "dob" => time })
require 'rubygems'
puts ENV["GEM_HOME"]
require 'mongoid'
require 'mongoid/version'
puts "Using Mongoid: #{Mongoid::VERSION}"
Mongoid.master = Mongo::Connection.new.db("mongoid_playground")
class Orphan
- semantic_form_for [:admin, @organization, @contact] do |f|
= hidden_field_tag 'partial', 'addresses'
- @contact.addresses.each do |address|
- f.semantic_fields_for :addresses, address do |af|
- af.inputs(:name => "Address #{remove_fieldset}") do
= render :partial => "addresses/fields", :locals => {:form => af}
class Message < ActiveRecord::Base
include Postgre::Searchable
has_many :tags, inverse_of: :message
search_on do |message|
[ message.subject,
message.body,
message.tags.map(&:name) ]
end