How does the Rails router Journey work? - ruby-on-rails

Looking at the Readme of the Journey Router (which is the Rails 4.0 router), it is rather empty. I was wondering how the router works conceptually, and if this 'abstract' grammer is catching the idea, of the router as mini-programming-language:
ROUTE := GET|POST|PUT|DELETE path
path := (static_path) | (dynamic_path)
So, how does the Rails 4.0 Router work?

If you want to understand Rails routing, the best place to start is Rails Routing from the Outside In in the edge guide.
After that, you'll have more luck looking at ActionDispatch::Routing's docs.
Note: The following relates to Journey v1.0.4, which was the latest at time of writing.
Journey itself is the Rails routing engine introduced in Rails 3.2. In the release notes, it is described with a single statement: "Route recognition also got a bunch faster thanks to the new Journey engine." That's not a lot of info specifically about Journey, of course, but Rails doesn't describe every implementation detail; that would take forever! :) Journey's gemspec also describes itself with the statement: "Journey is a router. It routes requests."
You could look at the api docs, but in v1.0.4, it has extremely sparse documentation (Journey::Router::Utils.normalize_path(path) is documented) other than the code itself and maybe its open and closed issues. You could take a look at the other S.O. posts with tag journey.
Some of the code is somewhat self-descriptive just via method names, etc. like in Journey::Routes. Journey's tests are also a great way to see how it works.
Some notes on the code itself:
It monkey-patches Hash in pre-1.9 versions of Ruby to add a keep_if method.
"gtg" stands for "generalized transition graph" (see here)
Funniest code is here (Easter egg: rails c then Journey::Path::Pattern.new(9))
Journey's visualizer might be interesting to play around with, also (note visualizer method in Journey::GTG::TransitionTable). Sample visualization here, and online demo here for now.

Related

Looking for a fully functional Rails application using Backbone.js

Backbone.js website has some examples. But barring the first one others are not open source. I am looking for a fully functional (meaning it just works) Rails application to study. The app does not need to have too many functionalities. I looked at github and all the apps are broken in some ways.
Recently i found https://github.com/malclocke/fulcrum and it seems to be the best Rails/Backbone example but its not mentioned on the backbone website. Its also a very functional pivotal tracker clone.
I have been working on some non open source projects that use a Rails and Backbone.js stack. Both frameworks can be integrated fairly easily. Of course, it depends on how the application is setup and how you configure each framework to control more or less business logic.
To get both frameworks to play with each other:
Make Backbone collections and models for each Rails model
Route resources for each Rails model
Setup the URL property for the Backbone collections and models to work with your rails routes
Use fetch() and save() in Backbone to get and post data with Rails
I wrote a german language noun trainer using RAILS and backbone.js. It was done a long while ago while I was still learning but you can peek at it if you want.
https://github.com/bradphelan/ohmyderdiedas
I've been actively working on Myelin: http://sourceforge.net/projects/myelin/ (funded from a corporate source)
There are some caveats:
This is essentially a first for me with every technology in there... from rails, to backbone / jquery / rspec... you name it... it's new, so take the code with some grains of salt ;)
I didn't use the Backbone routing, and built a very simple 'router' of my own.
You'll need ganglia and rrdtool installed (macports if you're on a mac should work)
You'll need to alter the development config for sure.
The models, are (mostly) straight up backbone, and I use sync often in the controllers so those should be pretty good examples.
The views are a little more chaotic.
If anyone needs help with anything, just drop a line to me on sourceforge.
There's now a gem in development that provides generators, called rails-backbone. It's Open Source and getting better every day. As of today it's up to date with current Rails 3.1 (actually 3.2 now), esp. including Asset Pipeline, which is very relevant to backbone.js.

How to create Rails App using cucumber

I am new for cucumber. I want to create on sample application using cucumber.
I am working on Windows 7 OS. Can any one guide me?
Thanks.
I think these are two separate problems:
Building a Rails application on Windows
Building a Rails application with Cucumber (using principles promoted by Cucumber like BDD, Specification by Example and an outside-in workflow)
Building a Rails application on Windows
There are a lot of tutorials, best starting point is probably: Getting Started with Rails on Rails Guides.
However the official advice there is using a virtual machine with Linux :-)
Here is a good thread with advice for running Rails under Windows.
Building a Rails application with Cucumber
An extraordinary example for this is the Book Rails 3 in Action by Manning.
This book really excels at showing how to create an application outside-in with Specification by Example using Cucumber.
In each chapter of the book a new feature is implemented. There are almost no screenshots explaining those features. Instead the authors are using Cucumber scenarios at the beginning of each chapter to exactly specify the desired new behavior that is the topic of given chapter. Then they are showing and explaining the necessary steps and the code that is needed to implement the functionality.
This is the best example I have found for using Cucumber with Rails so far.
This is also a really great example how Specfication by Example can be used for building a shared understanding - since teaching is all about sharing understanding.
However the scenarios in the book are quite focusing of specifying behavior on the UI level ... which is not commonly agreed on being best practice. But for teaching Rails it is great.
Rails 3 in Action is still in the early access program. I don't know where the official sources are.
But when you search on GitHub you already find different implementations of the example application (Tiketee):
https://github.com/rubykr/ticketee
https://github.com/oMartell/ticketee-rails-3-in-action
https://github.com/rails3book/ticketee
Here is the guide how to do it.
I found an interesting webcast video that does just this.
The basic procedure is define a couple features in cucumber. (actually the gherkin language)
Run cucumber
Fix errors
Repeat the
2 steps until you have a
working app.
It makes use of a few RoR gems like webrat and rspec.
http://railscasts.com/episodes/155-beginning-with-cucumber

