33
Evarist Lobo 2007 1 What is Hobo ? Evarist Lobo [email protected]

What Is Hobo ?

Embed Size (px)

DESCRIPTION

This is an introduction to Hobo a web application builder for Ruby on Rails. It helps beginners to understand the basic concepts and get started

Citation preview

Page 1: What Is Hobo ?

Evarist Lobo 20071

What is Hobo ?

Evarist Lobo

[email protected]

Page 2: What Is Hobo ?

Evarist Lobo 20072

Introduction

Hobo is an Open Source extension to Ruby on Rails which helps you build full blown web applications incredibly quickly and easily. Available as a Gem or Rails plugin.

Builds on other available plugins Hobo provides a simple, clean and elegant

development framework which allows for rapid prototyping or production of the most sophisticated web applications.

Created by Tom Locke

Page 3: What Is Hobo ?

Evarist Lobo 20073

Main Features

Rapid implementation of dynamic Ajax interfaces in your application with no extra programming.

Switchable themes. Customise and tweak your application structure and layout to meet any design goals.

Powerful mark-up language, DRYML, combines rapid development with ultimate design flexibility. The end of the cookie cutter blues!

Rails extends Ruby Hobo extends Rails !

Page 4: What Is Hobo ?

Evarist Lobo 20074

Audience

Hobo is a powerful tool for professional web developers who want to speed up the creation of rich, complex applications, as well as simple ‘off the cuff’ prototypes.

Hobo is great for designers too because the concise and elegant DRYML code makes design changes a snap to implement.

Newcomers to Ruby on Rails can use Hobo as a fast and easy way to learn and practice agile, iterative application development.

Page 5: What Is Hobo ?

Evarist Lobo 20075

What can Hobo do?

Hobo is nothing but extensions to Rails! There’s nothing that Rails can do that Hobo

can’t. DRYML Ajax Rendering Hobo Rapid Tag Library ActiveRecord Permission System

Page 6: What Is Hobo ?

Evarist Lobo 20076

What can Hobo do ?

Cross-Model Search Switchable Themes User Management (Acts As Authenticated) ActiveRecord Composable Query

Mechanism Generic Model Controller (CRUD Support) Migration Enhancements ID Names

Page 7: What Is Hobo ?

Evarist Lobo 20077

What is DRYML?

A powerful tag and template language Tags can be composed of other tags Contains anything you can put in an XHTML

page Simpler to use than Erb Comes with a large number of helpful tags Looks like HTML

Page 8: What Is Hobo ?

Evarist Lobo 20078

Ajax Rendering

Integrated with Scriptaculous Comes with an editor integrated Very simple to use Ability to mark a section of your page as a

“part”. Having done this, Hobo will track the object that was in context when the part was rendered, and can re-render the part using the new state of that object when requested.

Page 9: What Is Hobo ?

Evarist Lobo 20079

Hobo Rapid Tag Library

A tag library is basically just like a load of Rails helpers, but as DRYML tags instead of methods. Hobo Rapid contains the Hobo equivalent of link helpers, form helpers, ajax helpers, and pagination helpers. There’s also some “scaffold-like” tags for default pages, and support for navigation bars.

Page 10: What Is Hobo ?

Evarist Lobo 200710

ActiveRecord Permission System

Just a simple convention for declaring who’s allowed to do what to your models (create, update, delete and view). The permissions come into effect in two places: in customizing the views, and in validating http requests.

View customization is provided in the tag library. The output of various tags is dependent on permissions.

Page 11: What Is Hobo ?

Evarist Lobo 200711

Cross-Model Search

Fairly simple but useful. Models can declare which columns are searchable (there are sensible defaults of course!) and Hobo provides both server and client-side support for a nice ajaxified search that finds many types of model from a single search page.

Page 12: What Is Hobo ?

Evarist Lobo 200712

Switchable Themes

A Hobo theme consists of public assets such as a stylesheet and images, and some tag definitions.

The tags define the basic HTML structure of the various theme elements - the overall page, header and footer, navigation bar, sidebars, panels (boxed sections within the page) etc.

Themes are switchable by changing a configuration variable.

Page 13: What Is Hobo ?

Evarist Lobo 200713

User Management (Acts As Authenticated)

In order for the permission system to work, a user model must be present.

