• Stars
    star
    654
  • Rank 68,870 (Top 2 %)
  • Language
    Ruby
  • Created over 16 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MVC Components for Rails.

Apotomo

Web Components for Rails.

Overview

Do you need an interactive user interface for your Rails application? A cool Rich Client Application with dashboards, portlets and AJAX, Drag&Drop and jQuery?

Is your controller gettin' fat? And your partial-helper-AJAX pile is getting out of control?

Do you want a framework to make the implementation easier? You want Apotomo.

Apotomo is based on Cells, the popular View Components framework for Rails.

It gives you widgets and encapsulation, bubbling events, AJAX page updates, rock-solid testing and more. Check out http://apotomo.de for more information.

Installation

Easy as hell. You just need Ruby 1.9.3/2.0.0 and Rails 3/4.

Add Apotomo to your Gemfile:

gem 'apotomo'

Example

A shitty example is worse than a shitty framework, so let's choose wisely...

Say you had a blog application. The page showing the post should have a comments block, with a list of comments and a form to post a new comment. Submitting should validate and send back the updated comments list, via AJAX.

Let's wrap that comments block in a widget.

Generate

Go and generate a widget stub.

rails generate apotomo:widget Comments display write -e haml
create  app/widgets/
create  app/widgets/comments_widget.rb
create  app/widgets/comments/
create  app/widgets/comments/display.html.haml
create  app/widgets/comments/write.html.haml
create  test/widgets/
create  test/widgets/comments_widget_test.rb

Nothing special.

Plug it in

You now tell your controller about the new widget.

class PostsController < ApplicationController

  has_widgets do |root|
    root << widget(:comments, :post => @post)
  end

This creates a widget instance called comments_widget from the class CommentsWidget. We pass the current post into the widget - the block is executed in controller instance context, that's were @post comes from. Handy, isn't it?

Render the widget

Rendering usually happens in your controller view, app/views/posts/show.html.haml, for instance.

%h1= @post.title
%p
  = @post.body
%p
  = render_widget :comments, post: @post

Write the widget

A widget is like a cell which is like a mini-controller.

class CommentsWidget < Apotomo::Widget
  responds_to_event :post

  def display(args)
    @comments = args[:post].comments # the parameter from outside.

    render
  end

Having display as the default state when rendering, this method collects comments to show and renders its view.

And look at line 2 - if encountering a :post event we invoke #post, which is simply another state. How cool is that?

  def post(event)
    @comment = Comment.new :post_id => event[:post_id]
    @comment.update_attributes event[:comment] # a bit like params[].

    update :state => :display
  end
end

The event is processed with three steps in our widget:

  • create the new comment
  • re-render the display state
  • update itself on the page

Apotomo helps you focusing on your app and takes away the pain of action dispatching and page updating.

Triggering events

So how and where is the :post event triggered?

Take a look at the widget's view display.html.haml.

= widget_div do
  %ul
    - for comment in @comments
      %li= comment.text

  = form_for :comment, :url => url_for_event(:post), :remote => true do |f|
    = f.text_field :text
    = f.submit

That's a lot of familiar view code, almost looks like a partial.

As soon as the form is submitted, the form gets serialized and sent using the standard Rails mechanisms. The interesting part here is the endpoint URL returned by #url_for_event as it will trigger an Apotomo event.

Event processing

Now what happens when the event request is sent? Apotomo - again - does three things for you, it

  • accepts the request on a special event route it adds to your app
  • triggers the event in your ruby widget tree, which will invoke the #post state in our comment widget
  • sends back the page updates your widgets rendered

JavaScript Agnosticism

In this example, we use jQuery for triggering. We could also use Prototype, RightJS, YUI, or a self-baked framework, that's up to you.

Also, updating the page is in your hands. Where Apotomo provides handy helpers as #replace, you could also emit your own JavaScript.

Look, replace basically generates

jQuery("comments").replaceWith(<the rendered view>);

If that's not what you want, do

def post(event)
  if event[:comment][:text].explicit?
    render :text => 'alert("Hey, you wanted to submit a pervert comment!");'
  end
end

Apotomo doesn't depend on any JS framework - you choose!

Testing

Apotomo comes with its own test case and assertions to build rock-solid web components.

class CommentsWidgetTest < Apotomo::TestCase
  has_widgets do |root|
    root << widget(:comments, :post => @pervert_post)
  end

  def test_render
    render_widget :comments
    assert_select "li#me"

    trigger :post, :comment => {:text => "Sex on the beach"}
    assert_response 'alert("Hey, you wanted to submit a pervert comment!");'
  end
