Skip to content

Commit 601ddc1

Browse files
committed
messages now show up in the app itself
1 parent cb58333 commit 601ddc1

9 files changed

Lines changed: 78 additions & 1 deletion

File tree

app/controllers/application_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ class ApplicationController < ActionController::Base
99
# Scrub sensitive parameters from your log
1010
# filter_parameter_logging :password
1111
helper_method :current_user
12+
helper_method :current_user=
13+
14+
def current_user=(user)
15+
@current_user = user
16+
end
1217

1318
private
1419
def current_user_session
1520
return @current_user_session if defined?(@current_user_session)
1621
@current_user_session = UserSession.find
1722
end
1823

24+
1925
def current_user
2026
@current_user = current_user_session && current_user_session.record
2127
end

app/controllers/messages_controller.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
class MessagesController < ApplicationController
2+
3+
prepend_around_filter ApiAuthorizedFilter.new
4+
25
def index
36
@messages = Message.find(:all, :conditions => ["recipient_id == ?", current_user.id])
7+
respond_to do |format|
8+
format.html
9+
format.yaml {require 'yaml'; render :text => (Hash.from_xml(@messages.to_xml)).to_yaml}
10+
end
411
end
512

613
def show
714
@message = Message.find(params[:id])
15+
respond_to do |format|
16+
format.html
17+
format.yaml {require 'yaml'; render :text => (Hash.from_xml(@message.to_xml)).to_yaml}
18+
end
819
end
920

1021
def new

app/models/message.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
class Message < ActiveRecord::Base
22
belongs_to :sender, :class_name => "User"
33
belongs_to :recipient, :class_name => "User"
4+
5+
after_save :from
6+
7+
attr_accessor :from
8+
9+
def from
10+
@from = @attributes["from"] = sender.username unless sender.nil?
11+
end
12+
13+
def after_initialize
14+
from
15+
end
16+
417
end

app/views/messages/new.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<td><%= f.label :recipient_id %></td>
88
<td><%= f.collection_select(:recipient_id, User.find(:all, :order => :username) - [current_user], :id, :username) %></td>
99
</tr>
10+
<tr>
11+
<td><%= f.label :subject %></td>
12+
<td><%= f.text_field :subject %></td>
13+
</tr>
1014
<tr>
1115
<td><%= f.label :text %></td>
1216
<td><%= f.text_area :text %></td>

app/views/users/_user.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<td><%= form.label :password_confirmation, "Confirm Password" %></td>
1818
<td><%= form.password_field :password_confirmation %></td>
1919
</tr>
20+
<tr>
21+
<td><%= form.label :api_key, "Secret Key" %></td>
22+
<td><%= @user.api_key %></td>
23+
</tr>
2024
<tr>
2125
<td><%= form.label :link %></td>
2226
<td><%= form.text_field :link %></td>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddReadToMessages < ActiveRecord::Migration
2+
def self.up
3+
add_column :messages, :read, :boolean, :default => false
4+
end
5+
6+
def self.down
7+
remove_column :messages, :read
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddSubjectToMessages < ActiveRecord::Migration
2+
def self.up
3+
add_column :messages, :subject, :string
4+
end
5+
6+
def self.down
7+
remove_column :messages, :subject
8+
end
9+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# It's strongly recommended to check this file into your version control system.
1111

12-
ActiveRecord::Schema.define(:version => 20091219143931) do
12+
ActiveRecord::Schema.define(:version => 20091220000641) do
1313

1414
create_table "comments", :force => true do |t|
1515
t.integer "user_id"
@@ -25,6 +25,8 @@
2525
t.text "text"
2626
t.datetime "created_at"
2727
t.datetime "updated_at"
28+
t.boolean "read", :default => false
29+
t.string "subject"
2830
end
2931

3032
create_table "programs", :force => true do |t|

lib/api_authorized_filter.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Use this filter as an around_filter around actions that can be
2+
# accessed via the API.
3+
#
4+
# Example:
5+
# class ItemsController < ApplicationController
6+
# prepend_around_filter ApiAuthorizedFilter.new, :only => [:create]
7+
# end
8+
#
9+
class ApiAuthorizedFilter
10+
def before(controller)
11+
return true unless controller.params[:api_key]
12+
controller.current_user = User.find_by_api_key(controller.params[:api_key])
13+
end
14+
15+
def after(controller)
16+
controller.current_user = nil
17+
end
18+
end
19+

0 commit comments

Comments
 (0)