From 8f691b566ffbc79f1d2d7a3f67d8607cc03a789c Mon Sep 17 00:00:00 2001 From: hellekin Date: Thu, 8 Oct 2020 16:51:30 +0200 Subject: Add model foundation for classifications Taxonomy > Category > Section This commit adds models for it. https://doc.incommon.cc/#classifications --- Gemfile | 2 ++ Gemfile.lock | 6 ++++ app/models/category.rb | 14 ++++++++ app/models/section.rb | 10 ++++++ app/models/taxonomy.rb | 17 ++++++++++ db/migrate/20201008132251_create_taxonomies.rb | 16 ++++++++++ db/migrate/20201008132731_create_categories.rb | 15 +++++++++ db/migrate/20201008133300_create_sections.rb | 14 ++++++++ db/schema.rb | 44 +++++++++++++++++++++++++- db/seeds.rb | 10 ++++++ 10 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 app/models/category.rb create mode 100644 app/models/section.rb create mode 100644 app/models/taxonomy.rb create mode 100644 db/migrate/20201008132251_create_taxonomies.rb create mode 100644 db/migrate/20201008132731_create_categories.rb create mode 100644 db/migrate/20201008133300_create_sections.rb diff --git a/Gemfile b/Gemfile index 5cc080e..e11ac05 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,8 @@ gem 'jbuilder', '~> 2.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' +# Use acts_as_list for taxonomies +gem 'acts_as_list' # Use bitfields in models gem 'bitfields' # Use Discourse API diff --git a/Gemfile.lock b/Gemfile.lock index 6b28c92..96d66e2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,8 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2, >= 2.2.2) + acts_as_list (1.0.2) + activerecord (>= 4.2) bindex (0.8.1) bitfields (0.14.0) activerecord (>= 5.1) @@ -95,6 +97,8 @@ GEM activerecord kaminari-core (= 1.2.1) kaminari-core (1.2.1) + leaflet-rails (1.7.0) + rails (>= 4.2.0) listen (3.2.1) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) @@ -210,12 +214,14 @@ PLATFORMS ruby DEPENDENCIES + acts_as_list bitfields bootsnap (>= 1.4.2) byebug discourse_api jbuilder (~> 2.7) kaminari + leaflet-rails listen (~> 3.2) pg (>= 0.18, < 2.0) pry diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 0000000..b8c49f1 --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,14 @@ +class Category < ApplicationRecord + belongs_to :taxonomy + has_many :sections, + -> { order(rank: :asc) }, + dependent: :destroy, + inverse_of: :category + + acts_as_list column: :rank, scope: :taxonomy + + validates :name, + presence: true, + uniqueness: { scope: :taxonomy_id }, + length: 3..64 +end diff --git a/app/models/section.rb b/app/models/section.rb new file mode 100644 index 0000000..a9d163e --- /dev/null +++ b/app/models/section.rb @@ -0,0 +1,10 @@ +class Section < ApplicationRecord + belongs_to :category + has_ony :taxonomy, through: :category + + acts_as_list column: :rank, scope: :category + + validates :name, + uniqueness: { scope: category_id }, + length: 3..64 +end diff --git a/app/models/taxonomy.rb b/app/models/taxonomy.rb new file mode 100644 index 0000000..0810fc0 --- /dev/null +++ b/app/models/taxonomy.rb @@ -0,0 +1,17 @@ +class Taxonomy < ApplicationRecord + # Universally Unique Identifier :uuid + include UUIDParameter + + default_scope { order(created_at: :asc) } + + belongs_to :agent + has_many :categories, -> { order(rank: :asc) }, dependent: :destroy, inverse_of: :taxonomy + has_many :sections, -> { order(rank: :asv) }, through: :categories + + validates :name, + presence: true, + uniqueness: true, + length: 3..64 + validates :summary, + length: 0..136 +end diff --git a/db/migrate/20201008132251_create_taxonomies.rb b/db/migrate/20201008132251_create_taxonomies.rb new file mode 100644 index 0000000..8c746b1 --- /dev/null +++ b/db/migrate/20201008132251_create_taxonomies.rb @@ -0,0 +1,16 @@ +class CreateTaxonomies < ActiveRecord::Migration[6.0] + def change + create_table :taxonomies do |t| + t.string :name, limit: 64 + t.string :summary, limit: 64 + t.text :description + t.uuid :uuid + t.integer :categories_count, default: 0 + t.references :agent, null: false, foreign_key: true + + t.timestamps + end + add_index :taxonomies, :name, unique: true + add_index :taxonomies, :uuid, unique: true + end +end diff --git a/db/migrate/20201008132731_create_categories.rb b/db/migrate/20201008132731_create_categories.rb new file mode 100644 index 0000000..eb5a6fa --- /dev/null +++ b/db/migrate/20201008132731_create_categories.rb @@ -0,0 +1,15 @@ +class CreateCategories < ActiveRecord::Migration[6.0] + def change + create_table :categories do |t| + t.string :name, limit: 64 + t.string :summary, limit: 136 + t.text :description + t.references :taxonomy, null: false, foreign_key: true + t.string :color, limit: 25 + t.integer :rank + t.integer :sections_count, default: 0 + + t.timestamps + end + end +end diff --git a/db/migrate/20201008133300_create_sections.rb b/db/migrate/20201008133300_create_sections.rb new file mode 100644 index 0000000..c71c055 --- /dev/null +++ b/db/migrate/20201008133300_create_sections.rb @@ -0,0 +1,14 @@ +class CreateSections < ActiveRecord::Migration[6.0] + def change + create_table :sections do |t| + t.string :name, limit: 64 + t.string :summary, limit: 136 + t.text :description + t.references :category, null: false, foreign_key: true + t.string :color, limit: 25 + t.integer :rank, default: 0 + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index fbd0924..5a8bba7 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_06_140511) do +ActiveRecord::Schema.define(version: 2020_10_08_133300) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -35,6 +35,19 @@ ActiveRecord::Schema.define(version: 2020_10_06_140511) do t.index ["uuid"], name: "index_agents_on_uuid", unique: true end + create_table "categories", force: :cascade do |t| + t.string "name", limit: 64 + t.string "summary", limit: 136 + t.text "description" + t.bigint "taxonomy_id", null: false + t.string "color", limit: 25 + t.integer "rank" + t.integer "sections_count", default: 0 + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["taxonomy_id"], name: "index_categories_on_taxonomy_id" + end + create_table "resources", force: :cascade do |t| t.uuid "uuid" t.jsonb "feature" @@ -45,6 +58,32 @@ ActiveRecord::Schema.define(version: 2020_10_06_140511) do t.index ["uuid"], name: "index_resources_on_uuid", unique: true end + create_table "sections", force: :cascade do |t| + t.string "name", limit: 64 + t.string "summary", limit: 136 + t.text "description" + t.bigint "category_id", null: false + t.string "color", limit: 25 + t.integer "rank", default: 0 + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["category_id"], name: "index_sections_on_category_id" + end + + create_table "taxonomies", force: :cascade do |t| + t.string "name", limit: 64 + t.string "summary", limit: 64 + t.text "description" + t.uuid "uuid" + t.integer "categories_count", default: 0 + t.bigint "agent_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["agent_id"], name: "index_taxonomies_on_agent_id" + t.index ["name"], name: "index_taxonomies_on_name", unique: true + t.index ["uuid"], name: "index_taxonomies_on_uuid", unique: true + end + create_table "users", force: :cascade do |t| t.string "name" t.string "username" @@ -59,5 +98,8 @@ ActiveRecord::Schema.define(version: 2020_10_06_140511) do add_foreign_key "agencies", "agents" add_foreign_key "agencies", "users" + add_foreign_key "categories", "taxonomies" add_foreign_key "resources", "agents" + add_foreign_key "sections", "categories" + add_foreign_key "taxonomies", "agents" end diff --git a/db/seeds.rb b/db/seeds.rb index 3ee3a11..8a4c670 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -16,3 +16,13 @@ Agent.find_or_create_by(name: agent_name, uuid: uuid) end +dewey = Agent.find_by(uuid: '6347f151-6782-437c-8bae-55730672a76f') or die('Dewey Agent should be available by now!') + +# Create a default taxonomy +Taxonomy.find_or_create_by( + name: 'Dewey Maps Taxonomy', + summary: 'Original taxonomy used in Belgique, Mode d\'Emploi', + description: '## Dewey Maps Taxonomy', + uuid: '2519915f-d19c-4281-b758-f5ddb889d7fa', + agent_id: dewey.id +) -- cgit v1.2.3