1
0
Fork 0

Add model Address

master
Alex Kotov 2 years ago
parent e4c1739fd9
commit 307a1bedfa
  1. 27
      app/models/address.rb
  2. 13
      db/migrate/20210310111839_create_addresses.rb
  3. 12
      db/schema.rb
  4. 7
      test/models/address_test.rb

@ -0,0 +1,27 @@
class Address < ApplicationRecord
scope :added_to_wallet, -> { where added_to_wallet: true }
scope :to_update_op_return, lambda {
added_to_wallet.where(
Address.arel_table[:op_return_updated_at].eq(nil)
.or(Address.arel_table[:op_return_updated_at].lt(1.hour.ago)),
)
}
validates :value, presence: true, uniqueness: true
def update_op_return?
added_to_wallet? && (
op_return_updated_at == nil ||
op_return_updated_at < 1.hour.ago
)
end
def add_to_wallet
AddAddressToWallet.perform_later self unless added_to_wallet?
end
def update_op_return
UpdateAddressOpReturn.perform_later self if update_op_return?
end
end

@ -0,0 +1,13 @@
class CreateAddresses < ActiveRecord::Migration[6.1]
def change
create_table :addresses do |t|
t.timestamps
t.string :value, null: false
t.boolean :added_to_wallet, null: false, default: false
t.string :op_return_value
t.datetime :op_return_updated_at
end
add_index :addresses, :value, unique: true
end
end

12
db/schema.rb generated

@ -10,7 +10,17 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_03_10_102531) do
ActiveRecord::Schema.define(version: 2021_03_10_111839) do
create_table "addresses", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "value", null: false
t.boolean "added_to_wallet", default: false, null: false
t.string "op_return_value"
t.datetime "op_return_updated_at"
t.index ["value"], name: "index_addresses_on_value", unique: true
end
create_table "followships", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false

@ -0,0 +1,7 @@
require "test_helper"
class AddressTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end