app/models/binda/field_group.rb in binda-0.1.5 vs app/models/binda/field_group.rb in binda-0.1.6

- old
+ new

@@ -18,11 +18,11 @@ # Slug extend FriendlyId friendly_id :default_slug, use: [:slugged, :finders] - after_create :update_position + after_create :set_default_position # Friendly id preference on slug generation # # Method inherited from friendly id # @see https://github.com/norman/friendly_id/issues/436 @@ -56,13 +56,15 @@ end end private - def update_position - if self.position.nil? - self.update_attribute('position', self.structure.field_groups.length) - end + # Set a default position if isn't set and updates all related field settings + # Update all field settings related to the one created + def set_default_position + FieldGroup + .where(structure_id: self.structure_id) + .each{|field_group| field_group.increment(:position).save!} end end end