Monday, February 25, 2008

Merb Monday: The Leap Year Edition

News
Official documentation has been updated to include 0.9. Still a little light, but coming along. If you would like to help, create your own git branch and have at it!

Ivey had a baby. Congratulations!

Expect 0.9.1 (developer release) today (or soon).

You get an "extra" day this week. Use it wisely.

Teaser
Code name: Merblets.

Q&A

Q: What!? No RJS?

A: You can do anything rjs can do with *.js.erb templates (Answer by ezmobius)


Q: I read that Merb is template agnostic. What templates languages do I have available?

A: erb out of the box (erubis), haml (merb_haml as a dependency in init.rb), markaby (Answer by sqred)


Q: What does a named route in Merb look like?

A: r.match("/login").to(:controller => "Sessions", :action => "new").name(:login)
(Answer by jodo)


Q: Does Merb have a debugger tool?

A: You can use ruby-debug. Start Merb with the -D option to enable ruby-debug support.
Enter "debugger" somewhere in your code as a breakpoint. (Answer by jdempsey)


Q: How do I find out what version of Merb I'm using?

A: merb -v (Answer by ezmobius)


Q: How can I see the current routes?

A: merb -i
merb.show_routes
(Answer by ezmobius)


Q: Can the cookie session be used for large production apps?

A: As long as you don't store really sensitive info in the session then cookie sessions are fine (Answer by ezmobius)

Monday, February 18, 2008

Merb Monday: State of flux

This week's edition is a little short. Certainly not for lack of activity in #merb, but due to the continued disturbance in the force caused by the 0.9 transition.

0.5 to 0.9 is a big jump (no wonder they skipped 0.6, 0.7 and 0.8!) Most of the discussion this week continued to be related to the transition to 0.9 and trouble ticket type "stuff".

News
0.9 was released (kinda), however this is a "developer" release and still has some wrinkles to be ironed out before a general public release will be pushed to RubyForge. If you are anxious to give it a spin, check Merbivore.

It looks like there will likely be a 0.9.1 "developer" release in the near future. Once we get to a 0.9.x general public release things should settle down and this series should get back to normal.

merbful_authentication should soon be updated to be compatible with 0.9.

Required Reading
0.9 introduces some really cool changes. One of those is Rack integration. You should read Ezra's post "So merb-core is built on rack you say? Why should I care?"

Q: If I use ActiveRecord as the ORM, do I then use ActiveRecord's migrations (and its syntax) by default?

A: Yes. You can use ActiveRecord with Merb as you would with Rails. (Answer by amoeba)

Q: How do you generate an app in Merb 0.9?

A: ./script/generate is gone. In it's place now lives merb-gen.

merb-gen myapp # a normal merb app

merb-gen myapp --flat # a flattened app

merb-gen myapp --very-flat # a single-file app
(Answer by ivey)

Answer Of The Week

And the answer to many of the questions this week which followed the format of:

Q: _____________________ in Merb 0.9?

A: "It's not in -core anymore" (Answer by ivey)

Audience Participation

Q: Assuming you are using ActiveRecord with Merb, how can you share models with a Rails application?

If you leave your answer in the comments, I'll post it next week. (hint) (Question & link by seebg)

Monday, February 11, 2008

Merb Monday: "That's the spirit"

Tidbits from #merb for the week of Feb 4 - Feb 10.

A lot of the conversation this week revolved around the pre 0.9 release. Many of the questions involved bugs or other issues that one can expect in a pre-release. In general I didn't include them as they will quickly be outdated and likely be of little on going value.

Q: How do I install (freeze) gems within my application?

A: Navigate to the root of your Merb application.

gem install foo -i gems
In your merb.yml file include a line:
dependency "foo"
(Answer by wycats)

Q: How do I support a new MIME type? Say for an Atom feed.

A:
Merb.add_mime_type(:atom,:to_atom,%w[application/x-atom])
Replace application/x-atom with whatever the correct header for atom is then make a template: foo.atom.erb

In your controller, use:
provides :atom
(Answer by ezmobius)

Q: Where could I define a constant which would then be accessible in different views, but should have a different value depending on the environment merb is running in?

