Module: Coupler::Models::CommonModel

Included in:
Comparison, Connection, Field, Import, Job, Matcher, Notification, Project, Resource, Result, Scenario, Transformation, Transformer
Defined in:
lib/coupler/models/common_model.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

@@versioned =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/coupler/models/common_model.rb', line 36

def self.included(base)
  base.extend(ClassMethods)
  base.raise_on_save_failure = false
  base.plugin :validation_helpers

  # decide whether or not to version this model
  versions_table_name = base.versions_table_name
  if base.db.tables.include?(versions_table_name)
    @@versioned[base] = versions_table_name
    base.send(:attr_accessor, :delete_versions_on_destroy)
  end
end

Instance Method Details

#after_destroyObject



83
84
85
86
87
88
89
# File 'lib/coupler/models/common_model.rb', line 83

def after_destroy
  super
  if @delete_versions_on_destroy && (versions_table_name = @@versioned[self.class])
    dataset = self.db[versions_table_name]
    dataset.filter(:current_id => id).delete
  end
end

#after_saveObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/coupler/models/common_model.rb', line 69

def after_save
  super
  if @skip_new_version
    @skip_new_version = nil
  else
    if versions_table_name = @@versioned[self.class]
      dataset = self.db[versions_table_name]
      hash = self.values.clone
      hash[:current_id] = hash.delete(:id)
      dataset.insert(hash)
    end
  end
end

#before_createObject



49
50
51
52
53
54
# File 'lib/coupler/models/common_model.rb', line 49

def before_create
  super
  now = Time.now
  self[:created_at] = now
  self[:updated_at] = now
end

#before_saveObject



62
63
64
65
66
67
# File 'lib/coupler/models/common_model.rb', line 62

def before_save
  super
  if @@versioned[self.class] && !@skip_new_version
    self[:version] = self[:version].nil? ? 1 : self[:version] + 1
  end
end

#before_updateObject



56
57
58
59
60
# File 'lib/coupler/models/common_model.rb', line 56

def before_update
  super
  now = Time.now
  self[:updated_at] = now
end

#save!(*args) ⇒ Object



91
92
93
94
95
96
# File 'lib/coupler/models/common_model.rb', line 91

def save!(*args)
  if !save(*args)
    raise "couldn't save: " + errors.full_messages.join("; ")
  end
  self
end

#touch!Object



98
99
100
101
# File 'lib/coupler/models/common_model.rb', line 98

def touch!
  @skip_new_version = true
  update(:last_accessed_at => Time.now)
end