Skip to content

Commit 2507624

Browse files
committed
Set position=max+1 for new text block and add asserts to existing integration tests
1 parent 391863e commit 2507624

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

app/models/text_block.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class TextBlock < ActiveRecord::Base
55

66
validates :name, presence: true
77
validate :name_uniqueness
8+
validates_numericality_of :position, :only_integer => true
9+
before_create :set_position
10+
811
scope :sorted, ->{ order :position }
912

1013
private
@@ -21,4 +24,15 @@ def name_uniqueness
2124
end
2225
end
2326

27+
def set_position
28+
if project_id.present?
29+
max = self.class.where(:project_id => project_id).maximum(:position) || 0
30+
self.position = max + 1
31+
else
32+
binding.pry
33+
max = self.class.where(:project_id => nil).maximum(:position) || 0
34+
self.position = max + 1
35+
end
36+
end
37+
2438
end

test/integration/text_blocks_admin_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_textblock_crud
3131

3232
assert b = TextBlock.find_by_name('test')
3333
assert_equal 'lorem ipsum', b.text
34+
assert_equal 1, b.position
3435

3536
get "/text_blocks/#{b.id}/edit"
3637
assert_response :success
@@ -40,6 +41,17 @@ def test_textblock_crud
4041
assert_equal 'lorem ipsum', b.text
4142
assert_equal 'new', b.name
4243

44+
assert_difference 'TextBlock.count' do
45+
post '/text_blocks', params: { text_block: { name: 'test2', text: 'lorem ipsum2'}}
46+
end
47+
assert_redirected_to '/text_blocks'
48+
49+
follow_redirect!
50+
51+
assert b = TextBlock.find_by_name('test2')
52+
assert_equal 'lorem ipsum2', b.text
53+
assert_equal 2, b.position
54+
4355
assert_difference 'TextBlock.count', -1 do
4456
delete "/text_blocks/#{b.id}"
4557
end

test/integration/text_blocks_project_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def test_textblock_crud
4545

4646
assert b = TextBlock.find_by_name('test')
4747
assert_equal 'lorem ipsum', b.text
48+
assert_equal 1, b.position
4849

4950
get "/projects/ecookbook/text_blocks/#{b.id}/edit"
5051
assert_response :success
@@ -54,6 +55,17 @@ def test_textblock_crud
5455
assert_equal 'lorem ipsum', b.text
5556
assert_equal 'new', b.name
5657

58+
assert_difference 'TextBlock.count' do
59+
post '/projects/ecookbook/text_blocks', params: { text_block: { name: 'test2', text: 'lorem ipsum2'}}
60+
end
61+
assert_redirected_to '/projects/ecookbook/settings/text_blocks'
62+
63+
follow_redirect!
64+
65+
assert b = TextBlock.find_by_name('test2')
66+
assert_equal 'lorem ipsum2', b.text
67+
assert_equal 2, b.position
68+
5769
assert_difference 'TextBlock.count', -1 do
5870
delete "/projects/ecookbook/text_blocks/#{b.id}"
5971
end

0 commit comments

Comments
 (0)