Module: Avo::Concerns::HasItems

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#items_holderObject



20
21
22
# File 'lib/avo/concerns/has_items.rb', line 20

def items_holder
  @items_holder || Avo::Resources::Items::Holder.new
end

Instance Method Details

#fields(**args) ⇒ Object



40
41
42
# File 'lib/avo/concerns/has_items.rb', line 40

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

#get_field(id) ⇒ Object



168
169
170
171
172
# File 'lib/avo/concerns/has_items.rb', line 168

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

#get_field_definitions(only_root: false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/avo/concerns/has_items.rb', line 97

def get_field_definitions(only_root: false)
  only_fields(only_root:).map do |field|
    # When nested field hydrate the field with the nested resource
    resource = if field.try(:nested_on?, view)
      Avo.resource_manager.get_resource_by_model_class(model_class.reflections[field.id.to_s].klass)
        .new(view:, params:)
        .detect_fields
    else
      self
    end

    field.hydrate(resource:, user:, view:, record: resource.record)
  end
end

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/avo/concerns/has_items.rb', line 118

def get_fields(panel: nil, reflection: nil, only_root: false)
  fields = get_field_definitions(only_root: only_root)
    .select do |field|
      # Get the fields for this view
      field.visible_in_view?(view: 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 fields
  fields.map do |field|
    field.dup.hydrate(record: @record, view: @view, resource: self)
  end
end

#get_itemsObject



174
175
176
177
178
179
180
181
182
183
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
# File 'lib/avo/concerns/has_items.rb', line 174

def get_items
  # Each group is built only by standalone items or items that have their own panel, keeping the items order
  grouped_items = visible_items.slice_when do |prev, curr|
    # Slice when the item type changes from standalone to panel or vice-versa
    is_standalone?(prev) != is_standalone?(curr)
  end.to_a.map do |group|
    {elements: group, is_standalone: is_standalone?(group.first)}
  end

  # Add the header automatically as first item if the user didn't define one
  # This is to ensure that the header is always present
  if items.none? { |item| item.is_header? }
    header = Avo::Resources::Items::Header.new
    hydrate_item header
    grouped_items.unshift({elements: [header], is_standalone: false})
  end

  # For each standalone group, wrap items in a panel and card
  # If the resource has at least one panel defined, we compute nothing, user took control of the panels
  if items.none? { |item| item.is_panel? }
    grouped_items.select { |group| group[:is_standalone] }.each do |group|
      calculated_panel = Avo::Resources::Items::Panel.new
      hydrate_item calculated_panel

      # Create a card
      card = Avo::Resources::Items::Card.new
      hydrate_item card

      # Add the items to the card
      card.items_holder.items = group[:elements]

      # Add the card to the main panel
      calculated_panel.items_holder.items = [card]

      group[:elements] = calculated_panel
    end
  end

  grouped_items.flat_map { |group| group[:elements] }
end

#get_preview_fieldsObject



112
113
114
115
116
# File 'lib/avo/concerns/has_items.rb', line 112

def get_preview_fields
  get_field_definitions.select do |field|
    field.visible_in_view?(view: :preview)
  end
end

#invalid_fieldsObject

def items

items_holder.items

end



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/avo/concerns/has_items.rb', line 28

def invalid_fields
  invalid_fields = items_holder.invalid_fields

  items_holder.items.each do |item|
    if item.respond_to? :items
      invalid_fields += item.invalid_fields
    end
  end

  invalid_fields
end

#is_empty?Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/avo/concerns/has_items.rb', line 290

def is_empty?
  visible_items.blank?
end

#itemsObject



215
216
217
# File 'lib/avo/concerns/has_items.rb', line 215

def items
  items_holder&.items || []
end

#only_fields(only_root: false) ⇒ Object

Dives deep into panels and tabs to fetch all the fields for a resource.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/avo/concerns/has_items.rb', line 49

def only_fields(only_root: false)
  fields = []

  items.each do |item|
    next if item.nil?

    if only_root
      # When only_root == true we want to extract the panel and card items
      if item.is_panel? || item.is_card?
        if item.visible_in_view?(view: view)
          fields << extract_fields(item)
        end
      end
    else
      # Dive into panels to fetch their fields
      if item.is_panel?
        fields << extract_fields(item)
      end

      # Dive into tabs to fetch their fields
      if item.is_tab_group?
        item.items.map do |tab|
          fields << extract_fields(tab)
        end
      end

      # Dive into sidebar to fetch their fields
      if item.is_sidebar?
        fields << extract_fields(item)
      end

      if item.is_card?
        fields << extract_fields(item)
      end
    end

    if item.is_field?
      fields << item
    end

    if item.is_row?
      fields << extract_fields(item)
    end
  end

  fields.flatten
end

#tab_groupsObject



44
45
46
# File 'lib/avo/concerns/has_items.rb', line 44

def tab_groups
  self.class.tab_groups
end

#visible_itemsObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
# File 'lib/avo/concerns/has_items.rb', line 219

def visible_items
  items
    .map do |item|
      hydrate_item item

      if item.is_a? Avo::Resources::Items::TabGroup
        # Set the target to _top for all belongs_to fields in the tab group
        item.items.grep(Avo::Resources::Items::Tab).each do |tab|
          tab.items.grep(Avo::Resources::Items::Panel).each do |panel|
            set_target_to_top panel.items.grep(Avo::Fields::BelongsToField)

            panel.items.grep(Avo::Resources::Items::Row).each do |row|
              set_target_to_top row.items.grep(Avo::Fields::BelongsToField)
            end
          end
        end
      end

      item
    end
    .select do |item|
      item.visible?
    end
    .select do |item|
      if item.respond_to?(:visible_in_view?)
        item.visible_in_view? view: view
      else
        true
      end
    end
    .select do |item|
      # Check if record has the setter method
      # Next if the view is not on forms
      next true if !view.in?(%w[edit update new create])

      # Skip items that don't have an id
      next true if !item.respond_to?(:id)

      # Skip tab groups and tabs
      # Skip headings
      # Skip location fields
      # On location field we can have field coordinates and setters with different names
      #   like latitude and longitude
      next true if item.is_a?(Avo::Resources::Items::TabGroup) ||
        item.is_a?(Avo::Resources::Items::Tab) ||
        item.is_heading? ||
        item.is_a?(Avo::Fields::LocationField) ||
        item.is_header?

      # Skip nested fields
      next true if item.try(:nested_on?, view)

      # When the resource is a form, we want to show all items even if there is no setter method
      next true if defined?(Avo::Forms::Core::Resources::FormResource) && try(:resource).is_a?(Avo::Forms::Core::Resources::FormResource)

      item.resource.record.respond_to?(:"#{item.try(:for_attribute) || item.id}=")
    end
    .select do |item|
      # Check if the user is authorized to view it.
      # This is usually used for has_* fields
      if item.respond_to? :authorized?
        item.authorized?
      else
        true
      end
    end
    .select do |item|
      !item.is_a?(Avo::Resources::Items::Sidebar)
    end.compact
end