From 24d442497a0172486c9c21d016d35adb864bd3c5 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Sat, 20 Jan 2018 09:36:01 -0500 Subject: [PATCH] data migration and rake task for nonlikert categories --- ...45356_add_non_likert_fields_to_category.rb | 8 +++ db/schema.rb | 9 ++-- lib/tasks/data.rake | 52 ++++++------------- 3 files changed, 29 insertions(+), 40 deletions(-) create mode 100644 db/migrate/20180119145356_add_non_likert_fields_to_category.rb diff --git a/db/migrate/20180119145356_add_non_likert_fields_to_category.rb b/db/migrate/20180119145356_add_non_likert_fields_to_category.rb new file mode 100644 index 00000000..6e702280 --- /dev/null +++ b/db/migrate/20180119145356_add_non_likert_fields_to_category.rb @@ -0,0 +1,8 @@ +class AddNonLikertFieldsToCategory < ActiveRecord::Migration[5.0] + def change + add_column :categories, :benchmark, :float + add_column :categories, :benchmark_description, :string + + add_column :school_categories, :nonlikert, :float + end +end diff --git a/db/schema.rb b/db/schema.rb index 15f828a0..72ddc896 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171028181758) do +ActiveRecord::Schema.define(version: 20180119145356) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -39,9 +39,11 @@ ActiveRecord::Schema.define(version: 20171028181758) do t.text "description" t.string "external_id" t.integer "parent_category_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "slug" + t.float "benchmark" + t.string "benchmark_description" t.index ["slug"], name: "index_categories_on_slug", unique: true, using: :btree end @@ -144,6 +146,7 @@ ActiveRecord::Schema.define(version: 20171028181758) do t.integer "answer_index_total", default: 0 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.float "nonlikert" t.index ["category_id"], name: "index_school_categories_on_category_id", using: :btree t.index ["school_id"], name: "index_school_categories_on_school_id", using: :btree end diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 03a121d2..ec97177c 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -17,59 +17,37 @@ namespace :data do task load_categories: :environment do measures = JSON.parse(File.read(File.expand_path('../../../data/measures.json', __FILE__))) measures.each_with_index do |measure, index| - category = Category.create( - name: measure['title'], + category = Category.create_with( blurb: measure['blurb'], description: measure['text'], external_id: measure['id'] || index + 1 - ) + ).find_or_create_by(name: measure['title']) measure['sub'].keys.sort.each do |key| subinfo = measure['sub'][key] - subcategory = category.child_categories.create( - name: subinfo['title'], + subcategory = category.child_categories.create_with( blurb: subinfo['blurb'], description: subinfo['text'], external_id: key - ) + ).find_or_create_by(name: subinfo['title']) subinfo['measures'].keys.sort.each do |subinfo_key| subsubinfo = subinfo['measures'][subinfo_key] - subsubcategory = subcategory.child_categories.create( - name: subsubinfo['title'], + subsubcategory = subcategory.child_categories.create_with( blurb: subsubinfo['blurb'], description: subsubinfo['text'], external_id: subinfo_key - ) + ).find_or_create_by(name: subsubinfo['title']) - # if subsubinfo['nonlikert'].present? - # subsubinfo['nonlikert'].each do |nonlikert_info| - # next unless nonlikert_info['likert'].present? - # nonlikert = subsubcategory.child_measures.create( - # name: nonlikert_info['title'], - # description: nonlikert_info['benchmark_explanation'], - # benchmark: nonlikert_info['benchmark'] - # ) - # - # name_map = { - # "argenziano": "dr-albert-f-argenziano-school-at-lincoln-park", - # "healey": "arthur-d-healey-school", - # "brown": "benjamin-g-brown-school", - # "east": "east-somerville-community-school", - # "kennedy": "john-f-kennedy-elementary-school", - # "somervillehigh": "somerville-high-school", - # "west": "west-somerville-neighborhood-school", - # "winter": "winter-hill-community-innovation-school" - # } - # - # nonlikert_info['likert'].each do |key, likert| - # school_name = name_map[key.to_sym] - # next if school_name.nil? - # school = School.friendly.find(school_name) - # nonlikert.measurements.create(school: school, likert: likert, nonlikert: nonlikert_info['values'][key]) - # end - # end - # end + if subsubinfo['nonlikert'].present? + subsubinfo['nonlikert'].each do |nonlikert_info| + puts("NONLIKERT FOUND: #{nonlikert_info['title']}") + nonlikert = subsubcategory.child_categories.create_with( + benchmark_description: nonlikert_info['benchmark_explanation'], + benchmark: nonlikert_info['benchmark'] + ).find_or_create_by(name: nonlikert_info['title']) + end + end end end end