Skip to content

Instantly share code, notes, and snippets.

View durran's full-sized avatar

Durran Jordan durran

View GitHub Profile
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? }
# Problem: Given the following document in the database, I want to update
# Dave Gahan's second prescription for Dilaudid from a quantity of 45 to
# a quantity of 30.
{ "_id" : 1
"name" : "Dave Gahan"
"medications" : [
{ "id" : 23,
"name" : "Dilaudid",
"type" : "Rx",
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
class User
include Mongoid::Document
has_many :cars do
def find_or_foobar_by(attrs)
find_or_create_by(attrs)
end
end
end