From 149d59c73aa9cef72c0d1bd3d9baa31e832e1cda Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Fri, 19 Jan 2024 03:54:10 +0100 Subject: [PATCH] feat: remove unused code (#432) chore: cleanup schema.rb --- lib/closure_tree/active_record_support.rb | 17 ++------ lib/closure_tree/support.rb | 4 +- spec/spec_helper.rb | 14 +++---- spec/support/schema.rb | 48 +++++++++++------------ 4 files changed, 36 insertions(+), 47 deletions(-) diff --git a/lib/closure_tree/active_record_support.rb b/lib/closure_tree/active_record_support.rb index b1f7bca3..0ee7283b 100644 --- a/lib/closure_tree/active_record_support.rb +++ b/lib/closure_tree/active_record_support.rb @@ -1,20 +1,9 @@ module ClosureTree module ActiveRecordSupport + delegate :quote, to: :connection - def quote(field) - connection.quote(field) - end - - def ensure_fixed_table_name(table_name) - [ - ActiveRecord::Base.table_name_prefix, - remove_prefix_and_suffix(table_name), - ActiveRecord::Base.table_name_suffix - ].compact.join - end - - def remove_prefix_and_suffix(table_name) - pre, suff = ActiveRecord::Base.table_name_prefix, ActiveRecord::Base.table_name_suffix + def remove_prefix_and_suffix(table_name, model = ActiveRecord::Base) + pre, suff = model.table_name_prefix, model.table_name_suffix if table_name.start_with?(pre) && table_name.end_with?(suff) table_name[pre.size..-(suff.size + 1)] else diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index e887090e..2125541c 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -59,9 +59,9 @@ def hierarchy_table_name # because they may have overridden the table name, which is what we want to be consistent with # in order for the schema to make sense. tablename = options[:hierarchy_table_name] || - remove_prefix_and_suffix(table_name).singularize + "_hierarchies" + remove_prefix_and_suffix(table_name, model_class).singularize + "_hierarchies" - ActiveRecord::Base.table_name_prefix + tablename + ActiveRecord::Base.table_name_suffix + [model_class.table_name_prefix, tablename, model_class.table_name_suffix].join end def with_order_option(opts) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index eba943f7..201ff1bb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,13 +12,13 @@ require 'active_support/core_ext/array' -# Start Simplecov -if RUBY_ENGINE == 'ruby' - require 'simplecov' - SimpleCov.start do - add_filter '/spec/' - end -end +# # Start Simplecov +# if RUBY_ENGINE == 'ruby' +# require 'simplecov' +# SimpleCov.start do +# add_filter '/spec/' +# end +# end ActiveRecord::Base.configurations = { default_env: { diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 30a4a992..cca75c50 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -ActiveRecord::Schema.define(version: 0) do - create_table 'tags', force: :cascade do |t| +ActiveRecord::Schema.define(version: 1) do + create_table 'tags' do |t| t.string 'name' t.string 'title' t.references 'parent' @@ -9,13 +9,13 @@ t.timestamps null: false end - create_table 'tag_hierarchies', id: false, force: :cascade do |t| + create_table 'tag_hierarchies', id: false do |t| t.references 'ancestor', null: false t.references 'descendant', null: false t.integer 'generations', null: false end - create_table 'uuid_tags', id: false, force: :cascade do |t| + create_table 'uuid_tags', id: false do |t| t.string 'uuid', primary_key: true t.string 'name' t.string 'title' @@ -24,13 +24,13 @@ t.timestamps null: false end - create_table 'uuid_tag_hierarchies', id: false, force: :cascade do |t| + create_table 'uuid_tag_hierarchies', id: false do |t| t.string 'ancestor_id', null: false t.string 'descendant_id', null: false t.integer 'generations', null: false end - create_table 'destroyed_tags', force: :cascade do |t| + create_table 'destroyed_tags' do |t| t.string 'name' end @@ -38,81 +38,81 @@ name: 'tag_anc_desc_idx' add_index 'tag_hierarchies', [:descendant_id], name: 'tag_desc_idx' - create_table 'groups', force: :cascade do |t| + create_table 'groups' do |t| t.string 'name', null: false end - create_table 'groupings', force: :cascade do |t| + create_table 'groupings' do |t| t.string 'name', null: false end - create_table 'user_sets', force: :cascade do |t| + create_table 'user_sets' do |t| t.string 'name', null: false end - create_table 'teams', force: :cascade do |t| + create_table 'teams' do |t| t.string 'name', null: false end - create_table 'users', force: :cascade do |t| + create_table 'users' do |t| t.string 'email' t.references 'referrer' t.integer 'group_id' t.timestamps null: false end - create_table 'contracts', force: :cascade do |t| + create_table 'contracts' do |t| t.references 'user', null: false t.references 'contract_type' t.string 'title' end - create_table 'contract_types', force: :cascade do |t| + create_table 'contract_types' do |t| t.string 'name', null: false end - create_table 'referral_hierarchies', id: false, force: :cascade do |t| + create_table 'referral_hierarchies', id: false do |t| t.references 'ancestor', null: false t.references 'descendant', null: false t.integer 'generations', null: false end - create_table 'labels', force: :cascade do |t| + create_table 'labels' do |t| t.string 'name' t.string 'type' t.integer 'column_whereby_ordering_is_inferred' t.references 'mother' end - create_table 'label_hierarchies', id: false, force: :cascade do |t| + create_table 'label_hierarchies', id: false do |t| t.references 'ancestor', null: false t.references 'descendant', null: false t.integer 'generations', null: false end - create_table 'cuisine_types', force: :cascade do |t| + create_table 'cuisine_types' do |t| t.string 'name' t.references 'parent' end - create_table 'cuisine_type_hierarchies', id: false, force: :cascade do |t| + create_table 'cuisine_type_hierarchies', id: false do |t| t.references 'ancestor', null: false t.references 'descendant', null: false t.integer 'generations', null: false end - create_table 'namespace_types', force: :cascade do |t| + create_table 'namespace_types' do |t| t.string 'name' t.references 'parent' end - create_table 'namespace_type_hierarchies', id: false, force: :cascade do |t| + create_table 'namespace_type_hierarchies', id: false do |t| t.references 'ancestor', null: false t.references 'descendant', null: false t.integer 'generations', null: false end - create_table 'metal', force: :cascade do |t| + create_table 'metal' do |t| t.references 'parent' t.string 'metal_type' t.string 'value' @@ -120,19 +120,19 @@ t.integer 'sort_order' end - create_table 'metal_hierarchies', id: false, force: :cascade do |t| + create_table 'metal_hierarchies', id: false do |t| t.references 'ancestor', null: false t.references 'descendant', null: false t.integer 'generations', null: false end - create_table 'menu_items', force: :cascade do |t| + create_table 'menu_items' do |t| t.string 'name' t.references 'parent' t.timestamps null: false end - create_table 'menu_item_hierarchies', id: false, force: :cascade do |t| + create_table 'menu_item_hierarchies', id: false do |t| t.references 'ancestor', null: false t.references 'descendant', null: false t.integer 'generations', null: false