Skip to content

Commit

Permalink
Add address and registered provider fields to dwellings
Browse files Browse the repository at this point in the history
These are the fields that developers will fill in once a development is completed.

We don't want to generate an audit log when a developer does this, so exclude those from the auditing, which then also requires us to do the same monkey patch as we did on Development to stop it generating a validation error when they are changed without an audit comment.
  • Loading branch information
james committed Oct 31, 2019
1 parent 36da387 commit b5c6475
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/models/dwelling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Dwelling < ApplicationRecord
associated_with: :development,
if: :audit_changes?,
on: %i[create update destroy],
comment_required: true
comment_required: true,
except: %i[address registered_provider_id]
)

TENURES = %w[open social intermediate].freeze
Expand All @@ -16,4 +17,16 @@ class Dwelling < ApplicationRecord
validates :bedrooms, presence: true

delegate :audit_changes?, to: :development

private

def comment_required_state?
# Monkey patch explanation:
# The Audited gem doesn't create a changelog if there are no audited_changes
# eg in the situation where we're only changing `address`, and address is
# ignored by audited. So in this situation, we shouldn't require a comment
# either.
# TODO: Make this a PR on the audited gem itself
super && audited_changes.present?
end
end
6 changes: 6 additions & 0 deletions db/migrate/20191031132036_add_address_and_rp_to_dwellings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAddressAndRpToDwellings < ActiveRecord::Migration[6.0]
def change
add_column :dwellings, :address, :string
add_reference :dwellings, :registered_provider, foreign_key: true
end
end
5 changes: 5 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@
t.bigint "development_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "address"
t.bigint "registered_provider_id"
t.index ["development_id"], name: "index_dwellings_on_development_id"
t.index ["registered_provider_id"], name: "index_dwellings_on_registered_provider_id"
end

create_table "registered_providers", force: :cascade do |t|
t.string "name"
Expand All @@ -71,4 +75,5 @@
end

add_foreign_key "dwellings", "developments"
add_foreign_key "dwellings", "registered_providers"
end

0 comments on commit b5c6475

Please sign in to comment.