diff --git a/app/models/block.rb b/app/models/block.rb
index 5d52a14..b0f137d 100644
--- a/app/models/block.rb
+++ b/app/models/block.rb
@@ -1,2 +1,10 @@
class Block < ActiveRecord::Base
+ belongs_to :box
+
+ #position codl not be nil and must be an integer
+ validates_numericality_of :position, :only_integer => true , :message => _('%{fn} must be composed only of integers')
+
+ # A block must be associated to a box
+ validates_presence_of :box_id
+
end
diff --git a/app/models/box.rb b/app/models/box.rb
index 8e489df..ea414cb 100644
--- a/app/models/box.rb
+++ b/app/models/box.rb
@@ -2,8 +2,8 @@ class Box < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
#we cannot have two boxs with the same number to the same owner
- validates_uniqueness_of :number, :scope => :owner
+ validates_uniqueness_of :number, :scope => [:owner_type, :owner_id]
- #number must be a integer
+ #number could not be nil and must be an integer
validates_numericality_of :number, :only_integer => true, :message => _('%{fn} must be composed only of integers.')
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 4a57cf0..8d33177 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,2 +1,3 @@
class User < ActiveRecord::Base
+ has_many :boxes, :as => :owner
end
diff --git a/test/fixtures/boxes.yml b/test/fixtures/boxes.yml
index c786b5e..953874e 100644
--- a/test/fixtures/boxes.yml
+++ b/test/fixtures/boxes.yml
@@ -2,9 +2,15 @@
one:
id: 1
number: 1
+ owner_type: 'User'
+ owner_id: 1
two:
id: 2
number: 2
+ owner_type: 'User'
+ owner_id: 1
three:
id: 3
number: 3
+ owner_type: 'User'
+ owner_id: 1
diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml
new file mode 100644
index 0000000..7316778
--- /dev/null
+++ b/test/fixtures/users.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+ id: 1
+ name: 'Leandro Nunes dos Santos'
+two:
+ id: 2
+ name: 'Antonio S. A. Terceiro'
diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb
index 83c97a9..ffe2355 100644
--- a/test/unit/block_test.rb
+++ b/test/unit/block_test.rb
@@ -4,7 +4,55 @@ class BlockTest < Test::Unit::TestCase
fixtures :blocks
# Replace this with your real tests.
- def test_truth
- assert true
+ def test_create
+ count = Block.count
+ b = Block.new
+ assert !b.valid?
+ assert b.errors.invalid?(:box_id)
+ assert b.errors.invalid?(:position)
+
+ u = User.new
+ assert u.save
+ box = Box.new
+ box.owner = u
+ box.number = 1000
+ assert box.save
+ b.box = box
+ assert !b.valid?
+ assert b.errors.invalid?(:position)
+
+ b.position=1
+ assert b.save
+
+ assert_equal count + 1, Block.count
+ end
+
+ def test_box_presence
+ b = Block.new
+ b.position = 1000
+ assert !b.valid?
+ assert b.errors.invalid?(:box_id)
+
+ u = User.new
+ assert u.save
+ box = Box.new
+ box.owner = u
+ box.number = 1000
+ assert box.save
+ b.box = box
+ assert b.valid?
+
end
+
+ def test_destroy
+ b = Block.find(1)
+ assert b.destroy
+ end
+
+ def test_valid_fixtures
+ Block.find_all.each do |b|
+ assert b.valid?
+ end
+ end
+
end
diff --git a/test/unit/box_test.rb b/test/unit/box_test.rb
index c42e14e..31dc94c 100644
--- a/test/unit/box_test.rb
+++ b/test/unit/box_test.rb
@@ -43,4 +43,10 @@ class BoxTest < Test::Unit::TestCase
assert b.errors.invalid?(:number)
end
+ def test_presence_number
+ b = Box.new(:number => nil)
+ assert !b.valid?
+ assert b.errors.invalid?(:number)
+ end
+
end
--
libgit2 0.21.2