bitcoin_active_record 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +16 -16
  4. data/bitcoin_active_record.gemspec +1 -1
  5. data/lib/bitcoin_active_record/client.rb +11 -11
  6. data/lib/bitcoin_active_record/models/bitcoin_transaction.rb +18 -0
  7. data/lib/bitcoin_active_record/models/btc_address.rb +2 -2
  8. data/lib/bitcoin_active_record/models/received_bitcoin_transaction.rb +11 -0
  9. data/lib/bitcoin_active_record/models/sent_bitcoin_transaction.rb +10 -0
  10. data/lib/generators/templates/create_bitcoin_active_record_models.rb +10 -10
  11. data/lib/generators/templates/models/bitcoin_transaction.rb +3 -0
  12. data/lib/generators/templates/models/received_bitcoin_transaction.rb +3 -0
  13. data/lib/generators/templates/models/sent_bitcoin_transaction.rb +3 -0
  14. data/test/Gemfile.lock +1 -1
  15. data/test/app/models/bitcoin_transaction.rb +3 -0
  16. data/test/app/models/received_bitcoin_transaction.rb +3 -0
  17. data/test/app/models/sent_bitcoin_transaction.rb +3 -0
  18. data/test/db/migrate/20150429175618_create_bitcoin_active_record_models.rb +32 -0
  19. data/test/db/schema.rb +18 -18
  20. data/test/test/integration/bitcoin_payments/client_test.rb +19 -19
  21. data/test/test/integration/bitcoin_payments/generators_test.rb +2 -2
  22. metadata +16 -20
  23. data/lib/bitcoin_active_record/models/payment.rb +0 -18
  24. data/lib/bitcoin_active_record/models/received_payment.rb +0 -11
  25. data/lib/bitcoin_active_record/models/sent_payment.rb +0 -10
  26. data/lib/generators/templates/models/payment.rb +0 -3
  27. data/lib/generators/templates/models/received_payment.rb +0 -3
  28. data/lib/generators/templates/models/sent_payment.rb +0 -3
  29. data/test/app/models/.keep +0 -0
  30. data/test/app/models/concerns/.keep +0 -0
  31. data/test/app/models/payment.rb +0 -3
  32. data/test/app/models/received_payment.rb +0 -3
  33. data/test/app/models/sent_payment.rb +0 -3
  34. data/test/db/migrate/20150425185338_create_bitcoin_active_record_models.rb +0 -32
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d85004edd0e6c8f4afb2d16048160b52ca64eca1
4
- data.tar.gz: fb52ffaedf41521b369f369ad029582afe7f2421
3
+ metadata.gz: 209f93b703bd8f1e18fb444af7cf2946fe0cca2b
4
+ data.tar.gz: 28fae90950ef7feba274d8694c70828d4175e2a2
5
5
  SHA512:
6
- metadata.gz: 7eda7034c8fd306f05d6d2bb48e90d216f9b08b73514f098bccc2b1b9688d306f7a665543a7d1ea7b06f256d1be7ff4b9a17b241de1f3fc8797a21a4ec718320
7
- data.tar.gz: 3f0a7968539441466ec1e2bd41bda7a95bc66fa9cd083dc58b400e46bfc0421e3eec3afbc43016bda79c703e873c48e73337872618f3c044f12b27d4738d12f1
6
+ metadata.gz: a0594a50dd7ed631e2e48bd9d909331e5c7dc18e8039821031820f9d78869db8935dbcab919755eb5792f3d2cf99ef93f481492b135c5e26a1e825b7ab6c0f0a
7
+ data.tar.gz: dd1b31153534e7f1e53ea9e459101de9ed2600f231f9c426c154f9ef0bc07a87c19fb84603022cf31c7a389dfc46f281f6b7b37223b0ea1e14c9f7600afe9007
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bitcoin_active_record (0.0.2)
4
+ bitcoin_active_record (0.1.0)
5
5
  auto_strip_attributes (>= 2.0.4)
6
6
  httparty (>= 0.13.1)
7
7
  script_utils (= 0.0.3)
data/README.md CHANGED
@@ -25,8 +25,8 @@ client = BitcoinActiveRecord::Client.new(
25
25
  },
26
26
  )
