Module: Jav::Concerns::HasFields

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

Instance Method Summary collapse

Instance Method Details

#fields(**args) ⇒ Object



154
155
156
# File 'lib/jav/concerns/has_fields.rb', line 154

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

#get_field(id) ⇒ Object



214
215
216
217
218
# File 'lib/jav/concerns/has_fields.rb', line 214

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

#get_field_definitions(only_root: false) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/jav/concerns/has_fields.rb', line 162

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

  return [] if fields.blank?

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

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



172
173
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
# File 'lib/jav/concerns/has_fields.rb', line 172

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

    # Strip out the reflection field in index queries with a parent association.
    # regular non-polymorphic association
    # we're matching the reflection inverse_of foriegn key with the field's foreign_key
    if reflection.present? && field.is_a?(Jav::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

    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.



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
289
290
291
# File 'lib/jav/concerns/has_fields.rb', line 238

def get_items
  panelless_items = []
  panelfull_items = []

  items.each do |item|
    item.hydrate(view: view) if item.respond_to? :hydrate

    # fields and tabs can be hidden on some views
    next if item.respond_to?(:visible_on?) && !item.visible_on?(view)

    # 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
    next if item.respond_to?(:authorized?) && !item.hydrate(model: @model).authorized?

    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(Jav::TabGroup).each do |tab_group|
    tab_group.items.grep(Jav::Tab).each do |tab|
      tab.items.grep(Jav::Panel).each do |panel|
        set_target_to_top panel.items.grep(Jav::Fields::BelongsToField)

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

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

  # Add that panel to the main panel
  main_panel = Jav::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



150
151
152
# File 'lib/jav/concerns/has_fields.rb', line 150

def get_tabs
  tabs_holder
end

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



142
143
144
145
146
147
148
# File 'lib/jav/concerns/has_fields.rb', line 142

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

  self
end

#tab_groupsObject



158
159
160
# File 'lib/jav/concerns/has_fields.rb', line 158

def tab_groups
  self.class.tab_groups
end

#toolsObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/jav/concerns/has_fields.rb', line 220

def 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 { |item| item.visible_on?(view) }
end