Permalink
Browse files

08| Edit Objects

Adding `update` and `find` methods to the messages
Adding update and edit routes and MessagesController actions
Adding edit icon
Adding edit view
Creating the `MessageBinder` - @hby contribution, thank you!
  • Loading branch information...
robertodecurnex committed Aug 31, 2011
1 parent 9492639 commit 0b3959d3026419235c94c8e1b90577874c8aad34
@@ -0,0 +1,10 @@
+class MessageBinder < Pakyow::Presenter::Binder
+
+ # Target class to be bounded
+ binder_for :Message
+
+ def edit_link
+ {:href => "/messages/edit/#{bindable.id}"}
+ end
+
+end
@@ -23,4 +23,30 @@ def create
app.redirect_to('/messages')
end
+ def edit
+ message_id = request.params[:id].to_i
+
+ message = Message.find(message_id)
+
+ view = presenter.view.find('form')
+
+ action = "/messages/#{message.id}"
+
+ # Sets the edit message form action attribute
+ view.attributes.action = action
+
+ # Binds the message to the edit form.
+ # This will fill the edit form fields with the
+ # current object data.
+ view.bind(message)
+ end
+
+ def update
+ message = Message.find(request.params[:id].to_i)
+
+ message.update(request.params['message'])
+
+ app.redirect_to('/messages')
+ end
+
end
View
@@ -20,6 +20,13 @@ def self.all
@@messages
end
+ # Given a Message id searches and returns
+ # the targer instance.
+
+ def self.find(id)
+ @@messages.find { |message| message.id == id }
+ end
+
# Initialize the Message attributes given a
# hash of :attribute_name => :attribute_value
@@ -38,4 +45,13 @@ def save
@@messages << self
end
+ # Updates the Message attributes given a
+ # hash of :attribute_name => :attribute_value
+
+ def update(params={})
+ params.each do |key, value|
+ self.send("#{key}=", value)
+ end
+ end
+
end
View
@@ -1,6 +1,12 @@
-<p>
- <strong><span itemprop="message[author]"></span></strong> says:
- <br />
- <span itemprop="message[body]"></span>
-</p>
-
+<tr>
+ <td>
+ <span id="edit_link"></span>
+ </td>
+ <td>
+ <p>
+ <strong><span itemprop="message[author]"></span></strong> says:
+ <br />
+ <span itemprop="message[body]"></span>
+ </p>
+ </td>
+</tr>
@@ -0,0 +1,3 @@
+<h1>Editing Post</h1>
+
+<div id="form"></div>
@@ -0,0 +1,3 @@
+<a itemprop="message[edit_link]" href="#">
+ <img src="/images/edit.png"></img>
+</a>
@@ -1,11 +1,11 @@
<form action="/messages" method="post">
<label>As:</label>
<br />
- <input type="text" name="message[author]">
+ <input type="text" name="message[author]" itemprop="message[author]">
<br /><br />
<label>Message:</label>
<br />
- <textarea name="message[body]"></textarea>
+ <textarea name="message[body]" itemprop="message[body]"></textarea>
<br /><br />
<input type="submit">
</form>
@@ -2,4 +2,6 @@ <h1>Recent Messages</h1>
<a href="/messages/new">Post a Message</a>
-<div id="message"></div>
+<table>
+ <div id="message"></div>
+</table>
View
@@ -14,6 +14,10 @@ class Application < Pakyow::Application
get 'messages', :MessagesController, :index
post 'messages', :MessagesController, :create
+
+ get 'messages/edit/:id', :MessagesController, :edit
+
+ post 'messages/:id', :MessagesController, :update
end
middleware do
View
Binary file not shown.

0 comments on commit 0b3959d

Please sign in to comment.