Permalink
Browse files

06| Process the request

Creating the `create messages` route
Adding the `MessagesController` with its `create` action
Creating the `message` model
  • Loading branch information...
robertodecurnex committed Aug 27, 2011
1 parent a781476 commit 33fcb3cc7dfa5bc8fdb6f2deaf4507f85ea0c06b
Showing with 47 additions and 0 deletions.
  1. +9 −0 app/lib/messages_controller.rb
  2. +36 −0 app/lib/models/message.rb
  3. +2 −0 config/application.rb
@@ -0,0 +1,9 @@
+class MessagesController
+
+ include Pakyow::Helpers
+
+ def create
+ Message.new(request.params['message']).save
+ end
+
+end
View
@@ -0,0 +1,36 @@
+class Message
+
+ # Public attributes that the messages will have
+
+ attr_accessor :id, :author, :body
+
+ # In memory messagess storage. They will be
+ # destroyed if the server stops.
+
+ @@messages = []
+
+ # Keeps track of the latest id that has been
+ # used
+
+ @@latest_id = 0
+
+ # Initialize the Message attributes given a
+ # hash of :attribute_name => :attribute_value
+
+ def initialize(params={})
+ params.each do |key, value|
+ self.send("#{key}=", value)
+ end
+ end
+
+ # Sets the message id and adds it to the
+ # messages storege.
+
+ def save
+ @@latest_id += 1
+ self.id = @@latest_id
+ @@messages << self
+ end
+
+end
+
View
@@ -10,6 +10,8 @@ class Application < Pakyow::Application
routes do
default :ApplicationController, :index
+
+ post 'messages', :MessagesController, :create
end
middleware do

0 comments on commit 33fcb3c

Please sign in to comment.