end

You can render your widgets, spec the markup, trigger events and assert the event responses, so far. If you need more, let us know!

Using rspec? please check out rspec-apotomo.

Bubbling events

Note: Let's write this paragraph!

Bugs, Community

Please visit http://apotomo.de, the official project page with lots of examples.

If you have questions, visit us in the IRC channel #cells at irc.freenode.org.

If you wanna be cool, subscribe to our feed!

License

Copyright (c) 2007-2013 Nick Sutterer [email protected]

Released under the MIT License.

More Repositories

1

hooks

Generic hooks with callbacks for Ruby.
Ruby
281
star
2

disposable

Decorators on top of your ORM layer.
Ruby
171
star
3

gemgem-trbrb

The Trailblazer book's example app.
Ruby
137
star
4

paperdragon

Explicit image processing based on Dragonfly.
Ruby
114
star
5

uber

Gem-authoring extensions for classes and modules.
Ruby
94
star
6

cells_examples

A rails project to demonstrate cells in action.
Ruby
83
star
7

nit

Nit improves your git workflow.
Ruby
65
star
8

onfire

Have bubbling events and observers in all your Ruby objects.
Ruby
58
star
9

tyrant

Agnostic authentication gem with signup, signin, forgot password, sticky login, and so on.
Ruby
50
star
10

erbse

Next-generation ERB.
Ruby
48
star
11

kaminari-cells

Kaminari pagination in Cells.
Ruby
23
star
12

cells-blog-example

An exemplary Rails 3 Blog app using Cells.
JavaScript
22
star
13

pingback_engine

A Rails plugin for sending and receiving pingbacks.
Ruby
22
star
14

declarative

DSL for nested schemas.
Ruby
22
star
15

active_helper

Finally - helpers with proper encapsulation, delegation, interfaces and inheritance!
Ruby
19
star
16

gemgem

Sample Rails app using Trailblazer like a boss.com.au.
Ruby
18
star
17

rspec-apotomo

Spec your widgets.
Ruby
16
star
18

dashboard_tutorial

Rails app showing how to build dashboards with Apotomo.
Ruby
15
star
19

gemgem-hanami

Hanami with Trailblazer.
Ruby
14
star
20

cells-capture

Provides content_for for Cells.
Ruby
12
star
21

cells-sinatra

View Components for Sinatra.
Ruby
10
star
22

trb-cart

Shopping cart app with Trailblazer.
Ruby
10
star
23

declarative-option

Dynamic Options to evaluate at runtime.
Ruby
7
star
24

webmachinelovesroar

Ruby services to implement a RESTful shopping cart system with webmachine + roar.
Ruby
7
star
25

www.apotomo.de

Sources for the Apotomo project page, bundled as a Torture app (unreleased!).
Ruby
6
star
26

ruby-on-rest

The example apps from my" Ruby On REST" blog series.
Ruby
6
star
27

exp

My expense application, a Trailblazer+Sinatra example.
Ruby
6
star
28

torture

Tool collection to write and layout programmer's manuals.
Ruby
6
star
29

buildalib

The example app for the "Building your own authentication library with Trailblazer" book.
Ruby
6
star
30

gemgem-grape

HTTP API example application with Grape and Trailblazer.
Ruby
5
star
31

apotomo-stateful

Statefulness for your Apotomo widgets.
Ruby
5
star
32

cheatsheets

Cheatsheets for Cells, Apotomo and ROAR.
4
star
33

declarative-builder

Generic builder pattern.
Ruby
3
star
34

torture-server

Documentation framework
Ruby
3
star
35

pipetree

Functional nested pipeline dialect.
Ruby
3
star
36

screencastsofglory

The example project used for "Apotomo - Screencasts Of Glory"
Ruby
3
star
37

cells-filters

Before and after filters for your cells.
Ruby
3
star
38

cells-widget

Cells view models that can be updated via AJAX.
2
star
39

sinatra-on-cells

Sinatra app using Rails cells and cool Rails gems like simple_form.
Ruby
2
star
40

trb-rubyday

Where's my limoncello?
Ruby
2
star
41

cells-examples

Examples of using Cells with plain Ruby, Lotus, Sinatra, and more. Includes caching, view inheritance, etc.
Ruby
1
star
42

subliminal

My Sublime 3 configuration and snippets.
1
star
43

roar-form_json

Representers for the (unofficial) Form+JSON media type.
Ruby
1
star