Skip to content

Instantly share code, notes, and snippets.

View lightscalar's full-sized avatar

Matthew J. Lewis lightscalar

View GitHub Profile
This is my first test of gist.github.
document.observe("widget:frobbed", function(event) {
console.log("Element with ID (" + event.target.id +
") frobbed widget #" + event.memo.widgetNumber + ".");
});
var someNode = $('foo');
someNode.fire("widget:frobbed", { widgetNumber: 19 });
//-> "Element with ID (foo) frobbed widget #19."
Just discovered this: an easy way to humanize the attributes of a model. Allows you to rename the database column name to something more reasonable. Makes for nicer, more understandable error messages back to the user.
class User < ActiveRecord::Base
HUMANIZED_ATTRIBUTES = {
:user_email => "E-mail address"
}
def self.human_attribute_name(attr)
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
class Book < ActiveRecord::Base
has_one :author
has_many :pages
accepts_nested_attributes_for :author, :pages
end
accepts_nested_attributes_for :author,
:reject_if => proc { |attributes| attributes['name'].blank? }
So, this helps a lot when testing applications using RSPEC that use the authlogic gem for authentication.
Just require and include the following module in your rspec file. Then you can simply write things like:
before(:each) do
login_as_user
end
at the top of your specs, and you should be good to go...
module ControllerHelpers
time.strftime( string ) => string
Formats time according to the directives in the given format string. Any text not listed as a directive will be passed through to the output string.
Format meaning:
%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
@lightscalar
lightscalar / Using Different Paths in Devise
Created October 23, 2010 15:38
Or: How to leave my UsersController the hell alone
We're working in a Rails 3 App with devise attached to the user model
In routes.rb, if we write:
devise_for :users, :path => 'u'
Then we can obviate all the problems with nested resources. Now users will be directed to /u/sign_in and so forth.
@lightscalar
lightscalar / no_test_files
Created July 31, 2011 15:35
How to avoid generating test files when running a Rails generator...
rails g controller sample_controller new --no-test-framework
@lightscalar
lightscalar / rails.test.frameworks.md
Created September 17, 2011 18:58
Setting up my test environment with RSPEC/GUARD/SPORK

Add the following gems to your gem file:

group :development, :test do
  gem 'jasmine'
  gem 'jasminerice'
  gem 'database_cleaner'
  gem 'capybara'
  gem 'rspec', ">= 2.5.0"
 gem "rspec-rails", "&gt;= 2.5.0"
<html>
<link rel="stylesheet" href="styles.css" type="text/css">
<body>
<p>Hello</p>
</body>
</html>