Skip to content

Commit

Permalink
ensures position is created on a new structure item
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasBush committed Jul 19, 2016
1 parent 8b4b0e1 commit cd54c68
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/models/spina/structure_item.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
module Spina
class StructureItem < ApplicationRecord
before_validation :ensure_position
belongs_to :structure
has_many :structure_parts, dependent: :destroy

scope :sorted_by_structure, -> { order('position') }

validates_presence_of :position
accepts_nested_attributes_for :structure_parts, allow_destroy: true

def content(structure_part)
structure_parts.find_by(name: structure_part).try(:content)
end

def ensure_position
self.position ||= Time.now.to_i
end
end
end
end
5 changes: 5 additions & 0 deletions test/models/spina/structure_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ def setup
assert_equal nil, @structure_item_2.content(@structure_part_1.name)
end

test 'structure_item_1 position' do
assert_equal nil, @structure_item_1.position
@structure_item_1.save!
refute_nil @structure_item_1.position
end
end
end

0 comments on commit cd54c68

Please sign in to comment.