This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
before_filter :authenticate_user, except: [:index, :show] | |
def authenticate_user | |
# I presume you have a current_user helper defined on your controller to get the authenticated user? | |
raise ActionController::RoutingError, 'No valid user' unless current_user.present? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Recovering stashes that were cleared/dropped erroneously | |
If you mistakenly drop or clear stashes, they cannot be recovered through the normal safety mechanisms. However, you can try the | |
following incantation to get a list of stashes that are still in your repository, but not reachable any more: | |
git fsck --unreachable | | |
grep commit | cut -d\ -f3 | | |
xargs git log --merges --no-walk --grep=WIP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources :contracts do | |
get :reprint, on: :member | |
# or alternatively | |
member do | |
get :reprint | |
end | |
end | |
# will generate links like /contracts/123/reprint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## app/views/layouts/application.html.erb | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<% if current_user.admin? %> | |
<%= stylesheet_link_tag 'application_admin' %> | |
<% else %> | |
<%= stylesheet_link_tag 'application_nonadmin' %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filters = {} | |
filters[:name] = params[:name] if params[:name] | |
filters[:age] = params[:age] if params[:age] | |
filters[:gender] = params[:gender] if params[:gender] | |
# or some computation or other process for each field here instead, to mimic functionality that a simple slice wouldnt replicate | |
Person.where(filters) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails g model Foo | |
invoke active_record | |
create db/migrate/20140819015801_create_foos.rb | |
create app/models/foo.rb | |
invoke test_unit | |
create test/models/foo_test.rb | |
create test/fixtures/foos.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):004:0> helper.image_tag("ipads/150ipadmini.png", alt: "iPad Mini", :data => {:toggle => "collapse", :parent => "#ipadpanel", :target => "#ipadmini"}) | |
=> "<img alt=\"iPad Mini\" data-parent=\"#ipadpanel\" data-target=\"#ipadmini\" data-toggle=\"collapse\" src=\"/images/ipads/150ipadmini.png\" />" | |
# I went and made a file in app/assets/images/ipads/150ipadmini.png | |
irb(main):005:0> helper.image_tag("ipads/150ipadmini.png", alt: "iPad Mini", :data => {:toggle => "collapse", :parent => "#ipadpanel", :target => "#ipadmini"}) | |
=> "<img alt=\"iPad Mini\" data-parent=\"#ipadpanel\" data-target=\"#ipadmini\" data-toggle=\"collapse\" src=\"/assets/ipads/150ipadmini.png\" />" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails c | |
Loading development environment (Rails 3.2.19) | |
irb(main):001:0> "test".parameterize | |
=> "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT "spree_products".* | |
FROM "spree_products" | |
INNER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" | |
AND "spree_variants"."is_master" = 't' | |
AND "spree_variants"."deleted_at" IS NULL | |
INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" | |
AND "spree_prices"."deleted_at" IS NULL | |
WHERE "spree_products"."deleted_at" IS NULL | |
AND ("spree_products".deleted_at IS NULL or "spree_products".deleted_at >= '2014-09-01 07:57:37.112639') | |
AND ("spree_products".available_on <= '2014-09-01 07:57:37.113857') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Changes for the Second Edition | |
A lot has changed in the Rails testing world over the past five years, even if | |
the general principles stayed more or less the same. This book has been | |
substantially rewritten from its first edition, with almost no part of the book | |
unchanged. Here’s a more complete list of changes. (Not all of these changes | |
will be in the early beta versions.) | |
• All tools have been upgraded to their latest versions: Rails 4.1.x, Minitest | |
5.3.x, RSpec 3, and so on. | |
• The opening tutorial was completely re-written. It’s an all new example | |
which provides, I hope, a more gentle introduction to testing in Rails. |