Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[#2362] Added the update action to setting StatusNotifications

* Added update to create or set the status notification for the user
* Added some validation for the StatusNotification options
* Hooked up the StatusNotification form
* Added cucumber features and specs

Signed-off-by: Brian Terlson <btthalion@gmail.com>
  • Loading branch information...
commit 5f8da1566b3fd52b38acfc5e6c74fa636d4a99c0 1 parent 85104a4
@edavis10 edavis10 authored committed
View
14 app/controllers/status_notifications_controller.rb
@@ -8,6 +8,20 @@ class StatusNotificationsController < ApplicationController
def edit
@notification = User.current.status_notification
end
+
+ def update
+ @notification = User.current.status_notification
+ @notification ||= User.current.build_status_notification
+
+ @notification.option = params[:status_notification][:option] if params[:status_notification]
+ if @notification.save
+ flash[:notice] = "Preferences saved as #{@notification.option_to_string}."
+ redirect_to :controller => 'status_notifications', :action => 'edit'
+ else
+ flash[:error] = 'Could not save your preference. Please try again'
+ render :action => 'edit'
+ end
+ end
private
View
9 app/models/status_notification.rb
@@ -3,6 +3,13 @@ class StatusNotification < ActiveRecord::Base
:realtime => 'Realtime',
:hourly => 'Hourly',
:eight_hours => '8 Hours',
- :daily => 'daily'
+ :daily => 'Daily'
}
+
+ validates_inclusion_of :option, :in => VALID_OPTIONS.stringify_keys.keys, :allow_blank => true, :allow_nil => true
+
+
+ def option_to_string
+ VALID_OPTIONS[self.option.to_sym] unless self.option.blank?
+ end
end
View
4 app/views/status_notifications/edit.html.erb
@@ -2,9 +2,9 @@
<h2>Notification Preference</h2>
-<% labelled_tabular_form_for :status_notification, @notification, :url => { :controller => 'status_notification', :action => 'update'}, :html => {:id => 'notification_preference'} do |f| %>
+<% labelled_tabular_form_for :status_notification, @notification, :url => { :controller => 'status_notifications', :action => 'update'}, :html => {:id => 'notification_preference'} do |f| %>
<div class="box tabular">
- <p><%=f.select :option, StatusNotification::VALID_OPTIONS.collect {|name, string| [string,name]}, :include_blank => true, :label => "Preference", :required => true %></p>
+ <p><%=f.select(:option, StatusNotification::VALID_OPTIONS.collect {|name, string| [string,name.to_s]}, :include_blank => true, :label => "Preference", :required => true )%></p>
</div>
<%= submit_tag l(:button_save) %>
<% end %>
View
13 features/email_notifications.feature
@@ -13,3 +13,16 @@ Feature: Email notifications
Then I am on the "Notification Preferences" page
And I should see "Notification Preference"
And I should see a form for changing my perference
+
+ Scenario: Update my notification preference
+ Given I am logged in
+ And I am a member of a project
+ And I am on the Status page for the project
+
+ When I follow "Notification Preferences"
+ And I select "Hourly" from "status_notification_option"
+ And I press "Save"
+
+ Then I am on the "Notification Preferences" page
+ And I should see "Hourly"
+ And my preference should be "hourly"
View
4 features/step_definitions/status_steps.rb
@@ -75,6 +75,10 @@ def strip_hash_from_hashtag(hashtag)
end
end
+Then /^my preference should be "(.*)"$/ do |value|
+ @current_user.status_notification.should_not be_nil
+ @current_user.status_notification.option.should eql(value)
+end
Then /^I should see "(.*)" updates$/ do |count|
response.should have_tag("dd.status_message", :count => count.to_i)
View
2  init.rb
@@ -15,7 +15,7 @@
version '0.1.0'
project_module :statuses do
- permission :view_statuses, {:statuses => [:index, :tagged, :tag_cloud], :status_notifications => [:edit]}
+ permission :view_statuses, {:statuses => [:index, :tagged, :tag_cloud], :status_notifications => [:edit, :update]}
permission :create_statuses, {:statuses => [:new, :create]}
end
View
58 spec/controllers/status_notifications_controller_spec.rb
@@ -1,10 +1,5 @@
require File.dirname(__FILE__) + '/../spec_helper'
-describe StatusNotificationsController, "routing" do
-
-end
-
-
describe StatusNotificationsController, "#edit" do
before(:each) do
@notification = mock_model(StatusNotification)
@@ -34,4 +29,57 @@
end
describe StatusNotificationsController, "#update" do
+ before(:each) do
+ @notification = mock_model(StatusNotification, :option= => nil, :option => nil)
+ @current_user = mock_model(User, :admin? => false, :logged? => true, :language => :en, :allowed_to? => true, :status_notification => @notification)
+ User.stub!(:current).and_return(@current_user)
+ end
+
+ it 'should be found at /status_notifications/update' do
+ route_for(:controller => 'status_notifications', :action => 'update').should == '/status_notifications/update'
+ end
+
+ describe 'when successfully saved' do
+ before(:each) do
+ @notification.stub!(:save).and_return(true)
+ @notification.stub!(:option_to_string).and_return('Hourly')
+ end
+
+ it 'should redirect to edit' do
+ post :update, :system_notification => {:option => 'hourly'}
+ response.should be_redirect
+ response.should redirect_to(:controller => 'status_notifications', :action => 'edit')
+ end
+
+ it 'should display a flash message' do
+ post :update, :system_notification => {:option => 'hourly'}
+ flash[:notice].should include('Preferences saved')
+ end
+
+ it 'should display the saved option in the flash message' do
+ post :update, :system_notification => {:option => 'hourly'}
+ flash[:notice].should include('Hourly')
+ end
+ end
+
+ describe 'when unsuccessfully saved' do
+ before(:each) do
+ @notification.stub!(:save).and_return(false)
+ end
+
+ it 'should render the edit template' do
+ post :update, :system_notification => {:option => 'hourly'}
+ response.should render_template('edit')
+ end
+
+ it 'should setup the notification for the template' do
+ post :update, :system_notification => {:option => 'hourly'}
+ assigns[:notification].should_not be_nil
+ end
+
+ it 'should display a flash message' do
+ post :update, :system_notification => {:option => 'hourly'}
+ flash[:error].should eql('Could not save your preference. Please try again')
+ end
+ end
end
View
33 spec/models/status_notification_spec.rb
@@ -0,0 +1,33 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe StatusNotification, '#option_to_string' do
+ it 'should return nil if no option is set' do
+ StatusNotification.new.option_to_string.should be_nil
+ end
+
+ it 'should return nil if no valid option is set' do
+ StatusNotification.new(:option => 'fake').option_to_string.should be_nil
+ end
+
+ it 'should return the VALID_OPTION value' do
+ StatusNotification.new(:option => 'realtime').option_to_string.should eql('Realtime')
+ end
+end
+
+describe StatusNotification, '#validate' do
+ it 'should be valid with one of the VALID_OPTIONS' do
+ StatusNotification.new(:option => 'realtime').valid?.should be_true
+ end
+
+ it 'should be valid with a blank option' do
+ StatusNotification.new(:option => '').valid?.should be_true
+ end
+
+ it 'should be valid with a nil option' do
+ StatusNotification.new().valid?.should be_true
+ end
+
+ it 'should not be valid with a different option' do
+ StatusNotification.new(:option => 'fake').valid?.should be_false
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.