Class: EnumerationValue

Inherits:
JSONModel
  • Object
show all
Defined in:
backend/app/model/enumeration_value.rb,
frontend/app/models/enumeration_value.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handle_suppressed(ids, val) ⇒ Object



56
57
58
59
60
# File 'backend/app/model/enumeration_value.rb', line 56

def self.handle_suppressed(ids, val)
  obj = super
  Enumeration.broadcast_changes
  obj
end

Instance Method Details

#before_createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'backend/app/model/enumeration_value.rb', line 10

def before_create
  # bit clunky but this allows us to make sure that bulk updates are
  # positioned correctly
  unless self.position
    self.position = rand(100) + 1000000 # lets just give it a randomly high number
  end
  obj = super
  # now let's set it in the list
  100.times do
    DB.attempt {
      sibling = self.class.dataset.filter( :enumeration_id => self.enumeration_id).order(:position).last
      if sibling
        self.class.dataset.db[self.class.table_name].filter(:id => self.id ).update(:position => sibling[:position] + 1)
      else
        self.class.dataset.db[self.class.table_name].filter(:id => self.id ).update(:position => 0 )
      end
      return
    }.and_if_constraint_fails {
       # another transaction has slipped in...let's try again
    }
  end
  obj
end

#before_saveObject



34
35
36
37
38
# File 'backend/app/model/enumeration_value.rb', line 34

def before_save
  super

  self.value = self.value.strip
end

#update_position_only(target_position) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'backend/app/model/enumeration_value.rb', line 40

def update_position_only(target_position )
  # we need to swap places with what we're trying to replace.
  current_position = self.position
  sibling = self.class.dataset.filter( :enumeration_id => self.enumeration_id, :position => target_position ).first

  if sibling
    self.class.dataset.filter( :enumeration_id => self.enumeration_id, :position => target_position ).update(:position => Sequel.lit('position + 9999' ))
  end

  self.class.dataset.filter( :id => self.id ).update( :position => target_position )
  self.class.dataset.filter( :id => sibling.id ).update( :position => current_position ) if sibling
  self.enumeration.class.broadcast_changes

  target_position
end