Commit c1b92481 authored by hellekin's avatar hellekin

Complete classifications (refs #2)

- add cache_counters
- fix serializables
- switch from HABTM to HMT
parent d7db327e
......@@ -7,7 +7,7 @@ class Api::V0::TaxonomiesController < ApplicationController
fields: params[:fields],
include: include_from_params
# expose: [current_user, current_agent]
rescue UnknownRelationshipError => e
rescue INCOMMON::API::UnknownRelationshipError => e
render_jsonapi_error_unknown_relationship(e.message, model_types)
end
......@@ -17,13 +17,13 @@ class Api::V0::TaxonomiesController < ApplicationController
include: include_from_params,
fields: params[:fields],
links: { agent: api_agent_url(@taxonomy.agent) }
rescue UnknownRelationshipError => e
rescue INCOMMON::API::UnknownRelationshipError => e
render_jsonapi_error_unknown_relationship(e.message, model_types)
end
private
def model_types
%w(taxonomies categories sections taxonomy.agent taxonomies.agent agents agent).freeze
%w(agent categories sections).freeze
end
end
# frozen_string_literal: true
require_dependency 'incommon'
class ApplicationController < ActionController::API
# INCOMMON::API::Controller implements an ActiveSupport::Concern to handle API
# authorization and error handling for the whole application.
......
......@@ -5,7 +5,7 @@ class Category < ApplicationRecord
extend Mobility
translates :name, :summary, :description
belongs_to :taxonomy
belongs_to :taxonomy, counter_cache: true
has_many :sections,
-> { order(rank: :asc) },
dependent: :destroy,
......@@ -23,13 +23,14 @@ end
#
# Table name: categories
#
# id :bigint(8) not null, primary key
# color :string(25)
# rank :integer
# translations :json
# created_at :datetime not null
# updated_at :datetime not null
# taxonomy_id :bigint(8)
# id :bigint(8) not null, primary key
# color :string(25)
# rank :integer
# sections_count :integer default(0)
# translations :json
# created_at :datetime not null
# updated_at :datetime not null
# taxonomy_id :bigint(8)
#
# Indexes
#
......
......@@ -5,9 +5,16 @@ class Section < ApplicationRecord
extend Mobility
translates :name, :summary, :description
belongs_to :category
belongs_to :category, counter_cache: true
has_one :taxonomy, through: :category
has_and_belongs_to_many :resources
has_many :resource_sections, dependent: :destroy
with_options through: :resource_sections, source: :resource do
has_many :resources, source_type: 'Resource'
has_many :entities, source_type: 'Entity'
has_many :places, source_type: 'Place'
has_many :services, source_type: 'Service'
has_many :things, source_type: 'Thing'
end
acts_as_list column: :rank, scope: :category
......@@ -20,13 +27,14 @@ end
#
# Table name: sections
#
# id :bigint(8) not null, primary key
# color :string(25)
# rank :integer
# translations :json
# created_at :datetime not null
# updated_at :datetime not null
# category_id :bigint(8)
# id :bigint(8) not null, primary key
# color :string(25)
# rank :integer
# resources_count :integer default(0)
# translations :json
# created_at :datetime not null
# updated_at :datetime not null
# category_id :bigint(8)
#
# Indexes
#
......
......@@ -25,12 +25,13 @@ end
#
# Table name: taxonomies
#
# id :bigint(8) not null, primary key
# translations :json
# uuid :string(36)
# created_at :datetime not null
# updated_at :datetime not null
# agent_id :bigint(8)
# id :bigint(8) not null, primary key
# categories_count :integer default(0)
# translations :json
# uuid :string(36)
# created_at :datetime not null
# updated_at :datetime not null
# agent_id :bigint(8)
#
# Indexes
#
......
......@@ -12,8 +12,12 @@ class SerializableCategory < JSONAPI::Serializable::Resource
attribute :created_at
attribute :updated_at
has_many :sections
has_one :taxonomy
meta locale: I18n.locale
belongs_to :taxonomy
has_many :sections do
meta count: @object.sections_count
end
link :self do
@url_helpers.api_category_url(@object)
......
......@@ -3,24 +3,19 @@
class SerializableSection < JSONAPI::Serializable::Resource
type 'sections'
attribute :name do
@object.name
end
attribute :summary do
@object.summary
end
attribute :description do
@object.description
end
attribute :name
attribute :summary
attribute :description
attribute :color
attribute :created_at
attribute :updated_at
has_one :category
meta locale: I18n.locale
has_many :resources do
meta { { count: @object.resources.count } }
belongs_to :category
has_many :resources do
meta count: @object.resources_count
end
link :self do
......
......@@ -5,18 +5,18 @@ class SerializableTaxonomy < JSONAPI::Serializable::Resource
id { @object.uuid }
attribute :id
attribute :locale do
I18n.locale
end
attribute :name
attribute :summary
attribute :description
attribute :created_at
attribute :updated_at
meta locale: I18n.locale
belongs_to :agent
has_many :categories
has_many :categories do
meta count: @object.categories_count
end
has_many :sections
link :self do
......
class AddClassificationsCacheCounters < ActiveRecord::Migration[5.2]
def change
add_column :taxonomies, :categories_count, :integer, default: 0
add_column :categories, :sections_count, :integer, default: 0
add_column :sections, :resources_count, :integer, default: 0
end
end
class CreateResourceSections < ActiveRecord::Migration[5.2]
def change
create_table :resource_sections do |t|
t.references :resource, polymorphic: true
t.references :section, foreign_key: true
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment