From 87b38b336cc668a74803dec4628215e2e2941248 Mon Sep 17 00:00:00 2001 From: "M.Shibuya" Date: Wed, 26 Jun 2019 15:05:04 +0900 Subject: [PATCH] BREAKING CHANGE: Do not show models without table. Closes #3157 RailsAdmin no longer automatically exclude models for PaperTrail and ActiveStorage. Exclude manually if you don't need them. config.excluded_models = ['PaperTrail::Version', 'PaperTrail::VersionAssociation', 'ActiveStorage::Attachment', 'ActiveStorage::Blob'] --- lib/rails_admin/abstract_model.rb | 2 +- lib/rails_admin/config.rb | 4 +--- spec/dummy_app/app/active_record/without_table.rb | 2 ++ spec/orm/active_record.rb | 8 ++++++++ spec/rails_admin/abstract_model_spec.rb | 10 ++++++++++ .../adapters/active_record/association_spec.rb | 12 ++++++------ spec/rails_admin/adapters/active_record_spec.rb | 7 ++----- 7 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 spec/dummy_app/app/active_record/without_table.rb diff --git a/lib/rails_admin/abstract_model.rb b/lib/rails_admin/abstract_model.rb index c5f1fc9b1a..8f50f8260e 100644 --- a/lib/rails_admin/abstract_model.rb +++ b/lib/rails_admin/abstract_model.rb @@ -46,7 +46,7 @@ def reset_polymorphic_parents def initialize(model_or_model_name) @model_name = model_or_model_name.to_s ancestors = model.ancestors.collect(&:to_s) - if ancestors.include?('ActiveRecord::Base') && !model.abstract_class? + if ancestors.include?('ActiveRecord::Base') && !model.abstract_class? && model.table_exists? initialize_active_record elsif ancestors.include?('Mongoid::Document') initialize_mongoid diff --git a/lib/rails_admin/config.rb b/lib/rails_admin/config.rb index d823fd6a0b..81ee1c251c 100644 --- a/lib/rails_admin/config.rb +++ b/lib/rails_admin/config.rb @@ -210,9 +210,7 @@ def default_search_operator=(operator) # pool of all found model names from the whole application def models_pool - excluded = (excluded_models.collect(&:to_s) + %w(RailsAdmin::History PaperTrail::Version PaperTrail::VersionAssociation ActiveStorage::Attachment ActiveStorage::Blob)) - - (viable_models - excluded).uniq.sort + (viable_models - excluded_models.collect(&:to_s)).uniq.sort end # Loads a model configuration instance from the registry or registers diff --git a/spec/dummy_app/app/active_record/without_table.rb b/spec/dummy_app/app/active_record/without_table.rb new file mode 100644 index 0000000000..da5a23a207 --- /dev/null +++ b/spec/dummy_app/app/active_record/without_table.rb @@ -0,0 +1,2 @@ +class WithoutTable < ActiveRecord::Base +end diff --git a/spec/orm/active_record.rb b/spec/orm/active_record.rb index 305ba7d487..efafcba30a 100644 --- a/spec/orm/active_record.rb +++ b/spec/orm/active_record.rb @@ -62,6 +62,14 @@ def attribute_types Hash[columns.collect { |column| [column.name, lookup_attribute_type(column.type)] }] end + def table_exists? + true + end + + def primary_key + "id" + end + private def lookup_attribute_type(type) diff --git a/spec/rails_admin/abstract_model_spec.rb b/spec/rails_admin/abstract_model_spec.rb index 0859f58602..06d32694eb 100644 --- a/spec/rails_admin/abstract_model_spec.rb +++ b/spec/rails_admin/abstract_model_spec.rb @@ -1,6 +1,16 @@ require 'spec_helper' describe RailsAdmin::AbstractModel do + describe '.all' do + it 'returns abstract models for all models' do + expect(RailsAdmin::AbstractModel.all.map(&:model)).to include Player, Team + end + + it 'does not pick up a model without table', active_record: true do + expect(RailsAdmin::AbstractModel.all.map(&:model)).not_to include WithoutTable + end + end + describe '#to_s' do it 'returns model\'s name' do expect(RailsAdmin::AbstractModel.new(Cms::BasicPage).to_s).to eq Cms::BasicPage.to_s diff --git a/spec/rails_admin/adapters/active_record/association_spec.rb b/spec/rails_admin/adapters/active_record/association_spec.rb index 0469aa90d2..5c05176263 100644 --- a/spec/rails_admin/adapters/active_record/association_spec.rb +++ b/spec/rails_admin/adapters/active_record/association_spec.rb @@ -5,34 +5,34 @@ before :all do RailsAdmin::AbstractModel.reset_polymorphic_parents - class ARBlog < ActiveRecord::Base + class ARBlog < Tableless has_many :a_r_posts has_many :a_r_comments, as: :commentable belongs_to :librarian, polymorphic: true end - class ARPost < ActiveRecord::Base + class ARPost < Tableless belongs_to :a_r_blog has_and_belongs_to_many :a_r_categories has_many :a_r_comments, as: :commentable end - class ARCategory < ActiveRecord::Base + class ARCategory < Tableless has_and_belongs_to_many :a_r_posts belongs_to :librarian, polymorphic: true end - class ARUser < ActiveRecord::Base + class ARUser < Tableless has_one :a_r_profile has_many :a_r_categories, as: :librarian end - class ARProfile < ActiveRecord::Base + class ARProfile < Tableless belongs_to :a_r_user has_many :a_r_blogs, as: :librarian end - class ARComment < ActiveRecord::Base + class ARComment < Tableless belongs_to :commentable, polymorphic: true end diff --git a/spec/rails_admin/adapters/active_record_spec.rb b/spec/rails_admin/adapters/active_record_spec.rb index 88ccb26c8f..dad6c7dd54 100644 --- a/spec/rails_admin/adapters/active_record_spec.rb +++ b/spec/rails_admin/adapters/active_record_spec.rb @@ -257,12 +257,9 @@ def build_statement(type, value, operator) end it 'chooses like statement in per-model basis' do - connection = double('connection') - allow(FieldTest).to receive(:connection).and_return(connection) - - allow(connection).to receive(:adapter_name).and_return('postgresql') + allow(FieldTest.connection).to receive(:adapter_name).and_return('postgresql') expect(build_statement(:string, 'foo', 'default')).to eq(['(field ILIKE ?)', '%foo%']) - allow(connection).to receive(:adapter_name).and_return('sqlite3') + allow(FieldTest.connection).to receive(:adapter_name).and_return('sqlite3') expect(build_statement(:string, 'foo', 'default')).to eq(['(LOWER(field) LIKE ?)', '%foo%']) end