27
27
  ```
28
- * Run client.create_received_payments to create records of received payments
29
- * Run client.pay(public_key: public_key, amount: BigDecimal.new(1), comment: 'hello') to send a payment and save a record of it in the database
28
+ * Run client.create_received_bitcoin_transactions to create records of received transactions
29
+ * Run client.pay(public_key: public_key, amount: BigDecimal.new(1), comment: 'hello') to send a transaction and save a record of it in the database
30
30
 
31
31
  ## Usage
32
32
 
@@ -38,7 +38,7 @@ column_name | type | description
38
38
  --- | --- | ---
39
39
  public_key | string | public key of a bitcoin address
40
40
 
41
- payment.rb
41
+ bitcoin_transaction.rb
42
42
 
43
43
  column_name | type | description
44
44
  --- | --- | ---
@@ -46,20 +46,20 @@ btc_address_id | integer | foreign key
46
46
  amount | decimal | transaction amount
47
47
  txid | string | bitcoin transaction id
48
48
 
49
- received_payment.rb
49
+ received_bitcoin_transaction.rb
50
50
 
51
51
  column_name | type | description
52
52
  --- | --- | ---
53
- payment_id | integer | The btc address associated with this payment id is the address of the person who sent the payment
54
- btc_address_id | integer | This is the address in your wallet that the payment was sent to
53
+ bitcoin_transaction_id | integer | The btc address associated with this bitcoin transaction id is the address of the person who sent the transaction
54
+ btc_address_id | integer | This is the address in your wallet that the transaction was sent to
55
55
 
56
- sent_payment.rb
56
+ sent_bitcoin_transaction.rb
57
57
 
58
58
  column_name | type | description
59
59
  --- | --- | ---
60
- payment_id | integer | foreign key
60
+ bitcoin_transaction_id | integer | foreign key
61
61
 
62
- Sent payments are only recorded if you send them using the gem's api.
62
+ Sent transactions are only recorded if you send them using the gem's api.
63
63
 
64
64
  ### BitcoinActiveRecord::Client
65
65
 
@@ -73,7 +73,7 @@ client = BitcoinActiveRecord::Client.new(
73
73
  username: '',
74
74
  password: '',
75
75
  },
76
- # optional, amounts less than this will be ignored when running create_received_payments
76
+ # optional, amounts less than this will be ignored when running create_received_bitcoin_transactions
77
77
  minimum_amount: BigDecimal.new('0.001'),
78
78
  # optional, the wallet account you want to look for received transactions in
79
79
  account: :foo,
@@ -99,7 +99,7 @@ Get the public key of the transaction sender for a transaction with id txid
99
99
 
100
100
  * client.pay
101
101
 
102
- Pay someone and save a record of it as a SentPayment
102
+ Pay someone and save a record of it as a SentBitcoinTransaction
103
103
 
104
104
  ```ruby
105
105
  client.pay(
@@ -109,13 +109,13 @@ client.pay(
109
109
  amount: BigDecimal.new('1.23'),
110
110
  # optional, transaction comment
111
111
  comment: 'foo',
112
- ) do |sent_payment|
112
+ ) do |sent_bitcoin_transaction|
113
113
  # adding a block is optional
114
- # modify the sent_payment record before it gets saved
115
- sent_payment.custom_attribute = 'foo'
114
+ # modify the sent_bitcoin_transaction record before it gets saved
115
+ sent_bitcoin_transaction.custom_attribute = 'foo'
116
116
  end