A: config/environment/*.rb where * represents the desired environment, for example development.rb.

Q: How do I find the current controller and action in the view?

A: In a helper you could create the two following methods.
def controller_name
controller.request.controller_name.split('_').first
end

def action_name
controller.request.action
end
(Answer by slurry)

Q: What happened to MERB_ENV?

A: Merb's application constants have been re-factored as follows:
Old constant : New method

MERB_FRAMEWORK_ROOT : Merb.framework_root
MERB_ROOT : Merb.root
MERB_VIEW_ROOT : Merb.view_path
MERB_SKELETON_DIR : Merb.skeleton_path
MERB_LOGGER : Merb.logger
MERB_PATHS : Merb.load_paths
MERB_ENV : Merb.environment
(Answer by slurry)

Q: Is there a way to list all the generators available?

A: ruby ./script/generate

Q: Where is the source now?

A: The Merb project has switched from using SVN to Git for source code management. With that change the source is now at Github. Specifically you will want to use wycats repo, which you can find at: git://github.com/wycats/

The source code is broken down into merb-core, merb-more, and merb-plugins.

For more information on how to work with Git, see "So you want to contribute to Merb-core".

Once you have cloned the Git repos to your local system, you will then move into that directory and issue a "rake install" command to build the gem.
(Answer by ivey)

Q: Where are bugs / tickets being tracked now?

A: Along with the move to Github, bugs and ticket tracking has been moved from DevJaVu over to Lighthouse, specifically at merb.lighthouseapp.com.

Answer of the week

Here is one little snippet of conversation that may provide the answer regardless of your specific problem:

Q: Can I do _____________?

A:
hassox: I guess so
hassox: but I can't really help you on it...
grrt: :) I'll see how far I get
hassox: that's the spirit :)
grrt: it's great to just explore

Sometimes it's best to just try something out.

Audience Participation

Q: Merb-core has methods "render" and "display". What's the difference?

Leave your answer in the comments. (Hint)

Monday, February 4, 2008

Merb Monday: Environment variables, partials, etc.

I see a lot questions asked and answered in the #merb IRC room.

Many of them are questions that later I wish I remembered the answer to! So I'm going to try to keep track of questions I see asked and answered and record them here for the benefit of myself and others, and hopefully publish them here every Monday.

I might re-phrase the questions and answers slightly, as the writing tends to be quite terse on IRC.

Q: How do you check the current environment in Merb? I'm looking for the Merb equivalent to RAILS_ENV.

A: Merb.environment (Answered by: Booster)

Q: How do you render partials?

A: You use the partial method.

Scenario 1: You wish to render a partial template found in the same directory as your action's view template.

partial :foo
This will look in the current directory for a file named _foo.html.erb

Scenario 2: You wish to render a partial template found in a different directory than the action's view template.
partial 'common/foo'
This will look for the file views/common/_foo.html.erb
(Answered by: jp_n9)

Q: I'm trying to run my db:migrate and I'm getting a "no such file to load -- spec/rake/spectask" error.

A: Make sure you have the rspec gem installed.
sudo gem install rspec
(Answered by: octopod)

Q: How do I start merb in production mode?

A: Use the -e option. For example,
merb -e production
For a complete list of parameters type:
merb --help
(Answered by: Phoop)

Q: How do I get Merb to log data to the log file?

A: Make sure that you start Merb as a daemon with the -d directive, this will cause output to be logged to the log file located in the log/ directory (instead of being output to the console window).
merb -d
(Answered by: ezmobius)

Q: How can I use HAML for my templates?

A: Name your files with the .html.haml extension instead of the default .html.erb extension.

Example: name.html.haml

Second, you need to edit dependencies.rb to include the line:
dependency "haml"
You will also need to have the haml gem installed on your system. Restarting Merb will also likely be required.
(Answered by: QaDeS, bradly)

Q: Is there an easy way to get the current URL in a merb view?

A: I think it's request.uri
(Answered by: hassox)

In my testing the request variable does not appear to be directly available from a view template. However if you need that information you can easily pass the entire request variable, or just request.uri, into your view from the controller.