Skip to content

Instantly share code, notes, and snippets.

View josevalim's full-sized avatar

José Valim josevalim

View GitHub Profile
# PROBLEM
#
# In order to provide agnosticism, a controller generator should know
# which methods the ORM make available to find, save, update, etc.
#
# SOLUTION #1
#
# ORM should expose a generator builder that tell us what to do.
# This builder exposes AR methods and return custom implementations.
#
jose:~/Work/github/rails/railties$[gen*] ruby bin/gen model
Usage:
bin/gen model NAME [field:type, field:type] [options]
Options:
-o, [--orm=NAME] # Orm to be invoked
# Default: active_record
-t, [--test-framework=NAME] # Test framework to be invoked
# Default: test_unit
# Configure generators default options.
config.generators do |g|
g.rails :helper => true, :stylesheets => true
g.orm :active_record, :migration => true, :timestamps => true
g.template_engine :erb, :layout => true
g.test_framework :test_unit, :fixtures => true
end
class UsersController < ApplicationController
respond_to :html, :xml
# GET /users
# GET /users.xml
def index
@users = User.all
respond_with(@users)
end
class UsersController < ApplicationController
respond_to :html, :xml, :json
# GET /users
# GET /users.xml
def index
@users = User.all
respond_with(@users)
end
class ActionController::Presenter
attr_reader :controller, :request, :format, :resource, :resource_location
def initialize(controller, resource)
@controller = controller
@request = controller.request
@format = controller.formats.first
@resource = resource.is_a?(Array) ? resource.last : resource
@resource_location = resource
module InheritedResources
module Userstamp
def create_resource(obj)
record_user(obj, :creator, :updater)
super
end
def update_resource(obj, *args)
record_user(obj, :updater)
super
## Optimizing instrumentation
# Master
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100
129 198 250 224 802 630 6274 4411 4659
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100
128 195 247 222 798 621 6026 4314 4547
## Problem: change where to redirect on save with success
# scaffold respond_with
def create
@post = Post.new(params[:post])
flash[:notice] = "Post was successfully created" if @post.save
respond_with(@post)
end
# changed respond_with
# Instead of:
module MailForm::Callbacks
def self.extended(base)
base.class_eval do
include ActiveSupport::Callbacks
define_callbacks :create, :terminator => "result == false", :scope => [:kind, :name]
end
end
def before_create(*args, &block)