Skip to content

Instantly share code, notes, and snippets.

046dca3a09c346963ddf89277262c01ed071e89833b5dd8474466bfb7c99150494fd416de41238335440b30da8f4d47a3866a144674a8f21eda81c137407093171;effektz
@nitsujw
nitsujw / futures.rb
Created April 26, 2012 01:34 — forked from wycats/0_app.rb
Example of using a simple future library for parallel HTTP requests
require "thread"
class Future
attr_reader :exception, :cancelled
def initialize(&block)
@thread = Thread.new(&block)
@thread.abort_on_exception = false
@exception = nil
@cancelled = false
@nitsujw
nitsujw / application.rb
Created April 4, 2012 16:36 — forked from bradhe/application.rb
Generates a set of JavaScript functions that behave very similarly to Rails' named routes. Even plays nice with the assets pipeline.
module Sprockets::Helpers::RailsHelper
require File.join(Rails.root, "app", "helpers", "assets_helper.rb")
include AssetsHelper
end
@nitsujw
nitsujw / Gemfile
Created March 18, 2012 02:46
Rails Lightweight Stack. Most of this is detailed on Crafting Rails Applications - http://pragprog.com/book/jvrails/crafting-rails-applications
source :rubygems
# We are not loading Active Record, nor Active Resources etc.
# We can do this in any app by simply replacing the rails gem
# by the parts we want to use.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
gem "tzinfo"
# Let's use thin
class PostsController < ActionController::Base
def create
Post.create(post_params)
end
def update
Post.find(params[:id]).update_attributes!(post_params)
end
private
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="draggable" style="width: 50px; float: left; height: 50px; background: red;">1</div>
<div class="draggable" style="width: 50px; float: left; height: 50px; border: 1px solid red;">2</div>
<div class="draggable" style="width: 50px; float: left; height: 50px; border: 1px solid red;">3</div>
class Popup
def make
"Making from instance"
end
def self.make
"Making from class"
end
end
## Controller
def stats
@visits = Visit.all
end
## View that produces 2 sql hits
<%= @visits[0] %>
<% @visits.each do |v| %>
...
<% end %>
Rails 3 + DM 0.10.0
>> User.count
NoMethodError: undefined method `aggregate' for #<DataMapper::Adapters::MysqlAdapter:0xb77b7cc8>
from /home/justin/apps/rails/blog/vendor/gems/gems/dm-aggregates-0.10.0/lib/dm-aggregates/repository.rb:5:in `aggregate'
from /home/justin/apps/rails/blog/vendor/gems/gems/dm-aggregates-0.10.0/lib/dm-aggregates/aggregate_functions.rb:166:in `aggregate'
from /home/justin/apps/rails/blog/vendor/gems/gems/dm-aggregates-0.10.0/lib/dm-aggregates/aggregate_functions.rb:38:in `count'
from (irb):39
>> User.aggregate
Problem: With a form_for in Rails 3, it checks object.errors['field'].any?
which in DataMapper returns nil if no errors are found,
in ActiveRecord it returns an empty array.
<% form_for @user, :url => "/users" do |f| %>
<%= f.text_field :login %>
<% end %>
You have a nil object when you didn't expect it!
You might have expected an instance of Array.