From 6a64d18ad248d4ca6c44b42f99f07991294947f7 Mon Sep 17 00:00:00 2001 From: Didier Lafforgue Date: Fri, 8 Dec 2023 16:01:28 +0100 Subject: [PATCH] fix: issue #68 + chore: annotate models --- Gemfile | 4 +- Gemfile.lock | 4 ++ app/models/maglev/asset.rb | 14 ++++++ app/models/maglev/page.rb | 18 +++++++ app/models/maglev/page/search_concern.rb | 3 +- app/models/maglev/page_path.rb | 17 +++++++ app/models/maglev/site.rb | 24 ++++++++++ lib/tasks/auto_annotate_models.rake | 61 ++++++++++++++++++++++++ spec/dummy/config/application.rb | 2 +- spec/dummy/config/environments/test.rb | 10 ++-- spec/factories/maglev/assets.rb | 14 ++++++ spec/factories/maglev/pages.rb | 18 +++++++ spec/factories/maglev/sites.rb | 24 ++++++++++ spec/models/maglev/asset_spec.rb | 14 ++++++ spec/models/maglev/page/search_spec.rb | 27 +++++++++++ spec/models/maglev/page_path_spec.rb | 17 +++++++ spec/models/maglev/page_spec.rb | 18 +++++++ spec/models/maglev/site_spec.rb | 24 ++++++++++ 18 files changed, 305 insertions(+), 8 deletions(-) create mode 100644 lib/tasks/auto_annotate_models.rake create mode 100644 spec/models/maglev/page/search_spec.rb diff --git a/Gemfile b/Gemfile index 5e7e3ba3..6a1aa90c 100644 --- a/Gemfile +++ b/Gemfile @@ -14,7 +14,7 @@ gem 'rails', '~> 7.1' gem 'image_processing', '~> 1.12.2' # Use Sprockets to deal with assets in the dummy app -gem "sprockets-rails" +gem 'sprockets-rails' # Temporary use this branch because it solves a bug # gem 'maglev-injectable', path: '../injectable' @@ -41,6 +41,8 @@ group :development, :test do gem 'generator_spec' gem 'nokogiri', '>= 1.13.10' + + gem 'annotate' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 0f216d59..b68ab45f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,6 +86,9 @@ GEM minitest (>= 5.1) mutex_m tzinfo (~> 2.0) + annotate (3.2.0) + activerecord (>= 3.2, < 8.0) + rake (>= 10.4, < 14.0) ast (2.4.2) base64 (0.2.0) bcrypt (3.1.20) @@ -322,6 +325,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + annotate bcrypt factory_bot_rails (~> 6.2.0) generator_spec diff --git a/app/models/maglev/asset.rb b/app/models/maglev/asset.rb index 3d99053f..feff9121 100644 --- a/app/models/maglev/asset.rb +++ b/app/models/maglev/asset.rb @@ -26,3 +26,17 @@ def self.search(keyword, type, page = nil, per_page = nil) end end end + +# == Schema Information +# +# Table name: maglev_assets +# +# id :bigint not null, primary key +# byte_size :integer +# content_type :string +# filename :string +# height :integer +# width :integer +# created_at :datetime not null +# updated_at :datetime not null +# diff --git a/app/models/maglev/page.rb b/app/models/maglev/page.rb index 82674d91..fcc67c98 100644 --- a/app/models/maglev/page.rb +++ b/app/models/maglev/page.rb @@ -42,3 +42,21 @@ def translate_in(locale, source_locale) end end end + +# == Schema Information +# +# Table name: maglev_pages +# +# id :bigint not null, primary key +# lock_version :integer +# meta_description_translations :jsonb +# og_description_translations :jsonb +# og_image_url_translations :jsonb +# og_title_translations :jsonb +# sections_translations :jsonb +# seo_title_translations :jsonb +# title_translations :jsonb +# visible :boolean default(TRUE) +# created_at :datetime not null +# updated_at :datetime not null +# diff --git a/app/models/maglev/page/search_concern.rb b/app/models/maglev/page/search_concern.rb index f59682b8..865201a4 100644 --- a/app/models/maglev/page/search_concern.rb +++ b/app/models/maglev/page/search_concern.rb @@ -27,7 +27,8 @@ def search_title_clause(query, locale) def search_path_clause(query, locale) path = Maglev::PagePath.arel_table[:value] - path.matches(query).and(path.eq(locale)) + path_locale = Maglev::PagePath.arel_table[:locale] + path.matches(query).and(path_locale.eq(locale)) end def search_title_node(locale) diff --git a/app/models/maglev/page_path.rb b/app/models/maglev/page_path.rb index 8ef6e89d..c85d2ff9 100644 --- a/app/models/maglev/page_path.rb +++ b/app/models/maglev/page_path.rb @@ -44,3 +44,20 @@ def clean_value end end end + +# == Schema Information +# +# Table name: maglev_page_paths +# +# id :bigint not null, primary key +# canonical :boolean default(TRUE) +# locale :string not null +# value :string not null +# maglev_page_id :bigint +# +# Indexes +# +# canonical_speed (canonical,locale,value) +# index_maglev_page_paths_on_maglev_page_id (maglev_page_id) +# scoped_canonical_speed (canonical,maglev_page_id,locale) +# diff --git a/app/models/maglev/site.rb b/app/models/maglev/site.rb index a57ca514..e6802b86 100644 --- a/app/models/maglev/site.rb +++ b/app/models/maglev/site.rb @@ -27,3 +27,27 @@ def translate_in(locale, source_locale) end end end + +# == Schema Information +# +# Table name: maglev_sites +# +# id :bigint not null, primary key +# domain :string +# handle :string +# locales :jsonb +# lock_version :integer +# name :string +# navigation :jsonb +# sections_translations :jsonb +# siteable_type :string +# style :jsonb +# created_at :datetime not null +# updated_at :datetime not null +# siteable_id :bigint +# theme_id :string +# +# Indexes +# +# index_maglev_sites_on_siteable (siteable_type,siteable_id) +# diff --git a/lib/tasks/auto_annotate_models.rake b/lib/tasks/auto_annotate_models.rake new file mode 100644 index 00000000..091e8c96 --- /dev/null +++ b/lib/tasks/auto_annotate_models.rake @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +# NOTE: only doing this in development as some production environments (Heroku) +# NOTE: are sensitive to local FS writes, and besides -- it's just not proper +# NOTE: to have a dev-mode tool do its thing in production. +if Rails.env.development? + require 'annotate' + task set_annotation_options: :environment do + # You can override any of these by setting an environment variable of the + # same name. + Annotate.set_defaults( + active_admin: 'false', + additional_file_patterns: [], + routes: 'false', + models: 'true', + position_in_routes: 'before', + position_in_class: 'bottom', + position_in_test: 'bottom', + position_in_fixture: 'bottom', + position_in_factory: 'bottom', + position_in_serializer: 'bottom', + show_foreign_keys: 'true', + show_complete_foreign_keys: 'false', + show_indexes: 'true', + simple_indexes: 'false', + model_dir: 'app/models', + root_dir: '', + include_version: 'false', + require: '', + exclude_tests: 'false', + exclude_fixtures: 'false', + exclude_factories: 'false', + exclude_serializers: 'false', + exclude_scaffolds: 'true', + exclude_controllers: 'true', + exclude_helpers: 'true', + exclude_sti_subclasses: 'false', + ignore_model_sub_dir: 'false', + ignore_columns: nil, + ignore_routes: nil, + ignore_unknown_models: 'false', + hide_limit_column_types: 'integer,bigint,boolean', + hide_default_column_types: 'json,jsonb,hstore', + skip_on_db_migrate: 'false', + format_bare: 'true', + format_rdoc: 'false', + format_yard: 'false', + format_markdown: 'false', + sort: 'false', + force: 'false', + frozen: 'false', + classified_sort: 'true', + trace: 'false', + wrapper_open: nil, + wrapper_close: nil, + with_comment: 'true' + ) + end + + Annotate.load_tasks +end diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index 44820abf..71c7ff9a 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -15,7 +15,7 @@ class Application < Rails::Application else config.load_defaults 6.0 end - + # Settings in config/environments/* take precedence over those specified here. # Application configuration can go into files in config/initializers # -- all .rb files in that directory are automatically loaded after loading diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index 6344f61b..ebc8012d 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -28,11 +28,11 @@ config.cache_store = :null_store # Raise exceptions instead of rendering exception templates. - if Rails::VERSION::MAJOR > 6 - config.action_dispatch.show_exceptions = :none - else - config.action_dispatch.show_exceptions = false - end + config.action_dispatch.show_exceptions = if Rails::VERSION::MAJOR > 6 + :none + else + false + end # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false diff --git a/spec/factories/maglev/assets.rb b/spec/factories/maglev/assets.rb index f6b68e85..0c7182a6 100644 --- a/spec/factories/maglev/assets.rb +++ b/spec/factories/maglev/assets.rb @@ -7,3 +7,17 @@ content_type { 'image/jpg' } end end + +# == Schema Information +# +# Table name: maglev_assets +# +# id :bigint not null, primary key +# byte_size :integer +# content_type :string +# filename :string +# height :integer +# width :integer +# created_at :datetime not null +# updated_at :datetime not null +# diff --git a/spec/factories/maglev/pages.rb b/spec/factories/maglev/pages.rb index 615dc119..989746da 100644 --- a/spec/factories/maglev/pages.rb +++ b/spec/factories/maglev/pages.rb @@ -173,3 +173,21 @@ end end end + +# == Schema Information +# +# Table name: maglev_pages +# +# id :bigint not null, primary key +# lock_version :integer +# meta_description_translations :jsonb +# og_description_translations :jsonb +# og_image_url_translations :jsonb +# og_title_translations :jsonb +# sections_translations :jsonb +# seo_title_translations :jsonb +# title_translations :jsonb +# visible :boolean default(TRUE) +# created_at :datetime not null +# updated_at :datetime not null +# diff --git a/spec/factories/maglev/sites.rb b/spec/factories/maglev/sites.rb index eb2fec91..3b664112 100644 --- a/spec/factories/maglev/sites.rb +++ b/spec/factories/maglev/sites.rb @@ -107,3 +107,27 @@ end end end + +# == Schema Information +# +# Table name: maglev_sites +# +# id :bigint not null, primary key +# domain :string +# handle :string +# locales :jsonb +# lock_version :integer +# name :string +# navigation :jsonb +# sections_translations :jsonb +# siteable_type :string +# style :jsonb +# created_at :datetime not null +# updated_at :datetime not null +# siteable_id :bigint +# theme_id :string +# +# Indexes +# +# index_maglev_sites_on_siteable (siteable_type,siteable_id) +# diff --git a/spec/models/maglev/asset_spec.rb b/spec/models/maglev/asset_spec.rb index 9e2fb58c..12bfc157 100644 --- a/spec/models/maglev/asset_spec.rb +++ b/spec/models/maglev/asset_spec.rb @@ -32,3 +32,17 @@ end end end + +# == Schema Information +# +# Table name: maglev_assets +# +# id :bigint not null, primary key +# byte_size :integer +# content_type :string +# filename :string +# height :integer +# width :integer +# created_at :datetime not null +# updated_at :datetime not null +# diff --git a/spec/models/maglev/page/search_spec.rb b/spec/models/maglev/page/search_spec.rb new file mode 100644 index 00000000..e8fb086a --- /dev/null +++ b/spec/models/maglev/page/search_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Maglev::Page, type: :model do + describe 'search' do + before do + create(:page, title: 'Chicago', path: 'bands/chicago') + create(:page, title: 'Boston', path: 'bands/boston') + create(:page, title: 'San Francisco', path: 'cities/sf') + end + + subject { described_class.search(q, 'en') } + + describe 'Given we search from the title' do + let(:q) { 'boston' } + + it { expect(subject.all.map(&:title).sort).to eq ['Boston'] } + end + + describe 'Given we search from the path' do + let(:q) { 'bands' } + + it { expect(subject.all.map(&:title).sort).to eq %w[Boston Chicago] } + end + end +end diff --git a/spec/models/maglev/page_path_spec.rb b/spec/models/maglev/page_path_spec.rb index 49670ebe..7b024234 100644 --- a/spec/models/maglev/page_path_spec.rb +++ b/spec/models/maglev/page_path_spec.rb @@ -31,3 +31,20 @@ end end end + +# == Schema Information +# +# Table name: maglev_page_paths +# +# id :bigint not null, primary key +# canonical :boolean default(TRUE) +# locale :string not null +# value :string not null +# maglev_page_id :bigint +# +# Indexes +# +# canonical_speed (canonical,locale,value) +# index_maglev_page_paths_on_maglev_page_id (maglev_page_id) +# scoped_canonical_speed (canonical,maglev_page_id,locale) +# diff --git a/spec/models/maglev/page_spec.rb b/spec/models/maglev/page_spec.rb index 93e956fa..96fe9bf7 100644 --- a/spec/models/maglev/page_spec.rb +++ b/spec/models/maglev/page_spec.rb @@ -52,3 +52,21 @@ end end end + +# == Schema Information +# +# Table name: maglev_pages +# +# id :bigint not null, primary key +# lock_version :integer +# meta_description_translations :jsonb +# og_description_translations :jsonb +# og_image_url_translations :jsonb +# og_title_translations :jsonb +# sections_translations :jsonb +# seo_title_translations :jsonb +# title_translations :jsonb +# visible :boolean default(TRUE) +# created_at :datetime not null +# updated_at :datetime not null +# diff --git a/spec/models/maglev/site_spec.rb b/spec/models/maglev/site_spec.rb index 5891883e..299268e9 100644 --- a/spec/models/maglev/site_spec.rb +++ b/spec/models/maglev/site_spec.rb @@ -72,3 +72,27 @@ end end end + +# == Schema Information +# +# Table name: maglev_sites +# +# id :bigint not null, primary key +# domain :string +# handle :string +# locales :jsonb +# lock_version :integer +# name :string +# navigation :jsonb +# sections_translations :jsonb +# siteable_type :string +# style :jsonb +# created_at :datetime not null +# updated_at :datetime not null +# siteable_id :bigint +# theme_id :string +# +# Indexes +# +# index_maglev_sites_on_siteable (siteable_type,siteable_id) +#