Merb tips II »

Created at: 08.04.2008 05:11, source: Rails on the Run - Home, tagged: logger merb routes tips tricks

I the previous post I covered few useful tips for Merb 0.9. The good news is that Merb should get its wiki setup over the week end!

Here is another batch of hopefully useful tips:

  • In init.rb, you can define a dependency and specify a version number: dependency "merb_fu", ">= 1.0"

  • If you want to run your application from a subdirectory, once again, in your init.rb file, add: c[:path_prefix] = "/your_prefix" (note, that you can also do that in a specific environment file.)

  • You feel like limiting a route to a specific request such as a DELETE? In your router.rb file add the following:

1
2

  r.match("/:bucket_id", :method => :delete).to(:controller => "buckets", :action => "destroy")
  • Since we are talking about routes, what about an iPhone only route?
1
2

  r.match(%r[^/(.+)], :user_agent => /iPhone/).to(:controller => "mobile", :title => "Welcome Apple FanBoy", :action => "show")
  • what about an admin section for my blogposts?
1
2
3
4

  r.match('/admin') do |admin|
    admin.resources :blogposts
  end
1
2
3
4
5
6
7
8
9

  it "should allow you to restrict routes based on protocol" do
    Merb::Router.prepare do |r|
      r.match(:protocol => "http://").to(:controller => "foo", :action => "bar")
      r.default_routes
    end
    route_to("/foo/bar").should have_route(:controller => "foo", :action => "bar")
    route_to("/boo/hoo", :protocol => "https://").should have_route(:controller => "boo", :action => "hoo")
  end

You can set custom routes to only work when connected via SSL, that's just really nice!

  • Other quick tip. Last time we saw how to install locally all the required gems. Well, you can also freeze merb by doing:
1
2

  merb-gen frozen-merb
  • Another common IRC question, how do I use Merb's logger. It's really easy:
1
2

  Merb.logger.info('our stuff')

Where info is the debugging level you want to send your message to.

  • Finally, today in IRC a Rails user asked how reset a session in Merb. Rails has a reset_session method that resets the session by clearing out all the objects stored within and initializing a new session object. Merb simply uses a hash to store sessions, so session.clear will do it ;)

Feel free to add a comment with your Merb tips or leave a question regarding something you can't seem to be able to do with Merb.


more »

Merb tips II »

Created at: 08.04.2008 05:11, source: Rails on the Run - Home, tagged: logger merb routes tips tricks

* this content is now outdated and only applied to Merb 0.9*

I the previous post I covered few useful tips for Merb 0.9. The good news is that Merb should get its wiki setup over the week end!

Here is another batch of hopefully useful tips:

  • In init.rb, you can define a dependency and specify a version number: dependency "merb_fu", ">= 1.0"

  • If you want to run your application from a subdirectory, once again, in your init.rb file, add: c[:path_prefix] = "/your_prefix" (note, that you can also do that in a specific environment file.)

  • You feel like limiting a route to a specific request such as a DELETE? In your router.rb file add the following:

1
2

  r.match("/:bucket_id", :method => :delete).to(:controller => "buckets", :action => "destroy")
  • Since we are talking about routes, what about an iPhone only route?
1
2

  r.match(%r[^/(.+)], :user_agent => /iPhone/).to(:controller => "mobile", :title => "Welcome Apple FanBoy", :action => "show")
  • what about an admin section for my blogposts?
1
2
3
4

  r.match('/admin') do |admin|
    admin.resources :blogposts
  end
1
2
3
4
5
6
7
8
9

  it "should allow you to restrict routes based on protocol" do
    Merb::Router.prepare do |r|
      r.match(:protocol => "http://").to(:controller => "foo", :action => "bar")
      r.default_routes
    end
    route_to("/foo/bar").should have_route(:controller => "foo", :action => "bar")
    route_to("/boo/hoo", :protocol => "https://").should have_route(:controller => "boo", :action => "hoo")
  end

You can set custom routes to only work when connected via SSL, that's just really nice!

  • Other quick tip. Last time we saw how to install locally all the required gems. Well, you can also freeze merb by doing:
1
2

  merb-gen frozen-merb
  • Another common IRC question, how do I use Merb's logger. It's really easy:
1
2

  Merb.logger.info('our stuff')

Where info is the debugging level you want to send your message to.

  • Finally, today in IRC a Rails user asked how reset a session in Merb. Rails has a reset_session method that resets the session by clearing out all the objects stored within and initializing a new session object. Merb simply uses a hash to store sessions, so session.clear will do it ;)

Feel free to add a comment with your Merb tips or leave a question regarding something you can't seem to be able to do with Merb.


more »

Merb tips I »

Created at: 05.04.2008 08:59, source: Rails on the Run - Home, tagged: logger merb tricks

I'm working on a post reporting a recent benchmark I did comparing Rails vs Merb performances for a client's app.

