diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 83445dbde..f5fcad855 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,11 @@ class ApplicationController < ActionController::Base + before_filter :authenticate_user! + before_filter :set_user + def set_user + @user = current_user + true + end + protect_from_forgery :except => :receive layout 'application' diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index f39cecdbf..2509d9e72 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -1,5 +1,4 @@ class BlogsController < ApplicationController - before_filter :authenticate_user! def index diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 6defe2f07..9ed215880 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -1,5 +1,4 @@ class BookmarksController < ApplicationController - before_filter :authenticate_user! def index @bookmarks = Bookmark.criteria.all.order_by( [:created_at, :desc] ) diff --git a/app/controllers/friends_controller.rb b/app/controllers/friends_controller.rb index 55778861f..31be0a1f4 100644 --- a/app/controllers/friends_controller.rb +++ b/app/controllers/friends_controller.rb @@ -1,5 +1,4 @@ class FriendsController < ApplicationController - before_filter :authenticate_user! def index @friends = Friend.criteria.all.order_by( [:created_at, :desc] ) diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index c0507fcda..bf53ac7c5 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -1,5 +1,4 @@ class StatusMessagesController < ApplicationController - before_filter :authenticate_user! def index @status_message = StatusMessage.new @@ -15,11 +14,11 @@ class StatusMessagesController < ApplicationController end def create - @status_message = StatusMessage.new(params[:status_message]) - if @status_message.save + if current_user.post :status_message, params[:status_message] flash[:notice] = "Successfully created status message." redirect_to status_messages_url else + flash[:notics] = "You have failed to update your status." render :action => 'new' end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index da904fab0..10d0ffa4a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,5 @@ class UsersController < ApplicationController - before_filter :authenticate_user! def index @users = User.criteria.all.order_by( [:created_at, :desc] ) diff --git a/app/models/post.rb b/app/models/post.rb index 1e61c60ef..021b8a7cd 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -13,7 +13,6 @@ class Post belongs_to_related :person - before_create :set_defaults after_save :send_to_view @@ -53,8 +52,5 @@ class Post WebSocket.update_clients(self) end - def set_defaults - self.person ||= User.first - end end diff --git a/app/models/user.rb b/app/models/user.rb index a1ac4217f..c4d28c0ba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,5 +4,12 @@ class User < Person # :token_authenticatable, :confirmable, :lockable and :timeoutable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - + def post(post_type, options) + case post_type + when :status_message + StatusMessage.new(:person => self, :message => options[:message]).save + else + raise "Not a type I can post yet" + end + end end diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index ba63f2642..1e47c1bde 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -8,6 +8,8 @@ describe StatusMessage do it "should have a message" do n = Factory.build(:status_message, :message => nil) n.valid?.should be false + n.message = "" + n.valid?.should be false n.message = "wales" n.valid?.should be true end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b52406211..bf5c7e15c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require File.dirname(__FILE__) + '/../spec_helper' describe User do it "should require a real name" do @@ -16,5 +16,16 @@ describe User do Factory.create(:user) Person.count.should == n+1 end - + describe 'when posting' do + before do + @user = Factory.create :user + end + it "should be able to set a status message" do + @user.post :status_message, :text => "I feel good" + StatusMessage.where(:person_id => @user.id).last.message.should == "I feel good" + end + it "should return nil from an invalid post" do + @user.post(:status_message, :text => "").should be_false + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 62a4b907e..e23502036 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -39,5 +39,5 @@ Rspec.configure do |config| # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, comment the following line or assign false # instead of true. -# config.use_transactional_fixtures = true + config.use_transactional_fixtures = true end