For that reason we decided to go ahead and include AAA in Hobo.

The user model has been merged with Front controller The user model methods that are not commonly changed have

been moved out into a module to keep your user model clean. Hobo also adds the concept of a guest user, this is a pseudo

model that is not stored in the database. It’s just a place to define the permissions for someone who has

not logged in.

Page 14: What Is Hobo ?

Evarist Lobo 200714

Front controller

Hobo’s “front” controller gives you your front page and related pages like search, login and signup for FREE !

Also creates associated pages in the view You can customize them using DRYML

Page 15: What Is Hobo ?

Evarist Lobo 200715

ActiveRecord Composable Query Mechanism

It’s common practice with ActiveRecord to write query methods to capture standard queries your application needs beyond the magic find_by_* methods.

You are to be able to compose queries: find everything that matches this query or that one; find the first record that matches some query and some other one.

Page 16: What Is Hobo ?

Evarist Lobo 200716

CRUD Support

generate hobo_model_controller <model-name>

Hobo derives the model class from the controller class name

The basic CRUD methods are created: index, show, new, create, edit and update

also has support for your has_many and belongs_to associations

Page 17: What Is Hobo ?

Evarist Lobo 200717

CRUD support

Support for your associations when creating / updating models.

When you post (create) or put (update), you can set both has_many associations and belongs_to associations.

You can either pass the id of an existing record, or pass entirely new records in nested parameter hashes.

There’s support for all this in DRYML and Hobo Rapid

Page 18: What Is Hobo ?

Evarist Lobo 200718

Data filters

Data filters allow you to expose composable query mechanism to the web

A page with all the people not in the group with id 7 would then be available at

/people?where_not_in=group_17

Page 19: What Is Hobo ?

Evarist Lobo 200719

Enhanced autocomplete

Rails supports Scriptaculous auto-completer through the helper auto_complete_for

Hobo’s version is enhanced to work with the data filter mechanism.

Hobo Rapid has an auto-completer tag

Page 20: What Is Hobo ?

Evarist Lobo 200720

Remote procedure calls

Write an action in your controller as normal. Add web_method <method-name>.

Hobo will add a route for your method, and apply a before filter so the object in question is already available in @this.

Hobo’s AJAX mechanism supports updating the page with results from your method, and there’s integration into the permission system too.

Page 21: What Is Hobo ?

Evarist Lobo 200721

Alternative show methods

Alternatives to the standard view show_method :profile create app/views/users/profile.dryml. You also get a named route person_profile

so you can call person_profile_url(fred) and get the URL back.

Page 22: What Is Hobo ?

Evarist Lobo 200722

Migration Enhancements

This is a little one, but very useful. In create_table calls, you can add columns like this

t.string :name, :subject, :limit => 50 instead of t.column :name, :string, :limit => 50

t.column :subject, :string, :limit => 50 There’s also t.auto_dates that gives you updated_at and created_at, and

t.fkey :zip, :zap That will give you integer columns zipid and zapid

Has been used in latest version of Rails

Page 23: What Is Hobo ?

Evarist Lobo 200723

ID Names

In ActiveRecord everything has a numeric ID. It’s common however, that models also have an identifying name (e.g. a name column where every row is unique).

If a model has an ID name like this, it’s nice to be able to sometimes use it instead of the numeric ID, for example in readable URLs.

If your model declares hobo_model, you can also declare id_name <column-name> If you don’t specify a column it defaults to name.

This gives you a read/write attribute id_name which is just an alias for the column in question. You also get find_by_id_name on the class.

Page 24: What Is Hobo ?

Evarist Lobo 200724

ID Names

If you are using Hobo’s model_controller you can then use names instead of IDs in your URLs, e.g. (using ID names in combination with the associations support):

/categories/News/posts The helper object_url will generate this kind

of URL too - if the object you pass supports ID names.

Page 25: What Is Hobo ?

Evarist Lobo 200725

DRYML Details

DRYML: The Dont-Repeat-Yourself Markup Language. In case you hadn’t guessed, we’re intending to bring a higher level of re-use to Rails’ views.

