From b90ce01890907d1c7f46f46bafcef416570a4c4b Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 17 Dec 2012 17:48:39 +0100 Subject: enabled destroying email aliases - no ajax yet. --- users/app/controllers/email_aliases_controller.rb | 25 +++-------------------- users/app/models/email.rb | 8 ++++++++ users/app/models/user.rb | 4 ++++ users/app/views/emails/_email.html.haml | 3 ++- 4 files changed, 17 insertions(+), 23 deletions(-) (limited to 'users/app') diff --git a/users/app/controllers/email_aliases_controller.rb b/users/app/controllers/email_aliases_controller.rb index 751df85..3b0d5ac 100644 --- a/users/app/controllers/email_aliases_controller.rb +++ b/users/app/controllers/email_aliases_controller.rb @@ -4,29 +4,10 @@ class EmailAliasesController < ApplicationController respond_to :html - # get a list of email aliases for the given user? - def index - @aliases = @user.email_aliases - respond_with @aliases - end - - def create - @alias = @user.add_email_alias(params[:email_alias]) - flash[:notice] = t(:email_alias_created_successfully) unless @alias.errors - respond_with @alias, :location => edit_user_path(@user, :anchor => :email) - end - - def update - @alias = @user.get_email_alias(params[:id]) - @alias.set_email(params[:email_alias]) - flash[:notice] = t(:email_alias_updated_successfully) unless @alias.errors - respond_with @alias, :location => edit_user_path(@user, :anchor => :email) - end - def destroy - @alias = @user.get_email_alias(params[:id]) - flash[:notice] = t(:email_alias_destroyed_successfully) - @alias.destroy + @alias = @user.email_aliases.delete(params[:id]) + @user.save + flash[:notice] = t(:email_alias_destroyed_successfully, :alias => @alias) redirect_to edit_user_path(@user, :anchor => :email) end diff --git a/users/app/models/email.rb b/users/app/models/email.rb index 4b01838..0745fda 100644 --- a/users/app/models/email.rb +++ b/users/app/models/email.rb @@ -14,4 +14,12 @@ class Email def to_s email end + + def ==(other) + other.is_a?(String) ? self.email == other : super + end + + def to_param + email + end end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 10f358d..d66b0e9 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -45,6 +45,7 @@ class User < CouchRest::Model::Base view :by_login view :by_created_at view :by_email + view :by_email_alias, :map => <<-EOJS function(doc) { @@ -56,6 +57,7 @@ class User < CouchRest::Model::Base }); } EOJS + view :by_email_or_alias, :map => <<-EOJS function(doc) { @@ -70,6 +72,7 @@ class User < CouchRest::Model::Base }); } EOJS + end class << self @@ -127,6 +130,7 @@ class User < CouchRest::Model::Base end end + ## # Validation Functions ## diff --git a/users/app/views/emails/_email.html.haml b/users/app/views/emails/_email.html.haml index f182ed9..f5eb2d0 100644 --- a/users/app/views/emails/_email.html.haml +++ b/users/app/views/emails/_email.html.haml @@ -1,4 +1,5 @@ %li.pull-right %code= email - %i.icon-remove + = link_to(user_email_alias_path(@user, email), :method => :delete) do + %i.icon-remove .clearfix -- cgit v1.2.3 From be2d8fbd65f4ed2e1e97b19a3322da20e2fb4eda Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 17 Dec 2012 19:03:45 +0100 Subject: activate email tab after changing email settings --- users/app/assets/javascripts/users.js.coffee | 8 ++++++++ users/app/controllers/users_controller.rb | 2 +- users/app/helpers/users_helper.rb | 5 +++++ users/app/views/users/edit.html.haml | 4 ++-- 4 files changed, 16 insertions(+), 3 deletions(-) (limited to 'users/app') diff --git a/users/app/assets/javascripts/users.js.coffee b/users/app/assets/javascripts/users.js.coffee index 0c1fb55..86bacee 100644 --- a/users/app/assets/javascripts/users.js.coffee +++ b/users/app/assets/javascripts/users.js.coffee @@ -24,7 +24,15 @@ srp.error = (message) -> pollUsers = (query, process) -> $.get( "/users.json", query: query).done(process) +followLocationHash = -> + location = window.location.hash + if location + href_select = 'a[href="' + location + '"]' + link = $(href_select) + link.tab('show') if link + $(document).ready -> + followLocationHash() $('#new_user').submit preventDefault $('#new_user').submit srp.signup $('#new_session').submit preventDefault diff --git a/users/app/controllers/users_controller.rb b/users/app/controllers/users_controller.rb index 811e8e5..feb66c8 100644 --- a/users/app/controllers/users_controller.rb +++ b/users/app/controllers/users_controller.rb @@ -36,7 +36,7 @@ class UsersController < ApplicationController else flash[:error] = @user.errors.full_messages end - respond_with @user, :location => edit_user_path(@user) + respond_with @user, :location => edit_user_path(@user, :anchor => :email) end def destroy diff --git a/users/app/helpers/users_helper.rb b/users/app/helpers/users_helper.rb index b017bca..6d76d6f 100644 --- a/users/app/helpers/users_helper.rb +++ b/users/app/helpers/users_helper.rb @@ -29,4 +29,9 @@ module UsersHelper classes << (@user.new_record? ? 'new' : 'edit') classes.compact end + + def email_settings? + params[:user] && + params[:user].keys.detect{|key| key.index('email')} + end end diff --git a/users/app/views/users/edit.html.haml b/users/app/views/users/edit.html.haml index 92ab71b..a2a0942 100644 --- a/users/app/views/users/edit.html.haml +++ b/users/app/views/users/edit.html.haml @@ -1,9 +1,9 @@ .span8.offset2 %h2=t :settings %ul.nav.nav-tabs - %li.active + %li{:class => email_settings? ? :inactive : :active} %a{:href => '#account', 'data-toggle' => 'tab'}Account - %li + %li{:class => email_settings? ? :active : :inactive} %a{:href => '#email', 'data-toggle' => 'tab'}Email .tab-content -- cgit v1.2.3 From 6393a9d705f181bd7f81270b01448ae93acb96e5 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 18 Dec 2012 02:51:18 +0100 Subject: display errors on email tab properly still needs a bit of refactoring in the view --- users/app/controllers/users_controller.rb | 4 ++-- users/app/views/users/edit.html.haml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'users/app') diff --git a/users/app/controllers/users_controller.rb b/users/app/controllers/users_controller.rb index feb66c8..8ba6a7b 100644 --- a/users/app/controllers/users_controller.rb +++ b/users/app/controllers/users_controller.rb @@ -34,9 +34,9 @@ class UsersController < ApplicationController if @user.changed? and @user.save flash[:notice] = t(:user_updated_successfully) else - flash[:error] = @user.errors.full_messages + flash.now[:error] = @user.errors.full_messages.to_sentence end - respond_with @user, :location => edit_user_path(@user, :anchor => :email) + respond_with @user.reload, :location => edit_user_path(@user, :anchor => :email) end def destroy diff --git a/users/app/views/users/edit.html.haml b/users/app/views/users/edit.html.haml index a2a0942..8fc2cab 100644 --- a/users/app/views/users/edit.html.haml +++ b/users/app/views/users/edit.html.haml @@ -7,11 +7,11 @@ %a{:href => '#email', 'data-toggle' => 'tab'}Email .tab-content - .tab-pane.active#account + .tab-pane#account{:class => email_settings? ? :inactive : :active} = user_form_with 'login_field', :legend => :change_login = user_form_with 'password_fields', :legend => :change_password = render 'cancel_account' if @user == current_user - .tab-pane#email + .tab-pane#email{:class => email_settings? ? :active : :inactive} = user_form_with 'email_field', :legend => :set_email_address = user_form_with 'email_forward_field', :legend => :forward_email = user_form_with 'email_aliases', :legend => :add_email_alias -- cgit v1.2.3 From 285b23f39765d8346a658a81eca8b70d70b3e9bf Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 18 Dec 2012 13:41:41 +0100 Subject: refactored email_alias creation and validation using CouchRests user.email_aliases.build so the casted_by method is set in the alias Used this to move the validations into the alias itself. This is where they belong and allows us to render the errors inline along the email field they belong to. --- users/app/controllers/users_controller.rb | 6 +++--- users/app/models/local_email.rb | 21 ++++++++++++++++++++- users/app/models/user.rb | 26 +++++--------------------- users/app/views/emails/_email.html.haml | 11 ++++++----- users/app/views/users/_email_aliases.html.haml | 2 +- 5 files changed, 35 insertions(+), 31 deletions(-) (limited to 'users/app') diff --git a/users/app/controllers/users_controller.rb b/users/app/controllers/users_controller.rb index 8ba6a7b..5c6767c 100644 --- a/users/app/controllers/users_controller.rb +++ b/users/app/controllers/users_controller.rb @@ -27,16 +27,16 @@ class UsersController < ApplicationController end def edit + @email_alias = LocalEmail.new end def update @user.attributes = params[:user] + @email_alias = @user.email_aliases.last if @user.changed? and @user.save flash[:notice] = t(:user_updated_successfully) - else - flash.now[:error] = @user.errors.full_messages.to_sentence end - respond_with @user.reload, :location => edit_user_path(@user, :anchor => :email) + respond_with @user, :location => edit_user_path(@user, :anchor => :email) end def destroy diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb index 7cca4f4..c654fcb 100644 --- a/users/app/models/local_email.rb +++ b/users/app/models/local_email.rb @@ -1,6 +1,9 @@ class LocalEmail < Email validate :unique_on_server + validate :unique_alias_for_user + validate :differs_from_main_email + validates :casted_by, :presence => true def to_partial_path "emails/email" @@ -9,7 +12,23 @@ class LocalEmail < Email def unique_on_server has_email = User.find_by_email_or_alias(email) if has_email && has_email != self.base_doc - errors.add(:email, "has already been taken") + errors.add :email, "has already been taken" end end + + def unique_alias_for_user + aliases = self.casted_by.email_aliases + if aliases.select{|a|a.email == self.email}.count > 1 + errors.add :email, "is already your alias" + end + end + + def differs_from_main_email + user = self.casted_by + if user.email == self.email + errors.add :email, "may not be the same as your email address" + end + end + + end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index d66b0e9..0773f28 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -35,9 +35,7 @@ class User < CouchRest::Model::Base validates :email_forward, :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} - validate :no_duplicate_email_aliases - - validate :email_aliases_differ_from_email + validate :email_differs_from_email_aliases timestamps! @@ -117,34 +115,20 @@ class User < CouchRest::Model::Base APP_CONFIG['admins'].include? self.login end - def add_email_alias(email) - email = LocalEmail.new(email) unless email.is_a? Email - email_aliases << email - end - # this currently only adds the first email address submitted. # All the ui needs for now. def email_aliases_attributes=(attrs) - if attrs && attrs.values.first - add_email_alias attrs.values.first - end + email_aliases.build(attrs.values.first) if attrs end - ## # Validation Functions ## - # TODO: How do we handle these errors? - def no_duplicate_email_aliases - if email_aliases.count != email_aliases.map(&:email).uniq.count - errors.add(:email_aliases, "include a duplicate") - end - end - - def email_aliases_differ_from_email + def email_differs_from_email_aliases + return if email_aliases.last.errors.any? if email_aliases.map(&:email).include?(email) - errors.add(:email_aliases, "include the original email address") + errors.add(:email, "may not be the same as an alias") end end diff --git a/users/app/views/emails/_email.html.haml b/users/app/views/emails/_email.html.haml index f5eb2d0..3feb6f0 100644 --- a/users/app/views/emails/_email.html.haml +++ b/users/app/views/emails/_email.html.haml @@ -1,5 +1,6 @@ -%li.pull-right - %code= email - = link_to(user_email_alias_path(@user, email), :method => :delete) do - %i.icon-remove -.clearfix +- if email.valid? + %li.pull-right + %code= email + = link_to(user_email_alias_path(@user, email), :method => :delete) do + %i.icon-remove + .clearfix diff --git a/users/app/views/users/_email_aliases.html.haml b/users/app/views/users/_email_aliases.html.haml index 646480e..121acc4 100644 --- a/users/app/views/users/_email_aliases.html.haml +++ b/users/app/views/users/_email_aliases.html.haml @@ -2,5 +2,5 @@ %ul.unstyled =render @user.email_aliases .clearfix -= f.simple_fields_for :email_aliases, Email.new do |e| += f.simple_fields_for :email_aliases, @email_alias do |e| = e.input :email, :placeholder => "alias@#{request.domain}" -- cgit v1.2.3 From 76904d3f677520eb191a90f2abb15b9df5fd8ae9 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 18 Dec 2012 14:02:31 +0100 Subject: make sure we have email_aliases at all before testing for an error on the last --- users/app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'users/app') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 0773f28..1999a28 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -126,7 +126,7 @@ class User < CouchRest::Model::Base ## def email_differs_from_email_aliases - return if email_aliases.last.errors.any? + return if email_aliases.any? and email_aliases.last.errors.any? if email_aliases.map(&:email).include?(email) errors.add(:email, "may not be the same as an alias") end -- cgit v1.2.3 From dc827d0a80360aa245d4d724dc42bc47571faea6 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 18 Dec 2012 17:01:34 +0100 Subject: refactor: using tab partials for user editing --- users/app/controllers/users_controller.rb | 10 ++++++++++ users/app/helpers/users_helper.rb | 4 ---- users/app/views/users/edit.html.haml | 24 +++++++++--------------- 3 files changed, 19 insertions(+), 19 deletions(-) (limited to 'users/app') diff --git a/users/app/controllers/users_controller.rb b/users/app/controllers/users_controller.rb index 5c6767c..3d71c1a 100644 --- a/users/app/controllers/users_controller.rb +++ b/users/app/controllers/users_controller.rb @@ -3,6 +3,7 @@ class UsersController < ApplicationController skip_before_filter :verify_authenticity_token, :only => [:create] before_filter :fetch_user, :only => [:edit, :update, :destroy] + before_filter :set_anchor, :only => [:edit, :update] before_filter :authorize_admin, :only => [:index] respond_to :json, :html @@ -50,4 +51,13 @@ class UsersController < ApplicationController @user = User.find_by_param(params[:id]) access_denied unless admin? or (@user == current_user) end + + def set_anchor + @anchor = email_settings? ? :email : :account + end + + def email_settings? + params[:user] && + params[:user].keys.detect{|key| key.index('email')} + end end diff --git a/users/app/helpers/users_helper.rb b/users/app/helpers/users_helper.rb index 6d76d6f..45ca0e9 100644 --- a/users/app/helpers/users_helper.rb +++ b/users/app/helpers/users_helper.rb @@ -30,8 +30,4 @@ module UsersHelper classes.compact end - def email_settings? - params[:user] && - params[:user].keys.detect{|key| key.index('email')} - end end diff --git a/users/app/views/users/edit.html.haml b/users/app/views/users/edit.html.haml index 8fc2cab..820b80e 100644 --- a/users/app/views/users/edit.html.haml +++ b/users/app/views/users/edit.html.haml @@ -1,17 +1,11 @@ .span8.offset2 %h2=t :settings - %ul.nav.nav-tabs - %li{:class => email_settings? ? :inactive : :active} - %a{:href => '#account', 'data-toggle' => 'tab'}Account - %li{:class => email_settings? ? :active : :inactive} - %a{:href => '#email', 'data-toggle' => 'tab'}Email - - .tab-content - .tab-pane#account{:class => email_settings? ? :inactive : :active} - = user_form_with 'login_field', :legend => :change_login - = user_form_with 'password_fields', :legend => :change_password - = render 'cancel_account' if @user == current_user - .tab-pane#email{:class => email_settings? ? :active : :inactive} - = user_form_with 'email_field', :legend => :set_email_address - = user_form_with 'email_forward_field', :legend => :forward_email - = user_form_with 'email_aliases', :legend => :add_email_alias + - content_for :account do + = user_form_with 'login_field', :legend => :change_login + = user_form_with 'password_fields', :legend => :change_password + = render 'cancel_account' if @user == current_user + - content_for :email do + = user_form_with 'email_field', :legend => :set_email_address + = user_form_with 'email_forward_field', :legend => :forward_email + = user_form_with 'email_aliases', :legend => :add_email_alias + = render 'tabs/tabs', :tabs => [:account, :email] -- cgit v1.2.3