Skip to content

Commit bbbb321

Browse files
fix: avoid smart feature to be load more one time (#706)
1 parent 49ebe11 commit bbbb321

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/forest_liana/collection.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ def collection(collection_name, opts = {})
2424
end
2525

2626
def action(name, opts = {})
27+
return if model.actions.find { |action| action.name == name }
28+
2729
opts[:name] = name
2830
model.actions << ForestLiana::Model::Action.new(opts)
2931
end
3032

3133
def segment(name, opts = {}, &block)
34+
return if model.segments.find { |segment| segment.name == name }
35+
3236
opts[:name] = name
3337
model.segments << ForestLiana::Model::Segment.new(opts, &block)
3438
end
@@ -74,12 +78,14 @@ def field(name, opts, &block)
7478
opts[:validations] = [] unless opts.has_key?(:validations)
7579
opts[:is_virtual] = true unless opts.has_key?(:polymorphic_key) && opts[:polymorphic_key]
7680

77-
model.fields << opts.merge({
81+
field = opts.merge({
7882
field: name,
7983
is_filterable: !!opts[:is_filterable],
8084
is_sortable: !!opts[:is_sortable],
8185
})
8286

87+
add_field(field)
88+
8389
define_method(name) { self.data[name] } if smart_collection?
8490

8591
if serializer_name && ForestLiana::UserSpace.const_defined?(
@@ -108,13 +114,15 @@ def field(name, opts, &block)
108114
end
109115

110116
def has_many(name, opts, &block)
111-
model.fields << opts.merge({
117+
field = opts.merge({
112118
field: name,
113119
is_virtual: true,
114120
is_searchable: false,
115121
type: ['String']
116122
})
117123

124+
add_field(field)
125+
118126
define_method(name) { self.data[name] } if smart_collection?
119127

120128
if serializer_name && ForestLiana::UserSpace.const_defined?(
@@ -126,13 +134,15 @@ def has_many(name, opts, &block)
126134
end
127135

128136
def belongs_to(name, opts, &block)
129-
model.fields << opts.merge({
137+
field = opts.merge({
130138
field: name,
131139
is_virtual: true,
132140
is_searchable: false,
133141
type: 'String'
134142
})
135143

144+
add_field(field)
145+
136146
define_method(name) { self.data[name] } if smart_collection?
137147

138148
if serializer_name && ForestLiana::UserSpace.const_defined?(
@@ -145,6 +155,12 @@ def belongs_to(name, opts, &block)
145155

146156
private
147157

158+
def add_field(field)
159+
return if model.fields.find { |field| field[:field] == name }
160+
161+
model.fields << field
162+
end
163+
148164
def find_name(collection_name)
149165
# TODO: Remove once lianas prior to 2.0.0 are not supported anymore.
150166
model = ForestLiana.models.find { |collection| collection.try(:table_name) == collection_name.to_s }

0 commit comments

Comments
 (0)