Rails 3, RSpec and Cucumber setup »
Created at: 27.09.2010 22:53, source: Slash Dot Dash, tagged: ruby Ruby on Rails
This quick how-to runs through creating a new Rails 3 application with both RSpec and Cucumber for testing (rather than Test::Unit).
Rails 3 requires (the somewhat confusingly versioned) RSpec 2 and rspec-rails. Similarly, Cucumber uses cucumber-rails for bootstrapping your Rails app and provides generators for creating features.
Install RSpec 2
sudo gem install rspec-rails --pre
Install Cucumber
sudo gem install cucumber-rails
Create a new Rails 3 application
Ensure you have the latest version of Rails 3 installed then create a new application, skipping creation of the test directory since we’ll be using RSpec and Cucumber (with spec and features) instead.
rails new --skip-testunit
Add rspec-rails to the :test and :development groups to the Gemfile.
group :test, :development do
gem 'rspec-rails', '>= 2.0.0.beta.22'
end
Add cucumber-rails and dependencies to the :cucumber group in the Gemfile:
group :cucumber do
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'spork'
gem 'launchy'
end
Ensure all gems are installed by using Bundler:
sudo bundle install
Then install the RSpec extensions (creates the spec directory and creates a rake task):
rails generate rspec:install
Now we can specify that Rails itself uses RSpec when running generators by adding the following inside config/applicaton.rb:
config.generators do |g|
g.test_framework :rspec
end
Next, bootstrap your Rails app, for Cucumber:
rails generate cucumber:install --rspec --capybara
That’s it! Now the following rake tasks exist to execute your specs or cucumber features (by default rake will execute both).
rake spec
rake cucumber
more »
Rails 3 scopes with chaining »
Created at: 25.09.2010 15:33, source: Slash Dot Dash, tagged: Ruby on Rails
Here’s a quick gotcha you should be aware of when using the fantastic new scope facility in Rails 3 (formerly named_scope).
Here’s a snippet that demonstrates how to chain scopes, allowing you to call Post.recent to get posts that have been published, ordered by date.
class Post < ActiveRecord::Base
scope :published, lambda { where('published_at IS NOT NULL AND published_at <= ?', Time.zone.now) }
scope :recent, published.order(:published_at)
end
However it contains a subtle bug; when chaining a lambda scope you must also wrap it with a lambda or else you will end up with the wrong result. The correct example is as follows.
class Post < ActiveRecord::Base
scope :published, lambda { where('published_at IS NOT NULL AND published_at <= ?', Time.zone.now) }
scope :recent, lambda { published.order(:published_at) }
end
José Valim explains the reason in the Rails ticket #4960.
class Auction < ActiveRecord::Base scope :started, lambda { where("starting_at <= ?", Time.now) } scope :unfinished, lambda { where("ending_at > ?", Time.now) } scope :active, started.unfinished endYou can chain scopes, but they will be evaluated at the moment you call them.
That said, when you call started, it will execute the lambda, so it will have a frozen Time.now. In other words, chaining lambda scopes will likely give you the wrong result.
You need to wrap the scope :active in a lambda as well.
scope :active, lambda { started.unfinished }
more »
Moving (again) http://onrails.org from Heroku to EC2 »
Created at: 21.09.2010 00:48, source: OnRails.org, tagged: Ruby on Rails heroku ec2
If you read this then this blog is now running Typo 5.5 on Rails 2.3.9 on EC2.
I really love the ease of deployment to heroku and the fact that they manage the whole stack. However I receive regularly emails from Heroku stating that I get a specific number of “backlog too deep errors” indicating the app is receiving more requests than it is capable of responding to, or in other words that that my blog is outgrowing their developer instance. So I have the option to increment the number of requests my application can handle, which is easy thanks to the the heroku command line: heroku dynos +1 —app onrails. So I can increase the HTTP performance by cranking up the “dynos” to 2 which would be an additional $36 a month. I still think it’s a good deal for a managed environment.
However you also saw on my last blog entry that there are other issues with running Typo on Heroku and therefore I started playing with running Rails on EC2. And I like what I see and now that Amazon has an official and secured Amazon Linux AMI, I’ll be using that. Here are a few notes on how I did set it up.
I started with the new Basic 32-bit Amazon Linux AMI 1.0 (http://aws.amazon.com/amazon-linux-ami/). I take basically only a few clicks to get a server started. You can use the ec2 console (https://console.aws.amazon.com/ec2) to create and connect to the server, however you need to use ec2-user instead of root to connect due to the way the Amazon Linux AMI is setup.
Note Lee is right that if I continue to moving host I should create a Chef recipe for this.
Install dev tools
$ sudo yum install git
$ sudo yum install make gcc-c++ zlib-devel openssl-devel
$ sudo yum install ruby-devel ruby-libs ruby-mode ruby-rdoc ruby-irb ruby-ri ruby-docs
Install MySQL
$ sudo yum install mysql mysql-server
$ sudo /etc/init.d/mysqld restart
$ mysql_secure_installation
$ sudo yum install mysql-libs mysql-devel
Install ruby-mysql
$ sudo gem install mysql —no-ri —no-rdoc
Install rubygems
$ wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.zip
$ tar xzvf rubygems-1.3.7.tgz
$ cd rubygems-1.3.7
$ sudo ruby setup.rb
$ cd ..
Install Rails (note I use 2.3.9 for Typo )
$ sudo gem install rails —no-ri —no-rdoc
$ sudo gem install rails —no-ri —no-rdoc -v=2.3.9
Install Apache (2.2.15)
$ sudo yum install httpd
Install Passenger
$ sudo gem install passenger —no-ri —no-rdoc
$ sudo yum install httpd-devel
$ passenger-install-apache2-module
Configure Passenger
nano /etc/httpd/conf/httpd.conf
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15
PassengerRuby /usr/bin/rubySuppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:
<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /somewhere/public
<Directory /somewhere/public>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>Installing Typo (from source)
see http://wiki.github.com/fdv/typo/install-typo-from-sources
$ wget http://rubyforge.org/frs/download.php/71779/typo-5.5.zip
then unzip, that’s it. That’s your typo instance, now let’s configure it’s database.
Note I moved it to /var/www/typo/
Create config/database.yml i.e.
production
adapter: mysql
host: localhost
username: yourusername
password: secret
database: onrails_prod
Change the Rails version in config/environment.rb, replace 2.3.8 with 2.3.9
RAILS_GEM_VERSION = ‘2.3.8’ unless defined? RAILS_GEM_VERSION
setups gems required for Typo:
$ sudo rake gems:install
Creating/Copying database:
Note I migrated my blog so I need to copy the database to the new server.
$ scp -i .ssh/linux_ami.pem onrails_org.sql ec2-user@ec2-184-73-20-188.compute-1.amazonaws.com:onrails_org.sql
$ mysql -u yourusername -p
create database onrails_prod;
exit
$ mysql -u yourusername -p onrails_prod < onrails_org.sql
As I also update Typo to a new version I had to run a migration:
$ rake db:migrate RAILS_ENV=production
Note if you want a clean install you can just to a rake db:create and rake db:migrate.
Et voila! Well Heroku made it simpler but I always have fun playing with EC2.
Enjoy!
Daniel
more »
Announcing RailsDeveloper »
Created at: 01.09.2010 20:01, source: Robby on Rails, tagged: Ruby on Rails ruby programming PLANET ARGON railsdeveloper rubyonrails rails
Earlier today… our team at Planet Argon launched a new site for the Ruby on Rails community. If you have a few spare minutes, I’d love it if you’d to head over and read the announcement on RailsDeveloper.
Enjoy!
more »
Rails 3.0 Released (And 22 Free Videos To Bring You Up To Speed) »
Created at: 30.08.2010 03:21, source: Ruby Inside, tagged: News Ruby on Rails
Rails 3.0 has been underway for a good two years, so it’s with immense pleasure that we can declare it’s finally here. We’ve brought the work of more than 1,600 contributors together to make everything better, faster, cleaner, and more beautiful.
David Heinemeier Hansson
DHH rings the bell and announces that Rails 3.0 (final) has been released after two years of determined effort by the Rails core team (and, significantly, Merb team members, since Rails 3.0 is heavily influenced by the Merb merger). Grab it now with gem install rails --version 3.0.0 or, if you're in no rush, Rails 3.0.1 might come along within a week or two.
The Videos
DHH gives a quick roundup of some of Rails 3's new features but like Emma Watson's head PhotoShopped onto yet another naked body, it's nothing you haven't seen before. If you're really fresh to Rails 3.0, though, Gregg does an admirable job of boiling everything down into a digestible format with his (free!) Dive Into Rails 3.0 screencast series:
- Getting Started and Action Dispatch
- Bundler and Action Mailer
- Active Relation and Active Model
- Cross-site scripting and Unobtrusive JS
- The New Action Controller
Ryan Bates has also produced a fistful of his typically succinct but precise RailsCasts videos on a wide array of Rails 3.0 topics. Ryan always focuses on code and practicalities so these are a good place to start if you want to follow along and do some coding yourself:
- Upgrading to Rails 3.0: Part 1, Part 2 and Part 3
- Routing in Rails 3
- Active Record Queries in Rails 3
- Advanced Queries in Rails 3
- Controllers in Rails 3
- Rack in Rails 3
- Subdomains in Rails 3
- Active Model
- Generators in Rails 3
- Making Generators in Rails 3
- Validations in Rails 3
- ERB Blocks in Rails 3
- Action Mailer in Rails 3
- XSS Protection in Rails 3
- Bundler
If you don't like videos, still follow the links, because there are links to the ASCIIcasts regular HTML versions of the Railscasts videos. These are regular blog posts that you can follow at your own pace.
Or some books
Michael Hartl's Rails Tutorial book is the #1 (and only, in my opinion) place to start when it comes to books about learning Rails 3.0. Not only is it available to read for free online, but you can buy a well formatted PDF too. It's an amazing piece of work and, unusually, walks you through building a Rails app from start to finish with testing. If you want to just read one book/site and feel like a Rails 3.0 master by the end of it, pick RailsTutorial.org.
If you speak German, though, check out this "Ruby on Rails 3" book by Michael Voigt and Stefan Tennigkeit. It's one of the first Rails 3.0 specific books to hit the presses.
Or just dive into some code
If you want to just "get started" and check out a working Rails 3.0 application, try Daniel Kehoe's Rails3-Subdomain-Devise app. It's a basic Rails 3.0 app that demonstrates using the Devise authentication system, as well as custom subdomain access. Not just that, but Daniel has put together a walkthrough of how the app works and how it was put together.
more »
