Skip to content

Commit

Permalink
Allow editing an item.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Aug 12, 2012
1 parent d528ec6 commit f1f786f
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/assets/javascripts/routers/item_router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class @ItemRouter extends Backbone.Router
'': 'items'
'items/new': 'newItem'
'items/:id': 'item'
'items/:id/edit': 'edit'

constructor: (options) ->
super
Expand Down Expand Up @@ -33,5 +34,9 @@ class @ItemRouter extends Backbone.Router
@items.load(id).then (item) =>
@content new Item.Views.Show(model: item)

edit: (id) =>
@items.load(id).then (item) =>
@content new Item.Views.Edit(model: item)

content: (view) ->
@details.setView(view).render()
23 changes: 23 additions & 0 deletions app/assets/javascripts/templates/item/edit.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<form class="pane-content">
<h1>{{title}}</h1>
<p>
<label for="item-title">Title</label>
<input id="item-title" type="text" name="title" value="{{title}}">
</p>

{{#data}}
<p>
<label for="item-username">Username</label>
<input id="item-username" type="text" name="data.username" value="{{username}}">
</p>

<p>
<label for="item-password">Password</label>
<input id="item-password" type="password" name="data.password" value="{{password}}">
</p>
{{/data}}

<footer class="buttons">
<button type="submit">Save</button>
</footer>
</form>
4 changes: 4 additions & 0 deletions app/assets/javascripts/templates/item/show.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
</dd>
</dl>
{{/data}}

<footer>
<a class="button" href="#/items/{{id}}/edit">Edit</a>
</footer>
</div>
16 changes: 16 additions & 0 deletions app/assets/javascripts/views/item/edit.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Item.Views ?= {}

class Item.Views.Edit extends Backbone.View
template: 'templates/item/edit'

events:
'submit form': 'submit'

submit: (event) =>
params = @$('form').toObject(mode: 'combine')
@model.save params, success: (item) =>
Backbone.history.navigate "items/#{item.id}", true
false

serialize: ->
_.extend @model.toJSON(), data: @model.data()
4 changes: 4 additions & 0 deletions app/assets/javascripts/views/item/list_item.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ class Item.Views.ListItem extends Backbone.View
template: 'templates/item/list_item'
className: 'item'

constructor: ->
super
@model.on 'change', @render, @

serialize: ->
this.model.toJSON()
22 changes: 18 additions & 4 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ def index
end

def create
item = Item.create!(
:title => params[:title],
:encrypted_data => params[:encrypted_data]
)
item = Item.create!(item_params)
share = item.share_with(current_user, params[:key])
render :json => ItemPresenter.new(item, share), :status => :created
end

def update
item = Item.get(params[:id])
share = Share.first(:user_id => current_user.id, :item_id => item.id)
if share
item.update_attributes(item_params)
render :json => ItemPresenter.new(item, share)
else
head 404
end
end

private

def item_params
params.slice(:title, :encrypted_data)
end
end
14 changes: 13 additions & 1 deletion features/items.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Items

Scenario: creating an item
Scenario: creating and editing an item
Given I am signed in as "winnie@thepooh.com"
And "winnie@thepooh.com" has generated a key
And I am on the dashboard
Expand All @@ -23,3 +23,15 @@ Feature: Items
Then I should see "myusername"
When I follow "reveal"
Then I should see "mypassword"

When I follow "Edit"
Then the "Title" field should contain "example.com"
And the "Username" field should contain "myusername"
And the "Password" field should contain "mypassword"

When I fill in "Title" with "Example"
And I fill in "Username" with "updated-username"
And I press "Save"

Then I should see "Example" within the item list
And I should see "updated-username"
26 changes: 26 additions & 0 deletions spec/controllers/items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@

it { expect(subject.status).to be(200) }
end

describe 'update' do
let(:item) { Item.create! }

subject do
put :update, :id => item.id.to_s, :title => 'Updated'
end

context 'when user has access' do
before do
item.share_with current_user, 'key'
end

it { expect(subject.status).to be(200) }

it 'updates item' do
subject
item.reload
expect(item.title).to eql('Updated')
end
end

context 'when user does not have access' do
it { expect(subject.status).to be(404) }
end
end
end

context 'when signed out' do
Expand Down
3 changes: 3 additions & 0 deletions spec/support/controller_spec_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module ControllerSpecHelpers
attr_reader :current_user

def sign_in_as(user)
controller.stub(:current_user).and_return(user)
@current_user = user
end

def raw_post(action, body)
Expand Down

0 comments on commit f1f786f

Please sign in to comment.