Skip to content

Commit

Permalink
Merge 7348ae2 into d3fe0e0
Browse files Browse the repository at this point in the history
  • Loading branch information
simonreed committed Oct 8, 2019
2 parents d3fe0e0 + 7348ae2 commit fc1434f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 19 deletions.
6 changes: 6 additions & 0 deletions app/controllers/cc_conditions_controller.rb
Expand Up @@ -11,4 +11,10 @@ class CcConditionsController < ConstructController

# List of params that can be set and edited
@params_list = [:id, :literal, :logic]

private

def collection
@instrument.cc_conditions
end
end
2 changes: 1 addition & 1 deletion app/controllers/cc_questions_controller.rb
Expand Up @@ -58,6 +58,6 @@ def remove_variable

private
def collection
@instrument.cc_questions.includes(:response_unit, :question, :topic)
@instrument.cc_questions.includes(:response_unit, :question, :topic, :variable_topics, link: :topic)
end
end
4 changes: 4 additions & 0 deletions app/controllers/code_lists_controller.rb
Expand Up @@ -89,4 +89,8 @@ def safe_params
super
end

def collection
@instrument.code_lists.includes(:codes, :qgrids_via_h, :qgrids_via_v, response_domain_code: [:question_items, :question_grids])
end

end
6 changes: 5 additions & 1 deletion app/controllers/question_controller.rb
Expand Up @@ -38,4 +38,8 @@ def update_question(object, &block)
render json: @object.errors, status: :unprocessable_entity
end
end
end

def collection
@instrument.send(self.class.model_class.name.tableize).includes(rds_qs: :response_domain)
end
end
14 changes: 13 additions & 1 deletion app/controllers/question_items_controller.rb
Expand Up @@ -8,4 +8,16 @@ class QuestionItemsController < QuestionController

# List of params that can be set and edited
@params_list = [:literal, :label, :instruction_id]
end

def index
@response_domain_codes = @instrument.response_domain_codes.includes(:codes)
@response_domain_datetimes = @instrument.response_domain_datetimes
@response_domain_numerics = @instrument.response_domain_numerics
@response_domain_texts = @instrument.response_domain_texts
super
end

def collection
@instrument.question_items.includes(:rds_qs)
end
end
4 changes: 3 additions & 1 deletion app/models/question_item.rb
Expand Up @@ -15,11 +15,13 @@ class QuestionItem < ApplicationRecord
# XML tag name
TYPE = 'QuestionItem'

has_many :rds_qs, -> { order(rd_order: :desc) }, class_name: 'RdsQs', as: :question, dependent: :destroy

# Returns all response domains in order
#
# @return [Array] All response domains
def response_domains
self.rds_qs.order(:rd_order).includes(:response_domain).map &:response_domain
rds_qs.order(:rd_order).includes(:response_domain).map &:response_domain
end

# Exports as an XML fragment
Expand Down
17 changes: 3 additions & 14 deletions app/models/response_domain_code.rb
Expand Up @@ -15,28 +15,17 @@ class ResponseDomainCode < ApplicationRecord
# All ResponseDomainCodes must belong to a {CodeList}
belongs_to :code_list

# ResponseDomainCodes can have many {Code}s through {CodeList}
has_many :codes, through: :code_list

# Before creating a ResponseDomainCode in the database ensure the instrument has been set
before_create :set_instrument

# RDCs do not have their own label, so it is delagated to the {CodeList} it belongs to
delegate :label, to: :code_list


validates :min_responses, :max_responses, presence: true, numericality: { only_integer: true, allow_blank: true }

# Returns basic information on the response domain's codes
#
# @return [Array] List of codes as hashes
def codes
self.code_list.codes.includes(:code_list).map do |x|
{
label: x.category.label,
value: x.value,
order: x.order
}
end
end

private # private methods
# Sets instrument_id from the {CodeList} that this ResponseDomainCode belongs to
def set_instrument
Expand Down
12 changes: 11 additions & 1 deletion app/views/question_items/index.json.jbuilder
Expand Up @@ -2,8 +2,18 @@ json.array!(@collection) do |question_item|
json.extract! question_item, :id, :label, :literal
json.type question_item.class.name
json.instruction question_item.instruction.nil? ? '' : question_item.instruction.text
json.rds question_item.response_domains do |rd|
json.rds question_item.rds_qs.each do |rds_qs|
begin
rd = case rds_qs.response_domain_type
when "ResponseDomainCode"
@response_domain_codes.find{|rdc| rdc.id == rds_qs.response_domain_id}
when "ResponseDomainDatetime"
@response_domain_datetimes.find{|rdc| rdc.id == rds_qs.response_domain_id}
when "ResponseDomainNumeric"
@response_domain_numerics.find{|rdc| rdc.id == rds_qs.response_domain_id}
when "ResponseDomainText"
@response_domain_texts.find{|rdc| rdc.id == rds_qs.response_domain_id}
end
json.id rd.id
json.type rd.class.name
json.label rd.label
Expand Down

0 comments on commit fc1434f

Please sign in to comment.