Skip to content

Commit

Permalink
item: move subitems to the same case, when main item is moved #9
Browse files Browse the repository at this point in the history
  • Loading branch information
meise authored and Daniel Meißner committed Oct 11, 2018
1 parent 9860cfc commit ef5e5e9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def edit

def update
if @item.update_attributes(item_params)
# move subitems to same case
@item.move_sub_items

redirect_to case_path(@item.case), notice: "Updated '#{@item.name}'"
else
render action: 'edit'
Expand Down
12 changes: 12 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ def destroy
def md5_sum
Digest::MD5.hexdigest(self.to_json)
end

# Move all suitems to the same case.
#
# TODO: show message in view before save
def move_sub_items
self.items.each do |sub_item|
if self.case != sub_item.case
sub_item.case = self.case
sub_item.save!
end
end
end
end
28 changes: 27 additions & 1 deletion spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
RSpec.describe Item, type: :model do

before do
@item = Item.new(name: 'my item', model: 'foobar')
@case = Case.new(name: "möp", acronym: "bla")

@item = Item.new(name: 'my item', model: 'foobar', case: @case)
@item.item_type = ItemType.new(name: 'Fach')
end

Expand Down Expand Up @@ -46,6 +48,30 @@
end
end

context "#move_sub_items" do

before do
@new_case = Case.new(name: "new_case", acronym: "nc")
@sub_item = Item.new(name: "sub_item", case: @case)

@item.items << @sub_item
@item.save
end

it "should move all subitems to the same case" do
expect(@item.case).to eq @sub_item.case
expect(@item.items.count).to eq 1

@item.case = @new_case
@item.save

expect(@item.case).not_to eq @sub_item.case

@item.move_sub_items
expect(@item.case).to eq @sub_item.case
end
end

context "#delete" do
it "should not delete item" do
item = Item.new(name: 'foobar')
Expand Down

0 comments on commit ef5e5e9

Please sign in to comment.