Class: Binda::Repeater

Inherits:
ApplicationRecord show all
Includes:
FieldableAssociations
Defined in:
app/models/binda/repeater.rb

Instance Method Summary collapse

Methods included from FieldableAssociations

#find_or_create_a_field_by, #generate_fields

Instance Method Details

#set_default_positionobject

Set default position after create

This methods ensure that every repeater instance has an explicit position. The latest repeater created gets the highest position number. The first position is 1 (not 0).

Returns:

  • (object)

    Repeater instance



26
27
28
29
30
31
32
33
# File 'app/models/binda/repeater.rb', line 26

def set_default_position
    # apparently `self.fieldable != self.fieldable_type.constantize.find( self.fieldable_id )`
    # as the former has always one repeater, the latter has all repeaters created so far
    # that's way we use the longer version
    instance = self.fieldable_type.constantize.find( self.fieldable_id )
    position = instance.repeaters.find_all{ |r| r.field_setting_id == self.field_setting.id }.length
    self.update_attribute 'position', position
end