117
117
  ```
118
118
 
119
- * client.create_received_payments
119
+ * client.create_received_bitcoin_transactions
120
120
 
121
- Create a ReceivedPayment model for every transaction from client.account that's >= client.minimum_amount. If the latest transaction is already in the database it will assume all earlier transactions have already been saved.
121
+ Create a ReceivedBitcoinTransaction model for every transaction from client.account that's >= client.minimum_amount. If the latest transaction is already in the database it will assume all earlier transactions have already been saved.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'bitcoin_active_record'
3
- gem.version = '0.0.2'
3
+ gem.version = '0.1.0'
4
4
  gem.summary = 'bitcoin activerecord integration'
5
5
  gem.author = 'Lihan Li'
6
6
  gem.email = 'frankieteardrop@gmail.com'
@@ -57,21 +57,21 @@ module BitcoinActiveRecord
57
57
  def pay(public_key: raise('public key required'), amount: raise('amount required'), comment: '')
58
58
  raise('amount cant be zero or negative') unless amount > 0
59
59
 
60
- sent_payment = SentPayment.new(
61
- payment: Payment.new(
60
+ sent_bitcoin_transaction = SentBitcoinTransaction.new(
61
+ bitcoin_transaction: BitcoinTransaction.new(
62
62
  btc_address: BtcAddress.find_or_initialize_by(public_key: public_key),
63
63
  amount: amount,
64
64
  txid: request(:sendtoaddress, public_key, amount, comment),
65
65
  ),
66
66
  )
67
- yield(sent_payment) if block_given?
67
+ yield(sent_bitcoin_transaction) if block_given?
68
68
 
69
- sent_payment.save!
69
+ sent_bitcoin_transaction.save!
70
70
 
71
- sent_payment
71
+ sent_bitcoin_transaction
72
72
  end
73
73
 
74
- def create_received_payments
74
+ def create_received_bitcoin_transactions
75
75
  page = 0
76
76
 
77
77
  while true
@@ -80,14 +80,14 @@ module BitcoinActiveRecord
80
80
 
81
81
  transactions.each do |transaction|
82
82
  txid = transaction['txid']
83
- # already created payment for this transaction and all older ones
84
- return if Payment.where(txid: txid).count > 0
83
+ # already created record for this transaction and all older ones
84
+ return if BitcoinTransaction.where(txid: txid).count > 0
85
85
 
86
86
  amount = transaction['amount']
87
87
  from_key = get_sender_address(txid)
88
88
 
89
- received_payment = ReceivedPayment.create!(
90
- payment: Payment.new(
89
+ received_bitcoin_transaction = ReceivedBitcoinTransaction.create!(
90
+ bitcoin_transaction: BitcoinTransaction.new(
91
91
  # payment from this address
92
92
  btc_address: BtcAddress.find_or_initialize_by(
93
93
  public_key: from_key
@@ -99,7 +99,7 @@ module BitcoinActiveRecord
99
99
  btc_address: BtcAddress.find_or_initialize_by(public_key: transaction['address']),
100
100
  )
101
101
 
102
- puts("Received payment #{amount} BTC from #{from_key}: #{received_payment.inspect}")
102
+ puts("Received transaction #{amount} BTC from #{from_key}: #{received_bitcoin_transaction.inspect}")
103
103
  end
104
104
 
105
105
  page += 1
@@ -0,0 +1,18 @@
1
+ module BitcoinActiveRecord::Models::BitcoinTransaction
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ # sender's address for received transactions
6
+ # payee address for sent transactions
7
+ belongs_to(:btc_address, inverse_of: :bitcoin_transactions)
8
+
9
+ has_one(:received_bitcoin_transaction, inverse_of: :bitcoin_transaction)
10
+ has_one(:sent_bitcoin_transaction, inverse_of: :bitcoin_transaction)
11
+
12
+ validates(:txid, :btc_address, :amount, presence: true)
13
+ validates(:amount, numericality: { greater_than_or_equal_to: 0 })
14
+ validates(:txid, uniqueness: true)
15
+
16
+ auto_strip_attributes(:txid)
17
+ end
18
+ end
@@ -2,8 +2,8 @@ module BitcoinActiveRecord::Models::BtcAddress
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- has_many(:payments, inverse_of: :btc_address)
6
- has_many(:received_payments, inverse_of: :btc_address)
5
+ has_many(:bitcoin_transactions, inverse_of: :btc_address)
6
+ has_many(:received_bitcoin_transactions, inverse_of: :btc_address)
7
7
 
8
8
  validates(:public_key, presence: true, uniqueness: { case_sensitive: true })
9
9
 
@@ -0,0 +1,11 @@
1
+ module BitcoinActiveRecord::Models::ReceivedBitcoinTransaction
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ belongs_to(:bitcoin_transaction, inverse_of: :received_bitcoin_transaction, dependent: :destroy)
6
+ belongs_to(:btc_address, inverse_of: :received_bitcoin_transactions) # payment to this address
7
+
8
+ validates(:bitcoin_transaction, presence: true)
9
+ validates(:bitcoin_transaction_id, uniqueness: true)
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module BitcoinActiveRecord::Models::SentBitcoinTransaction
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ belongs_to(:bitcoin_transaction, inverse_of: :sent_bitcoin_transaction, dependent: :destroy)
6
+
7
+ validates(:bitcoin_transaction, presence: true)
8
+ validates(:bitcoin_transaction_id, uniqueness: true)
9
+ end
10
+ end
@@ -1,13 +1,13 @@
1
1
  class CreateBitcoinActiveRecordModels < ActiveRecord::Migration
2
2
  def change
3
- create_table "payments" do |t|
3
+ create_table "bitcoin_transactions" do |t|
4
4
  t.integer "btc_address_id", null: false
5
5
  t.decimal "amount", null: false
6
6
  t.string "txid", null: false
7
7
  t.timestamps
8
8
  end
9
- add_index "payments", ["btc_address_id"]
10
- add_index "payments", ["txid"], unique: true
9
+ add_index "bitcoin_transactions", ["btc_address_id"]
10
+ add_index "bitcoin_transactions", ["txid"], unique: true
11
11
 
12
12
  create_table "btc_addresses" do |t|
13
13
  t.string "public_key", null: false
@@ -15,18 +15,18 @@ class CreateBitcoinActiveRecordModels < ActiveRecord::Migration
15
15
  end
16
16
  add_index "btc_addresses", ["public_key"], unique: true
17
17
 
18
- create_table "received_payments" do |t|
19
- t.integer "payment_id", null: false
18
+ create_table "received_bitcoin_transactions" do |t|
19
+ t.integer "bitcoin_transaction_id", null: false
20
20
  t.integer "btc_address_id", null: false
21
21
  t.timestamps
22
22
  end
23
- add_index "received_payments", ["payment_id"], unique: true
24
- add_index :received_payments, :btc_address_id
23
+ add_index :received_bitcoin_transactions, :bitcoin_transaction_id, unique: true
24
+ add_index :received_bitcoin_transactions, :btc_address_id
25
25
 
26
- create_table "sent_payments" do |t|
27
- t.integer "payment_id", null: false
26
+ create_table "sent_bitcoin_transactions" do |t|
27
+ t.integer "bitcoin_transaction_id", null: false
28
28
  t.timestamps
29
29
  end
30
- add_index "sent_payments", ["payment_id"], unique: true
30
+ add_index "sent_bitcoin_transactions", ["bitcoin_transaction_id"], unique: true
31
31
  end
32
32
  end
@@ -0,0 +1,3 @@
1
+ class BitcoinTransaction < ActiveRecord::Base
2
+ bitcoin_active_record_model
3
+ end
@@ -0,0 +1,3 @@
1
+ class ReceivedBitcoinTransaction < ActiveRecord::Base
2
+ bitcoin_active_record_model
3
+ end
@@ -0,0 +1,3 @@
1
+ class SentBitcoinTransaction < ActiveRecord::Base
2
+ bitcoin_active_record_model
3
+ end
data/test/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- bitcoin_active_record (0.0.1)
4
+ bitcoin_active_record (0.1.0)
5
5
  auto_strip_attributes (>= 2.0.4)
6
6
  httparty (>= 0.13.1)
7
7
  script_utils (= 0.0.3)
@@ -0,0 +1,3 @@
1
+ class BitcoinTransaction < ActiveRecord::Base
2
+ bitcoin_active_record_model
3
+ end
@@ -0,0 +1,3 @@
1
+ class ReceivedBitcoinTransaction < ActiveRecord::Base
2
+ bitcoin_active_record_model
3
+ end
@@ -0,0 +1,3 @@
1
+ class SentBitcoinTransaction < ActiveRecord::Base
2
+ bitcoin_active_record_model
3
+ end
@@ -0,0 +1,32 @@
1
+ class CreateBitcoinActiveRecordModels < ActiveRecord::Migration
2
+ def change
3
+ create_table "bitcoin_transactions" do |t|
4
+ t.integer "btc_address_id", null: false
5
+ t.decimal "amount", null: false
6
+ t.string "txid", null: false
7
+ t.timestamps
8
+ end
9
+ add_index "bitcoin_transactions", ["btc_address_id"]
10
+ add_index "bitcoin_transactions", ["txid"], unique: true
11
+
12
+ create_table "btc_addresses" do |t|
13
+ t.string "public_key", null: false
14
+ t.timestamps
15
+ end
16
+ add_index "btc_addresses", ["public_key"], unique: true
17
+
18
+ create_table "received_bitcoin_transactions" do |t|
19
+ t.integer "bitcoin_transaction_id", null: false
20
+ t.integer "btc_address_id", null: false
21
+ t.timestamps
22
+ end
23
+ add_index :received_bitcoin_transactions, :bitcoin_transaction_id, unique: true
24
+ add_index :received_bitcoin_transactions, :btc_address_id
25
+
26
+ create_table "sent_bitcoin_transactions" do |t|
27
+ t.integer "bitcoin_transaction_id", null: false
28
+ t.timestamps
29
+ end
30
+ add_index "sent_bitcoin_transactions", ["bitcoin_transaction_id"], unique: true
31
+ end
32
+ end
data/test/db/schema.rb CHANGED
@@ -11,43 +11,43 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20150425185338) do
14
+ ActiveRecord::Schema.define(version: 20150429175618) do
15
15
 
16
- create_table "btc_addresses", force: :cascade do |t|
17
- t.string "public_key", null: false
16
+ create_table "bitcoin_transactions", force: :cascade do |t|
17
+ t.integer "btc_address_id", null: false
18
+ t.decimal "amount", null: false
19
+ t.string "txid", null: false
18
20
  t.datetime "created_at"
19
21
  t.datetime "updated_at"
20
22
  end
21
23
 
22
- add_index "btc_addresses", ["public_key"], name: "index_btc_addresses_on_public_key", unique: true
24
+ add_index "bitcoin_transactions", ["btc_address_id"], name: "index_bitcoin_transactions_on_btc_address_id"
25
+ add_index "bitcoin_transactions", ["txid"], name: "index_bitcoin_transactions_on_txid", unique: true
23
26
 
24
- create_table "payments", force: :cascade do |t|
25
- t.integer "btc_address_id", null: false
26
- t.decimal "amount", null: false
27
- t.string "txid", null: false
27
+ create_table "btc_addresses", force: :cascade do |t|
28
+ t.string "public_key", null: false
28
29
  t.datetime "created_at"
29
30
  t.datetime "updated_at"
30
31
  end
31
32
 
32
- add_index "payments", ["btc_address_id"], name: "index_payments_on_btc_address_id"
33
- add_index "payments", ["txid"], name: "index_payments_on_txid", unique: true
33
+ add_index "btc_addresses", ["public_key"], name: "index_btc_addresses_on_public_key", unique: true
34
34
 
35
- create_table "received_payments", force: :cascade do |t|
36
- t.integer "payment_id", null: false
37
- t.integer "btc_address_id", null: false
35
+ create_table "received_bitcoin_transactions", force: :cascade do |t|
36
+ t.integer "bitcoin_transaction_id", null: false
37
+ t.integer "btc_address_id", null: false
38
38
  t.datetime "created_at"
39
39
  t.datetime "updated_at"
40
40
  end
41
41
 
42
- add_index "received_payments", ["btc_address_id"], name: "index_received_payments_on_btc_address_id"
43
- add_index "received_payments", ["payment_id"], name: "index_received_payments_on_payment_id", unique: true
42
+ add_index "received_bitcoin_transactions", ["bitcoin_transaction_id"], name: "index_received_bitcoin_transactions_on_bitcoin_transaction_id", unique: true
43
+ add_index "received_bitcoin_transactions", ["btc_address_id"], name: "index_received_bitcoin_transactions_on_btc_address_id"
44
44
 
45
- create_table "sent_payments", force: :cascade do |t|
46
- t.integer "payment_id", null: false
45
+ create_table "sent_bitcoin_transactions", force: :cascade do |t|
46
+ t.integer "bitcoin_transaction_id", null: false
47
47
  t.datetime "created_at"
48
48
  t.datetime "updated_at"
49
49
  end
50
50
 
51
- add_index "sent_payments", ["payment_id"], name: "index_sent_payments_on_payment_id", unique: true
51
+ add_index "sent_bitcoin_transactions", ["bitcoin_transaction_id"], name: "index_sent_bitcoin_transactions_on_bitcoin_transaction_id", unique: true
52
52
 
53
53
  end
@@ -22,13 +22,13 @@ class BitcoinActiveRecordClientTest < ActiveSupport::TestCase
22
22
  @client.instance_variable_set(:@default_transaction_count, count)
23
23
  end
24
24
 
25
- def test_create_received_payments
25
+ def test_create_received_bitcoin_transactions
26
26
  set_default_transaction_count(2)
27
27
  @client.account = :test
28
28
 
29
- @client.create_received_payments
30
- received_payments = ReceivedPayment.all.to_a
31
- assert_equal(3, received_payments.size)
29
+ @client.create_received_bitcoin_transactions
30
+ received_bitcoin_transactions = ReceivedBitcoinTransaction.all.to_a
31
+ assert_equal(3, received_bitcoin_transactions.size)
32
32
 
33
33
  [
34
34
  {
@@ -50,18 +50,18 @@ class BitcoinActiveRecordClientTest < ActiveSupport::TestCase
50
50
  receiver_key: '1AC8jNajH7zYikxRzRKT3otNW72B9qVDFa',
51
51
  },
52
52
  ].each_with_index do |expected_values, i|
53
- received_payment = received_payments[i]
54
- payment = received_payment.payment
53
+ received_bitcoin_transaction = received_bitcoin_transactions[i]
54
+ bitcoin_transaction = received_bitcoin_transaction.bitcoin_transaction
55
55
 
56
- assert_equal(expected_values[:sender_key], payment.btc_address.public_key)
57
- assert_equal(BigDecimal.new(expected_values[:amount]), payment.amount)
58
- assert_equal(expected_values[:txid], payment.txid)
59
- assert_equal(expected_values[:receiver_key], received_payment.btc_address.public_key)
56
+ assert_equal(expected_values[:sender_key], bitcoin_transaction.btc_address.public_key)
57
+ assert_equal(BigDecimal.new(expected_values[:amount]), bitcoin_transaction.amount)
58
+ assert_equal(expected_values[:txid], bitcoin_transaction.txid)
59
+ assert_equal(expected_values[:receiver_key], received_bitcoin_transaction.btc_address.public_key)
60
60
  end
61
61
 
62
62
  # running again shouldnt do anything
63
- @client.create_received_payments
64
- assert_equal(3, ReceivedPayment.count)
63
+ @client.create_received_bitcoin_transactions
64
+ assert_equal(3, ReceivedBitcoinTransaction.count)
65
65
  end
66
66
 
67
67
  def test_get_received_transactions
@@ -104,15 +104,15 @@ class BitcoinActiveRecordClientTest < ActiveSupport::TestCase
104
104
  txid = 'kjdksfjlk2323'
105
105
  @client.expects(:request).with(:sendtoaddress, public_key, amount, comment).returns(txid)
106
106
 
107
- sent_payment = @client.pay(public_key: public_key, amount: amount, comment: comment) do |sent_payment|
108
- assert_equal(false, sent_payment.persisted?)
107
+ sent_bitcoin_transaction = @client.pay(public_key: public_key, amount: amount, comment: comment) do |sent_bitcoin_transaction|
108
+ assert_equal(false, sent_bitcoin_transaction.persisted?)
109
109
  end
110
110
 
111
- payment = sent_payment.payment
111
+ bitcoin_transaction = sent_bitcoin_transaction.bitcoin_transaction
112
112
 
113
- assert_equal(true, sent_payment.persisted?)
114
- assert_equal(public_key, payment.btc_address.public_key)
115
- assert_equal(amount, payment.amount)
116
- assert_equal(txid, payment.txid)
113
+ assert_equal(true, sent_bitcoin_transaction.persisted?)
114
+ assert_equal(public_key, bitcoin_transaction.btc_address.public_key)
115
+ assert_equal(amount, bitcoin_transaction.amount)
116
+ assert_equal(txid, bitcoin_transaction.txid)
117
117
  end
118
118
  end
@@ -18,11 +18,11 @@ class BitcoinActiveRecordGeneratorsTest < ActiveSupport::TestCase
18
18
 
19
19
  def test_install_generator
20
20
  lambda do
21
- migration_file_text = "class CreateBitcoinActiveRecordModels < ActiveRecord::Migration\n def change\n create_table \"payments\" do |t|\n t.integer \"btc_address_id\", null: false\n t.decimal \"amount\", null: false\n t.string \"txid\", null: false\n t.timestamps\n end\n add_index \"payments\", [\"btc_address_id\"]\n add_index \"payments\", [\"txid\"], unique: true\n\n create_table \"btc_addresses\" do |t|\n t.string \"public_key\", null: false\n t.timestamps\n end\n add_index \"btc_addresses\", [\"public_key\"], unique: true\n\n create_table \"received_payments\" do |t|\n t.integer \"payment_id\", null: false\n t.integer \"btc_address_id\", null: false\n t.timestamps\n end\n add_index \"received_payments\", [\"payment_id\"], unique: true\n add_index :received_payments, :btc_address_id\n\n create_table \"sent_payments\" do |t|\n t.integer \"payment_id\", null: false\n t.timestamps\n end\n add_index \"sent_payments\", [\"payment_id\"], unique: true\n end\nend\n"
21
+ migration_file_text = "class CreateBitcoinActiveRecordModels < ActiveRecord::Migration\n def change\n create_table \"bitcoin_transactions\" do |t|\n t.integer \"btc_address_id\", null: false\n t.decimal \"amount\", null: false\n t.string \"txid\", null: false\n t.timestamps\n end\n add_index \"bitcoin_transactions\", [\"btc_address_id\"]\n add_index \"bitcoin_transactions\", [\"txid\"], unique: true\n\n create_table \"btc_addresses\" do |t|\n t.string \"public_key\", null: false\n t.timestamps\n end\n add_index \"btc_addresses\", [\"public_key\"], unique: true\n\n create_table \"received_bitcoin_transactions\" do |t|\n t.integer \"bitcoin_transaction_id\", null: false\n t.integer \"btc_address_id\", null: false\n t.timestamps\n end\n add_index :received_bitcoin_transactions, :bitcoin_transaction_id, unique: true\n add_index :received_bitcoin_transactions, :btc_address_id\n\n create_table \"sent_bitcoin_transactions\" do |t|\n t.integer \"bitcoin_transaction_id\", null: false\n t.timestamps\n end\n add_index \"sent_bitcoin_transactions\", [\"bitcoin_transaction_id\"], unique: true\n end\nend\n"
22
22
  assert_equal(migration_file_text, File.read(@migration_file))
23
23
  end.()
24
24
 
25
- %w(btc_address payment received_payment sent_payment).each do |model_name|
25
+ %w(btc_address bitcoin_transaction received_bitcoin_transaction sent_bitcoin_transaction).each do |model_name|
26
26
  camel_case_model_name = model_name.camelize
27
27
  filename = "app/models/#{model_name}.rb"
28
28
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitcoin_active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lihan Li
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-28 00:00:00.000000000 Z
11
+ date: 2015-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -67,16 +67,16 @@ files:
67
67
  - lib/bitcoin_active_record.rb
68
68
  - lib/bitcoin_active_record/client.rb
69
69
  - lib/bitcoin_active_record/models.rb
70
+ - lib/bitcoin_active_record/models/bitcoin_transaction.rb
70
71
  - lib/bitcoin_active_record/models/btc_address.rb
71
- - lib/bitcoin_active_record/models/payment.rb
72
- - lib/bitcoin_active_record/models/received_payment.rb
73
- - lib/bitcoin_active_record/models/sent_payment.rb
72
+ - lib/bitcoin_active_record/models/received_bitcoin_transaction.rb
73
+ - lib/bitcoin_active_record/models/sent_bitcoin_transaction.rb
74
74
  - lib/generators/bitcoin_active_record/install_generator.rb
75
75
  - lib/generators/templates/create_bitcoin_active_record_models.rb
76
+ - lib/generators/templates/models/bitcoin_transaction.rb
76
77
  - lib/generators/templates/models/btc_address.rb
77
- - lib/generators/templates/models/payment.rb
78
- - lib/generators/templates/models/received_payment.rb
79
- - lib/generators/templates/models/sent_payment.rb
78
+ - lib/generators/templates/models/received_bitcoin_transaction.rb
79
+ - lib/generators/templates/models/sent_bitcoin_transaction.rb
80
80
  - test/.gitignore
81
81
  - test/Gemfile
82
82
  - test/Gemfile.lock
@@ -89,12 +89,10 @@ files:
89
89
  - test/app/controllers/concerns/.keep
90
90
  - test/app/helpers/application_helper.rb
91
91
  - test/app/mailers/.keep
92
- - test/app/models/.keep
92
+ - test/app/models/bitcoin_transaction.rb
93
93
  - test/app/models/btc_address.rb
94
- - test/app/models/concerns/.keep
95
- - test/app/models/payment.rb
96
- - test/app/models/received_payment.rb
97
- - test/app/models/sent_payment.rb
94
+ - test/app/models/received_bitcoin_transaction.rb
95
+ - test/app/models/sent_bitcoin_transaction.rb
98
96
  - test/app/views/layouts/application.html.erb
99
97
  - test/bin/bundle
100
98
  - test/bin/rails
@@ -119,7 +117,7 @@ files:
119
117
  - test/config/initializers/wrap_parameters.rb
120
118
  - test/config/locales/en.yml
121
119
  - test/config/routes.rb
122
- - test/db/migrate/20150425185338_create_bitcoin_active_record_models.rb
120
+ - test/db/migrate/20150429175618_create_bitcoin_active_record_models.rb
123
121
  - test/db/schema.rb
124
122
  - test/db/seeds.rb
125
123
  - test/lib/assets/.keep
@@ -176,12 +174,10 @@ test_files:
176
174
  - test/app/controllers/concerns/.keep
177
175
  - test/app/helpers/application_helper.rb
178
176
  - test/app/mailers/.keep
179
- - test/app/models/.keep
177
+ - test/app/models/bitcoin_transaction.rb
180
178
  - test/app/models/btc_address.rb
181
- - test/app/models/concerns/.keep
182
- - test/app/models/payment.rb
183
- - test/app/models/received_payment.rb
184
- - test/app/models/sent_payment.rb
179
+ - test/app/models/received_bitcoin_transaction.rb
180
+ - test/app/models/sent_bitcoin_transaction.rb
185
181
  - test/app/views/layouts/application.html.erb
186
182
  - test/bin/bundle
187
183
  - test/bin/rails
@@ -206,7 +202,7 @@ test_files:
206
202
  - test/config/initializers/wrap_parameters.rb
207
203
  - test/config/locales/en.yml
208
204
  - test/config/routes.rb
209
- - test/db/migrate/20150425185338_create_bitcoin_active_record_models.rb
205
+ - test/db/migrate/20150429175618_create_bitcoin_active_record_models.rb
210
206
  - test/db/schema.rb
211
207
  - test/db/seeds.rb
212
208
  - test/lib/assets/.keep
@@ -1,18 +0,0 @@
1
- module BitcoinActiveRecord::Models::Payment
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- # sender's address for received payments
6
- # payee address for sent payments
7
- belongs_to(:btc_address, inverse_of: :payments)
8
-
9
- has_one(:received_payment, inverse_of: :payment)
10
- has_one(:sent_payment, inverse_of: :payment)
11
-
12
- validates(:txid, :btc_address, :amount, presence: true)
13
- validates(:amount, numericality: { greater_than_or_equal_to: 0 })
14
- validates(:txid, uniqueness: true)
15
-
16
- auto_strip_attributes(:txid)
17
- end
18
- end
@@ -1,11 +0,0 @@
1
- module BitcoinActiveRecord::Models::ReceivedPayment
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- belongs_to(:payment, inverse_of: :received_payment, dependent: :destroy)
6
- belongs_to(:btc_address, inverse_of: :received_payments) # payment to this address
7
-
8
- validates(:payment, presence: true)
9
- validates(:payment_id, uniqueness: true)
10
- end
11
- end
@@ -1,10 +0,0 @@
1
- module BitcoinActiveRecord::Models::SentPayment
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- belongs_to(:payment, inverse_of: :sent_payment, dependent: :destroy)
6
-
7
- validates(:payment, presence: true)
8
- validates(:payment_id, uniqueness: true)
9
- end
10
- end
@@ -1,3 +0,0 @@
1
- class Payment < ActiveRecord::Base
2
- bitcoin_active_record_model
3
- end
@@ -1,3 +0,0 @@
1
- class ReceivedPayment < ActiveRecord::Base
2
- bitcoin_active_record_model
3
- end
@@ -1,3 +0,0 @@
1
- class SentPayment < ActiveRecord::Base
2
- bitcoin_active_record_model
3
- end
File without changes
File without changes
@@ -1,3 +0,0 @@
1
- class Payment < ActiveRecord::Base
2
- bitcoin_active_record_model
3
- end
@@ -1,3 +0,0 @@
1
- class ReceivedPayment < ActiveRecord::Base
2
- bitcoin_active_record_model
3
- end
@@ -1,3 +0,0 @@
1
- class SentPayment < ActiveRecord::Base
2
- bitcoin_active_record_model
3
- end
@@ -1,32 +0,0 @@
1
- class CreateBitcoinActiveRecordModels < ActiveRecord::Migration
2
- def change
3
- create_table "payments" do |t|
4
- t.integer "btc_address_id", null: false
5
- t.decimal "amount", null: false
6
- t.string "txid", null: false
7
- t.timestamps
8
- end
9
- add_index "payments", ["btc_address_id"]
10
- add_index "payments", ["txid"], unique: true
11
-
12
- create_table "btc_addresses" do |t|
13
- t.string "public_key", null: false
14
- t.timestamps
15
- end
16
- add_index "btc_addresses", ["public_key"], unique: true
17
-
18
- create_table "received_payments" do |t|
19
- t.integer "payment_id", null: false
20
- t.integer "btc_address_id", null: false
21
- t.timestamps
22
- end
23
- add_index "received_payments", ["payment_id"], unique: true
24
- add_index :received_payments, :btc_address_id
25
-
26
- create_table "sent_payments" do |t|
27
- t.integer "payment_id", null: false
28
- t.timestamps
29
- end
30
- add_index "sent_payments", ["payment_id"], unique: true
31
- end
32
- end