Module: Avo::Concerns::HasFields

Extended by:
ActiveSupport::Concern
Included in:
BaseAction, BaseResource, GridCollector, TabGroup
Defined in:
lib/avo/concerns/has_fields.rb

Instance Method Summary collapse

Instance Method Details

#fields(**args) ⇒ Object



152
153
154
# File 'lib/avo/concerns/has_fields.rb', line 152

def fields(**args)
  self.class.fields(**args)
end

#get_field(id) ⇒ Object



232
233
234
235
236
# File 'lib/avo/concerns/has_fields.rb', line 232

def get_field(id)
  get_field_definitions.find do |f|
    f.id == id.to_sym
  end
end

#get_field_definitions(only_root: false) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/avo/concerns/has_fields.rb', line 160

def get_field_definitions(only_root: false)
  fields = self.fields(only_root: only_root)

  return [] if fields.blank?

  items = fields.map do |field|
    field.hydrate(resource: self, user: user, view: view)
  end

  if Avo::App.license.lacks_with_trial(:custom_fields)
    items = items.reject do |field|
      field.custom?
    end
  end

  if Avo::App.license.lacks_with_trial(:advanced_fields)
    items = items.reject do |field|
      field.type == "tags"
    end
  end

  items
end

#get_fields(panel: nil, reflection: nil, only_root: false) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/avo/concerns/has_fields.rb', line 184

def get_fields(panel: nil, reflection: nil, only_root: false)
  fields = get_field_definitions(only_root: only_root)
    .select do |field|
      field.visible_on?(view)
    end
    .select do |field|
      field.visible?
    end
    .select do |field|
      is_valid = true

      # Strip out the reflection field in index queries with a parent association.
      if reflection.present?
        # regular non-polymorphic association
        # we're matching the reflection inverse_of foriegn key with the field's foreign_key
        if field.is_a?(Avo::Fields::BelongsToField)
          if field.respond_to?(:foreign_key) &&
              reflection.inverse_of.present? &&
              reflection.inverse_of.respond_to?(:foreign_key) &&
              reflection.inverse_of.foreign_key == field.foreign_key
            is_valid = false
          end

          # polymorphic association
          if field.respond_to?(:foreign_key) &&
              field.is_polymorphic? &&
              reflection.respond_to?(:polymorphic?) &&
              reflection.inverse_of.respond_to?(:foreign_key) &&
              reflection.inverse_of.foreign_key == field.reflection.foreign_key
            is_valid = false
          end
        end
      end

      is_valid
    end

  if panel.present?
    fields = fields.select do |field|
      field.panel_name == panel
    end
  end

  hydrate_fields(model: @model, view: @view)

  fields
end

#get_itemsObject

Separates the fields that are in a panel and those that are just hanging out. Take the ones that aren’t placed into a panel and add them to the “default” panel. This is to keep compatibility with the versions before 2.10 when you didn’t have the ability to add fields to panels.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/avo/concerns/has_fields.rb', line 262

def get_items
  panelless_items = []
  panelfull_items = []

  items.each do |item|
    # fields and tabs can be hidden on some views
    if item.respond_to? :visible_on?
      next unless item.visible_on?(view)
    end
    # each field has it's own visibility checker
    if item.respond_to? :visible?
      item.hydrate(view: view) if item.view.nil?

      next unless item.visible?
    end
    # check if the user is authorized to view it
    if item.respond_to? :authorized?
      next unless item.hydrate(model: @model).authorized?
    end

    if item.is_field?
      if item.has_own_panel?
        panelfull_items << item
      else
        panelless_items << item
      end
    else
      panelfull_items << item
    end
  end

  # Make sure all tabs panelfull_items are setted as inside tabs
  panelfull_items.grep(Avo::TabGroup).each do |tab_group|
    tab_group.items.grep(Avo::Tab).each do |tab|
      tab.items.grep(Avo::Panel).each do |panel|
        panel.items.grep(Avo::Fields::BelongsToField).each do |field|
          field.target = :_top
        end
      end
    end
  end

  # Add all the panelles fields to a new panel
  main_panel_holder = Avo::ItemsHolder.new
  main_panel_holder.items = panelless_items

  # Add that panel to the main panel
  main_panel = Avo::MainPanel.new
  main_panel.items_holder = main_panel_holder

  # Return all the items but this time with all the panelless ones inside the main panel
  [main_panel, *panelfull_items]
end

#get_tabsObject



148
149
150
# File 'lib/avo/concerns/has_fields.rb', line 148

def get_tabs
  tabs_holder
end

#hydrate_fields(model: nil, view: nil) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/avo/concerns/has_fields.rb', line 140

def hydrate_fields(model: nil, view: nil)
  fields.map do |field|
    field.hydrate(model: @model, view: @view, resource: self)
  end

  self
end

#tab_groupsObject



156
157
158
# File 'lib/avo/concerns/has_fields.rb', line 156

def tab_groups
  self.class.tab_groups
end

#toolsObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/avo/concerns/has_fields.rb', line 238

def tools
  check_license

  return [] if App.license.lacks_with_trial :resource_tools
  return [] if self.class.tools.blank?

  self.items
    .select do |item|
      next if item.nil?

      item.is_tool?
    end
    .map do |tool|
      tool.hydrate view: view
      tool
    end
    .select do |item|
      item.visible_on?(view)
    end
end