transactionable 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +29 -0
  5. data/app/assets/javascripts/transactionable/add_bank_account.js +45 -0
  6. data/app/assets/javascripts/transactionable/add_credit_card.js +87 -0
  7. data/app/assets/javascripts/transactionable/application.js +12 -0
  8. data/app/assets/stylesheets/transactionable/application.css +13 -0
  9. data/app/controllers/transactionable/application_controller.rb +4 -0
  10. data/app/helpers/transactionable/application_helper.rb +4 -0
  11. data/app/models/transactionable/bank_account.rb +35 -0
  12. data/app/models/transactionable/credit.rb +28 -0
  13. data/app/models/transactionable/credit_card.rb +39 -0
  14. data/app/models/transactionable/debit.rb +28 -0
  15. data/app/models/transactionable/refund.rb +5 -0
  16. data/app/models/transactionable/remote_bank_account.rb +4 -0
  17. data/app/models/transactionable/remote_credit_card.rb +7 -0
  18. data/app/models/transactionable/remote_customer.rb +39 -0
  19. data/app/models/transactionable/remote_entity.rb +42 -0
  20. data/app/models/transactionable/remote_transaction.rb +4 -0
  21. data/app/models/transactionable/reversal.rb +5 -0
  22. data/app/models/transactionable/transaction.rb +25 -0
  23. data/app/views/layouts/transactionable/application.html.erb +14 -0
  24. data/config/routes.rb +2 -0
  25. data/db/migrate/20140108145608_create_transactionable_remote_entities.rb +14 -0
  26. data/db/migrate/20140108182203_create_transactionable_credit_cards.rb +17 -0
  27. data/db/migrate/20140108190511_create_transactionable_transactions.rb +18 -0
  28. data/db/migrate/20140108213120_create_transactionable_bank_accounts.rb +17 -0
  29. data/lib/tasks/transactionable_tasks.rake +4 -0
  30. data/lib/transactionable.rb +9 -0
  31. data/lib/transactionable/acts_as_transactionable.rb +17 -0
  32. data/lib/transactionable/balanced_customer.rb +23 -0
  33. data/lib/transactionable/bank_account_transactionable.rb +23 -0
  34. data/lib/transactionable/credit_card_transactionable.rb +23 -0
  35. data/lib/transactionable/engine.rb +12 -0
  36. data/lib/transactionable/exceptions.rb +6 -0
  37. data/lib/transactionable/version.rb +3 -0
  38. data/spec/dummy/README.rdoc +28 -0
  39. data/spec/dummy/Rakefile +6 -0
  40. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  41. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  42. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  43. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  44. data/spec/dummy/app/models/user.rb +4 -0
  45. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  46. data/spec/dummy/bin/bundle +3 -0
  47. data/spec/dummy/bin/rails +4 -0
  48. data/spec/dummy/bin/rake +4 -0
  49. data/spec/dummy/config.ru +4 -0
  50. data/spec/dummy/config/application.rb +28 -0
  51. data/spec/dummy/config/boot.rb +5 -0
  52. data/spec/dummy/config/database.yml +25 -0
  53. data/spec/dummy/config/environment.rb +5 -0
  54. data/spec/dummy/config/environments/development.rb +29 -0
  55. data/spec/dummy/config/environments/production.rb +80 -0
  56. data/spec/dummy/config/environments/test.rb +36 -0
  57. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  58. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  59. data/spec/dummy/config/initializers/inflections.rb +16 -0
  60. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  61. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  62. data/spec/dummy/config/initializers/session_store.rb +3 -0
  63. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  64. data/spec/dummy/config/locales/en.yml +23 -0
  65. data/spec/dummy/config/routes.rb +4 -0
  66. data/spec/dummy/db/development.sqlite3 +0 -0
  67. data/spec/dummy/db/migrate/20140108162102_create_users.rb +8 -0
  68. data/spec/dummy/db/schema.rb +76 -0
  69. data/spec/dummy/db/test.sqlite3 +0 -0
  70. data/spec/dummy/log/development.log +471 -0
  71. data/spec/dummy/log/test.log +2875 -0
  72. data/spec/dummy/public/404.html +58 -0
  73. data/spec/dummy/public/422.html +58 -0
  74. data/spec/dummy/public/500.html +57 -0
  75. data/spec/dummy/public/favicon.ico +0 -0
  76. data/spec/factories/transactionable_credit_cards.rb +15 -0
  77. data/spec/factories/transactionable_debits.rb +6 -0
  78. data/spec/factories/transactionable_refunds.rb +6 -0
  79. data/spec/factories/transactionable_remote_credit_cards.rb +6 -0
  80. data/spec/factories/transactionable_remote_customers.rb +6 -0
  81. data/spec/factories/transactionable_remote_transactions.rb +6 -0
  82. data/spec/integration/acts_as_bank_account_transactionable_spec.rb +24 -0
  83. data/spec/integration/acts_as_credit_card_transactionable_spec.rb +46 -0
  84. data/spec/integration/models/bank_account_spec.rb +0 -0
  85. data/spec/integration/models/credit_card_spec.rb +28 -0
  86. data/spec/integration/models/credit_spec.rb +0 -0
  87. data/spec/integration/models/debit_spec.rb +0 -0
  88. data/spec/lib/transactionable_spec.rb +6 -0
  89. data/spec/models/transactionable/bank_account_spec.rb +7 -0
  90. data/spec/models/transactionable/credit_card_spec.rb +7 -0
  91. data/spec/models/transactionable/credit_spec.rb +7 -0
  92. data/spec/models/transactionable/debit_spec.rb +7 -0
  93. data/spec/models/transactionable/refund_spec.rb +7 -0
  94. data/spec/models/transactionable/remote_bank_account_spec.rb +7 -0
  95. data/spec/models/transactionable/remote_credit_card_spec.rb +7 -0
  96. data/spec/models/transactionable/remote_customer_spec.rb +7 -0
  97. data/spec/models/transactionable/remote_transaction_spec.rb +7 -0
  98. data/spec/models/transactionable/reversal_spec.rb +7 -0
  99. data/spec/models/transactionable/transaction_spec.rb +7 -0
  100. data/spec/spec_helper.rb +40 -0
  101. metadata +319 -0
