Skip to content

Instantly share code, notes, and snippets.

@senny
senny / gist:4951454
Created February 14, 2013 09:03
CONTROLLER environment variable to filter routes
» rake routes
root GET / pages#home
» export CONTROLLER='non-existing-controller'
» rake routes
» unset CONTROLLER
» rake routes
root GET / pages#home
@senny
senny / respond_with_scaffold.rb
Created February 6, 2013 10:30
Rails 3.2.x scaffolded Controller
class ProductsController < ApplicationController
respond_to :html, :xml
def index
@products = Product.all
respond_with @products
end
def show
@product = Product.find(params[:id])
# Hook to save the html page of every failed scenario into the temp
# file directory taht it can be checked after cucumber has finished running.
require 'fileutils'
require 'capybara/util/save_and_open_page'
FAILED_SCENARIO_PAGES_PATH = File.join Rails.root, 'tmp', 'failed_scenarios'
FAILED_SCENARIO_IMAGES_PATH = File.join Rails.root, 'tmp', 'failed_scenarios_images'
FileUtils.rm_rf FAILED_SCENARIO_PAGES_PATH
FileUtils.rm_rf FAILED_SCENARIO_IMAGES_PATH
@senny
senny / routes.rb
Created January 22, 2013 16:09
:controller option in rails route definitions
MyApp::Application.routes.draw do
# works mostly but has problems with `url_for :controller => 'module/controller'`
resources :posts, :controller => 'Module::Controller'
# works
resources :articles, :controller => 'module/controller'
end
@senny
senny / gist:4446037
Last active December 10, 2015 14:08
class Article < ActiveRecord::Base
end
module Posts
end
Posts::Builder # => uninitialized constant Posts::Builder
Article::Builder # => ActiveRecord::Associations::Builder
Post.const_get('Builder') # => ActiveRecord::Associations::Builder
diff --git a/lib/spring.rb b/lib/spring.rb
index 38a22bf..20669c6 100644
--- a/lib/spring.rb
+++ b/lib/spring.rb
@@ -50,6 +50,7 @@ class Spring
end
def run(args)
+ puts "#run"
boot_server unless server_running?
@senny
senny / _form.html.erb
Created December 31, 2012 14:45
Simple Example how to use `accepts_nested_attributes_for`
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
class User < ActiveRecord::Base
validates :account_id, :presence => true
end
# in the View (using simple_form):
#
# f.select :account, :collection => Account.all
@senny
senny / full
Created November 20, 2012 09:58
Rails Plugin Generator
railties :: (refactor_plugin_new_generator) » ./bin/rails plugin new ~/Projects/playground/rails/full_engine --full
create
create README.rdoc
create Rakefile
create full_engine.gemspec
create MIT-LICENSE
create .gitignore
create Gemfile
create app/models
create app/models/.keep
@senny
senny / output (PostgreSQL)
Created October 27, 2012 15:04
Performance of ActiveRecord-Setters
user system total real
variable 1.590000 0.000000 1.590000 ( 1.599274)
AR 69.490000 2.440000 71.930000 ( 71.920999)
AR is 44.97 times slower.