How to use Rails or Ruby documentation?

I'm coming from C#, and recently I started to write some Ruby on Rails applications.
My biggest problem with it is the documentation because I find it extremly difficult to use. For example, finding out how to call generator from my controller took me about 2 hours and then 15 minutes after that I found Rails::Generators.invoke method to figure out what arguments should I pass to this function.
Maybe I use the documentation the wrong way; First I take a wild guess and search in the Netbeans code completion, which is rarely helpful, then I search in Google, then go with the API.
Can some experienced Rails programmer give me some advice?
If you're new to rails I recommend you read a book before you dive into the API documentation.
Here are two recommendations:
Agile Web Development With Rails - Great introduction and reference on how to use rails.
Programming Ruby 1.9: The Pragmatic Programmers' Guide - More on Ruby. Great references!
After you've worked your way through those books, you'll have an idea of how Rails is designed and where to look for stuff.
Both books are great reference books. If you want to access the Rails and Ruby API's online, make sure to checkout RailsApi.com
I am describing my own very personal workflow with Ruby/Rails documentation:
You can use ri and rdoc if you are familiar with the console, but...
Personally, I recommend two very good Ruby/Rails documentation sites:
apidock.com (no good search, but sometimes fine examples)
railsapi.com (very good AJAX search, plus you can customize what documentation you would like to see - by versions and by Gems)
Every iteration of the Rails guides (http://guides.rubyonrails.org/) has a ton of useful stuff in it. That should be the first stop.
After that I search away in the API - http://api.rubyonrails.org.
Sometimes I even end up just reading the source code trying to find things - https://github.com/rails/rails.
I think the best place in the source code to be reading is in here - https://github.com/rails/rails/tree/master/railties/lib/rails.
I upvoted each of the other answers here.

How to document a rails application?

I just started to document a rails application. I know this is actually done by rdoc, so I followed some rdoc guides regarding syntax and so on, but I got stuck when I tried to describe attributes of models, validations and the relationship between models, mostly because these things are part of ActiveRecord. So I wonder if there is some guide or a good practice regarding how to document a rails application or if there is something I'm missing?
I know that I could put all of this in the class description, but I wonder if there is a way more closely tied to the declaration itself (has_many, validates_presence_of, etc.) and what about the attributes?
I personally prefer YARD - http://yardoc.org , as it does a better job in documenting IMHO.
I don't know if there's a specific handler for Rails available, but it's quite easy to write one - http://yardoc.org/guides/extending-yard/writing-handlers.html
A good example might be the attribute handler - part of the yard gem:
lib/yard/handlers/ruby/attribute_handler.rb
Remember your tests are part of the documentation (for developers), particularly if you are using Cucumber where scenarios are easy to read. If you keep your methods very short and there is a test method with a descriptive name e.g. "should set the users name" I find I typically don't need comments on the method.
Validations or other parts of Rails I would not document. Part of being a Rails developer is understanding how these work, I think it is a fair assumption that another maintainer of your code reading it down the road will know validations, or other things built in to Rails. By that same logic, if you can use features of the framework or happy paths (not deviate much) with [documented] third party code, a lot of the documentation will be written for you.

Rails architecture?

I would like to be able to read Rails source code, but I think I can't really do it, because I don't know were to start and how different parts of the code are connected. For example, it's obvious what ActiveModel does and I can read the code, but I don't understand how a Rails app generally gets loaded and when exactly ActiveModel comes to play and which part of rails invokes it.
I'm not talking about this general stuff people say in manuals that first Rack gets loaded, then Rails is a rack app... No. I want something more detailed, so that I could actually look up every step in the code. So I guess some sort of chart explaining how rails app gets loaded and maybe a note to each of the subdir, saying where those files are used, what they do and where on the load chart they do load would be nice.
Any tips on that?
Rails follows a pretty traditional MVC (Model/View/Controller) design, which you can read up on.
If you'd like to get a good overview of how a request is processed by rails, this looks like a decent place to start:
http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/
Here's a more detailed article about how the Rails initializers and environment are loaded:
http://toolmantim.com/thoughts/environments_and_the_rails_initialisation_process
The link is broken, here is the same article on github.
https://github.com/toolmantim/toolmantim/blob/master/articles/environments_and_the_rails_initialisation_process.haml
It's not yet complete, but this series of articles, Rails from Request to Response by Andrew Berls helped me:
Introduction
Routing
ActionController

Resources