Skip to content

Commit 3a135b7

Browse files
authored
fix: prevent duplication of models from multiple ActiveRecord application classes (#566)
1 parent cf91036 commit 3a135b7

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

lib/forest_liana/bootstrapper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ def fetch_models
114114
def fetch_model(model)
115115
begin
116116
if model.abstract_class?
117-
model.descendants.each { |submodel| fetch_model(submodel) }
117+
model.subclasses.each { |submodel| fetch_model(submodel) }
118118
else
119119
if is_sti_parent_model?(model)
120-
model.descendants.each { |submodel_sti| fetch_model(submodel_sti) }
120+
model.subclasses.each { |submodel_sti| fetch_model(submodel_sti) }
121121
end
122122

123123
if analyze_model?(model)
124-
ForestLiana.models << model
124+
ForestLiana.models << model unless ForestLiana.models.include?(model)
125125
end
126126
end
127127
rescue => exception
@@ -136,7 +136,7 @@ def cast_to_array value
136136
end
137137

138138
def create_factories
139-
ForestLiana.models.uniq.map do |model|
139+
ForestLiana.models.map do |model|
140140
ForestLiana::SerializerFactory.new.serializer_for(model)
141141
ForestLiana::ControllerFactory.new.controller_for(model)
142142
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ApplicationRecord < ActiveRecord::Base
2+
self.abstract_class = true
3+
end

spec/dummy/app/models/product.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
class Product < ActiveRecord::Base
1+
class Product < ApplicationRecord
22
validates :uri, presence: true, format: { with: URI::DEFAULT_PARSER.make_regexp }
33
end

spec/dummy/app/models/reference.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
class Reference < ActiveRecord::Base
1+
class Reference < SubApplicationRecord
22
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SubApplicationRecord < ApplicationRecord
2+
self.abstract_class = true
3+
end

spec/lib/forest_liana/bootstrapper_spec.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module ForestLiana
22
describe Bootstrapper do
3+
before do
4+
allow(ForestLiana).to receive(:env_secret).and_return(nil)
5+
end
6+
37
describe 'setup_forest_liana_meta' do
48
it "should put statistic data related to user stack on a dedicated object" do
59
expect(ForestLiana.meta[:stack])
@@ -9,6 +13,50 @@ module ForestLiana
913
end
1014
end
1115

16+
describe 'models' do
17+
let(:expected_models) do
18+
[
19+
Island,
20+
Location,
21+
Owner,
22+
Product,
23+
Reference,
24+
Tree,
25+
User
26+
]
27+
end
28+
29+
it 'should populate the models correctly' do
30+
ForestLiana::Bootstrapper.new
31+
rails_models = [ActiveRecord::InternalMetadata, ActiveRecord::SchemaMigration]
32+
33+
expect(ForestLiana.models).to match_array(ForestLiana.models.uniq)
34+
expect(ForestLiana.models).to match_array(expected_models + rails_models)
35+
end
36+
37+
it 'should generate serializers for all models' do
38+
factory = instance_double(ForestLiana::SerializerFactory, serializer_for: nil)
39+
allow(ForestLiana::SerializerFactory).to receive(:new).and_return(factory)
40+
41+
ForestLiana::Bootstrapper.new
42+
43+
expected_models.each do |model|
44+
expect(factory).to have_received(:serializer_for).with(model).once
45+
end
46+
end
47+
48+
it 'should generate controllers for all models' do
49+
factory = instance_double(ForestLiana::ControllerFactory, controller_for: nil)
50+
allow(ForestLiana::ControllerFactory).to receive(:new).and_return(factory)
51+
52+
ForestLiana::Bootstrapper.new
53+
54+
expected_models.each do |model|
55+
expect(factory).to have_received(:controller_for).with(model).once
56+
end
57+
end
58+
end
59+
1260
describe 'generate_action_hooks' do
1361
schema = '{
1462
"collections": [
@@ -102,7 +150,6 @@ module ForestLiana
102150

103151

104152
it "Should return actions hooks empty for the island collection" do
105-
allow(ForestLiana).to receive(:env_secret).and_return(nil)
106153
bootstrapper = Bootstrapper.new
107154
content = JSON.parse(schema)
108155
bootstrapper.instance_variable_set(:@collections_sent, content['collections'])

0 commit comments

Comments
 (0)