Recent notes
RSS feedInteresting usage for polymorphic asset model :)
…to automatically define default scopes of inherited classes.
class Asset < ActiveRecord::Base belongs_to :resource, :polymorphic => true before_save :set_asset_type def set_asset_type self.asset_type = self.class.name end def self.inherited(subclass) super subclass.send(:default_scope, :conditions => "asset_type='#{subclass.name}'") end end
W3CDTF Format
Here is the formatted string for the W3CDTF datetime format (http://www.w3.org/TR/NOTE-datetime). It has a semicolon in the timezone part, therefore you cannot use ‘%z’:
Time::DATE_FORMATS[:w3cdtf] = lambda { |time| time.strftime("%Y-%m-%dT%H:%M:%S#{time.formatted_offset}") }
Using html text instead of default response
If you have a string containing html and want to assert_select against it, as the doc states you have to pass in an element (HTML::Node) as the first argument. You can do something like this:
doc = HTML::Document.new('<p><span>example</span></p>') assert_select doc.root, 'span'
Escape brackets in selector
If you need to escape brackets in a selector, this is the way to do it:
assert_select "input[type=hidden][name='user[role_ids][]']"
Reverse version of camelize
Reverse version of camelize is underscore
Argument Accepted
Accepts a single argument record_separator which is the character or string to chomp.
Why isn’t this shown in the method def at the top?
Argument Accepted
Accepts a single argument sep_string
Undeprecated version
The undeprecated version of this function is here: ActionView::Helpers::PrototypeHelper#link_to_remote
Use this in controllers
Sometimes you’re gonna need this in controllers. Just put this in the controller:
include ActionView::Helpers::NumberHelper
:find takes more keys than written
The documentation says that the :find keywords “may include the :conditions, :joins, :include, :offset, :limit, and :readonly options”. Note that this does not mean that only those options are supported. :sort also works like it should, for example.
Will this method get rid of existing data?
Will this method get rid of existing data?
Default fallback
You can specifly :default option which is useful when the translation is not found. For example:
t(:this_translation_doesnt_exist, :default => 'Ooops!') # => Ooops!
Or even any number of “fallbacks” - the first not nil is returned:
t(:missing, :default => [:missing_too, :existing, 'Sad panda']) # => :existing translation
Good introduction to Rails I18n is http://guides.rubyonrails.org/i18n.html
Includes descendants
May be helpful to know that this returns true if A is any descendant of B, not just a direct one. As an example:
class Foo; end class Bar < Foo; end class Baz < Bar; end Bar < Foo #=> true Baz < Foo #=> true
If you want direct descendance try Class#superclass:
Bar.superclass == Foo #=> true Baz.superclass == Foo #=> false
Status Codes
Full detail: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
-
100 - Continue
-
101 - Switching Protocols
-
200 - OK
-
201 - Created
-
202 - Accepted
-
203 - Non-Authoritative Information
-
204 - No Content
-
205 - Reset Content
-
206 - Partial Content
-
300 - Multiple Choices
-
301 - Moved Permanently
-
302 - Found
-
303 - See Other
-
304 - Not Modified
-
305 - Use Proxy
-
306 - No Longer Used
-
307 - Temporary Redirect
-
400 - Bad Request
-
401 - Not Authorised
-
402 - Payment Required
-
403 - Forbidden
-
404 - Not Found
-
405 - Method Not Allowed
-
406 - Not Acceptable
-
407 - Proxy Authentication Required
-
408 - Request Timeout
-
409 - Conflict
-
410 - Gone
-
411 - Length Required
-
412 - Precondition Failed
-
413 - Request Entity Too Large
-
415 - Unsupported Media Type
-
416 - Requested Range Not Satisfiable
-
417 - Expectation Failed
-
500 - Internal Server Error
-
501 - Not Implemented
-
502 - Bad Gateway
-
503 - Service Unavailable
-
504 - Gateway Timeout
-
505 - HTTP Version Not Supported
Doesn't return nil if the object you try from isn't nil.
Note that this doesn’t prevent a NoMethodError if you attempt to call a method that doesn’t exist on a valid object.
a = Article.new a.try(:author) #=> #<Author ...> nil.try(:doesnt_exist) #=> nil a.try(:doesnt_exist) #=> NoMethodError: undefined method `doesnt_exist' for #<Article:0x106c7d5d8>
This is on Ruby 1.8.7 patchlevel 174
Replacing with "\" and match — a simple solution
A somewhat different approach to the same problem:
v.gsub(/(?=\W)/, '\\') #=> Foo\ Bar\!
But from what you are trying to achieve I suspect you might be interested in Regexp.escape method :)
Attribute names are Strings, not Symbols
Another possible gotcha – the returned hash keys are of type String, not Symbol:
user.attributes["login"] # => "joe" user.attributes[:login] # => nil
expire_page outside controller and sweepers
If you want to expire cached pages from scripts or console just use class-method expire_page. But don’t forget about difference between instance and class method. In class-method you pass page url, not a hash of action/controller:
ActionController::Base.expire_page your_page_url
Expire cache from console or whatever...
I know only one possible method:
ApplicationController.new.expire_fragment(:your_fragment)
Version Ranges
To specify a version range, use array syntax like this:
config.gem 'paperclip', :version => ['>= 2.3.1.1', '< 3.0']
The example will, of course, match any version 2.3.1.1 or newer up until (not including) 3.0 or later.