Skip to content

Commit

Permalink
Models for contexts, questions, and responses
Browse files Browse the repository at this point in the history
  • Loading branch information
CoralineAda committed Oct 29, 2024
1 parent 0d31bfe commit ec3b143
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 36 deletions.
10 changes: 10 additions & 0 deletions app/models/context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# A Context reflects a demographic category.
class Context < ApplicationRecord

validates_presence_of :name
validates_presence_of :display_name
validates_uniqueness_of :name

has_many :questions

end
38 changes: 3 additions & 35 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
# A Question is a representation of a survey question.
# This class provides convenience methods for navigating question keys and labels, as well as selecting topical subsets of questions.
# For now, Questions are hardcoded and not persisted.
class Question
class Question < ApplicationRecord

attr_accessor :key, :label

QUESTIONS = {
age_given: "Age",
age_exp: "Experience with Age",
klass_given: "Class",
klass_exp: "Experience with Class",
race_ethnicity_given: "Race/Ethnicity",
race_ethnicity_exp: "Experience with Race/Ethnicity",
religion_given: "Religion",
religion_exp: "Experience with Religion",
disability_given: "Disability",
disability_exp: "Experience with Disability",
neurodiversity_given: "Neurodiversity",
neurodiversity_exp: "Experience with Neurodiversity",
gender_given: "Gender",
gender_exp: "Experience with Gender",
lgbtqia_given: "LGBTQIA+ Status",
lgbtqia_exp: "Experience with LGBTQIA+",
pronouns_given: "Pronouns Given",
pronouns_exp: "Experience with Pronouns",
pronouns_feel: "Pronoun Feelings",
affinity: "Identity Affinities",
notes: "Identity Reflection"
}
has_many :responses
belongs_to :context

def self.from(key)
new(key: key, label: QUESTIONS[key.to_sym])
Expand All @@ -45,15 +22,6 @@ def self.freeform_questions
[:pronouns_feel, :affinity, :notes]
end

def initialize(attrs={})
self.key = attrs[:key]
self.label = attrs[:label]
end

def context
"#{self.key}".gsub("_given","").gsub("klass","class").gsub("_exp", "").gsub("_","-")
end

def codes_field
"#{self.key}_codes".gsub("given","id")
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Response < ApplicationRecord

belongs_to :survey_response
belongs_to :question

end
1 change: 1 addition & 0 deletions app/models/survey_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SurveyResponse < ApplicationRecord
validates_uniqueness_of :response_id

has_one :annotation
has_many :responses

