From 31850c6ca118b7828dbaa3ad1a87dab4287718f5 Mon Sep 17 00:00:00 2001 From: hellekin Date: Thu, 8 Oct 2020 22:10:41 +0200 Subject: Add categories, sections, and import data --- ...4053_add_dewey_id_to_categories_and_sections.rb | 8 ++++++++ ...08190558_create_join_table_resource_sections.rb | 8 ++++++++ db/schema.rb | 11 ++++++++++- db/seeds.rb | 22 ++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20201008184053_add_dewey_id_to_categories_and_sections.rb create mode 100644 db/migrate/20201008190558_create_join_table_resource_sections.rb (limited to 'db') diff --git a/db/migrate/20201008184053_add_dewey_id_to_categories_and_sections.rb b/db/migrate/20201008184053_add_dewey_id_to_categories_and_sections.rb new file mode 100644 index 0000000..d876b47 --- /dev/null +++ b/db/migrate/20201008184053_add_dewey_id_to_categories_and_sections.rb @@ -0,0 +1,8 @@ +class AddDeweyIdToCategoriesAndSections < ActiveRecord::Migration[6.0] + def change + add_column :categories, :dewey_id, :integer + add_column :sections, :dewey_id, :integer + add_index :categories, :dewey_id, unique: true + add_index :sections, :dewey_id, unique: true + end +end diff --git a/db/migrate/20201008190558_create_join_table_resource_sections.rb b/db/migrate/20201008190558_create_join_table_resource_sections.rb new file mode 100644 index 0000000..d01c27f --- /dev/null +++ b/db/migrate/20201008190558_create_join_table_resource_sections.rb @@ -0,0 +1,8 @@ +class CreateJoinTableResourceSections < ActiveRecord::Migration[6.0] + def change + create_join_table :resources, :sections do |t| + # t.index [:resource_id, :section_id] + # t.index [:section_id, :resource_id] + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5a8bba7..16ba65c 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: 2020_10_08_133300) do +ActiveRecord::Schema.define(version: 2020_10_08_190558) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -45,6 +45,8 @@ ActiveRecord::Schema.define(version: 2020_10_08_133300) do t.integer "sections_count", default: 0 t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.integer "dewey_id" + t.index ["dewey_id"], name: "index_categories_on_dewey_id", unique: true t.index ["taxonomy_id"], name: "index_categories_on_taxonomy_id" end @@ -58,6 +60,11 @@ ActiveRecord::Schema.define(version: 2020_10_08_133300) do t.index ["uuid"], name: "index_resources_on_uuid", unique: true end + create_table "resources_sections", id: false, force: :cascade do |t| + t.bigint "resource_id", null: false + t.bigint "section_id", null: false + end + create_table "sections", force: :cascade do |t| t.string "name", limit: 64 t.string "summary", limit: 136 @@ -67,7 +74,9 @@ ActiveRecord::Schema.define(version: 2020_10_08_133300) do t.integer "rank", default: 0 t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.integer "dewey_id" t.index ["category_id"], name: "index_sections_on_category_id" + t.index ["dewey_id"], name: "index_sections_on_dewey_id", unique: true end create_table "taxonomies", force: :cascade do |t| diff --git a/db/seeds.rb b/db/seeds.rb index 8a4c670..ea9fc25 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -26,3 +26,25 @@ Taxonomy.find_or_create_by( uuid: '2519915f-d19c-4281-b758-f5ddb889d7fa', agent_id: dewey.id ) + +# Create French categories and sections +if Category.count == 0 + dewey_taxo = Taxonomy.first + cats = JSON.parse(IO.read('doc/import/categories-fr.json')) + cats.each do |cat| + c = Category.create(name: cat['name'], dewey_id: cat['id'], taxonomy_id: dewey_taxo.id, color: cat['color']) + cat['sections'].each do |sec| + Section.create(name: sec['name'], category_id: c.id, dewey_id: sec['id'], color: sec['color']) + end + end + # Now that we have all we need, update resources_sections... + Resource.all.each do |res| + sec_ids = res.feature['properties']['categories'] + next if sec_ids.empty? + sec_ids.each do |id| + s = Section.find_by(dewey_id: id) + res.sections << s if s.present? + res.save + end + end +end -- cgit v1.2.3