Things to consider when starting a new Rails app
I thought I'd summarize what I've been testing tonight...
Rails Templates
Since Rails 2.3 you can write application templates, which let you specify a bunch of defaults for your new rails app, before actually generating them:
- RubyGems your application will need
- Additional gem sources to use
- Plugins to install
- Things to be generated initially
- Rake tasks to run initially
- Git initialization
- Arbitrary commands you might find useful
To get us started with, here's a simple template:
# rails 2.3: adds gem to your environment.rb # rails 3.x: adds gem to your Gemfile gem 'haml' # config/database.yml is ignored by .gitignore. run "cp config/database.yml config/database.yml.example" # initialize git repository git :init # add the whole app (rails 3 generates a .gitignore file # automatically, unless --skip-git is specified) git :add => '.' # and commit the empty app git :commit => "-m 'initial commit'"
We saw three things here:
- The gem command adds RubyGem dependencies
- By using run we can execute arbitrary shell commands. Note that you cannot run commands that require user input. This is because run eats the output of your command.
- To keep everything under control, the git command lets you specify git commands.
All these commands have in common that they are wrapped to allow pretending. This means that you can run rails with the -p or --pretend option to see what would have been done, without actually doing it.
Of course there are a lot more things you can do, these links should get you where you want:
- The new Rails generator is based on Thor. It inherits from Thor::Group.
- You get to use Thor::Actions and Rails::Generators::Actions
- Many people have written many templates already
Bundler
A clean way to manage your gem dependencies without suffering mixups with your rails plugins and initializers has been due for long. Finally Bundler made it into the mix. I've mentioned the Gemfile above. It works pretty simple:
gem rails, :version => '3.0.0' gem 'haml' # this gem will be pulled via git, built and installed specifically for your application # you could specify a branch or tag to checkout. gem 'authlogic', :git => 'git://github.com/binarylogic/authlogic.git' # (actually you need this in rails 3 right now because authlogic 2.1.6 is broken) group :test do gem 'shoulda' gem 'mocha' gem 'rspec' end
Then, to bring your gems up to date simply issue this:
bundle
You might be asked to type in your password, this allows you to install system wide RubyGems.
HAML, {Comp,S}ASS, Grids
With the HTML Abstraction Markup Language and Syntactically Awesome StyleSheets (which now are even more awesome, as they let us use (S)CSS Syntax again) we can not only spice up our syntax, but also programatically enhance our application layout.
While HAML and S{A,C}SS are the basic tools, Compass is the full-blown framework you'll want to consider when working with grids.
To get HAML and Compass (or sass) into your Rails project, add this to your Gemfile (or your rails generator template, for that matter):
gem 'haml' gem 'compass'
Now if you're familiar with previous releases of Compass, you might be new to this syntax we'll use for making compass install everything to our needs into our application code:
compass init --app rails
This will ask you a few question, which is fine if you're running this once, but again, you might want to put it in a generator template, so here's how to shut compass off:
- Syntax: Use the -x option to specify either sass or scss.
- Stylesheet Sources: Sass defaults to public/stylesheets/sass/, compass defaults to app/stylesheets/, use whatever you want. set it with the --sass-dir option
- CSS Output: option is --css-dir. If you're using sass only, you can point this to public/stylesheets/, but you might have legacy styles in there and want to keep things clean. Again, do what you want.
A full example might look like this:
compass init --app rails -x scss --sass-dir app/stylesheets/ --css-dir public/stylesheets/compiled/
This is a line you could pass to run within a rails generator. Make sure to mention the compass gem beforehand and run bundle if required.
Now to actually get some useful code into your stylesheets, you should install a framework:
compass install blueprint/basic
Blueprint is a good thing to get you started, though you have a wider choice of Compass Plugins at your hand, which can be installed via the same procedure, once installed and made known to compass via the -r option.
If you now point your editor to app/stylesheets/basic.scss (or .sass if you chose that syntax), add this to the end of the file:
# SCSS Version @include blueprint; # SASS Version +blueprint
This way you will have all the basic CSS classes available, that you had using the original blueprint framework:
// _____________
// | | |
// | | |
// | | |
// | | |
// |______|______|
%div.container
%div.span-12
Left Column
%div.span-12.last
Right Column
To dig deeper into compass and blueprint, check out the docs:
jQuery
One of the big news in the rails 3 release was the ability to work JavaScript unobstrusive and agnostic. This means not only, that as little JavaScript as possible is scattered around the HTML (which was getting really messy in prior rails versions), but also that you can freely switch between JavaScript frameworks without breaking all the rails helper you grew accustomed to.
For many rails developers, this is the final punch to eventually switch to jQuery, which opens doors to a huge collection of plugins.
To get jQuery bound to Rails 3, download this rails.js file into your public/javascripts directory:
rm public/javascripts/rails.js && wget http://github.com/rails/jquery-ujs/raw/master/src/rails.js -O public/javascripts/rails.js
Now download the latest jQuery release, put it there as well and adjust your default javascripts in config/application.rb:
# ...
config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
# ...
The application.js file will automatically be included as well. To make this actually work you need to link your jquery.js:
ln -s public/javascripts/jquery-1.4.2.min.js public/javascripts/jquery.js
This way you just need to change the symlink around in order to switch to a new jQuery version. Do the same with all the jQuery plugins you choose to build on.
*Update:* Instead of downloading and copying the js files yourself, you can also use the 'jquery-rails' gem., which provides a generator (rails generate jquery:install) to set up your rails app working with jquery.
As a final word about jQuery I'd like to point out the Compass jQuery Plugin. I haven't tried it out myself yet, but it's description looks pretty promising. Drop a note if you have any news about it.
Trackbacks
Verwenden Sie den folgenden Link zur Rückverlinkung von Ihrer eigenen Seite:
http://praktikanten.brueckenschlaeger.org/trackbacks?article_id=267