Sha256: 61eca84c55745b1e9e67d277d2e896829b8dfc4aef25064a658d157f88ea42b0

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# Marking 是条码和物品的标记关系。

class Unidom::ArticleNumber::Marking < Unidom::ArticleNumber::ApplicationRecord

  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  }

  def self.mark!(barcode: nil, marked: nil, marker: nil, opened_at: Time.now)

    raise ArgumentError.new('The barcode argument is required.'  ) if barcode.blank?
    raise ArgumentError.new('The marked argument is required.'   ) if marked.blank?
    raise ArgumentError.new('The opened_at argument is required.') if opened_at.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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unidom-article_number-2.2 app/models/unidom/article_number/marking.rb
unidom-article_number-2.1 app/models/unidom/article_number/marking.rb
unidom-article_number-2.0.2 app/models/unidom/article_number/marking.rb
unidom-article_number-2.0.1 app/models/unidom/article_number/marking.rb