update former type column on changes to relation_type

pull/6104/head
Jens Ulferts 7 years ago
parent 0aa2f86f99
commit 10a6e736f6
No known key found for this signature in database
GPG Key ID: 3CAA4B1182CF5308
  1. 22
      app/models/relation.rb
  2. 2
      spec/features/work_packages/details/details_relations_spec.rb
  3. 13
      spec/models/relation_spec.rb

@ -177,6 +177,14 @@ class Relation < ActiveRecord::Base
changed.include?('relation_type')
end
def relation_type_was
if changes['relation_type']
changes['relation_type'].first
else
relation_type
end
end
def relation_type
if @relation_type.present?
@relation_type
@ -247,6 +255,8 @@ class Relation < ActiveRecord::Base
end
end
private
def shared_hierarchy?
to_from = hierarchy_but_not_self(to: to, from: from)
from_to = hierarchy_but_not_self(to: from, from: to)
@ -256,8 +266,6 @@ class Relation < ActiveRecord::Base
.any?
end
private
def validate_sanity_of_relation
return unless from && to
@ -268,11 +276,15 @@ class Relation < ActiveRecord::Base
end
def set_type_column
return unless relation_type
if relation_type_changed? && relation_type_was
was_column = self.class.relation_column(relation_type_was)
send("#{was_column}=", 0)
end
column = self.class.relation_column(relation_type)
return unless relation_type
new_column = self.class.relation_column(relation_type)
send("#{column}=", 1)
send("#{new_column}=", 1)
end
# Reverses the relation if needed so that it gets stored in the proper way

@ -147,7 +147,7 @@ describe 'Work package relations tab', js: true, selenium: true do
expect(page).to have_selector('.relation-row--type', text: 'Related To')
updated_relation = Relation.find(relation_1.id)
expect(updated_relation.relation_type).to eq(:blocks) # ! Reports mixed
expect(updated_relation.relation_type).to eq('blocks')
end
end

@ -120,19 +120,20 @@ describe Relation, type: :model do
end
describe '#relation_type=' do
let(:column_name) { :relates }
let(:column_name) { 'relates' }
let(:type) { :relates }
let(:relation) do
FactoryGirl.build_stubbed(:relation,
relation_type: :relates,
relates: 1)
FactoryGirl.build(:relation,
relation_type: 'relates')
end
it 'updates the column value' do
relation.save!
expect(relation.relates).to eq 1
relation.relation_type = :duplicates
expect(relation.relation_type).to eq(:duplicates)
relation.relation_type = 'duplicates'
relation.save!
expect(relation.relation_type).to eq('duplicates')
expect(relation.relates).to eq 0
expect(relation.duplicates).to eq 1

Loading…
Cancel
Save