2017-06-23 21:19:08 +08:00
|
|
|
class SampleMyModule < ApplicationRecord
|
2016-07-21 19:11:15 +08:00
|
|
|
after_create :increment_nr_of_module_samples
|
|
|
|
after_destroy :decrement_nr_of_module_samples
|
2016-09-21 21:35:23 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
validates :sample, :my_module, presence: true
|
|
|
|
|
|
|
|
# One sample can only be assigned once to a specific module
|
2017-06-28 21:21:32 +08:00
|
|
|
validates_uniqueness_of :sample_id, scope: :my_module_id
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :assigned_by,
|
|
|
|
foreign_key: 'assigned_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2017-06-28 21:29:10 +08:00
|
|
|
belongs_to :sample, inverse_of: :sample_my_modules, optional: true
|
|
|
|
belongs_to :my_module, inverse_of: :sample_my_modules, optional: true
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def increment_nr_of_module_samples
|
|
|
|
my_module.increment!(:nr_of_assigned_samples)
|
|
|
|
sample.increment!(:nr_of_modules_assigned_to)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrement_nr_of_module_samples
|
|
|
|
my_module.decrement!(:nr_of_assigned_samples)
|
|
|
|
sample.decrement!(:nr_of_modules_assigned_to)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|