Class: EnumerationValue

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ASModel

all_models, included, update_publish_flag, update_suppressed_flag

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_repository, strict_mode, strict_mode?, with_repository

Class Method Details

+ (Object) handle_suppressed(ids, val)



50
51
52
53
54
# File 'backend/app/model/enumeration_value.rb', line 50

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

Instance Method Details

- (Object) before_create



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

- (Object) update_position_only(target_position)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'backend/app/model/enumeration_value.rb', line 34

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