Class: MegaBar::Layable

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/mega_bar/layable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deterministic_id(layout_id, layout_section_id) ⇒ Object

Deterministic ID generation for Layables ID range: 20000-20999



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/mega_bar/layable.rb', line 12

def self.deterministic_id(layout_id, layout_section_id)
  # Use layout_id and layout_section_id to create unique identifier
  identifier = "#{layout_id}_#{layout_section_id}"
  hash = Digest::MD5.hexdigest(identifier)
  base_id = 20000 + (hash.to_i(16) % 1000)
  
  # Check for collisions and increment if needed
  while MegaBar::Layable.exists?(id: base_id)
    base_id += 1
    break if base_id >= 21000  # Don't overflow into next range
  end
  
  base_id
end

Instance Method Details

#delete_dependentsObject



27
28
29
30
# File 'app/models/mega_bar/layable.rb', line 27

def delete_dependents
 #only destroy layoutsections if no other layables use it.
 LayoutSection.destroy(self.layout_section_id) if Layable.where(layout_section_id: self.layout_section_id).blank?
end