# Displays the query and its explanation for locating the SurveyResponse's associated Persona in the graph.
def graph_query
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20241029003225_create_contexts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateContexts < ActiveRecord::Migration[7.2]
def change
create_table :contexts do |t|
t.string :name
t.string :display_name
t.timestamps
end
end
end
16 changes: 16 additions & 0 deletions db/migrate/20241029003455_populate_default_contexts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class PopulateDefaultContexts < ActiveRecord::Migration[7.2]
def change
Context.create(name: "age", display_name: "Age")
Context.create(name: "class", display_name: "Class")
Context.create(name: "race-ethnicity", display_name: "Race/Ethnicity")
Context.create(name: "religion", display_name: "Religion")
Context.create(name: "disability", display_name: "Disability")
Context.create(name: "neurodiversity", display_name: "Neurodiversity")
Context.create(name: "gender", display_name: "Gender")
Context.create(name: "lgbtqia", display_name: "LGBTQIA+ Status")
Context.create(name: "pronouns", display_name: "Pronouns")
Context.create(name: "pronoun_feelings", display_name: "Pronoun Feelings")
Context.create(name: "identity_affinities", display_name: "Identity Affinities")
Context.create(name: "identity_reflection", display_name: "Identity Reflection")
end
end
15 changes: 15 additions & 0 deletions db/migrate/20241029004905_create_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateQuestions < ActiveRecord::Migration[7.2]
def change
create_table :questions do |t|
t.string :key
t.string :label
t.boolean :is_experience, default: false
t.boolean :is_identity, default: false
t.boolean :is_feeling, default: false
t.boolean :is_affinity, default: false
t.boolean :is_reflection, default: false
t.timestamps
end
add_reference :questions, :context, null: true, foreign_key: true
end
end
25 changes: 25 additions & 0 deletions db/migrate/20241029010233_populate_default_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class PopulateDefaultQuestions < ActiveRecord::Migration[7.2]
def change
Question.create(key: 'age_given', label: "Age", is_identity: true, context_id: Context.find_by(name: 'age').id)
Question.create(key: 'age_exp', label: "Experience with Age", is_experience: true, context_id: Context.find_by(name: 'age').id)
Question.create(key: 'klass_given', label: "Class", is_identity: true, context_id: Context.find_by(name: 'class').id)
Question.create(key: 'klass_exp', label: "Experience with Class", is_experience: true, context_id: Context.find_by(name: 'class').id)
Question.create(key: 'race_ethnicity_given', label: "Race/Ethnicity", is_identity: true, context_id: Context.find_by(name: 'race-ethnicity').id)
Question.create(key: 'race_ethnicity_exp', label: "Experience with Race/Ethnicity", is_experience: true, context_id: Context.find_by(name: 'race-ethnicity').id)
Question.create(key: 'religion_given', label: "Religion", is_identity: true, context_id: Context.find_by(name: 'religion').id)
Question.create(key: 'religion_exp', label: "Experience with Religion", is_experience: true, context_id: Context.find_by(name: 'religion').id)
Question.create(key: 'disability_given', label: "Disability", is_identity: true, context_id: Context.find_by(name: 'disability').id)
Question.create(key: 'disability_exp', label: "Experience with Disability", is_experience: true, context_id: Context.find_by(name: 'disability').id)
Question.create(key: 'neurodiversity_given', label: "Neurodiversity", is_identity: true, context_id: Context.find_by(name: 'neurodiversity').id)
Question.create(key: 'neurodiversity_exp', label: "Experience with Neurodiversity", is_experience: true, context_id: Context.find_by(name: 'neurodiversity').id)
Question.create(key: 'gender_given', label: "Gender", is_identity: true, context_id: Context.find_by(name: 'gender').id)
Question.create(key: 'gender_exp', label: "Experience with Gender", is_experience: true, context_id: Context.find_by(name: 'gender').id)
Question.create(key: 'lgbtqia_given', label: "LGBTQIA+ Status", is_identity: true, context_id: Context.find_by(name: 'lgbtqia').id)
Question.create(key: 'lgbtqia_exp', label: "Experience with LGBTQIA+", is_experience: true, context_id: Context.find_by(name: 'lgbtqia').id)
Question.create(key: 'pronouns_given', label: "Pronouns Given", is_identity: true, context_id: Context.find_by(name: 'pronouns').id)
Question.create(key: 'pronouns_exp', label: "Experience with Pronouns", is_experience: true, context_id: Context.find_by(name: 'pronouns').id)
Question.create(key: 'pronouns_feel', label: "Pronoun Feelings", is_feeling: true, context_id: Context.find_by(name: 'pronoun_feelings').id)
Question.create(key: 'affinity', label: "Identity Affinities", is_affinity: true, context_id: Context.find_by(name: 'identity_affinities').id)
Question.create(key: 'notes', label: "Identity Reflection", is_reflection: true, context_id: Context.find_by(name: 'identity_reflection').id)
end
end
11 changes: 11 additions & 0 deletions db/migrate/20241029011624_create_responses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateResponses < ActiveRecord::Migration[7.2]
def change
create_table :responses do |t|
t.text :value
t.string :raw_codes, array: true, default: []
t.timestamps
end
add_reference :responses, :question, null: false, foreign_key: true
add_reference :responses, :survey_response, null: false, foreign_key: true
end
end
32 changes: 32 additions & 0 deletions db/migrate/20241029012413_populate_responses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class PopulateResponses < ActiveRecord::Migration[7.2]
def up
SurveyResponse.all.each do |survey_response|
Response.create(question_id: Question.find_by(key: 'age_given').id, survey_response_id: survey_response.id, value: survey_response.age_given, raw_codes: survey_response.age_id_codes)
Response.create(question_id: Question.find_by(key: 'age_exp').id, survey_response_id: survey_response.id, value: survey_response.age_exp, raw_codes: survey_response.age_exp_codes)
Response.create(question_id: Question.find_by(key: 'klass_given').id, survey_response_id: survey_response.id, value: survey_response.klass_given, raw_codes: survey_response.klass_id_codes)
Response.create(question_id: Question.find_by(key: 'klass_exp').id, survey_response_id: survey_response.id, value: survey_response.klass_exp, raw_codes: survey_response.klass_exp_codes)
Response.create(question_id: Question.find_by(key: 'race_ethnicity_given').id, survey_response_id: survey_response.id, value: survey_response.race_ethnicity_given, raw_codes: survey_response.race_ethnicity_id_codes)
Response.create(question_id: Question.find_by(key: 'race_ethnicity_exp').id, survey_response_id: survey_response.id, value: survey_response.race_ethnicity_exp, raw_codes: survey_response.race_ethnicity_exp_codes)
Response.create(question_id: Question.find_by(key: 'religion_given').id, survey_response_id: survey_response.id, value: survey_response.religion_given, raw_codes: survey_response.religion_id_codes)
Response.create(question_id: Question.find_by(key: 'religion_exp').id, survey_response_id: survey_response.id, value: survey_response.religion_exp, raw_codes: survey_response.religion_exp_codes)
Response.create(question_id: Question.find_by(key: 'disability_given').id, survey_response_id: survey_response.id, value: survey_response.disability_given, raw_codes: survey_response.disability_id_codes)
Response.create(question_id: Question.find_by(key: 'disability_exp').id, survey_response_id: survey_response.id, value: survey_response.disability_exp, raw_codes: survey_response.disability_exp_codes)
Response.create(question_id: Question.find_by(key: 'neurodiversity_given').id, survey_response_id: survey_response.id, value: survey_response.neurodiversity_given, raw_codes: survey_response.neurodiversity_id_codes)
Response.create(question_id: Question.find_by(key: 'neurodiversity_exp').id, survey_response_id: survey_response.id, value: survey_response.neurodiversity_exp, raw_codes: survey_response.neurodiversity_exp_codes)
Response.create(question_id: Question.find_by(key: 'gender_given').id, survey_response_id: survey_response.id, value: survey_response.gender_given, raw_codes: survey_response.gender_id_codes)
Response.create(question_id: Question.find_by(key: 'gender_exp').id, survey_response_id: survey_response.id, value: survey_response.gender_exp, raw_codes: survey_response.gender_exp_codes)
Response.create(question_id: Question.find_by(key: 'lgbtqia_given').id, survey_response_id: survey_response.id, value: survey_response.lgbtqia_given, raw_codes: survey_response.lgbtqia_id_codes)
Response.create(question_id: Question.find_by(key: 'lgbtqia_exp').id, survey_response_id: survey_response.id, value: survey_response.lgbtqia_exp, raw_codes: survey_response.lgbtqia_exp_codes)
Response.create(question_id: Question.find_by(key: 'pronouns_given').id, survey_response_id: survey_response.id, value: survey_response.pronouns_given, raw_codes: survey_response.pronouns_id_codes)
Response.create(question_id: Question.find_by(key: 'pronouns_exp').id, survey_response_id: survey_response.id, value: survey_response.pronouns_exp, raw_codes: survey_response.pronouns_exp_codes)
Response.create(question_id: Question.find_by(key: 'pronouns_feel').id, survey_response_id: survey_response.id, value: survey_response.pronouns_feel, raw_codes: survey_response.pronouns_feel_codes)
Response.create(question_id: Question.find_by(key: 'affinity').id, survey_response_id: survey_response.id, value: survey_response.affinity, raw_codes: survey_response.affinity_codes)
end
end

def down
Response.destroy_all
end

end

38 changes: 37 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec3b143

Please sign in to comment.