From bb8f8f3832c0fd95d65b470f3d51666924569aea Mon Sep 17 00:00:00 2001 From: arlyxiao Date: Sat, 28 May 2016 00:13:54 +0800 Subject: [PATCH] Add content method test in model structure_item and refactor --- app/models/spina/structure_item.rb | 3 +-- test/fixtures/spina/lines.yml | 2 ++ test/fixtures/spina/structure_items.yml | 5 +++++ test/fixtures/spina/structure_parts.yml | 5 +++++ test/fixtures/spina/structures.yml | 5 +++++ test/models/spina/structure_item_test.rb | 24 ++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/spina/lines.yml create mode 100644 test/fixtures/spina/structure_items.yml create mode 100644 test/fixtures/spina/structure_parts.yml create mode 100644 test/fixtures/spina/structures.yml create mode 100644 test/models/spina/structure_item_test.rb diff --git a/app/models/spina/structure_item.rb b/app/models/spina/structure_item.rb index 1ec3a76cd..e5ef5d847 100644 --- a/app/models/spina/structure_item.rb +++ b/app/models/spina/structure_item.rb @@ -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 \ No newline at end of file diff --git a/test/fixtures/spina/lines.yml b/test/fixtures/spina/lines.yml new file mode 100644 index 000000000..820319a8c --- /dev/null +++ b/test/fixtures/spina/lines.yml @@ -0,0 +1,2 @@ +line_1: + content: line_content_1 \ No newline at end of file diff --git a/test/fixtures/spina/structure_items.yml b/test/fixtures/spina/structure_items.yml new file mode 100644 index 000000000..03c294061 --- /dev/null +++ b/test/fixtures/spina/structure_items.yml @@ -0,0 +1,5 @@ +structure_item_1: + structure_id: <%= ActiveRecord::FixtureSet.identify(:structure_1) %> + +structure_item_2: + structure_id: <%= ActiveRecord::FixtureSet.identify(:structure_2) %> diff --git a/test/fixtures/spina/structure_parts.yml b/test/fixtures/spina/structure_parts.yml new file mode 100644 index 000000000..a6b95f548 --- /dev/null +++ b/test/fixtures/spina/structure_parts.yml @@ -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 \ No newline at end of file diff --git a/test/fixtures/spina/structures.yml b/test/fixtures/spina/structures.yml new file mode 100644 index 000000000..569e2b937 --- /dev/null +++ b/test/fixtures/spina/structures.yml @@ -0,0 +1,5 @@ +structure_1: + id: 1 + +structure_2: + id: 2 diff --git a/test/models/spina/structure_item_test.rb b/test/models/spina/structure_item_test.rb new file mode 100644 index 000000000..aab9d8cfb --- /dev/null +++ b/test/models/spina/structure_item_test.rb @@ -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