diff options
author | Azul <azul@leap.se> | 2012-12-10 11:00:24 +0100 |
---|---|---|
committer | Azul <azul@leap.se> | 2012-12-10 11:00:24 +0100 |
commit | 28b21959f39b0e28c450bba54b8696632a0187fa (patch) | |
tree | 7f13cad03190324ca0d242d3aee74bfb4efb00fa | |
parent | e4c7f2fb8fa2833037508f1b88f802944855fd77 (diff) |
created generic Email class and use it with EmailAliases
-rw-r--r-- | users/app/models/email.rb (renamed from users/app/models/email_alias.rb) | 7 | ||||
-rw-r--r-- | users/app/models/user.rb | 4 | ||||
-rw-r--r-- | users/app/views/emails/_email.html.haml | 4 | ||||
-rw-r--r-- | users/app/views/users/_email_aliases.html.haml | 8 | ||||
-rw-r--r-- | users/test/unit/email_alias_test.rb | 50 | ||||
-rw-r--r-- | users/test/unit/email_aliases_test.rb | 30 | ||||
-rw-r--r-- | users/test/unit/email_test.rb | 33 |
7 files changed, 76 insertions, 60 deletions
diff --git a/users/app/models/email_alias.rb b/users/app/models/email.rb index 25e4b27..0988d9f 100644 --- a/users/app/models/email_alias.rb +++ b/users/app/models/email.rb @@ -1,6 +1,9 @@ -class EmailAlias +class Email include CouchRest::Model::Embeddable property :email, String - timestamps! + + def to_s + email + end end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 3ad69c7..fcb211e 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -6,7 +6,7 @@ class User < CouchRest::Model::Base property :email, String, :accessible => true property :email_forward, String, :accessible => true - property :email_aliases, [EmailAlias] + property :email_aliases, [Email] validates :login, :password_salt, :password_verifier, :presence => true @@ -77,7 +77,7 @@ class User < CouchRest::Model::Base def email_aliases_attributes=(attrs) if attrs - email_alias = EmailAlias.new(attrs.values.first) + email_alias = Email.new(attrs.values.first) email_aliases << email_alias end end diff --git a/users/app/views/emails/_email.html.haml b/users/app/views/emails/_email.html.haml new file mode 100644 index 0000000..f182ed9 --- /dev/null +++ b/users/app/views/emails/_email.html.haml @@ -0,0 +1,4 @@ +%li.pull-right + %code= email + %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 41d4f9e..646480e 100644 --- a/users/app/views/users/_email_aliases.html.haml +++ b/users/app/views/users/_email_aliases.html.haml @@ -1,10 +1,6 @@ .span6 %ul.unstyled - - @user.email_aliases.each do |email_alias| - %li.pull-right - %code= email_alias.email - %i.icon-remove - .clearfix + =render @user.email_aliases .clearfix -= f.simple_fields_for :email_aliases, EmailAlias.new do |e| += f.simple_fields_for :email_aliases, Email.new do |e| = e.input :email, :placeholder => "alias@#{request.domain}" diff --git a/users/test/unit/email_alias_test.rb b/users/test/unit/email_alias_test.rb deleted file mode 100644 index 86cb10f..0000000 --- a/users/test/unit/email_alias_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'test_helper' - -class EmailAliasTest < ActiveSupport::TestCase - - setup do - @attribs = User.valid_attributes_hash - User.find_by_login(@attribs[:login]).try(:destroy) - @user = User.new(@attribs) - end - - test "no email aliases set in the beginning" do - assert_equal [], @user.email_aliases - end - - test "adding email alias" do - email_alias = "valid_alias@domain.net" - @user.add_email_alias(email_alias) - assert_equal [email_alias], @user.email_aliases - end - - test "email aliases need to be unique" do - # todo build helper for this ... make_record(User) - email_alias = "valid_alias@domain.net" - attribs = User.valid_attributes_hash.merge(:login => "other_user") - User.find_by_login(attribs[:login]).try(:destroy) - other_user = User.new(attribs) - other_user.add_email_alias(email_alias) - @user.add_email_alias(email_alias) - # todo: how do we handle errors? Should email_alias become an ActiveModel? - assert_equal [], @user.email_aliases - end - - test "email aliases may not conflict with emails" do - # todo build helper for this ... make_record(User) - email_alias = "valid_alias@domain.net" - attribs = User.valid_attributes_hash.merge(:login => "other_user", :email => email_alias) - User.find_by_login(attribs[:login]).try(:destroy) - other_user = User.new(attribs) - @user.add_email_alias(email_alias) - # todo: how do we handle errors? Should email_alias become an ActiveModel? - assert_equal [], @user.email_aliases - end - - test "can retrieve user by email alias" do - email_alias = "valid_alias@domain.net" - @user.add_email_alias(email_alias) - assert_equal @user, User.find_by_email_alias(email_alias) - assert_equal @user, User.find_by_email(email_alias) - end -end diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb new file mode 100644 index 0000000..e737773 --- /dev/null +++ b/users/test/unit/email_aliases_test.rb @@ -0,0 +1,30 @@ +require 'test_helper' + +class EmailAliasTest < ActiveSupport::TestCase + + setup do + @attribs = User.valid_attributes_hash + User.find_by_login(@attribs[:login]).try(:destroy) + @user = User.new(@attribs) + end + + test "no email aliases set in the beginning" do + assert_equal [], @user.email_aliases + end + + test "adding email alias" do + email_alias = "valid_alias@domain.net" + @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + assert @user.changed? + assert @user.save + assert_equal email_alias, @user.email_aliases.first.to_s + end + + test "can retrieve user by email alias" do + email_alias = "valid_alias@domain.net" + @user.attributes = {:email_aliases => [email_alias]} + @user.save + assert_equal @user, User.find_by_email_alias(email_alias) + assert_equal @user, User.find_by_email(email_alias) + end +end diff --git a/users/test/unit/email_test.rb b/users/test/unit/email_test.rb new file mode 100644 index 0000000..d421b77 --- /dev/null +++ b/users/test/unit/email_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class EmailAliasTest < ActiveSupport::TestCase + + setup do + @attribs = User.valid_attributes_hash + User.find_by_login(@attribs[:login]).try(:destroy) + @user = User.new(@attribs) + end + + test "email aliases need to be unique" do + # TODO build helper for this ... make_record(User) + email_alias = "valid_alias@domain.net" + attribs = User.valid_attributes_hash.merge(:login => "other_user") + User.find_by_login(attribs[:login]).try(:destroy) + other_user = User.new(attribs) + other_user.attributes = {:email_aliases => [email_alias]} + other_user.save + @user.attributes = {:email_aliases => [email_alias]} + assert !@user.valid? + # TODO handle errors + end + + test "email aliases may not conflict with emails" do + # TODO build helper for this ... make_record(User) + email_alias = "valid_alias@domain.net" + attribs = User.valid_attributes_hash.merge(:login => "other_user", :email => email_alias) + User.find_by_login(attribs[:login]).try(:destroy) + other_user = User.new(attribs) + @user.attributes = {:email_aliases => [email_alias]} + assert !@user.valid? + end +end |