@@ -24,11 +24,15 @@ def collection(collection_name, opts = {})
24
24
end
25
25
26
26
def action ( name , opts = { } )
27
+ return if model . actions . find { |action | action . name == name }
28
+
27
29
opts [ :name ] = name
28
30
model . actions << ForestLiana ::Model ::Action . new ( opts )
29
31
end
30
32
31
33
def segment ( name , opts = { } , &block )
34
+ return if model . segments . find { |segment | segment . name == name }
35
+
32
36
opts [ :name ] = name
33
37
model . segments << ForestLiana ::Model ::Segment . new ( opts , &block )
34
38
end
@@ -74,12 +78,14 @@ def field(name, opts, &block)
74
78
opts [ :validations ] = [ ] unless opts . has_key? ( :validations )
75
79
opts [ :is_virtual ] = true unless opts . has_key? ( :polymorphic_key ) && opts [ :polymorphic_key ]
76
80
77
- model . fields << opts . merge ( {
81
+ field = opts . merge ( {
78
82
field : name ,
79
83
is_filterable : !!opts [ :is_filterable ] ,
80
84
is_sortable : !!opts [ :is_sortable ] ,
81
85
} )
82
86
87
+ add_field ( field )
88
+
83
89
define_method ( name ) { self . data [ name ] } if smart_collection?
84
90
85
91
if serializer_name && ForestLiana ::UserSpace . const_defined? (
@@ -108,13 +114,15 @@ def field(name, opts, &block)
108
114
end
109
115
110
116
def has_many ( name , opts , &block )
111
- model . fields << opts . merge ( {
117
+ field = opts . merge ( {
112
118
field : name ,
113
119
is_virtual : true ,
114
120
is_searchable : false ,
115
121
type : [ 'String' ]
116
122
} )
117
123
124
+ add_field ( field )
125
+
118
126
define_method ( name ) { self . data [ name ] } if smart_collection?
119
127
120
128
if serializer_name && ForestLiana ::UserSpace . const_defined? (
@@ -126,13 +134,15 @@ def has_many(name, opts, &block)
126
134
end
127
135
128
136
def belongs_to ( name , opts , &block )
129
- model . fields << opts . merge ( {
137
+ field = opts . merge ( {
130
138
field : name ,
131
139
is_virtual : true ,
132
140
is_searchable : false ,
133
141
type : 'String'
134
142
} )
135
143
144
+ add_field ( field )
145
+
136
146
define_method ( name ) { self . data [ name ] } if smart_collection?
137
147
138
148
if serializer_name && ForestLiana ::UserSpace . const_defined? (
@@ -145,6 +155,12 @@ def belongs_to(name, opts, &block)
145
155
146
156
private
147
157
158
+ def add_field ( field )
159
+ return if model . fields . find { |field | field [ :field ] == name }
160
+
161
+ model . fields << field
162
+ end
163
+
148
164
def find_name ( collection_name )
149
165
# TODO: Remove once lianas prior to 2.0.0 are not supported anymore.
150
166
model = ForestLiana . models . find { |collection | collection . try ( :table_name ) == collection_name . to_s }
0 commit comments