Skip to content

Instantly share code, notes, and snippets.

José Valim josevalim

Block or report user

Report or block josevalim

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View messages_builder.rb
class MessagesBuilder
def initialize
@description = nil
@failure = nil
end
def description(arg = nil, &block)
@description = arg || block
end
View tasks_controller.rb
# Current way of overwriting your create behaviour when the task can't be saved with success:
class TasksController < InheritedResources::Base
# Project.find_by_title!(params[:project_title]).tasks
belongs_to :project, :finder => :find_by_title!, :param => :project_title
def create
# create! == super
create! do |format|
unless @task.errors.empty?
format.html { redirect_to project_url(@project) }
View receive.rb
# TasksController
describe TasksController do
describe "responding to GET show" do
receive :find, :on => Task, :with => '37', :and_return => :mock_task
get :show, :id => '37'
it { should match_expectations }
it { should ensure_assign(:project, :task).equal(mock_project, mock_task) }
end
end
View sigleton_with_nesting.rb
class GroupsController < InheritedResources::Base
protected
def begin_of_association_chain
@current_user # or a method that returns the @current_user
end
end
View comparision.rb
# Vamos supor que você quer fazer algo quando em special ("something_special!")
# antes de criar seus projetos. Você deve fazer isso:
#
class ProjectsController < InheritedResources::Base
def create
@project = Project.new(params[:project])
@project.something_special!
create!
end
end
View user_spec.rb
describe User do
valid_attributes :name => 'José Valim', :email => 'myemail@josevalim.com', :password => '12345'
it{ should validates_presence_of(:email) }
end
View 1_class_variables.rb
require 'rubygems'
require 'active_support'
class Father
cattr_accessor :name
end
class Son < Father
end
View build_ruby19.sh
#!/bin/sh
mkdir -p /usr/local/src && cd /usr/local/src
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.bz2
tar -xjvf ruby-1.9.1-p0.tar.bz2
cd ruby-1.9.1-p0
./configure --prefix=/usr --program-suffix=19 --enable-shared
make && make install
View fixtures.rb
class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
cattr_accessor :current_fixtures
def self.destroy_fixtures(table_names)
NestedScenarios.delete_tables(table_names)
end
end
module ActiveRecord #:nodoc:
module TestFixtures #:nodoc:
View controller_example_group.rb
module Spec
module Rails
module Example
# Controller Examples live in $RAILS_ROOT/spec/controllers/.
#
# Controller Examples use Spec::Rails::Example::ControllerExampleGroup, which supports running specs for
# Controllers in two modes, which represent the tension between the more granular
# testing common in TDD and the more high level testing built into
# rails. BDD sits somewhere in between: we want to a balance between
# specs that are close enough to the code to enable quick fault
You can’t perform that action at this time.