Rails controllers.
This commit is contained in:
parent
36fae08dcf
commit
fd8841045c
4
ios.md
4
ios.md
|
@ -2,6 +2,10 @@ Multiple Exchange accounts:
|
|||
|
||||
scp root@iphone.local:/private/var/mobile/Library/Preferences/com.apple.accountsettings.plist .
|
||||
|
||||
Copy photos:
|
||||
|
||||
scp -r root@iphone.local:/User/Media/DCIM/100APPLE ./photos
|
||||
|
||||
Ringtone conversion using ffmpeg:
|
||||
|
||||
ffmpeg -i foo.mp3 -ac 1 -ab 128000 -f mp4 -acodec libfaac -y target.m4r
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
title: Middleman
|
||||
---
|
||||
|
||||
### Compass config
|
||||
|
||||
compass_config do |config|
|
||||
config.output_style = :compact
|
||||
end
|
||||
|
||||
### Config
|
||||
|
||||
# Automatic image dimensions on image_tag helper
|
||||
activate :automatic_image_sizes
|
||||
|
||||
### Gems
|
||||
|
||||
# Susy grids in Compass
|
||||
# First: gem install compass-susy-plugin
|
||||
require 'susy'
|
||||
|
||||
# CodeRay syntax highlighting in Haml
|
||||
# First: gem install haml-coderay
|
||||
require 'haml-coderay'
|
||||
|
||||
# CoffeeScript filters in Haml
|
||||
# First: gem install coffee-filter
|
||||
require 'coffee-filter'
|
||||
|
||||
|
||||
### Page command
|
||||
|
||||
# With no layout
|
||||
page "/path/to/file.html", :layout => false
|
||||
|
||||
# With alternative layout
|
||||
page "/path/to/file.html", :layout => :otherlayout
|
||||
|
||||
# A path which all have the same layout
|
||||
with_layout :admin do
|
||||
page "/admin/*"
|
||||
end
|
||||
|
||||
# Proxy (fake) files
|
||||
page "/this-page-has-no-template.html", :proxy => "/template-file.html" do
|
||||
@which_fake_page = "Rendering a fake page with a variable"
|
||||
end
|
||||
|
||||
### Helpers
|
||||
|
||||
helpers do
|
||||
def some_helper
|
||||
"Helping"
|
||||
end
|
||||
end
|
||||
|
||||
### Directories
|
||||
|
||||
set :css_dir, "alternative_css_directory"
|
||||
set :js_dir, "alternative_js_directory"
|
||||
set :images_dir, "alternative_image_directory"
|
||||
|
||||
# Build-specific configuration
|
||||
|
||||
configure :build do
|
||||
# For example, change the Compass output style for deployment
|
||||
activate :minify_css
|
||||
|
||||
# Minify Javascript on build
|
||||
activate :minify_javascript
|
||||
|
||||
# Enable cache buster
|
||||
activate :cache_buster
|
||||
|
||||
# Use relative URLs
|
||||
activate :relative_assets
|
||||
|
||||
# Compress PNGs after build
|
||||
# First: gem install middleman-smusher
|
||||
# require "middleman-smusher"
|
||||
activate :smusher
|
||||
|
||||
# Or use a different image path
|
||||
set :http_path, "/Content/images/"
|
||||
end
|
|
@ -0,0 +1,156 @@
|
|||
title: Rails Controllers
|
||||
---
|
||||
|
||||
### Common stuff
|
||||
|
||||
redirect_to root_url
|
||||
redirect_to root_url, notice: "Good."
|
||||
|
||||
### Special hashes
|
||||
|
||||
session[:user_id] = nil
|
||||
|
||||
flash[:notice] = "Hello" # Gets flushed on next request
|
||||
flash.keep # Persist flash values
|
||||
flash.now[:error] = "Boo" # Available on the same request
|
||||
|
||||
cookies[:hello] = "Hi"
|
||||
|
||||
params[:page]
|
||||
|
||||
# params is a combination of:
|
||||
query_parameters
|
||||
path_parameters
|
||||
request_parameters
|
||||
|
||||
### respond_to
|
||||
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.xml { render xml: @users }
|
||||
format.json { render json: @users }
|
||||
format.js # Will be executed by the browser
|
||||
end
|
||||
|
||||
### default_url_options
|
||||
|
||||
# The options parameter is the hash passed in to 'url_for'
|
||||
def default_url_options(options)
|
||||
{:locale => I18n.locale}
|
||||
end
|
||||
|
||||
### Filters
|
||||
|
||||
# Filter with callbacks
|
||||
before_filter :authenticate
|
||||
before_filter :authenticate, except: [:login]
|
||||
before_filter :authenticate, only: [:login]
|
||||
def authenticate
|
||||
redirect_to login_url unless controller.logged_in?
|
||||
end
|
||||
|
||||
# Filter with inline
|
||||
before_filter do |controller|
|
||||
redirect_to login_url unless controller.logged_in?
|
||||
end
|
||||
|
||||
# Filter with external classes
|
||||
before_filter LoginFilter
|
||||
class LoginFilter
|
||||
def self.filter(controller) ...; end
|
||||
end
|
||||
|
||||
# Filter exceptions
|
||||
skip_before_filter :require_login, only: [:new, :create]
|
||||
|
||||
# Before/after filters
|
||||
around_filter :wrap_in_transaction
|
||||
def wrap_in_transaction(&blk)
|
||||
ActiveRecord::Base.transation { yield }
|
||||
end
|
||||
|
||||
### HTTP basic authentication
|
||||
|
||||
before_filter :authenticate
|
||||
|
||||
# Basic authentication:
|
||||
def authenticate
|
||||
authenticate_or_request_with_http_basic { |u, p|
|
||||
u == "root" && p == "alpine"
|
||||
}
|
||||
end
|
||||
|
||||
# ...or digest (hashed) authentication:
|
||||
# uses the ha1 hash (username:realm:password)
|
||||
def authenticate_by_digest
|
||||
realm = "Secret3000"
|
||||
users = {
|
||||
"rsc" => Digest::MD5.hexdigest("rsc:#{realm}:passwordhere")
|
||||
}
|
||||
|
||||
authenticate_or_request_with_http_digest(realm) { |user|
|
||||
users[user]
|
||||
}
|
||||
end
|
||||
|
||||
# For integration tests
|
||||
def test_access
|
||||
auth = ActionController::HttpAuthentication::Basic.encode_credentials(user, pass)
|
||||
get "/notes/1.xml", nil, 'HTTP_AUTHORIZATION' => auth
|
||||
end
|
||||
|
||||
# Token auth
|
||||
is_logged_in = authenticate_with_http_token do |token, options|
|
||||
token == our_secret_token
|
||||
end
|
||||
|
||||
request_http_token_authentication unless is_logged_in
|
||||
|
||||
### Request/response
|
||||
|
||||
request.host #=> "www.example.com"
|
||||
request.domain #=> "www.example.com"
|
||||
request.domain(n=2) #=> "example.com"
|
||||
request.port #=> 80
|
||||
request.protocol #=> "http://"
|
||||
request.query_string #=> "q=duck+tales"
|
||||
request.url #=> "http://www.example.com/search?q=duck+tales"
|
||||
request.fullpath #=> "/search?q=duck+tales"
|
||||
|
||||
request.headers # Returns a hash
|
||||
|
||||
request.format #=> "text/html"
|
||||
request.remote_ip #=> "203.167.220.220"
|
||||
request.local? #=> true (if localhost/127.0.0.1)
|
||||
|
||||
request.xhr?
|
||||
|
||||
request.method #=> "POST"
|
||||
request.method_symbol #=> :post
|
||||
request.get?
|
||||
request.post?
|
||||
request.put?
|
||||
request.delete?
|
||||
request.head?
|
||||
|
||||
### response
|
||||
|
||||
response.body
|
||||
response.status #=> 404
|
||||
response.location # Redirect location
|
||||
response.content_type
|
||||
response.charset
|
||||
response.headers
|
||||
|
||||
response.headers["Content-Type"] = "application/pdf"
|
||||
|
||||
### Streaming
|
||||
|
||||
send_data pdfdata, filename: "foo.pdf", type: "application/pdf"
|
||||
send_file Rails.root.join('public','filename.txt') [filename: '..', type: '..']
|
||||
|
||||
### References
|
||||
|
||||
* [Guide](http://guides.rubyonrails.org/action_controller_overview.html)
|
||||
* [HttpAuthentication::Basic](http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic.html)
|
||||
* [HttpAuthentication::Token](http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token.html)
|
48
rspec.md
48
rspec.md
|
@ -101,20 +101,42 @@ title: RSpec
|
|||
|
||||
expect { thing.destroy }.to change(Thing, :count).by(-1)
|
||||
|
||||
### Mocking a class
|
||||
### Mocking - basic
|
||||
|
||||
user_mock = mock "User"
|
||||
user_mock.should_receive(:authenticate).with("password").and_return(true)
|
||||
user_mock.should_receive(:coffee).exactly(3).times.and_return(:americano)
|
||||
user_mock.should_receive(:coffee).exactly(5).times.and_raise(NotEnoughCoffeeExcep
|
||||
ion)
|
||||
book.stub(:title) { "The RSpec Book" }
|
||||
book.stub(:title => "The RSpec Book")
|
||||
book.stub(:title).and_return("The RSpec Book")
|
||||
|
||||
people_stub = mock "people"
|
||||
people_stub.stub!(:each).and_yield(mock_user)
|
||||
people_stub.stub!(:bad_method).and_raise(RuntimeError)
|
||||
# First arg is a name, it's optional
|
||||
book = double("book", :title => "The RSpec Book")
|
||||
|
||||
user_stub = mock_model("User", :id => 23, :username => "pat", :email =>
|
||||
"pat@example.com")
|
||||
### Mocking - consecutive return values
|
||||
|
||||
die.stub(:roll).and_return(1,2,3)
|
||||
die.roll # => 1
|
||||
die.roll # => 2
|
||||
die.roll # => 3
|
||||
die.roll # => 3
|
||||
die.roll # => 3
|
||||
|
||||
### Expectations
|
||||
|
||||
double.should_receive(:msg).with(no_args())
|
||||
double.should_receive(:msg).with(any_args())
|
||||
double.should_receive(:msg).with(1, kind_of(Numeric), "b") #2nd argument can any kind of Numeric
|
||||
double.should_receive(:msg).with(1, boolean(), "b") #2nd argument can true or false
|
||||
double.should_receive(:msg).with(1, /abc/, "b") #2nd argument can be any String matching the submitted Regexp
|
||||
double.should_receive(:msg).with(1, anything(), "b") #2nd argument can be anything at all
|
||||
double.should_receive(:msg).with(1, ducktype(:abs, :div), "b") #2nd argument can be object that responds to #abs and #div
|
||||
|
||||
double.should_receive(:msg).once
|
||||
double.should_receive(:msg).twice
|
||||
double.should_receive(:msg).exactly(n).times
|
||||
double.should_receive(:msg).at_least(:once)
|
||||
double.should_receive(:msg).at_least(:twice)
|
||||
double.should_receive(:msg).at_least(n).times
|
||||
double.should_receive(:msg).at_most(:once)
|
||||
double.should_receive(:msg).at_most(:twice)
|
||||
double.should_receive(:msg).at_most(n).times
|
||||
double.should_receive(:msg).any_number_of_times
|
||||
|
||||
my_instance.stub!(:msg).and_return(value)
|
||||
MyClass.stub!(:msg).and_return(value)
|
||||
|
|
Loading…
Reference in New Issue