Skip to content

Instantly share code, notes, and snippets.

View carllerche's full-sized avatar
😎
working

Carl Lerche carllerche

😎
working
View GitHub Profile
Router.prepare do |r|
r.match("/something",
:user_agent => %r[(?:one|two|three)]
).to(:controller => "foo", :action => "bar")
end
Router.prepare do |r|
r.match(:controller => "users") do
r.match("/users/:id", :method => :get).to(:action => "show")
r.match("/users/:id", :method => :put).to(:action => "update")
end
end
# Both routes generate the exact same path, so do we just
# require the minimum necessary to differentiate them?
# NOTE, all names, syntax, etc... are just off the top of my head to illustrate
# the general idea. Don't take anything too literally.
# I just started a blank project with DataMapper. DM Migrations knows
# that the current production database is empty because no schema has been "committed".
#
# I then created a new model:
class User
include DataMapper::Resource
describe '#save' do
describe 'with a new resource' do
it "should create when the resource is dirty" do
Zoo.new(:city => "San Diego").save
Zoo.first.name.should == "San Diego"
end
end
end
# current behavior
zoo1 = Zoo.new(:city => "SF")
zoo2 = Zoo.new(:city => "SF")
zoo1.eql?(zoo2) # => true
zoo1.save
zoo2.save
zoo1.eql?(zoo2) # => false
class Users < Application
# === Option 1 ===
def create
@user = User.first
User.allow_access(:staff) do
@user.update_attributes :staff => true
end
end
class Users < Application
def update
@user = User.find(params[:id])
if @user.with_access_to(:staff, :banned).update_attributes(params[:user])
# success
end
r.match(:domain => "admin") do |admin|
admin.match("/users").to(:controller => "admin/users")
end
r.match(:domain => "site") do |site|
site.match("/users").to(:controller => "site/users")
end
#!/usr/bin/env ruby
require 'rubygems'
require 'dm-core'
class Zoo
include DataMapper::Resource
property :id, Serial
end
Merb::Router.prepare do |r|
r.domain(:admin, "admin.domain.com") do |a|
a.resources :users
end
r.domain(:site, "domain.com") do |s|
s.resources :users
end