Peter's Guide

This is a brief tutorial for setting up a small Rails 2.3 app with Apotomo widgets. It’s meant as a starting point for learning Apotomo, so grab a beer and read on!

The code is available in a git repo, feel free to clone.

During the course we are developing a rich application for twittering. The first widget looks like this. Pretty straight-forward.

Tweeter

Installation

You already got Rails? Ok.

$ rails bar
    create  
    create  app/controllers
    create  app/helpers
    create  app/models
    create  app/views/layouts
    ...

Now, rewrite config/environment.rb:

RAILS_GEM_VERSION = '2.3.8' unless defined? RAILS_GEM_VERSION
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
  config.gem "haml"
  config.gem "cells", :version => '3.3.4'
  config.gem "apotomo"
end

Apotomo.js_framework = :jquery

We’re gonna use JQuery in this examples.

Install the gems by hand, if you like:

$ gem install haml cells apotomo 

Layout

Next, we could need a layout.

app/views/layouts/application.html.haml
1%html
2%head
3  = javascript_include_tag "jquery-1.4.2.min"
4  = stylesheet_link_tag "app"
5
6%body
7  = yield

The Tweet model

Say we’re writing a small widget that let’s you post and displays your recent posts. We could need a Tweet model for that.

$ script/generate model tweet text:string
$ rake db:migrate

Controller

Widgets are usually embedded into controllers, so there should be one.

$ script/generate controller dashboard index
    exists  app/controllers/
    exists  app/helpers/
    ...

The first Widget!

Writing a tweet widget is as easy as creating a new controller.

$ script/generate widget twitter_widget display_form --haml
    exists  app/cells/
    create  app/cells/twitter_widget
    exists  test/widgets
    create  app/cells/twitter_widget.rb
    create  app/cells/twitter_widget/display_form.html.haml
    create  test/widgets/twitter_widget_test.rb

    

The widget class should look as follows.

app/cells/twitter_widget.rb
 1class TwitterWidget < Apotomo::Widget
 2  responds_to_event :submit, :with => :process_tweet
 3
 4  def display_form
 5    @tweets = Tweet.find(:all)
 6    render
 7  end
 8  
 9  def process_tweet
10    Tweet.new(:text => param(:text)).save
11
12    @tweets = Tweet.find(:all) # this is wet!
13    replace :view => :display_form
14  end
15end

The corresponding view for display_form is rendered in line 6. It contains the same tiresome markup as any other view in Rails.

app/cells/twitter_widget/display_form.html.haml
 1- widget_div do
 2  %ul
 3    - for tweet in @tweets
 4      %li
 5        = tweet.text
 6
 7  %p
 8    What are you up to?
 9  
10  - form_tag "", "data-event-url" => url_for_event(:submit) do
11    = text_field_tag 'text'
12  
13    = submit_tag "Tweet!"
14  
15  :javascript
16    var form = $("##{@name} form");
17  
18    form.submit(function() {
19      $.ajax({url: form.attr("data-event-url"), data: form.serialize()})
20      return false;
21    });
22  

Putting the tweeter in the dashboard

To plug your new widget in a controller, the controller should look like this.

app/controllers/dashboard_controller.rb
 1class DashboardController < ApplicationController
 2  include Apotomo::Rails::ControllerMethods
 3
 4  has_widgets do |root|
 5    root << widget(:twitter_widget, 'parrot', :display_form)
 6  end
 7
 8  def index
 9  end
10end

Change the index view.

app/views/dashboard/index.haml
1%h1 My Dashboard
2
3= render_widget 'parrot'

Running it

Next, start the engine

$ script/server

…and browse to http://localhost:3000/dashboard

Happy “tweeting”.


blog comments powered by Disqus