model specs and validations

Umar M. Sheikh %!s(int64=11) %!d(string=hace) años
padre
commit
68527eff67
Se han modificado 4 ficheros con 23 adiciones y 3 borrados
  1. 1 1
      app/models/user.rb
  2. 2 0
      app/models/user_credential.rb
  3. 11 1
      spec/models/user_credential_spec.rb
  4. 9 1
      spec/models/users_spec.rb

+ 1 - 1
app/models/user.rb

@@ -23,7 +23,7 @@ class User < ActiveRecord::Base
23 23
   validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid"
24 24
 
25 25
   has_many :user_credentials, :dependent => :destroy
26
-  accepts_nested_attributes_for :user_credentials, :reject_if => lambda { |attrs| attrs[:name].blank? },
26
+  accepts_nested_attributes_for :user_credentials,
27 27
                                 :allow_destroy => true
28 28
   attr_accessible :user_credentials_attributes
29 29
   has_many :events, :order => "events.created_at desc", :dependent => :delete_all, :inverse_of => :user

+ 2 - 0
app/models/user_credential.rb

@@ -1,4 +1,6 @@
1 1
 class UserCredential < ActiveRecord::Base
2 2
   attr_accessible :credential_name, :credential_value, :user_id
3 3
   belongs_to :user
4
+  validates_presence_of :credential_name
5
+  validates_uniqueness_of :credential_name, :scope => :user_id
4 6
 end

+ 11 - 1
spec/models/user_credential_spec.rb

@@ -1,5 +1,15 @@
1
+require 'pry'
1 2
 require 'spec_helper'
2 3
 
3 4
 describe UserCredential do
4
-  pending "add some examples to (or delete) #{__FILE__}"
5
+  describe "validation" do
6
+    it {should validate_uniqueness_of(:credential_name).scoped_to(:user_id)}
7
+  end
8
+  describe "mass assignment" do
9
+    it {should allow_mass_assignment_of :credential_name}
10
+
11
+    it {should allow_mass_assignment_of :credential_value}
12
+
13
+    it {should allow_mass_assignment_of :user_id}
14
+  end
5 15
 end

+ 9 - 1
spec/models/users_spec.rb

@@ -16,4 +16,12 @@ describe User do
16 16
       end
17 17
     end
18 18
   end
19
-end
19
+
20
+  describe "nested attributes" do
21
+    it { should accept_nested_attributes_for(:user_credentials).allow_destroy(true) }
22
+  end
23
+
24
+  describe "mass assignment" do
25
+    it {should allow_mass_assignment_of :user_credentials_attributes}
26
+  end
27
+end