@@ -0,0 +1,4 @@
1
+ module Transactionable
2
+ class RemoteTransaction < RemoteEntity
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module Transactionable
2
+ class Reversal < Transaction
3
+ belongs_to :credit
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ module Transactionable
2
+ class Transaction < ActiveRecord::Base
3
+ has_one :remote_transaction, as: :local_entity, dependent: :destroy
4
+ belongs_to :transactionable, polymorphic: true
5
+ belongs_to :transaction_loggable, polymorphic: true
6
+
7
+ TRANSACTION_TYPES = [:hold, :credit, :debit, :refund, :reversal]
8
+
9
+ def self.create_from_remote(remote_trans)
10
+ transaction = create(
11
+ amount: remote_trans.amount/100.0,
12
+ status: remote_trans.status,
13
+ description: remote_trans.description
14
+ )
15
+ RemoteTransaction.create(local_entity: transaction, uri: remote_trans.uri)
16
+ transaction
17
+ end
18
+
19
+ def remote
20
+ if remote_transaction && remote_transaction.synced?
21
+ remote_transaction.fetch
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Transactionable</title>
5
+ <%= stylesheet_link_tag "transactionable/application", media: "all" %>
6
+ <%= javascript_include_tag "transactionable/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,2 @@
1
+ Transactionable::Engine.routes.draw do
2
+ end
@@ -0,0 +1,14 @@
1
+ class CreateTransactionableRemoteEntities < ActiveRecord::Migration
2
+ def change
3
+ create_table :transactionable_remote_entities do |t|
4
+ t.string :uri
5
+ t.string :service_name
6
+ t.integer :local_entity_id
7
+ t.string :local_entity_type
8
+ t.time :synced_at
9
+ t.string :type
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ class CreateTransactionableCreditCards < ActiveRecord::Migration
2
+ def change
3
+ create_table :transactionable_credit_cards do |t|
4
+ t.integer :credit_cardable_id
5
+ t.string :credit_cardable_type
6
+ t.string :name
7
+ t.string :description
8
+ t.integer :last_four
9
+ t.string :brand
10
+ t.integer :expiration_month
11
+ t.integer :expiration_year
12
+ t.date :expiration_date
13
+
14
+ t.timestamps
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ class CreateTransactionableTransactions < ActiveRecord::Migration
2
+ def change
3
+ create_table :transactionable_transactions do |t|
4
+ t.integer :transactionable_id
5
+ t.string :transactionable_type
6
+ t.integer :transaction_loggable_id
7
+ t.string :transaction_loggable_type
8
+ t.integer :credit_id
9
+ t.integer :debit_id
10
+ t.decimal :amount, precision: 8, scale: 2
11
+ t.string :status
12
+ t.string :description
13
+ t.string :type
14
+
15
+ t.timestamps
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ class CreateTransactionableBankAccounts < ActiveRecord::Migration
2
+ def change
3
+ create_table :transactionable_bank_accounts do |t|
4
+ t.integer :bank_accountable_id
5
+ t.string :bank_accountable_type
6
+ t.string :account_number
7
+ t.string :name
8
+ t.string :bank_name
9
+ t.string :description
10
+ t.string :account_type
11
+ t.boolean :can_debit
12
+ t.boolean :verified
13
+
14
+ t.timestamps
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :transactionable do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,9 @@
1
+ require "transactionable/engine"
2
+ require "transactionable/balanced_customer"
3
+ require "transactionable/credit_card_transactionable"
4
+ require "transactionable/bank_account_transactionable"
5
+ require "transactionable/exceptions"
6
+ require "transactionable/acts_as_transactionable"
7
+
8
+ module Transactionable
9
+ end
@@ -0,0 +1,17 @@
1
+ module Transactionable
2
+ module ActsAsTransactionable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def acts_as_credit_card_transactionable(options = {})
7
+ include Transactionable::CreditCardTransactionable
8
+ end
9
+
10
+ def acts_as_bank_account_transactionable(options = {})
11
+ include Transactionable::BankAccountTransactionable
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ ActiveRecord::Base.send :include, Transactionable::ActsAsTransactionable
@@ -0,0 +1,23 @@
1
+ module Transactionable
2
+ module BalancedCustomer
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_one :remote_customer, as: :local_entity, dependent: :destroy, class_name: "Transactionable::RemoteCustomer"
7
+
8
+ def sync_customer
9
+ if remote_customer
10
+ remote_customer.build_or_update_remote
11
+ else
12
+ Transactionable::RemoteCustomer.create(local_entity: self)
13
+ end
14
+ end
15
+
16
+ def remote
17
+ if remote_customer && remote_customer.synced?
18
+ remote_customer.fetch
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Transactionable
2
+ module BankAccountTransactionable
3
+ extend ActiveSupport::Concern
4
+ include BalancedCustomer
5
+
6
+ included do
7
+ has_many :bank_accounts, as: :bank_accountable, dependent: :destroy, class_name: "Transactionable::BankAccount"
8
+
9
+ def add_bank_account(balanced_uri)
10
+ bank_accounts.destroy_all if one_bank_account?
11
+ bank_account = bank_accounts.create
12
+ remote_account = RemoteBankAccount.create(uri: balanced_uri, local_entity: bank_account)
13
+ bank_account.reload.sync
14
+ sync_customer
15
+ self.reload.remote.add_bank_account(balanced_uri)
16
+ end
17
+
18
+ def one_bank_account?
19
+ false
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Transactionable
2
+ module CreditCardTransactionable
3
+ extend ActiveSupport::Concern
4
+ include BalancedCustomer
5
+
6
+ included do
7
+ has_many :credit_cards, as: :credit_cardable, dependent: :destroy, class_name: "Transactionable::CreditCard"
8
+
9
+ def add_credit_card(balanced_uri, credit_card = {})
10
+ self.credit_cards.destroy_all if one_card?
11
+ credit_card = self.credit_cards.create
12
+ remote_card = RemoteCreditCard.create(uri: balanced_uri, local_entity: credit_card)
13
+ credit_card.reload.sync
14
+ sync_customer
15
+ self.reload.remote.add_card(balanced_uri)
16
+ end
17
+
18
+ def one_card?
19
+ false
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ require 'balanced'
2
+
3
+ module Transactionable
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace Transactionable
6
+
7
+ config.generators do |g|
8
+ g.test_framework :rspec, fixture: false
9
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module Transactionable
2
+ module Exceptions
3
+ class InvalidRefundAmount < StandardError; end
4
+ class InvalidReversalAmount < StandardError; end
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Transactionable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ acts_as_credit_card_transactionable
3
+ acts_as_bank_account_transactionable
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_mailer/railtie"
7
+ require "sprockets/railtie"
8
+ # require "rails/test_unit/railtie"
9
+
10
+ Bundler.require(*Rails.groups)
11
+ require "transactionable"
12
+
13
+ module Dummy
14
+ class Application < Rails::Application
15
+ # Settings in config/environments/* take precedence over those specified here.
16
+ # Application configuration should go into files in config/initializers
17
+ # -- all .rb files in that directory are automatically loaded.
18
+
19
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
20
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
21
+ # config.time_zone = 'Central Time (US & Canada)'
22
+
23
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
24
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
25
+ # config.i18n.default_locale = :de
26
+ end
27
+ end
28
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/development.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ # Warning: The database defined as "test" will be erased and
13
+ # re-generated from your development database when you run "rake".
14
+ # Do not set this db to the same as development or production.
15
+ test:
16
+ adapter: sqlite3
17
+ database: db/test.sqlite3
18
+ pool: 5
19
+ timeout: 5000
20
+
21
+ production:
22
+ adapter: sqlite3
23
+ database: db/production.sqlite3
24
+ pool: 5
25
+ timeout: 5000