Skip to content

Commit

Permalink
Merge pull request zendesk#644 from zendesk/grosser/fix
Browse files Browse the repository at this point in the history
fix duplicate id issues when copying flowdock or slack
  • Loading branch information
grosser committed Nov 18, 2015
2 parents 9bbd07f + 0157810 commit fc3d37c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/models/stage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def self.reorder(new_order)
def self.build_clone(old_stage)
new(old_stage.attributes.except("id")).tap do |new_stage|
Samson::Hooks.fire(:stage_clone, old_stage, new_stage)
new_stage.new_relic_applications.build(old_stage.new_relic_applications.map { |app| app.attributes.except("id") })
new_stage.new_relic_applications.build(old_stage.new_relic_applications.map { |app| app.attributes.except("id", "updated_at", "created_at") })
new_stage.command_ids = old_stage.command_ids
end
end
Expand Down
2 changes: 1 addition & 1 deletion plugins/flowdock/lib/samson_flowdock/samson_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Engine < Rails::Engine
Samson::Hooks.view :deploy_view, 'samson_flowdock/notify_buddy_box'

Samson::Hooks.callback :stage_clone do |old_stage, new_stage|
new_stage.flowdock_flows.build(old_stage.flowdock_flows.map(&:attributes))
new_stage.flowdock_flows.build(old_stage.flowdock_flows.map { |f| f.attributes.except("id", "created_at", "updated_at") })
end

Samson::Hooks.callback :stage_permitted_params do
Expand Down
9 changes: 9 additions & 0 deletions plugins/flowdock/test/models/hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@
Samson::Hooks.fire(:after_deploy, deploy, nil)
end
end

describe :stage_clone do
it "copies all attributes except id" do
stage.flowdock_flows << FlowdockFlow.new(name: "test", token: "abcxyz", enabled: false)
new_stage = Stage.new
Samson::Hooks.fire(:stage_clone, stage, new_stage)
new_stage.flowdock_flows.map(&:attributes).must_equal [{"id"=>nil, "name"=>"test", "token"=>"abcxyz", "stage_id"=>nil, "created_at"=>nil, "updated_at"=>nil, "enabled"=>false}]
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ def change
end

add_index :slack_channels, :stage_id

end
end
2 changes: 1 addition & 1 deletion plugins/slack/lib/samson_slack/samson_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Engine < Rails::Engine
Samson::Hooks.view :stage_form, "samson_slack/fields"

Samson::Hooks.callback :stage_clone do |old_stage, new_stage|
new_stage.slack_channels.build(old_stage.slack_channels.map(&:attributes))
new_stage.slack_channels.build(old_stage.slack_channels.map { |s| s.attributes.except("id", "created_at", "updated_at") })
end

Samson::Hooks.callback :stage_permitted_params do
Expand Down
9 changes: 9 additions & 0 deletions plugins/slack/test/models/hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@
Samson::Hooks.fire(:after_deploy, deploy, nil)
end
end

describe :stage_clone do
it "copies all attributes except id" do
stage.slack_channels << SlackChannel.new(channel_id: 1, name: 'channel1')
new_stage = Stage.new
Samson::Hooks.fire(:stage_clone, stage, new_stage)
new_stage.slack_channels.map(&:attributes).must_equal [{"id"=>nil, "name"=>"channel1", "channel_id"=>"1", "stage_id"=>nil, "created_at"=>nil, "updated_at"=>nil}]
end
end
end
5 changes: 0 additions & 5 deletions test/models/stage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,6 @@
@clone.id.wont_equal subject.id
end

it "copies over the flowdock flows" do
assert_equal @clone.flowdock_flows.map { |f| f.attributes.except("stage_id") },
subject.flowdock_flows.map { |f| f.attributes.except("stage_id") }
end

it "copies over the new relic applications" do
assert_equal @clone.new_relic_applications.map { |n| n.attributes.except("stage_id", "id") },
subject.new_relic_applications.map { |n| n.attributes.except("stage_id", "id") }
Expand Down

0 comments on commit fc3d37c

Please sign in to comment.