Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: chischaschos/sinatra-todo
base: daa92d787f
...
head fork: chischaschos/sinatra-todo
compare: b8c5fa3d40
  • 3 commits
  • 16 files changed
  • 0 commit comments
  • 1 contributor
View
2  assets/js/application.js
@@ -10,8 +10,6 @@
//= require_tree ./todo/routers
//
-_.extend(Backbone.View.prototype, App.ErrorHandlingHelpers);
-
$(document).ajaxError(function (event, xhr, options) {
if (xhr.status == 501) {
todoRouter.navigate('', { trigger: true, replace: true} );
View
2  assets/js/todo/collections/todos_collection.coffee
@@ -2,3 +2,5 @@
class App.TodosCollection extends Backbone.Collection
url: '/api/list_item'
+
+ model: App.TodoModel
View
6 assets/js/todo/models/session_model.coffee
@@ -1,7 +1,11 @@
@App ||= {}
class App.SessionModel extends Backbone.Model
- urlRoot: '/api/session'
+ initialize: ->
+ @id = 1
+
+ url: ->
+ '/api/session'
toJSON: ->
user: _.clone(@attributes)
View
6 assets/js/todo/models/todo_model.coffee
@@ -2,6 +2,12 @@
class App.TodoModel extends Backbone.Model
urlRoot: '/api/list_item'
+ defaults:
+ description: ''
+ priority: ''
+ completed: ''
+ due_date: ''
+
set: (attributes, options) ->
if attributes['completed'] == 'on'
attributes['completed'] = true
View
24 assets/js/todo/routers/router.coffee
@@ -3,16 +3,28 @@ class App.TodoRouter extends Backbone.Router
initialize: ->
@container = $('#container')
+ switchView: (view) ->
+ @currentView && @currentView.remove()
+ @currentView = view
+ @container.html(@currentView.render().el)
+
routes:
- '' : 'authenticate'
- 'todos' : 'todos'
+ '' : 'authenticate'
+ 'todos' : 'todos'
+ 'sign-out' : 'signOut'
authenticate: ->
- authenticationView = new App.AuthenticationView el: @container
- authenticationView.render()
+ @switchView(new App.AuthenticationView)
todos: ->
todosView = new App.TodosView
- el: @container
collection: new App.TodosCollection
- todosView.render()
+ @switchView(todosView)
+
+ signOut: ->
+ (new App.SessionModel id: 1).destroy
+ wait: true
+ error: (model, response, options) ->
+ todoRouter.navigate('', trigger: true, replace: true)
+ success: (model, response, options) ->
+ todoRouter.navigate('', trigger: true, replace: true)
View
2  assets/js/todo/templates/authentication.jst.ejs
@@ -1,7 +1,7 @@
<form id='authentication_view'>
<fieldset>
<legend>
- <%= title %> or <a href='' id='switch'><%= other_title %></a>
+ <%= title %> or <a href='#' id='switch'><%= other_title %></a>
</legend>
<div id='messages' name='error'></div>
View
16 assets/js/todo/templates/new_todo.jst.ejs
@@ -1,33 +1,29 @@
<form id='add-todo-form'>
<fieldset>
<legend>
- Add todo -
+ <%= title %>
<a href='#' id='cancel-todo'>cancel</a>
</legend>
<div id='messages' name='error'></div>
- Description <input type='text' id='description' name='description'
-value="<%= description %>"/>
+ Description <input type='text' id='description' name='description' value="<%= description %>"/>
<div id='description_error' name='error'></div>
<br>
- Priority <input type='text' id='priority' name='priority' value="<%=
-priority %>"/>
+ Priority <input type='text' id='priority' name='priority' value="<%= priority %>"/>
<div id='priority_error' name='error'></div>
<br>
- Completed <input type='checkbox' id='completed' name='completed' value="<%=
-completed %>"/>
+ Completed <input type='checkbox' id='completed' name='completed' value="<%= completed %>"/>
<div id='completed_error' name='error'></div>
<br>
- Due Date <input type='text' id='due_date' name='due_date'
-value="<%= due_date %>"/>
+ Due Date <input type='text' id='due_date' name='due_date' value="<%= due_date %>"/>
<div id='due_date_error' name='error'></div>
<br>
- <input type='submit' value='Create' />
+ <input type='submit' value='<%= action %>' />
</fieldset>
</form>
View
4 assets/js/todo/templates/todos.jst.ejs
@@ -1,10 +1,10 @@
<div id='menu'>
<ul>
<li>
- <a href='' id='add-todo'>add</a>
+ <a href='#add-todo' id='add-todo'>add</a>
</li>
<li>
- <a href='' id='sign out'>sign out</a>
+ <a href='#sign-out' id='sign out'>sign out</a>
</li>
</ul>
</div>
View
16 .../todo/views/error_handling_helpers.coffee → assets/js/todo/views/_base_view.coffee
@@ -1,6 +1,6 @@
@App ||= {}
-@App.ErrorHandlingHelpers =
+class App.BaseView extends Backbone.View
blockForm: ->
@$el.find('input').attr('disabled', 'disabled')
@@ -22,11 +22,11 @@
@unblockForm()
- getErrors: (response) ->
- if response.responseJSON && !_.isEmpty(response.responseJSON.errors)
- response.responseJSON.errors
- else
- { default: 'Unknown error' }
+ getErrors: (response) ->
+ if response.responseJSON && !_.isEmpty(response.responseJSON.errors)
+ response.responseJSON.errors
+ else
+ { default: 'Unknown error' }
- cleanErrors: ->
- @$el.find("[name=error]").html('')
+ cleanErrors: ->
+ @$el.find("[name=error]").html('')
View
9 assets/js/todo/views/authentication_view.coffee
@@ -1,5 +1,6 @@
@App ||= {}
-class App.AuthenticationView extends Backbone.View
+
+class App.AuthenticationView extends App.BaseView
template: JST['todo/templates/authentication']
events:
@@ -20,10 +21,10 @@ class App.AuthenticationView extends Backbone.View
initialize: ->
@signInFlow = true
- render: ->
+ render: (event) ->
+ event && event.preventDefault()
@$el.html(@template(@selectMessages()))
-
- return false
+ @
selectMessages: ->
@signInFlow = !@signInFlow
View
22 assets/js/todo/views/new_todo_view.coffee
@@ -1,5 +1,5 @@
@App ||= {}
-class App.NewTodoView extends Backbone.View
+class App.NewTodoView extends App.BaseView
template: JST['todo/templates/new_todo']
events:
@@ -7,13 +7,21 @@ class App.NewTodoView extends Backbone.View
'submit #add-todo-form' : 'submitForm'
'change input' : 'changed'
+ messages:
+ edit:
+ title: 'Editing TODO item'
+ action: 'Save'
+ new:
+ title: 'Creating new TODO item'
+ action: 'Create'
+
initialize: (options) ->
@parent = options.parent
if options.model
- @edit = true
+ @type = 'edit'
@model = options.model
else
- @edit = false
+ @type = 'new'
@model = new App.TodoModel
back: (event) ->
@@ -22,7 +30,13 @@ class App.NewTodoView extends Backbone.View
@parent.render()
render: ->
- @$el.html(@template(@model.toJSON()))
+ attributes = _.extend(
+ {},
+ @model.toJSON().list_item,
+ @messages[@type]
+ )
+ @$el.html(@template(attributes))
+ @
changed: (event) ->
changed = event.currentTarget
View
5 assets/js/todo/views/todo_view.coffee
@@ -1,5 +1,5 @@
@App ||= {}
-class App.TodoView extends Backbone.View
+class App.TodoView extends App.BaseView
template: JST['todo/templates/todo']
tagName: 'ul'
@@ -13,7 +13,8 @@ class App.TodoView extends Backbone.View
@parent = options.parent
render: ->
- @$el.html(@template(@model.toJSON()))
+ @$el.html(@template(@model.toJSON().list_item))
+ @
destroy: (event) =>
event.preventDefault()
View
15 assets/js/todo/views/todos_view.coffee
@@ -1,5 +1,5 @@
@App ||= {}
-class App.TodosView extends Backbone.View
+class App.TodosView extends App.BaseView
template: JST['todo/templates/todos']
events:
@@ -18,24 +18,21 @@ class App.TodosView extends Backbone.View
todoView = new App.TodoView
model: model
parent: @
- @$el.find('#todos').append todoView.render()
+ @$el.find('#todos').append(todoView.render().el)
createTodo: (event) ->
event.preventDefault()
newTodoView = new App.NewTodoView
- el: @$el.find('#todos')
parent: @
- newTodoView.render()
+ @$el.find('#todos').html(newTodoView.render().el)
editTodo: (model) ->
newTodoView = new App.NewTodoView
- el: @$el.find('#todos')
parent: @
model: model
- newTodoView.render()
-
+ @$el.find('#todos').html(newTodoView.render().el)
render: ->
- @$el.html(@template())
@collection.fetch(reset: true)
-
+ @$el.html(@template())
+ @
View
2  lib/todo/api.rb
@@ -89,7 +89,7 @@ class Api < Application
end
post '/api/list_item', auth_required: true do
- list_item = @session.user.list_items.create(params[:list_item])
+ list_item = @session.user.list_items.create(params[:list_item] || {} )
if list_item.saved?
list_item.to_json
View
7 lib/todo/models/list_item.rb
@@ -10,6 +10,13 @@ class ListItem
property :due_date, Date
belongs_to :user, 'Todo::Models::User'
+
+ validates_presence_of :description
+
+ def h_errors
+ { errors: self.errors.to_hash }
+ end
+
end
end
end
View
49 spec/api/todos_spec.rb
@@ -119,19 +119,46 @@
end
- it 'should create a list item' do
- post '/api/list_item', { list_item: default_list_params }
+ context 'when creating list items' do
+ it 'should create return lits fields' do
+ post '/api/list_item', { list_item: default_list_params }
- expect(last_response).to be_json
- expect(last_response).not_to have_cookie 'access_token'
- last_response.body.tap do |body|
- expect(body).to have_json_path 'id'
- expect(body).to have_json_path 'description'
- expect(body).to have_json_path 'priority'
- expect(body).to have_json_path 'due_date'
- expect(body).to have_json_path 'completed'
+ expect(last_response).to be_json
+ expect(last_response).not_to have_cookie 'access_token'
+ last_response.body.tap do |body|
+ expect(body).to have_json_path 'id'
+ expect(body).to have_json_path 'description'
+ expect(body).to have_json_path 'priority'
+ expect(body).to have_json_path 'due_date'
+ expect(body).to have_json_path 'completed'
+ end
+ expect(last_response.status).to eq 200
end
- expect(last_response.status).to eq 200
+
+ it 'should create a list item with defaults' do
+ post '/api/list_item', { list_item: { description: 'some' } }
+
+ expect(last_response).to be_json
+ expect(last_response).not_to have_cookie 'access_token'
+ last_response.body.tap do |body|
+ expect(body).to have_json_path 'id'
+ expect(body).to have_json_path 'description'
+ expect(body).to have_json_path 'priority'
+ expect(body).to have_json_path 'due_date'
+ expect(body).to have_json_path 'completed'
+ end
+ expect(last_response.status).to eq 200
+ end
+
+ it 'should return errors' do
+ post '/api/list_item'
+
+ expect(last_response).to be_json
+ expect(last_response).not_to have_cookie 'access_token'
+ expect(last_response.body).to have_json_path('errors/description')
+ expect(last_response.status).to eq 404
+ end
+
end
it 'should edit a list item owned by you' do

No commit comments for this range

Something went wrong with that request. Please try again.