Merge pull request #1844 from consul/polls-persist-officer-data
Persist a shift's officer data
This commit is contained in:
@@ -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
|
||||
@@ -11,7 +11,7 @@
|
||||
<% @shifts.each do |shift| %>
|
||||
<tr id="shift_<%= shift.id %>" class="shift">
|
||||
<td><%= l(shift.date.to_date, format: :long) %></td>
|
||||
<td><%= shift.officer.name %></td>
|
||||
<td><%= shift.officer_name %></td>
|
||||
<td class="text-right">
|
||||
<%= link_to t("admin.poll_shifts.new.remove_shift"),
|
||||
admin_booth_shift_path(@booth, shift),
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user