Skip to content

Commit

Permalink
BREAKING CHANGE: Do not show models without table. Closes #3157
Browse files Browse the repository at this point in the history
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']
  • Loading branch information
mshibuya committed Jun 26, 2019
1 parent 1550260 commit 87b38b3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/rails_admin/abstract_model.rb
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/rails_admin/config.rb
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy_app/app/active_record/without_table.rb
@@ -0,0 +1,2 @@
class WithoutTable < ActiveRecord::Base
end
8 changes: 8 additions & 0 deletions spec/orm/active_record.rb
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions 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
Expand Down
12 changes: 6 additions & 6 deletions spec/rails_admin/adapters/active_record/association_spec.rb
Expand Up @@ -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

Expand Down
7 changes: 2 additions & 5 deletions spec/rails_admin/adapters/active_record_spec.rb
Expand Up @@ -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

Expand Down

0 comments on commit 87b38b3

Please sign in to comment.