Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Got basic todo working

  • Loading branch information...
commit 7d728bc2f0f24f8794b8e65153ce598f9a7054e5 1 parent 414e785
Ramon Tayag + Eumir Gaspar authored
BIN  .Gemfile.swp
View
Binary file not shown
1  Gemfile
View
@@ -5,6 +5,7 @@ gem 'rails', '3.1.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'apotomo'
+gem 'haml'
gem 'sqlite3'
gem 'json'
2  Gemfile.lock
View
@@ -50,6 +50,7 @@ GEM
erubis (2.7.0)
execjs (1.2.6)
multi_json (~> 1.0)
+ haml (3.1.3)
hike (1.2.1)
hooks (0.1.4)
i18n (0.6.0)
@@ -118,6 +119,7 @@ PLATFORMS
DEPENDENCIES
apotomo
coffee-rails (~> 3.1.0)
+ haml
jquery-rails
json
rails (= 3.1.0)
7 app/controllers/home_controller.rb
View
@@ -0,0 +1,7 @@
+class HomeController < ApplicationController
+ has_widgets do |root|
+ root << widget(:todo_list)
+ root << widget(:todo_form)
+ root << widget(:todo_counter)
+ end
+end
2  app/models/todo.rb
View
@@ -0,0 +1,2 @@
+class Todo < ActiveRecord::Base
+end
3  app/views/home/index.html.haml
View
@@ -0,0 +1,3 @@
+= render_widget :todo_form
+= render_widget :todo_list
+= render_widget :todo_counter
2  app/widgets/todo_counter/display.html.haml
View
@@ -0,0 +1,2 @@
+= widget_div do
+ = "Have #{@count} thing/s todo"
13 app/widgets/todo_counter_widget.rb
View
@@ -0,0 +1,13 @@
+class TodoCounterWidget < Apotomo::Widget
+ responds_to_event :new_todo, :with => :update, :passing => :root
+ responds_to_event :delete_todo, :with => :update, :passing => :root
+
+ def display
+ @count = Todo.count
+ render
+ end
+
+ def update(event)
+ replace :state => :display
+ end
+end
7 app/widgets/todo_form/display.html.haml
View
@@ -0,0 +1,7 @@
+= widget_div do
+ = form_for @todo, :url => url_for_event(:submit), :remote => true do |f|
+ %fieldset
+ %ul
+ %li= f.label :title
+ %li= f.text_field :title
+ = f.submit "Submit"
16 app/widgets/todo_form_widget.rb
View
@@ -0,0 +1,16 @@
+class TodoFormWidget < Apotomo::Widget
+ responds_to_event :submit
+
+ def display
+ @todo = Todo.new
+ render
+ end
+
+ def submit(event)
+ @todo = Todo.new(event[:todo])
+ if @todo.save
+ trigger :new_todo, :todo => @todo
+ replace :state => :display
+ end
+ end
+end
5 app/widgets/todo_list/display.html.haml
View
@@ -0,0 +1,5 @@
+= widget_div do
+ %h1 Todo List
+ %ul#todo-items
+ - @todos.each do |todo|
+ = render({:state => :item}, todo)
5 app/widgets/todo_list/item.html.haml
View
@@ -0,0 +1,5 @@
+= widget_div do
+ %li{:id => dom_id(@todo)}
+ = @todo.title
+ |
+ = link_to "Delete", url_for_event(:delete_todo, :todo_id => @todo.id), :remote => true, :confirm => "Sure?"
29 app/widgets/todo_list_widget.rb
View
@@ -0,0 +1,29 @@
+class TodoListWidget < Apotomo::Widget
+ include ActionView::Helpers::JavaScriptHelper
+ responds_to_event :new_todo, :with => :add_item, :passing => :root
+ responds_to_event :delete_todo, :with => :destroy_item
+
+ def display
+ @todos = Todo.all
+ render
+ end
+
+ def add_item(event)
+ ::Rails.logger.info("IN ADD_ITEM, event: #{event[:todo].inspect}")
+ @todo = event[:todo]
+ ::Rails.logger.info "This is the todo: #{@todo.inspect}"
+ render :text => %{$('ul#todo-items').append("#{escape_javascript render({:state => :item}, @todo)}")}
+ end
+
+ def destroy_item(event)
+ @todo = Todo.find event[:todo_id]
+ if @todo.destroy
+ render :text => %{$('li#todo_#{@todo.id}').fadeOut();}
+ end
+ end
+
+ def item(todo)
+ @todo = todo
+ display
+ end
+end
2  config/routes.rb
View
@@ -48,7 +48,7 @@
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
- # root :to => 'welcome#index'
+ root :to => 'home#index'
# See how all your routes lay out with "rake routes"
8 db/migrate/20110915024821_create_todos.rb
View
@@ -0,0 +1,8 @@
+class CreateTodos < ActiveRecord::Migration
+ def change
+ create_table :todos do |t|
+ t.string :title
+ t.timestamps
+ end
+ end
+end
21 db/schema.rb
View
@@ -0,0 +1,21 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20110915024821) do
+
+ create_table "todos", :force => true do |t|
+ t.string "title"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
11 test/fixtures/todos.yml
View
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
7 test/unit/todo_test.rb
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TodoTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
12 test/widgets/counter_widget_test.rb
View
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class TodoList::CounterWidgetTest < Apotomo::TestCase
+ has_widgets do |root|
+ root << widget(:counter, 'me')
+ end
+
+ test "display" do
+ render_widget 'me'
+ assert_select "h1"
+ end
+end
12 test/widgets/form_widget_test.rb
View
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class TodoList::FormWidgetTest < Apotomo::TestCase
+ has_widgets do |root|
+ root << widget(:form, 'me')
+ end
+
+ test "display" do
+ render_widget 'me'
+ assert_select "h1"
+ end
+end
12 test/widgets/list_widget_test.rb
View
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class TodoList::ListWidgetTest < Apotomo::TestCase
+ has_widgets do |root|
+ root << widget(:list, 'me')
+ end
+
+ test "display" do
+ render_widget 'me'
+ assert_select "h1"
+ end
+end
12 test/widgets/todo_counter_widget_test.rb
View
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class TodoCounterWidgetTest < Apotomo::TestCase
+ has_widgets do |root|
+ root << widget(:todo_counter, 'me')
+ end
+
+ test "display" do
+ render_widget 'me'
+ assert_select "h1"
+ end
+end
12 test/widgets/todo_form_widget_test.rb
View
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class TodoFormWidgetTest < Apotomo::TestCase
+ has_widgets do |root|
+ root << widget(:todo_form, 'me')
+ end
+
+ test "display" do
+ render_widget 'me'
+ assert_select "h1"
+ end
+end
12 test/widgets/todo_list_widget_test.rb
View
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class TodoListWidgetTest < Apotomo::TestCase
+ has_widgets do |root|
+ root << widget(:todo_list, 'me')
+ end
+
+ test "display" do
+ render_widget 'me'
+ assert_select "h1"
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.