This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
has_many :cars do | |
def find_or_foobar_by(attrs) | |
find_or_create_by(attrs) | |
end | |
end | |
end |