email prefs are now saved. need more tests and use it in user#mail
This commit is contained in:
parent
155290fb51
commit
e9a843b095
8 changed files with 119 additions and 22 deletions
|
|
@ -13,37 +13,44 @@ class UsersController < ApplicationController
|
||||||
def edit
|
def edit
|
||||||
@aspect = :user_edit
|
@aspect = :user_edit
|
||||||
@user = current_user
|
@user = current_user
|
||||||
|
@email_prefs = Hash.new(true)
|
||||||
|
@user.user_preferences.each do |pref|
|
||||||
|
@email_prefs[pref.email_type] = false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
|
||||||
|
u = params[:user]
|
||||||
@user = current_user
|
@user = current_user
|
||||||
|
|
||||||
params[:user].delete(:password) if params[:user][:password].blank?
|
u.delete(:password) if u[:password].blank?
|
||||||
params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
|
u.delete(:password_confirmation) if u[:password].blank? and u[:password_confirmation].blank?
|
||||||
params[:user].delete(:language) if params[:user][:language].blank?
|
u.delete(:language) if u[:language].blank?
|
||||||
|
|
||||||
# change email notifications
|
# change email notifications
|
||||||
if params[:user][:disable_mail]
|
if u[:email_preferences]
|
||||||
@user.update_attributes(:disable_mail => params[:user][:disable_mail])
|
pp u[:email_preferences]
|
||||||
|
@user.update_user_preferences(u[:email_preferences])
|
||||||
flash[:notice] = I18n.t 'users.update.email_notifications_changed'
|
flash[:notice] = I18n.t 'users.update.email_notifications_changed'
|
||||||
# change passowrd
|
# change passowrd
|
||||||
elsif params[:user][:current_password] && params[:user][:password] && params[:user][:password_confirmation]
|
elsif u[:current_password] && u[:password] && u[:password_confirmation]
|
||||||
if @user.update_with_password(params[:user])
|
if @user.update_with_password(u)
|
||||||
flash[:notice] = I18n.t 'users.update.password_changed'
|
flash[:notice] = I18n.t 'users.update.password_changed'
|
||||||
else
|
else
|
||||||
flash[:error] = I18n.t 'users.update.password_not_changed'
|
flash[:error] = I18n.t 'users.update.password_not_changed'
|
||||||
end
|
end
|
||||||
elsif params[:user][:language]
|
elsif u[:language]
|
||||||
if @user.update_attributes(:language => params[:user][:language])
|
if @user.update_attributes(:language => u[:language])
|
||||||
I18n.locale = @user.language
|
I18n.locale = @user.language
|
||||||
flash[:notice] = I18n.t 'users.update.language_changed'
|
flash[:notice] = I18n.t 'users.update.language_changed'
|
||||||
else
|
else
|
||||||
flash[:error] = I18n.t 'users.update.language_not_changed'
|
flash[:error] = I18n.t 'users.update.language_not_changed'
|
||||||
end
|
end
|
||||||
elsif params[:user][:a_ids]
|
elsif u[:a_ids]
|
||||||
@user.aspects.update_all(:open => false)
|
@user.aspects.update_all(:open => false)
|
||||||
unless params[:user][:a_ids] == ["home"]
|
unless u[:a_ids] == ["home"]
|
||||||
@user.aspects.where(:id => params[:user][:a_ids]).update_all(:open => true)
|
@user.aspects.where(:id => u[:a_ids]).update_all(:open => true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ class User < ActiveRecord::Base
|
||||||
has_many :contacts
|
has_many :contacts
|
||||||
has_many :contact_people, :through => :contacts, :source => :person
|
has_many :contact_people, :through => :contacts, :source => :person
|
||||||
has_many :services
|
has_many :services
|
||||||
|
has_many :user_preferences
|
||||||
|
|
||||||
before_destroy :disconnect_everyone, :remove_person
|
before_destroy :disconnect_everyone, :remove_person
|
||||||
before_save do
|
before_save do
|
||||||
|
|
@ -45,6 +46,22 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
attr_accessible :getting_started, :password, :password_confirmation, :language, :disable_mail
|
attr_accessible :getting_started, :password, :password_confirmation, :language, :disable_mail
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def update_user_preferences(pref_hash)
|
||||||
|
pref_hash.keys.each do |key|
|
||||||
|
if pref_hash[key] == 'true'
|
||||||
|
self.user_preferences.find_or_create_by_email_type(key)
|
||||||
|
else
|
||||||
|
block = self.user_preferences.where(:email_type => key).first
|
||||||
|
if block
|
||||||
|
block.destroy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def strip_and_downcase_username
|
def strip_and_downcase_username
|
||||||
if username.present?
|
if username.present?
|
||||||
username.strip!
|
username.strip!
|
||||||
|
|
|
||||||
3
app/models/user_preference.rb
Normal file
3
app/models/user_preference.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
class UserPreference < ActiveRecord::Base
|
||||||
|
belongs_to :user
|
||||||
|
end
|
||||||
|
|
@ -77,11 +77,39 @@
|
||||||
= form_for @user do |f|
|
= form_for @user do |f|
|
||||||
= f.error_messages
|
= f.error_messages
|
||||||
|
|
||||||
%p.checkbox_select
|
= f.fields_for :email_preferences do |type|
|
||||||
= f.label :disable_mail, t('.receive_email_notifications')
|
#email_prefs
|
||||||
= f.check_box :disable_mail, {:checked => !@user.disable_mail}, false, true
|
%p.checkbox_select
|
||||||
%br
|
= type.label t('.also_commented')
|
||||||
= f.submit t('.change')
|
= type.check_box :also_commented, {:checked => @email_prefs['also_commented']}, false, true
|
||||||
|
%br
|
||||||
|
%p.checkbox_select
|
||||||
|
= type.label t('.mentioned')
|
||||||
|
= type.check_box :mentioned, {:checked => @email_prefs['mentioned']}, false, true
|
||||||
|
|
||||||
|
|
||||||
|
%br
|
||||||
|
%p.checkbox_select
|
||||||
|
= type.label t('.comment_on_post')
|
||||||
|
= type.check_box :comment_on_post, {:checked => @email_prefs['comment_on_post']}, false, true
|
||||||
|
|
||||||
|
%br
|
||||||
|
%p.checkbox_select
|
||||||
|
= type.label t('.request_received')
|
||||||
|
= type.check_box :request_received, {:checked => @email_prefs['request_received']}, false, true
|
||||||
|
|
||||||
|
%br
|
||||||
|
%p.checkbox_select
|
||||||
|
= type.label t('.private_message')
|
||||||
|
= type.check_box :private_message, {:checked => @email_prefs['private_message']}, false, true
|
||||||
|
|
||||||
|
%br
|
||||||
|
%p.checkbox_select
|
||||||
|
= type.label t('.request_acceptence')
|
||||||
|
= type.check_box :request_acceptence, {:checked => @email_prefs['request_accpetence']}, false, true
|
||||||
|
|
||||||
|
%br
|
||||||
|
= f.submit t('.change')
|
||||||
|
|
||||||
%br
|
%br
|
||||||
%br
|
%br
|
||||||
|
|
@ -100,5 +128,5 @@
|
||||||
%h3
|
%h3
|
||||||
= t('.close_account')
|
= t('.close_account')
|
||||||
= link_to t('.close_account'), current_user,
|
= link_to t('.close_account'), current_user,
|
||||||
:confirm => t('are_you_sure'), :method => :delete,
|
:confirm => t('are_you_sure'), :method => :delete,
|
||||||
:class => "button"
|
:class => "button"
|
||||||
|
|
|
||||||
14
db/migrate/20110311183826_create_user_preferences.rb
Normal file
14
db/migrate/20110311183826_create_user_preferences.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
class CreateUserPreferences < ActiveRecord::Migration
|
||||||
|
def self.up
|
||||||
|
create_table :user_preferences do |t|
|
||||||
|
t.string :email_type
|
||||||
|
t.integer :user_id
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
drop_table :user_preferences
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -476,6 +476,13 @@ ActiveRecord::Schema.define(:version => 20110311220249) do
|
||||||
t.string "name"
|
t.string "name"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "user_preferences", :force => true do |t|
|
||||||
|
t.string "email_type"
|
||||||
|
t.integer "user_id"
|
||||||
|
t.datetime "created_at"
|
||||||
|
t.datetime "updated_at"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "users", :force => true do |t|
|
create_table "users", :force => true do |t|
|
||||||
t.string "username"
|
t.string "username"
|
||||||
t.text "serialized_private_key"
|
t.text "serialized_private_key"
|
||||||
|
|
@ -501,6 +508,7 @@ ActiveRecord::Schema.define(:version => 20110311220249) do
|
||||||
t.string "mongo_id"
|
t.string "mongo_id"
|
||||||
t.string "invitation_service", :limit => 127
|
t.string "invitation_service", :limit => 127
|
||||||
t.string "invitation_identifier", :limit => 127
|
t.string "invitation_identifier", :limit => 127
|
||||||
|
t.text "email_disabled"
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "users", ["email"], :name => "index_users_on_email"
|
add_index "users", ["email"], :name => "index_users_on_email"
|
||||||
|
|
|
||||||
5
spec/models/user_preferences_spec.rb
Normal file
5
spec/models/user_preferences_spec.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe UserPreferences do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
|
|
@ -215,6 +215,22 @@ describe User do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'update_user_preferences' do
|
||||||
|
it 'unsets disable mail and makes the right amount of prefs' do
|
||||||
|
alice.disable_mail = true
|
||||||
|
proc {
|
||||||
|
alice.update_user_preferences({})
|
||||||
|
}.should change(alice.user_preferences, :count).by(6)
|
||||||
|
end
|
||||||
|
it 'still sets new prefs to false on update' do
|
||||||
|
alice.disable_mail = true
|
||||||
|
proc {
|
||||||
|
alice.update_user_preferences({'mentioned' => false})
|
||||||
|
}.should change(alice.user_preferences, :count).by(5)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
describe ".find_for_authentication" do
|
describe ".find_for_authentication" do
|
||||||
it 'finds a user' do
|
it 'finds a user' do
|
||||||
|
|
@ -450,11 +466,10 @@ describe User do
|
||||||
alice.mail(Job::MailRequestReceived, alice.id, 'contactrequestid')
|
alice.mail(Job::MailRequestReceived, alice.id, 'contactrequestid')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not enqueue a mail job' do
|
it 'does not enqueue a mail job if the correct corresponding job has a prefrence entry' do
|
||||||
alice.disable_mail = true
|
|
||||||
alice.save
|
alice.save
|
||||||
alice.reload
|
alice.reload
|
||||||
|
alice.user_preferences.create(:email_type => 'request_received')
|
||||||
Resque.should_not_receive(:enqueue)
|
Resque.should_not_receive(:enqueue)
|
||||||
alice.mail(Job::MailRequestReceived, alice.id, 'contactrequestid')
|
alice.mail(Job::MailRequestReceived, alice.id, 'contactrequestid')
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue