Permalink
Browse files

Lists and items exist

  • Loading branch information...
Nathan Sobo & Zef Houssney
Nathan Sobo & Zef Houssney committed Jul 4, 2012
1 parent 75e8550 commit 64c226ebcaaa578990330228f36bb66df1913b4e
View
@@ -0,0 +1,3 @@
+[submodule "vendor/monarch"]
+ path = vendor/monarch
+ url = git@github.com:nathansobo/monarch.git
View
@@ -13,6 +13,7 @@ gem 'monarch', :path => 'vendor/monarch'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
+ gem 'underscore-rails'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
View
@@ -1,6 +1,5 @@
-GIT
- remote: git@github.com:nathansobo/monarch.git
- revision: d3902b029bfc0b0f409fb49cfec03c9ac877da9a
+PATH
+ remote: vendor/monarch
specs:
monarch (0.0.1)
@@ -118,6 +117,7 @@ GEM
uglifier (1.2.6)
execjs (>= 0.3.0)
multi_json (~> 1.3)
+ underscore-rails (1.3.1)
PLATFORMS
ruby
@@ -131,3 +131,4 @@ DEPENDENCIES
sass-rails (~> 3.2.3)
sqlite3
uglifier (>= 1.0.3)
+ underscore-rails
View
Binary file not shown.
@@ -0,0 +1,24 @@
+# This is a manifest file that'll be compiled into application.js, which will include all the files
+# listed below.
+#
+# Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
+# or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
+#
+# It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
+# the compiled file.
+#
+# WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
+# GO AFTER THE REQUIRES BELOW.
+#
+#= require jquery
+#= require jquery_ujs
+#= require underscore
+#= require space-pen
+#= require monarch
+#= require_tree .
+#
+
+Monarch.snakeCase = true
+
+$ ->
+ $('body').html(new ListsView)
@@ -1,16 +0,0 @@
-// This is a manifest file that'll be compiled into application.js, which will include all the files
-// listed below.
-//
-// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
-// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
-//
-// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
-// the compiled file.
-//
-// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
-// GO AFTER THE REQUIRES BELOW.
-//
-//= require jquery
-//= require jquery_ujs
-//= require monarch
-//= require_tree .
@@ -0,0 +1,8 @@
+class window.Item extends Monarch.Record
+ @extended(this)
+
+ @columns
+ text: 'string'
+ listId: 'integer'
+ order: 'float'
+
@@ -0,0 +1,8 @@
+class window.List extends Monarch.Record
+ @extended(this)
+
+ @columns
+ name: 'string'
+
+ @hasMany 'items'
+
@@ -0,0 +1,25 @@
+class window.ListView extends View
+ @content: (list) ->
+ @div class: 'list', =>
+ @input value: list.name(), outlet: 'name', change: 'updateName'
+ @ol outlet: 'items'
+ @input outlet: 'newItem', keyup: 'handleNewItemKeyup'
+
+ initialize: (@list) ->
+ @redrawItems()
+ @list.items().onInsert (item) =>
+ @redrawItems()
+
+ redrawItems: ->
+ @items.empty()
+ @list.items().each (item) =>
+ @items.append $$ -> @li item.text()
+
+ updateName: ->
+ @list.update(name: @name.val())
+
+ handleNewItemKeyup: (e) ->
+ if (e.originalEvent.keyIdentifier == 'Enter')
+ @list.items().create
+ text: @newItem.val()
+ @newItem.val('')
@@ -0,0 +1,20 @@
+class window.ListsView extends View
+ @content: ->
+ @div =>
+ @div "Lists"
+ @button "New List", click: 'createList'
+ @ol class: 'lists', outlet: 'lists'
+
+ initialize: ->
+ $.ajax
+ url: '/lists'
+ dataType: 'json'
+ success: (records) ->
+ Monarch.Repository.update(records)
+
+ List.onInsert (list) =>
+ listView = new ListView(list)
+ @lists.append(listView)
+
+ createList: ->
+ List.create()
@@ -0,0 +1,3 @@
+// Place all the styles related to the items controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -1,3 +1,14 @@
-// Place all the styles related to the lists controller here.
-// They will automatically be included in application.css.
-// You can use Sass (SCSS) here: http://sass-lang.com/
+body {
+ background-color: #ccc;
+}
+
+.lists {
+ .list {
+ float: left;
+ border: 1px solid black;
+ margin: 20px;
+ padding: 10px;
+ width: 300px;
+ }
+}
+
@@ -0,0 +1,14 @@
+class ItemsController < ApplicationController
+ respond_to :json
+
+ def create
+ item = Item.create(params[:field_values])
+ respond_with item
+ end
+
+ def update
+ item = Item.find(params[:id])
+ item.update_attributes(params[:field_values])
+ respond_with item
+ end
+end
@@ -1,4 +1,26 @@
class ListsController < ApplicationController
+ respond_to :json
+
def index
+ respond_to do |format|
+ format.html
+ format.json do
+ lists_by_id = Hash[List.all.map {|l| [l.id, l]}]
+ items_by_id = Hash[Item.all.map {|l| [l.id, l]}]
+ render json: {'List' => lists_by_id, 'Item' => items_by_id}
+ end
+ end
end
+
+ def create
+ list = List.create
+ respond_with list
+ end
+
+ def update
+ list = List.find(params[:id])
+ list.update_attributes(params[:field_values])
+ respond_with list
+ end
+
end
@@ -0,0 +1,2 @@
+module ItemsHelper
+end
View
@@ -0,0 +1,4 @@
+class Item < ActiveRecord::Base
+ belongs_to :list
+ attr_accessible :order, :text, :list_id
+end
View
@@ -0,0 +1,4 @@
+class List < ActiveRecord::Base
+ has_many :items, order: :order
+ attr_accessible :name
+end
@@ -1,2 +0,0 @@
-<h1>Lists#index</h1>
-<p>Find me in app/views/lists/index.html.erb</p>
View
@@ -1,60 +1,5 @@
Butterfly::Application.routes.draw do
- get "lists/index"
-
- # The priority is based upon order of creation:
- # first created -> highest priority.
-
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', :on => :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
+ resources :lists
+ resources :items
root :to => 'lists#index'
-
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id))(.:format)'
end
@@ -0,0 +1,9 @@
+class CreateLists < ActiveRecord::Migration
+ def change
+ create_table :lists do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,12 @@
+class CreateItems < ActiveRecord::Migration
+ def change
+ create_table :items do |t|
+ t.text :text
+ t.float :order
+ t.references :list
+
+ t.timestamps
+ end
+ add_index :items, :list_id
+ end
+end
View
@@ -0,0 +1,32 @@
+# encoding: UTF-8
+# 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 => 20120704000124) do
+
+ create_table "items", :force => true do |t|
+ t.text "text"
+ t.float "order"
+ t.integer "list_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "items", ["list_id"], :name => "index_items_on_list_id"
+
+ create_table "lists", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe ItemsController do
+
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the ItemsHelper. For example:
+#
+# describe ItemsHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe ItemsHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
View
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Item do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
View
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe List do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
Oops, something went wrong.

0 comments on commit 64c226e

Please sign in to comment.