Why? Rails already has a bunch of mechanisms for re-use within views. Namely: layouts, partials and helpers. Each serves a different purpose, and yet, IMHO, there’s still a gap. I find that small fragments of mark-up tend to crop up over and over in my views. Yes I could extract them into paritals or helpers, but I find myself disinclined to do so.

Helpers are good for small bits of dynamic content, but not so convenient for re-use of HTML fragments - I don’t really want to embed loads of HTML in strings in Ruby source. Nor do I want to make excessive use of the tag helper. I want to put mark-up in mark-up files.

.

Page 26: What Is Hobo ?

Evarist Lobo 200726

DRYML Details

So partials would be appropriate. But each partial needs it’s own file, which again discourages me from making heavy use of them.

If you ever needed to call a helper passing in HTML mark-up in the parameters, that’s HTML within Ruby within HTML — not pretty.

ERB doesn’t need fixing. DRYML supports ERB scriptlets, but allows you to hide them away behind nice clean custom tags, leaving you with beautiful clean templates [swoon] where you can see what’s going on at a glance

Page 27: What Is Hobo ?

Evarist Lobo 200727

DRYML Details

Defining a tag essentially just creates a new helper method for you, and a use of that tag is nothing more than a call to that helper. This is just syntax. Witness:

hello_world.dryml <def tag="hello">Hello World!</def> <p>Here it is: <hello/></p>

<p>And here it is again: <%= hello %></p> Groovy baby :-) (check out the Hello World post to see how to get set up so you can try

this) And just as you can call a tag from Ruby instead of mark-up, you can

define them in Ruby if you wish. You can do so in your helpers. Try this

app/helpers/application_helper.rb module ApplicationHelper include Hobo::DefineTags

def_tag :from_ruby do "As easy as that eh?" end end

Page 28: What Is Hobo ?

Evarist Lobo 200728

DRYML Details

That will give you a <from_ruby/> tag in all of your DRYML templates. If your tag contains more code than mark-up, it’s cleaner to define it in a module like this.

You could even define the tag in a helper module and call it from an ERB scriptlet, but that would be kinda strange… Hey — knock yourself out, it’s all good :-)

I’m trying not to get ahead of myself because there’s a Quick Guide to DRYML coming right up. I just want to show you that this is just a thin layer on top of your regular Rails template. They’re just purdier is all.

Actually there’s a lot more under the hood in DRYML — it’s not just syntax. We’ll get there.

Page 29: What Is Hobo ?

Evarist Lobo 200729

CRUD Details

The generic controller. A standard implementation of the CRUD actions that can be used out-of-the-box in a large majority of cases.

The idea is that the concrete controllers that make up an application will need no custom code at all — just a few declarations: I need an auto-completer for this attribute, an ajax-setter for that one… I don’t see why this can’t be done.

Of course if an application needs to do something unusual, then sure you might want a custom action or two. But a huge number of apps are really nothing more than a web-interface to a database, and nearly all apps have at least some parts that fit this description.

There are a couple of challenges though. Firstly, what should these standard actions render? It’s all very well to have a single URL that you hit to, say, create a new event in your calendar, but depending on where in your app you’re coming from, you’re likely to want a different page (or ajax update) to follow.

Page 30: What Is Hobo ?

Evarist Lobo 200730

CRUD Details

Simple answer - parameterize it. Have the browser request “create me a new event, then refresh parts a, b, and c of my page”. That functionality is now part of Hobo and seems to work great. As well as getting us closer to a fully generic controller, this idea has also yielded a very simple approach to ajax.

Another problem I’ve hit is that sometimes an application feature requires a whole graph of related models be created in one go. The solution to that one has been to extend the way ActiveRecord handles the hash you pass to MyModel.new. With Hobo’s ActiveRecord extensions, that single call to new can set up arbitrary relationships with other models, either existing or new.

Hobo’s approach will make ajax programming easier than with anything else out there.

Page 31: What Is Hobo ?

Evarist Lobo 200731

Getting started

http://www.hobocentral.net Download plugin Look at screencasts for quickstart Read the blogs Read the forums

Page 32: What Is Hobo ?

Evarist Lobo 200732

Contributing

Upload code to SVN Create documentation Create sample applications and tutorials

Page 33: What Is Hobo ?

Evarist Lobo 200733

Thank You

Thanks to Tom Locke and others for creating Hobo.

Please email me your feedback at [email protected]