Skip to content

Commit

Permalink
Adds Tag model in the ActiveAdmin and associate it with LRIs (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmatayoshi authored Nov 23, 2020
1 parent 1b2f3cc commit 125cd92
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app/admin/lab_record_imports.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ActiveAdmin.register LabRecordImport do
actions :index, :show
actions :index, :edit, :update
permit_params :tags, tag_ids: []

includes :site

Expand All @@ -17,8 +18,12 @@
column :header_row
column :data_rows_from
column :data_rows_to
column :tags
column :error_message
column :uploaded_at
if Tag.count > 0 && current_admin_user.admin?
actions
end
end

filter :site
Expand All @@ -27,4 +32,12 @@
filter :error_message
filter :created_at
filter :uploaded_at

form title: 'Tags', :html => {:class => "tags_list"} do |f|
f.input :tags, as: :select, collection: Tag.all, multiple: true, label: false
f.actions do
f.action :submit, as: :button, label: 'Update Tags'
f.action :cancel, :wrapper_html => { :class => 'cancel'}, label: 'Cancel'
end
end
end
26 changes: 26 additions & 0 deletions app/admin/tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ActiveAdmin.register Tag do
permit_params :name

index do
id_column
column :name
column :created_at
actions
end

filter :id
filter :name
filter :created_at

controller do
def destroy
tag = Tag.find(params[:id])
if tag.destroy
flash[:notice] = "Tag '#{tag.name}' successfully deleted"
else
flash[:error] = "Couldn't delete tag '#{tag.name}'. Is it currently being used by any Lab Record Import?"
end
redirect_to action: :index
end
end
end
4 changes: 4 additions & 0 deletions app/assets/stylesheets/active_admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@
form#edit_site fieldset > ol > li.boolean label {
padding-left: 0;
}

.tags_list li {
list-style: none;
}
2 changes: 2 additions & 0 deletions app/models/lab_record_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class LabRecordImport < ApplicationRecord
has_one_attached :sheet_file
has_one_attached :rows_file
has_many :lab_records, dependent: :destroy
has_many :lab_record_imports_tags, dependent: :destroy
has_many :tags, through: :lab_record_imports_tags

# accepts_nested_attributes_for :lab_records

Expand Down
6 changes: 6 additions & 0 deletions app/models/lab_record_imports_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class LabRecordImportsTag < ApplicationRecord
belongs_to :lab_record_import
belongs_to :tag

validates :tag_id, uniqueness: { scope: [:lab_record_import_id] }
end
5 changes: 5 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Tag < ApplicationRecord
validates :name, presence: true, uniqueness: true
has_many :lab_record_imports_tags, dependent: :restrict_with_error
has_many :lab_record_imports, through: :lab_record_imports_tags
end
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@

en:
hello: "Hello world"
active_admin:
resources:
lab_record_import:
edit: "Edit tags"
10 changes: 10 additions & 0 deletions db/migrate/20201117221635_create_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateTags < ActiveRecord::Migration[5.2]
def change
create_table :tags do |t|
t.string :name, :null => false
t.timestamps

t.index [:name], unique: true
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20201117224218_create_lab_record_imports_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateLabRecordImportsTags < ActiveRecord::Migration[5.2]
def change
create_table :lab_record_imports_tags do |t|
t.references :lab_record_import, foreign_key: true, :null => false
t.references :tag, foreign_key: true, :null => false
t.timestamps

t.index [ :lab_record_import_id, :tag_id ], :unique => true, :name => 'index_on_lab_record_import_id_and_tag_id'
end
end
end
21 changes: 20 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_04_20_153254) do
ActiveRecord::Schema.define(version: 2020_11_17_224218) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -128,6 +128,16 @@
t.index ["site_id"], name: "index_lab_record_imports_on_site_id"
end

create_table "lab_record_imports_tags", force: :cascade do |t|
t.bigint "lab_record_import_id", null: false
t.bigint "tag_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["lab_record_import_id", "tag_id"], name: "index_on_lab_record_import_id_and_tag_id", unique: true
t.index ["lab_record_import_id"], name: "index_lab_record_imports_tags_on_lab_record_import_id"
t.index ["tag_id"], name: "index_lab_record_imports_tags_on_tag_id"
end

create_table "lab_records", force: :cascade do |t|
t.bigint "lab_record_import_id"
t.bigint "site_id"
Expand Down Expand Up @@ -269,6 +279,13 @@
t.datetime "updated_at", null: false
end

create_table "tags", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["name"], name: "index_tags_on_name", unique: true
end

create_table "users", force: :cascade do |t|
t.string "provider", default: "email", null: false
t.string "uid", default: "", null: false
Expand All @@ -293,6 +310,8 @@
add_foreign_key "antibiotic_consumption_stats", "sites"
add_foreign_key "electronic_pharmacy_stock_records", "sites"
add_foreign_key "lab_record_imports", "sites"
add_foreign_key "lab_record_imports_tags", "lab_record_imports"
add_foreign_key "lab_record_imports_tags", "tags"
add_foreign_key "lab_records", "lab_record_imports"
add_foreign_key "lab_records", "sites"
add_foreign_key "patient_entries", "patient_locations"
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/lab_record_imports_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryBot.define do
factory :lab_record_imports_tag do
end
end
5 changes: 5 additions & 0 deletions spec/factories/tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :tag do
name { FFaker::Tweet.tags }
end
end
3 changes: 3 additions & 0 deletions spec/models/lab_record_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
it 'obfuscates if both `patient_or_lab_record_id` and `phi` are non-empty' do
expect(create(:lab_record_import).skip_obfuscation?).to eq(false)
end

it { is_expected.to have_many(:tags).through(:lab_record_imports_tags) }
it { is_expected.to have_many(:lab_record_imports_tags).dependent(:destroy) }
end
11 changes: 11 additions & 0 deletions spec/models/lab_record_imports_tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe LabRecordImportsTag, type: :model do
subject do
l = create(:lab_record_import)
t = create(:tag)
build(:lab_record_imports_tag, lab_record_import_id: l.id, tag_id: t.id)
end

it { is_expected.to validate_uniqueness_of(:tag_id).scoped_to(:lab_record_import_id) }
end
11 changes: 11 additions & 0 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe Tag, type: :model do
subject { build :tag }

it { is_expected.to be_valid }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_uniqueness_of(:name) }
it { is_expected.to have_many(:lab_record_imports).through(:lab_record_imports_tags) }
it { is_expected.to have_many(:lab_record_imports_tags).dependent(:restrict_with_error) }
end

0 comments on commit 125cd92

Please sign in to comment.