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
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #code ⇒ Object readonly
- #item_id ⇒ Object readonly
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 Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#code ⇒ Object (readonly)
31 |
# File 'app/models/amazon_transparency_code.rb', line 31 validates :item_id, :code, presence: true |
#item_id ⇒ Object (readonly)
31 |
# File 'app/models/amazon_transparency_code.rb', line 31 validates :item_id, :code, presence: true |
Class Method Details
.claim(item_id, how_many = 1) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'app/models/amazon_transparency_code.rb', line 55 def self.claim(item_id, how_many = 1) atcs = unclaimed.where(item_id: item_id).limit(how_many) raise CodeNotAvailable, "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
45 46 47 48 49 |
# File 'app/models/amazon_transparency_code.rb', line 45 def claim! raise CodeAlreadyClaimed, "#{code} already claimed!" if claimed_on.present? update_attribute(:claimed_on, Time.current) end |
#claimed? ⇒ Boolean
41 42 43 |
# File 'app/models/amazon_transparency_code.rb', line 41 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
51 52 53 |
# File 'app/models/amazon_transparency_code.rb', line 51 def unclaim! update_attribute(:claimed_on, nil) end |