Skip to content

Commit eb38c4c

Browse files
committed
Add versioning fields to editable join tables
1 parent b193eb4 commit eb38c4c

8 files changed

+35
-5
lines changed

app/models/measure_category.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class MeasureCategory < ApplicationRecord
1+
class MeasureCategory < VersionedRecord
22
belongs_to :measure
33
belongs_to :category
44
accepts_nested_attributes_for :measure

app/serializers/measure_category_serializer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class MeasureCategorySerializer
2-
include FastApplicationSerializer
2+
include FastVersionedSerializer
33

44
attributes :measure_id, :category_id
55

app/serializers/user_role_serializer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class UserRoleSerializer
2-
include FastApplicationSerializer
2+
include FastVersionedSerializer
33

44
attributes :user_id, :role_id
55

db/migrate/20180528083426_add_last_modified_user_id_to_versioned_records.rb

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def down
2929
Page,
3030
ProgressReport,
3131
Recommendation,
32+
Sdgtarget,
3233
Taxonomy,
3334
User,
3435
UserRole
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddVersionedFieldsToJoins < ActiveRecord::Migration[6.1]
2+
def change
3+
change_table(:measure_categories) do |t|
4+
t.belongs_to :updated_by, foreign_key: {to_table: "users"}, index: true, null: true
5+
end
6+
end
7+
end

db/schema.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@
130130
t.text "indicator_summary"
131131
t.text "target_date_comment"
132132
t.integer "updated_by_id"
133-
t.string "reference"
134133
t.datetime "relationship_updated_at", precision: 6
135134
t.bigint "relationship_updated_by_id"
135+
t.string "reference"
136136
t.index ["draft"], name: "index_measures_on_draft"
137137
end
138138

@@ -324,7 +324,7 @@
324324
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
325325
end
326326

327-
create_table "versions", id: :serial, force: :cascade do |t|
327+
create_table "versions", force: :cascade do |t|
328328
t.string "item_type", null: false
329329
t.bigint "item_id", null: false
330330
t.string "event", null: false

spec/controllers/measure_categories_controller_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@
7070
post :create, format: :json, params: {measure_category: {description: "desc only", taxonomy_id: 999}}
7171
expect(response).to have_http_status(422)
7272
end
73+
74+
it "will record what manager created the measure category", versioning: true do
75+
expect(PaperTrail).to be_enabled
76+
sign_in user
77+
json = JSON.parse(subject.body)
78+
expect(json.dig("data", "attributes", "updated_by_id").to_i).to eq user.id
79+
end
7380
end
7481
end
7582

spec/controllers/user_roles_controller_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@
254254
post :create, format: :json, params: {user_role: {description: "desc only", taxonomy_id: 999}}
255255
expect(response).to have_http_status(422)
256256
end
257+
258+
it "will record what admin created the user role", versioning: true do
259+
expect(PaperTrail).to be_enabled
260+
sign_in admin
261+
subject = post :create,
262+
format: :json,
263+
params: {
264+
user_role: {
265+
user_id: manager.id,
266+
role_id: admin_role.id
267+
}
268+
}
269+
json = JSON.parse(subject.body)
270+
expect(json.dig("data", "attributes", "updated_by_id").to_i).to eq admin.id
271+
end
257272
end
258273
end
259274

0 commit comments

Comments
 (0)