Skip to content

Commit

Permalink
Merge branch 'master' of https://github.umn.edu/hst/study_finder_umn
Browse files Browse the repository at this point in the history
…into 2.1

# Conflicts:
#	db/schema.rb
  • Loading branch information
machinehum committed Oct 26, 2023
2 parents 87d3602 + d8e4466 commit 65c7ec6
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 6 deletions.
7 changes: 5 additions & 2 deletions app/controllers/api/studies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Api::StudiesController < ApiController
def index
@trials = Trial.includes(:trial_keywords, :conditions, :trial_interventions, :locations).all
@trials = Trial.includes(:trial_keywords, :conditions, :subgroups, :trial_interventions, :locations).all
end

def visible
Expand All @@ -9,7 +9,7 @@ def visible
end

def show
@trial = Trial.includes(:trial_keywords, :conditions, :trial_interventions, :locations).find_by(system_id: params[:id])
@trial = Trial.includes(:trial_keywords, :conditions, :subgroups, :trial_interventions, :locations).find_by(system_id: params[:id])
end

def update
Expand All @@ -19,6 +19,7 @@ def update
@trial.update_interventions!(params[:interventions].to_a)
@trial.update_keywords!(params[:keywords])
@trial.update_conditions!(params[:conditions])
@trial.update_subgroups!(params[:subgroups])
@trial.update_locations!(params[:locations])
@trial.update!(trial_params)
end
Expand All @@ -35,6 +36,7 @@ def create
@trial.transaction do
@trial.update_keywords!(params[:keywords])
@trial.update_conditions!(params[:conditions])
@trial.update_subgroups!(params[:subgroups])
@trial.update_locations!(params[:locations])
@trial.update_interventions!(params[:interventions])
end
Expand Down Expand Up @@ -93,6 +95,7 @@ def api_params
:protocol_type,
:recruiting,
:simple_description,
:subgroups,
:display_simple_description,
:system_id,
:verification_date,
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/studies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def index

@attribute_settings = SystemInfo.trial_attribute_settings
@group = Group.where(id: search_hash[:category]).first
@subgroup = Subgroup.where(id: search_hash[:subcat]).first
@trials = Trial.execute_search(search_hash).page(search_params[:page]).results
@search_query = search_hash[:q].try(:downcase) || ""
@search_category = search_hash[:category].to_i
Expand Down Expand Up @@ -96,6 +97,6 @@ def email_me
private

def search_params
params.permit(:page, search: [:category, :q, :healthy_volunteers, :gender, :children, :adults])
params.permit(:page, search: [:category, :subcat, :q, :healthy_volunteers, :gender, :children, :adults])
end
end
38 changes: 36 additions & 2 deletions app/models/trial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Trial < ApplicationRecord
has_many :conditions, through: :trial_conditions
has_many :condition_groups, through: :trial_conditions

has_many :trial_subgroups
has_many :subgroups, through: :trial_subgroups

has_many :trial_sites
has_many :sites, through: :trial_sites

Expand Down Expand Up @@ -102,6 +105,10 @@ def category_ids
condition_groups.map { |e| e.group_id }.uniq
end

def subcategory_ids
trial_subgroups.map { |e| e.subgroup_id }
end

def keyword_suggest
{
input: trial_keywords.where.not(keyword: nil).map { |k| k.keyword.try(:downcase) }
Expand All @@ -128,6 +135,28 @@ def condition_values
conditions.map(&:condition).sort
end

def subgroup_values
subgroups.map(&:name).sort
end

def update_subgroups!(subgroups)
return if subgroups.nil?

existing_subgroups = subgroup_values
subgroups_to_add = subgroups - existing_subgroups
subgroups_to_delete = existing_subgroups - subgroups

transaction(requires_new: true) do
trial_subgroups.includes(:subgroup).where(Subgroup.table_name => { name: subgroups_to_delete }).delete_all

subgroups_to_add.each do |subgroup|
trial_subgroups.create(subgroup: Subgroup.find_or_initialize_by(name: subgroup, group_id: category_ids))
end
end

__elasticsearch__.update_document
end

def update_keywords!(keywords)
return if keywords.nil?

Expand Down Expand Up @@ -284,6 +313,7 @@ def update_interventions!(intervention_data)
indexes :pi_id

indexes :category_ids
indexes :subcategory_ids
indexes :keyword_suggest, type: 'completion', analyzer: 'typeahead', search_analyzer: 'typeahead'


Expand Down Expand Up @@ -393,7 +423,7 @@ def as_indexed_json(options={})
]
}
},
methods: [:display_title, :min_age, :max_age, :interventions, :conditions_map, :category_ids, :keywords, :keyword_suggest]
methods: [:display_title, :min_age, :max_age, :interventions, :conditions_map, :category_ids, :subcategory_ids, :keywords, :keyword_suggest]
)
end

Expand Down Expand Up @@ -526,7 +556,7 @@ def self.filters(search)
ret = []
ret << { term: { visible: true } }
ret << { term: { approved: true } }
if (search.has_key?('healthy_volunteers') and search[:healthy_volunteers] == "1") or search.has_key?('category') or search.has_key?('gender')
if (search.has_key?('healthy_volunteers') and search[:healthy_volunteers] == "1") or search.has_key?('category') or search.has_key?('subcat') or search.has_key?('gender')
if search.has_key?('healthy_volunteers') and search[:healthy_volunteers] == "1"
ret << { term: { healthy_volunteers: true } }
end
Expand All @@ -535,6 +565,10 @@ def self.filters(search)
ret << { term: { category_ids: search[:category] } }
end

if search.has_key?('subcat')
ret << { term: { subcategory_ids: search[:subcat] } }
end

if (search.has_key?('gender')) and (search[:gender] == 'Male' or search[:gender] == 'Female')
ret << { terms: { gender: ['all', 'Both', search[:gender].try(:downcase)] }}
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/trial_subgroup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TrialSubgroup < ApplicationRecord
self.table_name = 'study_finder_trial_subgroups'

belongs_to :subgroup
belongs_to :trial
end
2 changes: 1 addition & 1 deletion app/views/studies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<% url = request.original_url.encode('utf-8', 'ascii-8bit') %>
<% p = request.query_parameters.dup %>
<% @group.subgroups.sort_by{ |l| l.name }.each do |subgroup| %>
<% p[:search][:q] = subgroup.name.try(:downcase) %>
<% p[:search][:subcat] = subgroup.id %>
<%= link_to subgroup.name, "studies?#{p.to_query}", class: 'btn btn-school btn-subgroup' %>
<% end %>
</div>
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20231024190156_create_study_finder_trial_subgroups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateStudyFinderTrialSubgroups < ActiveRecord::Migration[7.0]
def change
create_table :study_finder_trial_subgroups do |t|
t.references :subgroup, null: false, foreign_key: {to_table: :study_finder_subgroups}
t.references :trial, null: false, foreign_key: {to_table: :study_finder_trials}

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions spec/factories/trial_subgroups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :trial_subgroup do
subgroup { nil }
trial { nil }
end
end
5 changes: 5 additions & 0 deletions spec/models/trial_subgroup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe TrialSubgroup, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 65c7ec6

Please sign in to comment.