Module: ASModel::SequelHooks

Defined in:
backend/app/model/ASModel_sequel.rb

Overview

Hooks for firing behaviour on Sequel::Model events

Defined Under Namespace

Modules: BlobHack

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



5
6
7
# File 'backend/app/model/ASModel_sequel.rb', line 5

def self.included(base)
  base.extend(BlobHack)
end

Instance Method Details

- (Object) _save_refresh

We can save quite a lot of database chatter by only refreshing our top-level records upon save. Pure-nested records don’t need refreshing, so skip them.



13
14
15
16
17
# File 'backend/app/model/ASModel_sequel.rb', line 13

def _save_refresh
  if self.class.respond_to?(:has_jsonmodel?) && self.class.has_jsonmodel? && self.class.my_jsonmodel.schema['uri']
    _refresh(this.opts[:server] ? this : this.server(:default))
  end
end

- (Object) around_save



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'backend/app/model/ASModel_sequel.rb', line 38

def around_save
  values_to_reapply = {}

  self.class.blob_columns_to_fix.each do |column|
    if self[column]
      values_to_reapply[column] = self[column]
      self[column] = "around_save_placeholder"
    end
  end

  ret = super

  if !values_to_reapply.empty?
    ps = self.class.dataset.where(:id => self.id).prepare(:update, :update_blobs,
                                                         Hash[values_to_reapply.keys.map {|c| [c, :"$#{c}"]}])

    ps.call(Hash[values_to_reapply.map {|k, v| [k, DB.blobify(v)]}])

    self.refresh
  end

  ret
end

- (Object) before_create



19
20
21
22
23
24
25
26
# File 'backend/app/model/ASModel_sequel.rb', line 19

def before_create
  if RequestContext.get(:current_username)
    self.created_by = self.last_modified_by = RequestContext.get(:current_username)
  end
  self.create_time = Time.now
  self.system_mtime = self.user_mtime = Time.now
  super
end

- (Object) before_update



29
30
31
32
33
34
35
# File 'backend/app/model/ASModel_sequel.rb', line 29

def before_update
  if RequestContext.get(:current_username)
    self.last_modified_by = RequestContext.get(:current_username)
  end
  self.system_mtime = Time.now
  super
end