Skip to content

Commit

Permalink
Add content method test in model structure_item and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyxiao committed May 27, 2016
1 parent 37f4f2f commit bb8f8f3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
3 changes: 1 addition & 2 deletions app/models/spina/structure_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class StructureItem < ActiveRecord::Base
accepts_nested_attributes_for :structure_parts, allow_destroy: true

def content(structure_part)
structure_part = structure_parts.where(name: structure_part).first
structure_part.try(:content)
structure_parts.find_by(name: structure_part).try(:content)
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures/spina/lines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
line_1:
content: line_content_1
5 changes: 5 additions & 0 deletions test/fixtures/spina/structure_items.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
structure_item_1:
structure_id: <%= ActiveRecord::FixtureSet.identify(:structure_1) %>

structure_item_2:
structure_id: <%= ActiveRecord::FixtureSet.identify(:structure_2) %>
5 changes: 5 additions & 0 deletions test/fixtures/spina/structure_parts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
structure_part_1:
structure_item_id: <%= ActiveRecord::FixtureSet.identify(:structure_item_1) %>
structure_partable_id: <%= ActiveRecord::FixtureSet.identify(:line_1) %>
structure_partable_type: Spina::Line
name: name_1
5 changes: 5 additions & 0 deletions test/fixtures/spina/structures.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
structure_1:
id: 1

structure_2:
id: 2
24 changes: 24 additions & 0 deletions test/models/spina/structure_item_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'test_helper'

module Spina
class StructureItemTest < ActiveSupport::TestCase

def setup
@structure_1 = spina_structures(:structure_1)

@structure_item_1 = spina_structure_items(:structure_item_1)
@structure_item_2 = spina_structure_items(:structure_item_2)

@structure_part_1 = spina_structure_parts(:structure_part_1)
end

test 'structure_item_1 content' do
assert_equal 'line_content_1', @structure_item_1.content(@structure_part_1.name)
end

test 'structure_item_2 content' do
assert_equal nil, @structure_item_2.content(@structure_part_1.name)
end

end
end

0 comments on commit bb8f8f3

Please sign in to comment.