Rowan Hick @ Toronto Rails Night, June 2008
(Adapted S9 Version from Original PDF Slide Deck)
Super Bike
Sport Cars
The Porsche, you just open the door, turn the key, and drive. It has a/c, a stereo, plush upholstery, everything. Accidents can and do happen but few and far between, but pretty user friendly.
The sports bike, it’s raw, unadulterated. You can’t hop on it wrapped in cotton wool, way easier to get burned. However it’s faster, easier to replace, modify and tune things at a lower cost.
Rails is…
Merb is…
merb-core is the basic slimmed down bare minimum codemerb-more contains niceties to help you along your way (action-args, assets, builder, cache, freezer, gen, haml, jquery, mailer, parts, slices)To start off with get both core and more
gem install merb-core merb-more
Choose your ORM
gem install merb-activerecordgem install sequelgem sources -a http://gems.datamapper.org && sudo gem install addressable english rspec data_objects do_mysql do_postgres do_sqlite3 dm-core dm-more
merb-gen
rowan$ merb-gen app speeddemon
RubiGen::Scripts::Generate
create log
create app
...etc
merb-gen app yourapp --flat (or) --very-flat (puts your application on a serious diet)| . | Rails | Merb |
| Routes | config/routes.rb |
config/router.rb |
| Initialization | config/environment.rb |
config/init.rb |
| Environment specific | config/environments/* |
config/environments/* |
| Plugins | vendor/plugins |
gems |
| Migrations | db/migrate |
schema/migrations |
| Testing | test |
test (or) spec |
| Public | public |
public |
| Models | app/models |
app/models |
| Controllers | app/controllers |
app/controllers |
| Helpers | app/helpers |
app/helpers |
| Lib | lib/ |
(config in init.rb)
|
config/init.rbChoose your ORM
use_orm: activerecord use_test: rspec
Include dependencies (eg mailer)
dependencies "merb_helpers", "merb-parts" Merb::BootLoader.after_app_loads do # Add dependencies here that must load after the application loads: dependencies "merb-mailer" # dependency "magic_admin" # this gem uses the app's model classes end
database.ymlPatience. Because you’ve told merb you want active record, just fire merb for the first time:
rowan-laptop:speeddemon rowan$ merb ~ Loaded DEVELOPMENT Environment... ~ loading gem 'merb_activerecord' ... ~ loading gem 'activerecord' ... ~ No database.yml file found in /Users/rowan/testing/speeddemon/config. ~ A sample file was created called database.sample.yml for you to copy and edit.
Copy, edit and save.
merb-gen generatorsLet’s you know what you can create automagically:
rowan-laptop:b2b_merb rowan$ merb-gen generators ..... Installed Generators Rubygems: authenticated, controller, database_sessions_migration, migration, model, part_controller, resource, resource_controller
see more rubigen.rubyforge.org
merb-gen model mymodelSynonymous with script/generate mymodel
Extra points:
merb-gen again$ merb-gen resource Product title:string
Looks reasonably similar:
class Products < Application
before :check_access_level
provides :xml
def index
@products = Product.find(:all, :order => :sort_order )
display @products
end
def show
@product = Product.find_by_product_id(params[:id])
raise NotFound unless @product
display @product
end
...
| Rails | Merb |
products_controller |
products |
redirect_to( hash ) |
redirect( string ) |
respond_to |
provides / only_provides / display |
before/after_filter |
before/after |
except |
exclude |
render(hash) *optional |
render or render('hello world') *mandatory to get a view |
logger |
merb.logger |
url_for |
url |
displayprovides :xml def index @product = Product.find(params[:id]) display @product end
http://127.0.0.1:4000/product/2.xmlindex.xml.erb, if not found then calls @product.to_xmldef my_action(id) product = Product.find(id) end
id from params[:id]merb-more (merb-action-args)merb-more (merb-parts)In config/init.rb
dependency 'merb-parts'
Use merb-gen to set it up for you
$ merb-gen part_controller widget ... create app/helpers/widget_part_helper.rb create app/parts/widget_part.rb create app/parts/views/widget_part/index.html.erb
Edit action(s) then call it from a view
<%= WidgetPart => :index %>
app/controllers/exceptions.rbdef show @product = Product.find_by_product_id(params[:id]) raise NotFound unless @product display @product end
exceptions.not_found methodExample from Yehudas slides at RailsConf
Merb::Router.prepare do |r|
r.match(%r{/login/(.*)}, :user_agent => /MSIE/).
to(:controller => "acount",
:action => "login",
:id => "[1]"
end
merb -p 3000 -e development
merb -p 3000 -e production -a thin
Should I use it ?