Skip to content

Instantly share code, notes, and snippets.

@coreyhaines
coreyhaines / .rspec
Last active August 15, 2024 15:13
Active Record Spec Helper - Loading just active record
--colour
-I app
@avdi
avdi / github_service.rb
Created December 28, 2011 04:12
Leadlight sneak peek
class GithubService
Leadlight.build_service(self) do
url 'https://api.github.com'
build_connection do |c|
c.adapter :typhoeus
end
tint 'root' do
match_path('/')
@jcasimir
jcasimir / api.markdown
Created September 12, 2011 20:08
Exposing an API in Rails 3

Exposing an API

APIs are becoming an essential feature of modern web applications. Rails does a good job of helping your application provide an API using the same MVC structure you're accustomed to.

In the Controller

Let's work with the following example controller:

class ArticlesController < ApplicationController

Handling Parameters

The controller's job is to work with the request parameters and determine how to activate the domain logic and data to respond to requests. The parameters are key to completing that job.

And, at the same time, parameters are the cause of the most problems in a typical controller. A great action method should be about eight lines of Ruby, but many actions spiral out of control with all kinds of switching based on the input parameters.

params Helper

First, a small point of order: people commonly refer to params as a variable but it isn't -- it is a helper method provided by ActionController which returns a hash containing the request parameters.

@vargonaut
vargonaut / be_allowed_to_match.rb
Created May 10, 2011 04:41
Custom Matcher for Declarative Authorization authorization specs with RSpec
# in spec/spec_helper.rb, add this to make it work:
# require 'support/be_allowed_to'
# RSpec.configure |config|
# config.include(BeAllowedToMatcher)
# end
#
# allows: some_user.should be_allowed_to(:edit, thing)
# some_user.should be_allowed_to(:edit, :things)
# some_user.should be_allowed_to(:edit, thing, in_this_context)
# along with the should_not variants
@jacobandresen
jacobandresen / jasmine BDD
Created April 12, 2011 23:06
ExtJS 4 Jasmine BDD
var reviewGrid = Ext.create("ReviewGrid", {renderTo: 'review'});
var productStore = Ext.create("ProductStore", {});
var productForm = Ext.create("ProductForm", {store:productStore, renderTo: 'product', reviewGrid: reviewGrid});
describe("ProductForm", function() {
it("should be able to navigate 1 product forward ", function() {
waitsFor(5000, function () {
return ( productStore.isLoading() == false);
}, "productStore load");
## conduce - be conducive to; "The use of computers in the classroom lead to better writing"
#
# a model+view component for rails that combines the conductor and presenter
# pattern via a model capable of generating view-centric methods
#
module Conducer
# base class
#
@kares
kares / integration_test_session_patch.rb
Created October 26, 2010 09:39
making sessions work in integration tests (broken since Rails 2.3.8)
# session is not preserved between requests in integration tests (since 2.3.8)
# https://rails.lighthouseapp.com/projects/8994/tickets/4941-session-is-not-preserved-between-requests-in-integration-tests
# this monkey-fix deals with the issue and is based on the provided patch from
# the above ticket. require in your test_helper or integration_test_helper ...
require 'action_controller/session/abstract_store'
module ActionController
Just in case you still think bundle sucks, consider the alternative
This is me getting my blog, a very small Rails 2.3 app with only a few dependencies,
Up and running on my local machine
PAINFUL!
~/dev $ git clone paulbarry.com:paulbarry.git
Cloning into paulbarry...
remote: Counting objects: 1408, done.
remote: Compressing objects: 100% (1071/1071), done.
remote: Total 1408 (delta 274), reused 1285 (delta 207)
@mnutt
mnutt / Instrument Anything in Rails 3.md
Created September 6, 2010 06:50
How to use Rails 3.0's new notification system to inject custom log events

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)