app/models/unidom/article_number/marking.rb in unidom-article_number-0.3 vs app/models/unidom/article_number/marking.rb in unidom-article_number-1.0
- old
+ new
@@ -2,16 +2,33 @@
class Unidom::ArticleNumber::Marking < ActiveRecord::Base
self.table_name = 'unidom_markings'
+ include Unidom::Common::Concerns::ModelExtension
+
belongs_to :barcode, polymorphic: true
belongs_to :marked, polymorphic: true
belongs_to :marker, polymorphic: true
scope :barcode_is, ->(barcode) { where barcode: barcode }
scope :marked_by, ->(marker) { where marker: marker }
scope :marked_is, ->(marked) { where marked: marked }
- include Unidom::Common::Concerns::ModelExtension
+ def self.mark(barcode: nil, marked: nil, marker: nil, opened_at: Time.now)
+
+ raise ArgumentError.new('Barcode is required.') if barcode.blank?
+ raise ArgumentError.new('Marked is required.' ) if marked.blank?
+
+ query = barcode_is(barcode).marked_is(marked).valid_at.alive
+ creation = { opened_at: opened_at }
+ if marker.present? && marker.respond_to?(:id)
+ creation[:marker] = marker
+ else
+ creation[:marker_id] = Unidom::Common::NULL_UUID
+ creation[:marker_type] = ''
+ end
+ query.first_or_create! creation
+
+ end
end