diff --git a/app/models/poll/shift.rb b/app/models/poll/shift.rb
index 8ee646ea4..cc6a43425 100644
--- a/app/models/poll/shift.rb
+++ b/app/models/poll/shift.rb
@@ -8,6 +8,7 @@ class Poll
validates :date, presence: true
validates :date, uniqueness: { scope: [:officer_id, :booth_id] }
+ before_create :persist_data
after_create :create_officer_assignments
def create_officer_assignments
@@ -18,5 +19,11 @@ class Poll
Poll::OfficerAssignment.create!(attrs)
end
end
+
+ def persist_data
+ self.officer_name = officer.name
+ self.officer_email = officer.email
+ end
+
end
end
\ No newline at end of file
diff --git a/app/views/admin/poll/shifts/_shifts.html.erb b/app/views/admin/poll/shifts/_shifts.html.erb
index 4c10f9ab5..f2de53548 100644
--- a/app/views/admin/poll/shifts/_shifts.html.erb
+++ b/app/views/admin/poll/shifts/_shifts.html.erb
@@ -11,7 +11,7 @@
<% @shifts.each do |shift| %>
| <%= l(shift.date.to_date, format: :long) %> |
- <%= shift.officer.name %> |
+ <%= shift.officer_name %> |
<%= link_to t("admin.poll_shifts.new.remove_shift"),
admin_booth_shift_path(@booth, shift),
diff --git a/db/migrate/20170908175149_add_officer_data_to_poll_shifts.rb b/db/migrate/20170908175149_add_officer_data_to_poll_shifts.rb
new file mode 100644
index 000000000..0982345b4
--- /dev/null
+++ b/db/migrate/20170908175149_add_officer_data_to_poll_shifts.rb
@@ -0,0 +1,6 @@
+class AddOfficerDataToPollShifts < ActiveRecord::Migration
+ def change
+ add_column :poll_shifts, :officer_name, :string
+ add_column :poll_shifts, :officer_email, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 9b4ffd97b..ba856107c 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170905111444) do
+ActiveRecord::Schema.define(version: 20170908175149) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -683,6 +683,8 @@ ActiveRecord::Schema.define(version: 20170905111444) do
t.date "date"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "officer_name"
+ t.string "officer_email"
end
add_index "poll_shifts", ["booth_id", "officer_id"], name: "index_poll_shifts_on_booth_id_and_officer_id", using: :btree
diff --git a/spec/features/admin/poll/shifts_spec.rb b/spec/features/admin/poll/shifts_spec.rb
index b55e40c7c..761a79536 100644
--- a/spec/features/admin/poll/shifts_spec.rb
+++ b/spec/features/admin/poll/shifts_spec.rb
@@ -98,6 +98,20 @@ feature 'Admin shifts' do
expect(page).to have_css(".shift", count: 0)
end
+ scenario "Destroy an officer" do
+ poll = create(:poll)
+ booth = create(:poll_booth)
+ officer = create(:poll_officer)
+
+ shift = create(:poll_shift, officer: officer, booth: booth)
+ officer.destroy
+
+ visit new_admin_booth_shift_path(booth)
+
+ expect(page).to have_css(".shift", count: 1)
+ expect(page).to have_content(officer.name)
+ end
+
scenario "Empty" do
poll = create(:poll)
booth = create(:poll_booth)
diff --git a/spec/models/poll/shift_spec.rb b/spec/models/poll/shift_spec.rb
index e70b5f9a0..409e38f93 100644
--- a/spec/models/poll/shift_spec.rb
+++ b/spec/models/poll/shift_spec.rb
@@ -57,5 +57,27 @@ describe :shift do
end
end
+
+ describe "#persist_data" do
+
+ let(:user) { create(:user, username: "Ana", email: "ana@example.com") }
+ let(:officer) { create(:poll_officer, user: user) }
+ let(:shift) { create(:poll_shift, officer: officer) }
+
+ it "should maintain officer data after destroying associated user" do
+ shift.officer.user.destroy
+
+ expect(shift.officer_name).to eq "Ana"
+ expect(shift.officer_email).to eq "ana@example.com"
+ end
+
+ it "should maintain officer data after destroying officer role" do
+ shift.officer.destroy
+
+ expect(shift.officer_name).to eq "Ana"
+ expect(shift.officer_email).to eq "ana@example.com"
+ end
+
+ end
end
|