Commit 1f75bc060aeedeaf60f75c65f4a5db1cc3a75fee
1 parent
3c48d74f
Exists in
master
and in
29 other branches
r226@sede: terceiro | 2007-07-28 16:54:22 -0300
ActionItem0: bringing box from old flexible_template git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@230 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
122 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,62 @@ |
1 | +module Design | |
2 | + | |
3 | + # A design is composed of one or more boxes. Each box defines an area in the | |
4 | + # screen identified by a number and possibly by a name. A box contains inside | |
5 | + # it several Block objects, and is owner by some other object, possibly one | |
6 | + # from the application model. | |
7 | + class Box < ActiveRecord::Base | |
8 | + | |
9 | + set_table_name 'design_boxes' | |
10 | + | |
11 | + has_many :blocks | |
12 | + belongs_to :owner, :polymorphic => true | |
13 | + | |
14 | + validates_presence_of :owner | |
15 | + validates_presence_of :name | |
16 | + | |
17 | + #we cannot have two boxs with the same number to the same owner | |
18 | + validates_uniqueness_of :number, :scope => [:owner_type, :owner_id] | |
19 | + | |
20 | + #<tt>number</tt> could not be nil and must be an integer | |
21 | + validates_numericality_of :number, :only_integer => true | |
22 | + #TODO see the internationalization questions | |
23 | + #, :message => _('%{fn} must be composed only of integers.') | |
24 | + | |
25 | + # Return all blocks of the current box object sorted by the position block | |
26 | + def blocks_sort_by_position | |
27 | + self.blocks.sort{|x,y| x.position <=> y.position} | |
28 | + end | |
29 | + | |
30 | + def save | |
31 | + self[:name] ||= "Box " + self.number.to_s | |
32 | + super | |
33 | + end | |
34 | + | |
35 | + def owner= owner | |
36 | + self[:owner_type] = owner.class.to_s | |
37 | + self[:owner_id] = owner.id | |
38 | + n_boxes = self.owner.boxes.count if self.owner | |
39 | + if !n_boxes.nil? | |
40 | + self[:number] ||= n_boxes == 0 ? 1 : n_boxes + 1 | |
41 | + else | |
42 | + self[:number] ||= nil | |
43 | + end | |
44 | + end | |
45 | + | |
46 | + def switch_number box | |
47 | + throw :cant_switch_without_save if self[:id].nil? and (box.nil? or box.id.nil?) | |
48 | + | |
49 | + max_box = box.owner.boxes.count | |
50 | + transaction do | |
51 | + n = self[:number] | |
52 | + self[:number] = box.number | |
53 | + box.number = max_box + 1 | |
54 | + box.save | |
55 | + self.save | |
56 | + box.number = n | |
57 | + box.save | |
58 | + end | |
59 | + end | |
60 | + | |
61 | + end | |
62 | +end | ... | ... |
... | ... | @@ -0,0 +1,60 @@ |
1 | +require File.dirname(__FILE__) + '/test_helper' | |
2 | + | |
3 | +class BoxTest < Test::Unit::TestCase | |
4 | + | |
5 | + include Design | |
6 | + | |
7 | + def setup | |
8 | + @owner = DesignTestUser.create!(:name => 'my test user') | |
9 | + end | |
10 | + | |
11 | + def teardown | |
12 | + Box.delete_all | |
13 | + DesignTestUser.delete_all | |
14 | + end | |
15 | + | |
16 | + def test_block_should_have_an_owner | |
17 | + count = Box.count | |
18 | + b = Box.new | |
19 | + b.owner = @owner | |
20 | + assert b.save | |
21 | + assert count + 1, Box.count | |
22 | + end | |
23 | + | |
24 | + def test_should_only_accept_integers_as_number | |
25 | + b = Box.new | |
26 | + b.number = "none" | |
27 | + assert !b.valid? | |
28 | + assert b.errors.invalid?(:number) | |
29 | + | |
30 | + b = Box.new | |
31 | + b.owner = @owner | |
32 | + b.number = 10.2 | |
33 | + assert !b.valid? | |
34 | + assert b.errors.invalid?(:number) | |
35 | + | |
36 | + b = Box.new | |
37 | + b.owner = @owner | |
38 | + b.number = 10 | |
39 | + assert b.save | |
40 | + end | |
41 | + | |
42 | + def test_should_require_unique_number | |
43 | + b1 = Box.new | |
44 | + b1.owner = @owner | |
45 | + assert b1.save | |
46 | + | |
47 | + b2 = Box.new | |
48 | + b2.owner = @owner | |
49 | + b2.number = b1.number | |
50 | + assert !b2.valid? | |
51 | + assert b2.errors.invalid?(:number) | |
52 | + end | |
53 | + | |
54 | + def test_should_require_presence_of_number | |
55 | + b = Box.new(:number => nil) | |
56 | + assert !b.valid? | |
57 | + assert b.errors.invalid?(:number) | |
58 | + end | |
59 | + | |
60 | +end | ... | ... |