Class: AmazonTransparencyCode
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- AmazonTransparencyCode
- Includes:
- Models::Auditable
- Defined in:
- app/models/amazon_transparency_code.rb
Overview
== Schema Information
Table name: amazon_transparency_codes
Database name: primary
id :integer not null, primary key
claimed_on :datetime
code :string(100) not null
created_at :datetime
updated_at :datetime
creator_id :integer
item_id :integer not null
updater_id :integer
Indexes
index_amazon_transparency_codes_on_claimed_on (claimed_on)
index_amazon_transparency_codes_on_code (code) UNIQUE
index_amazon_transparency_codes_on_item_id (item_id)
Foreign Keys
fk_rails_... (item_id => items.id) ON DELETE => cascade
Defined Under Namespace
Classes: CodeAlreadyClaimed, CodeNotAvailable
Constant Summary
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Belongs to collapse
Methods included from Models::Auditable
Class Method Summary collapse
- .claim(item_id, how_many = 1) ⇒ Object
-
.unclaimed ⇒ ActiveRecord::Relation<AmazonTransparencyCode>
A relation of AmazonTransparencyCodes that are unclaimed.
Instance Method Summary collapse
Methods included from Models::Auditable
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Models::EventPublishable
Class Method Details
.claim(item_id, how_many = 1) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'app/models/amazon_transparency_code.rb', line 52 def self.claim(item_id, how_many = 1) atcs = unclaimed.where(item_id: item_id).limit(how_many) raise CodeNotAvailable.new("Unable to claim requested quantity, only #{atcs.size} available") if atcs.size != how_many transaction do atcs.each(&:claim!) end atcs.map(&:code) end |
.unclaimed ⇒ ActiveRecord::Relation<AmazonTransparencyCode>
A relation of AmazonTransparencyCodes that are unclaimed. Active Record Scope
34 |
# File 'app/models/amazon_transparency_code.rb', line 34 scope :unclaimed, -> { where(claimed_on: nil).order(:created_at) } |
Instance Method Details
#claim! ⇒ Object
43 44 45 46 |
# File 'app/models/amazon_transparency_code.rb', line 43 def claim! raise CodeAlreadyClaimed.new("#{code} already claimed!") if claimed_on.present? update_attribute(:claimed_on, Time.current) end |
#claimed? ⇒ Boolean
39 40 41 |
# File 'app/models/amazon_transparency_code.rb', line 39 def claimed? claimed_on.present? end |
#item ⇒ Item
29 |
# File 'app/models/amazon_transparency_code.rb', line 29 belongs_to :item, inverse_of: :amazon_transparency_codes, optional: true |
#unclaim! ⇒ Object
48 49 50 |
# File 'app/models/amazon_transparency_code.rb', line 48 def unclaim! update_attribute(:claimed_on, nil) end |