In the meantime, here are few tricks you might need when using Merb 0.9x

  1. In the init.rb file, uncomment and rename c[:session_id_key] (in the Merb::Config.use block)

  2. In the same block, add c[:log_level] = :debug to set a log level

  3. By default, Merb logs to STDOUT, to log to a file, in the config block add c[:log_file] = Merb.log_path + '/development.log' (note that you need to create the file yourself, Merb won't do that)

  4. to save your gems locally, do: sudo gem install gem_name -i gems

  5. need basic HTTP auth? it's now available in core

  6. don't forget to require any plugins, extra gems you need (such as merb_helpers or merb-assets)

  7. don't forget to select your ORM before using the generator( so your generated goodies will be adapted to your ORM)

  8. routes are easy to use. In the console (merb -i) type merb.show_routes to see all your named routes

  9. if you want to use linkto, install merbassets

  10. nested routes example:

1
2
3
4
5
6

  r.resources :channels do |channels|
    channels.resources :shows do |shows|
     shows.resources :episodes
    end
   end

usage:

1
2
3
4

  url(:channel_shows, :channel_id => channel)

  link_to h(channel.description), url(:channel, :id => channel)

That's it for today :)

In the meantime, check this Merb presentation by Ezra and this DataMapper presentation by Wycats


more »

Merb tips I »

Created at: 05.04.2008 08:59, source: Rails on the Run - Home, tagged: logger merb tricks

* this content is now outdated and only applied to Merb 0.9*

I'm working on a post reporting a recent benchmark I did comparing Rails vs Merb performances for a client's app.

In the meantime, here are few tricks you might need when using Merb 0.9x

  1. In the init.rb file, uncomment and rename c[:session_id_key] (in the Merb::Config.use block)

  2. In the same block, add c[:log_level] = :debug to set a log level

  3. By default, Merb logs to STDOUT, to log to a file, in the config block add c[:log_file] = Merb.log_path + '/development.log' (note that you need to create the file yourself, Merb won't do that)

  4. to save your gems locally, do: sudo gem install gem_name -i gems

  5. need basic HTTP auth? it's now available in core

  6. don't forget to require any plugins, extra gems you need (such as merb_helpers or merb-assets)

  7. don't forget to select your ORM before using the generator( so your generated goodies will be adapted to your ORM)

  8. routes are easy to use. In the console (merb -i) type merb.show_routes to see all your named routes

  9. if you want to use linkto, install merbassets

  10. nested routes example:

1
2
3
4
5
6

  r.resources :channels do |channels|
    channels.resources :shows do |shows|
     shows.resources :episodes
    end
   end

usage:

1
2
3
4

  url(:channel_shows, :channel_id => channel)

  link_to h(channel.description), url(:channel, :id => channel)

That's it for today :)

In the meantime, check this Merb presentation by Ezra and this DataMapper presentation by Wycats


more »

How to use github and submit a patch »

Created at: 03.03.2008 10:22, source: Rails on the Run - Home, tagged: contribute git github merb

If you don't know about git and github yet, it's time you clean up your RSS feeds and find some good source of information.

Github is used by the Merb core team and I'll show you how to use github to fork Merb, make your modifications and "submit your patch".

This is the exact reason why github is simply awesome, it makes forking projects just super simple and submitting changes even easier.

First thing, you need to have a github account, if you don't have one yet, email me, I have a couple of invitations left, otherwise, just wait until github gets public.

Now, let's go to Merb's repository and fork Merb-core by clicking on the fork button.

fork merb

Actually, for this example, I'll fork merb-plugins because I want to improve the ActiveRecord rake tasks.

Because I forked merb-plugins, I now have my own forked repo: !my forked repo

I'll start by checking out/cloning my forked repo locally.


git clone git@github.com:matta/merb-plugins.git

!git clone

Great I can now make my own changes.... but wait, what if the merb core team makes a change to the code? Well, I need to track their changes. Here is how:


git remote add coreteam git://github.com/wycats/merb-plugins.git

FYI, it adds the following to edit .git/config:

1
2
3
4

  [remote "coreteam"]
  url = git://github.com/wycats/merb-plugins.git
  fetch = +refs/heads/*:refs/remotes/coreteam/*

then


git fetch coreteam

and finally


git checkout -b coreteam coreteam/master

You can now track the latest change and merge them with your branch. Note that you can also track other forks and merge some other changes. (just that feature is worth using git)

Alright, now you can do your stuff, and push your local change to your remote repo at github.

Once you are done, you can simply click on the "pull request button"

pull request button

fill up the form and select the recipient. (wycats in this example if you want him to merge your changes into the official version of Merb).

pull request

p.s: The github guys are working on a gem to make our loves easier, give http://github.com/defunkt/github-gem/tree/master a try. I'll post about the gem when it will be a bit more stable.


more »