Skip to content

Commit 2298081

Browse files
authored
fix: take into account subclasses of ActiveRecord models (#569)
1 parent f891ee5 commit 2298081

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

lib/forest_liana/bootstrapper.rb

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,39 +96,25 @@ def generate_apimap
9696
end
9797
end
9898

99-
def is_sti_parent_model?(model)
100-
return false unless model.try(:table_exists?)
101-
102-
model.inheritance_column && model.columns.find { |column| column.name == model.inheritance_column }
103-
end
104-
10599
def analyze_model?(model)
106100
model && model.table_exists? && !SchemaUtils.habtm?(model) &&
107101
SchemaUtils.model_included?(model)
108102
end
109103

110104
def fetch_models
111-
ActiveRecord::Base.subclasses.each { |model| fetch_model(model) }
105+
ActiveRecord::Base.descendants.each { |model| fetch_model(model) }
112106
end
113107

114108
def fetch_model(model)
115-
begin
116-
if model.abstract_class?
117-
model.subclasses.each { |submodel| fetch_model(submodel) }
118-
else
119-
if is_sti_parent_model?(model)
120-
model.subclasses.each { |submodel_sti| fetch_model(submodel_sti) }
121-
end
122-
123-
if analyze_model?(model)
124-
ForestLiana.models << model unless ForestLiana.models.include?(model)
125-
end
126-
end
127-
rescue => exception
128-
FOREST_REPORTER.report exception
129-
FOREST_LOGGER.error "Cannot fetch properly model #{model.name}:\n" \
109+
return if model.abstract_class?
110+
return if ForestLiana.models.include?(model)
111+
return unless analyze_model?(model)
112+
113+
ForestLiana.models << model
114+
rescue => exception
115+
FOREST_REPORTER.report exception
116+
FOREST_LOGGER.error "Cannot fetch properly model #{model.name}:\n" \
130117
"#{exception}"
131-
end
132118
end
133119

134120
def cast_to_array value

spec/dummy/app/models/town.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Town < Location
2+
end

spec/lib/forest_liana/bootstrapper_spec.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,32 @@ module ForestLiana
1414
end
1515

1616
describe 'models' do
17-
let(:expected_models) do
17+
let(:application_models) do
18+
ForestLiana.models.reject do |model|
19+
rails_models.any? { |rails_model| model <= rails_model }
20+
end
21+
end
22+
let(:rails_models) { [ActiveRecord::InternalMetadata, ActiveRecord::SchemaMigration] }
23+
24+
let(:expected_application_models) do
1825
[
1926
Island,
2027
Location,
2128
Owner,
2229
Product,
2330
Reference,
31+
Town,
2432
Tree,
2533
User
2634
]
2735
end
2836

2937
it 'should populate the models correctly' do
3038
ForestLiana::Bootstrapper.new
31-
rails_models = [ActiveRecord::InternalMetadata, ActiveRecord::SchemaMigration]
3239

3340
expect(ForestLiana.models).to match_array(ForestLiana.models.uniq)
34-
expect(ForestLiana.models).to match_array(expected_models + rails_models)
41+
expect(ForestLiana.models).to include(*rails_models)
42+
expect(application_models).to match_array(expected_application_models)
3543
end
3644

3745
it 'should generate serializers for all models' do
@@ -40,7 +48,7 @@ module ForestLiana
4048

4149
ForestLiana::Bootstrapper.new
4250

43-
expected_models.each do |model|
51+
expected_application_models.each do |model|
4452
expect(factory).to have_received(:serializer_for).with(model).once
4553
end
4654
end
@@ -51,7 +59,7 @@ module ForestLiana
5159

5260
ForestLiana::Bootstrapper.new
5361

54-
expected_models.each do |model|
62+
expected_application_models.each do |model|
5563
expect(factory).to have_received(:controller_for).with(model).once
5664
end
5765
end

0 commit comments

Comments
 (0)