From 680193e1037d51a8e8258f981e801af52c1e779f Mon Sep 17 00:00:00 2001 From: Ramon Tayag Date: Fri, 22 Apr 2016 09:02:10 +0800 Subject: [PATCH 1/5] And binstub for rails So that we can generate migration files the way Rails guides suggests --- .envrc | 1 + bin/rails | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 .envrc create mode 100755 bin/rails diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..57d59c52 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +export PATH=./bin:$PATH diff --git a/bin/rails b/bin/rails new file mode 100755 index 00000000..80910fc4 --- /dev/null +++ b/bin/rails @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby +# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application. + +ENGINE_ROOT = File.expand_path('../..', __FILE__) +ENGINE_PATH = File.expand_path('../../lib/plutus/engine', __FILE__) + +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) + +require 'rails/all' +require 'rails/engine/commands' From fba78863249cb699d9865aa6fc625b6f4acdbf8b Mon Sep 17 00:00:00 2001 From: Ramon Tayag Date: Fri, 22 Apr 2016 09:02:36 +0800 Subject: [PATCH 2/5] Move engine into its own file This is the typical configuration of an Rails engine --- lib/plutus.rb | 7 ++----- lib/plutus/engine.rb | 5 +++++ 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 lib/plutus/engine.rb diff --git a/lib/plutus.rb b/lib/plutus.rb index ec12afd9..476d84d6 100644 --- a/lib/plutus.rb +++ b/lib/plutus.rb @@ -2,11 +2,6 @@ require "rails" module Plutus - class Engine < Rails::Engine - isolate_namespace Plutus - end - - # ------------------------------ tenancy ------------------------------ # configuration to enable or disable tenancy mattr_accessor :enable_tenancy @@ -21,3 +16,5 @@ def self.config yield(self) end end + +require "plutus/engine" diff --git a/lib/plutus/engine.rb b/lib/plutus/engine.rb new file mode 100644 index 00000000..2ff36c2b --- /dev/null +++ b/lib/plutus/engine.rb @@ -0,0 +1,5 @@ +module Plutus + class Engine < Rails::Engine + isolate_namespace Plutus + end +end From 189e1d721b36e2a151edb7b8317b5c088d52a8da Mon Sep 17 00:00:00 2001 From: Ramon Tayag Date: Fri, 22 Apr 2016 09:10:15 +0800 Subject: [PATCH 3/5] Use standard way of generating migrations in engines --- README.markdown | 4 +--- .../20160422010135_create_plutus_tables.rb | 8 +------- .../20170710173916_create_plutus_tables.rb | 8 +------- .../plutus/templates/add_date_migration.rb | 6 ------ .../plutus/templates/tenant_migration.rb | 6 ------ .../plutus/templates/update_migration.rb | 17 ----------------- 6 files changed, 3 insertions(+), 46 deletions(-) rename lib/generators/plutus/templates/migration.rb => db/migrate/20160422010135_create_plutus_tables.rb (87%) delete mode 100644 lib/generators/plutus/templates/add_date_migration.rb delete mode 100644 lib/generators/plutus/templates/tenant_migration.rb delete mode 100644 lib/generators/plutus/templates/update_migration.rb diff --git a/README.markdown b/README.markdown index 5b57cae5..5cc9cb9d 100644 --- a/README.markdown +++ b/README.markdown @@ -16,9 +16,7 @@ Installation ============ - Add the gem to your Gemfile `gem "plutus"` - -- generate migration files `rails g plutus` - +- generate migration files `rake plutus:install:migrations` - run migrations `rake db:migrate` Overview diff --git a/lib/generators/plutus/templates/migration.rb b/db/migrate/20160422010135_create_plutus_tables.rb similarity index 87% rename from lib/generators/plutus/templates/migration.rb rename to db/migrate/20160422010135_create_plutus_tables.rb index 1879a3ab..7be31ff6 100644 --- a/lib/generators/plutus/templates/migration.rb +++ b/db/migrate/20160422010135_create_plutus_tables.rb @@ -1,5 +1,5 @@ class CreatePlutusTables < ActiveRecord::Migration[4.2] - def self.up + def change create_table :plutus_accounts do |t| t.string :name t.string :type @@ -30,10 +30,4 @@ def self.up add_index :plutus_amounts, [:account_id, :entry_id] add_index :plutus_amounts, [:entry_id, :account_id] end - - def self.down - drop_table :plutus_accounts - drop_table :plutus_entries - drop_table :plutus_amounts - end end diff --git a/fixture_rails_root/db/migrate/20170710173916_create_plutus_tables.rb b/fixture_rails_root/db/migrate/20170710173916_create_plutus_tables.rb index 7851eee3..65f09755 100644 --- a/fixture_rails_root/db/migrate/20170710173916_create_plutus_tables.rb +++ b/fixture_rails_root/db/migrate/20170710173916_create_plutus_tables.rb @@ -1,5 +1,5 @@ class CreatePlutusTables < ActiveRecord::Migration[4.2] - def self.up + def change create_table :plutus_accounts do |t| t.string :name t.string :type @@ -30,10 +30,4 @@ def self.up add_index :plutus_amounts, [:account_id, :entry_id] add_index :plutus_amounts, [:entry_id, :account_id] end - - def self.down - drop_table :plutus_accounts - drop_table :plutus_entries - drop_table :plutus_amounts - end end diff --git a/lib/generators/plutus/templates/add_date_migration.rb b/lib/generators/plutus/templates/add_date_migration.rb deleted file mode 100644 index 2ba4c6ea..00000000 --- a/lib/generators/plutus/templates/add_date_migration.rb +++ /dev/null @@ -1,6 +0,0 @@ -class AddDateToPlutusEntries < ActiveRecord::Migration[4.2] - def change - add_column :plutus_entries, :date, :date - add_index :plutus_entries, :date - end -end diff --git a/lib/generators/plutus/templates/tenant_migration.rb b/lib/generators/plutus/templates/tenant_migration.rb deleted file mode 100644 index ca1cba7d..00000000 --- a/lib/generators/plutus/templates/tenant_migration.rb +++ /dev/null @@ -1,6 +0,0 @@ -class TenantPlutusTables < ActiveRecord::Migration[4.2] - def change - # add a tenant column to plutus accounts table. - add_column :plutus_accounts, :tenant_id, :integer, index: true - end -end diff --git a/lib/generators/plutus/templates/update_migration.rb b/lib/generators/plutus/templates/update_migration.rb deleted file mode 100644 index 1d86451d..00000000 --- a/lib/generators/plutus/templates/update_migration.rb +++ /dev/null @@ -1,17 +0,0 @@ -class UpdatePlutusTables < ActiveRecord::Migration[4.2] - def change - # we have to remove these indexes because the temporary - # table index name is too long - remove_index :plutus_amounts, [:account_id, :transaction_id] - remove_index :plutus_amounts, [:transaction_id, :account_id] - remove_index :plutus_transactions, column: [:commercial_document_id, :commercial_document_type], :name => "index_transactions_on_commercial_doc" - - rename_table :plutus_transactions, :plutus_entries - rename_column :plutus_amounts, :transaction_id, :entry_id - - # adding the indexes back - add_index :plutus_amounts, [:account_id, :entry_id] - add_index :plutus_amounts, [:entry_id, :account_id] - add_index :plutus_entries, [:commercial_document_id, :commercial_document_type], :name => "index_entries_on_commercial_doc" - end -end From 9e82180bc3b5e8efba76acf5331a8ad30286289f Mon Sep 17 00:00:00 2001 From: Ramon Tayag Date: Tue, 12 Jul 2016 11:34:37 +0800 Subject: [PATCH 4/5] Add note about possible breaking changes --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa3a99a1..4ea96cec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ # Not Released - +## Fixed - Fix loading of jquery-ui files (Fixes https://github.com/mbulat/plutus/issues/58) + +## Added - Add `Account#amounts` and `Account#entries` to get all amounts and entries, respectively + +## Changed +- Required jquery-ui version +- How migrations are done, which may be a breaking change from older versions of Plutus From 9606fbff1e2a7809342a048dc9ff9787c5723c68 Mon Sep 17 00:00:00 2001 From: Ramon Tayag Date: Fri, 22 Apr 2016 12:36:27 +0800 Subject: [PATCH 5/5] Always support tenancy Simplifies code and testing --- CHANGELOG.md | 19 +++++---- README.markdown | 41 ++++--------------- app/models/plutus/account.rb | 7 +--- app/models/plutus/no_tenancy.rb | 9 ---- app/models/plutus/tenancy.rb | 15 ------- ...d_tenant_id_and_tenant_type_to_accounts.rb | 21 ++++++++++ fixture_rails_root/Gemfile.lock | 4 ++ ...t_id_and_tenant_type_to_accounts.plutus.rb | 22 ++++++++++ fixture_rails_root/db/schema.rb | 4 +- 9 files changed, 72 insertions(+), 70 deletions(-) delete mode 100644 app/models/plutus/no_tenancy.rb delete mode 100644 app/models/plutus/tenancy.rb create mode 100644 db/migrate/20160422034059_add_tenant_id_and_tenant_type_to_accounts.rb create mode 100644 fixture_rails_root/db/migrate/20160422044435_add_tenant_id_and_tenant_type_to_accounts.plutus.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ea96cec..fd3dfad0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,15 @@ -# Not Released -## Fixed -- Fix loading of jquery-ui files (Fixes https://github.com/mbulat/plutus/issues/58) - -## Added -- Add `Account#amounts` and `Account#entries` to get all amounts and entries, respectively +# Change Log +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). -## Changed +## [Unreleased] +### Changed +- Tenancy is enabled from the get go. Assigning a tenant is optional. See "Previous Versions" in the README on upgrading. - Required jquery-ui version - How migrations are done, which may be a breaking change from older versions of Plutus + +### Fixed +- Fix loading of jquery-ui files (Fixes https://github.com/mbulat/plutus/issues/58) + +### Added +- Add `Account#amounts` and `Account#entries` to get all amounts and entries, respectively diff --git a/README.markdown b/README.markdown index 5cc9cb9d..5d952655 100644 --- a/README.markdown +++ b/README.markdown @@ -267,41 +267,11 @@ entry = Plutus::Entry.build( Multitenancy Support ===================== -Plutus supports multitenant applications. Multitenancy is acheived by associating all Accounts under `Plutus::Account` with a "Tenant" object (typically some model in your Rails application). To add multi-tenancy support to Plutus, you must do the following: +Each account may belong to a polymorphic `tenant`. In previous versions plutus, the tenant code and tenant column had to be explicitly included by the developer. This complicated future changes (migrations, code testing) though and has since changed. -- Generate the migration which will add `tenant_id` to the plutus accounts table +In current versions of plutus, the accounts table have `tenant_id` and `tenant_type`. If you do not need accounts to belong to tenants, then you need not set these. The data storage overhead for empty columns with relational databases is negligible. -```sh -bundle exec rails g plutus:tenancy -``` - -- Run the migration - -```sh -rake db:migrate -``` - -- Add an initializer to your Rails application, i.e. `config/initializers/plutus.rb` - -```ruby -Plutus.config do |config| - config.enable_tenancy = true - config.tenant_class = 'Tenant' -end -``` -*NOTE: When building entries, be sure to specify the account directly, rather than use the `account_name` feature. Otherwise you'll probably end up with the wrong account.* - -```ruby -debit_account = Plutus::Account.where(:name => "Cash", :tenant => my_tenant).last -credit_account = Plutus::Account.where(:name => "Unearned Revenue", :tenant => my_tenant).last -entry = Plutus::Entry.new( - :description => "Order placed for widgets", - :date => Date.yesterday, - :debits => [ - {:account => debit_account, :amount => 100.00}], - :credits => [ - {:account => credit_account, :amount => 100.00}]) -``` +See "Previous Versions" on how to upgrade. Reporting Views =============== @@ -324,6 +294,11 @@ mount Plutus::Engine => "/plutus", :as => "plutus" Previous Versions ================= +If you're upgrading from older versions prior to the tenancy changes, you need to: + +1. `rake plutus:install:migrations` +2. `rake db:migrate` + For the rails 3 version, you can go here: [https://github.com/mbulat/plutus/tree/rails3](https://github.com/mbulat/plutus/tree/rails3) diff --git a/app/models/plutus/account.rb b/app/models/plutus/account.rb index c26ee54e..93c0c682 100644 --- a/app/models/plutus/account.rb +++ b/app/models/plutus/account.rb @@ -41,11 +41,8 @@ class Account < ActiveRecord::Base validates_presence_of :type - if Plutus.enable_tenancy - include Plutus::Tenancy - else - include Plutus::NoTenancy - end + validates :name, presence: true, uniqueness: { scope: [:tenant_id, :tenant_type] } + belongs_to :tenant, polymorphic: true # The balance of the account. This instance method is intended for use only # on instances of account subclasses. diff --git a/app/models/plutus/no_tenancy.rb b/app/models/plutus/no_tenancy.rb deleted file mode 100644 index b6729966..00000000 --- a/app/models/plutus/no_tenancy.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Plutus - module NoTenancy - extend ActiveSupport::Concern - - included do - validates :name, presence: true, uniqueness: true - end - end -end diff --git a/app/models/plutus/tenancy.rb b/app/models/plutus/tenancy.rb deleted file mode 100644 index cb6e2a0e..00000000 --- a/app/models/plutus/tenancy.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Plutus - module Tenancy - extend ActiveSupport::Concern - - included do - validates :name, presence: true, uniqueness: { scope: :tenant_id } - - if ActiveRecord::VERSION::MAJOR > 4 - belongs_to :tenant, class_name: Plutus.tenant_class, optional: true - else - belongs_to :tenant, class_name: Plutus.tenant_class - end - end - end -end diff --git a/db/migrate/20160422034059_add_tenant_id_and_tenant_type_to_accounts.rb b/db/migrate/20160422034059_add_tenant_id_and_tenant_type_to_accounts.rb new file mode 100644 index 00000000..e7ee3df5 --- /dev/null +++ b/db/migrate/20160422034059_add_tenant_id_and_tenant_type_to_accounts.rb @@ -0,0 +1,21 @@ +class AddTenantIdAndTenantTypeToAccounts < ActiveRecord::Migration + def up + unless Plutus.enable_tenancy + add_column :plutus_accounts, :tenant_id, :integer + end + + add_column :plutus_accounts, :tenant_type, :string + + Plutus::Account.reset_column_information + Plutus::Account.update_all(tenant_type: Plutus.tenant_class) + + add_index :plutus_accounts, [:tenant_id, :tenant_type] + end + + def down + unless Plutus.enable_tenancy + remove_column :plutus_accounts, :tenant_id, :integer + end + remove_column :plutus_accounts, :tenant_type, :string + end +end diff --git a/fixture_rails_root/Gemfile.lock b/fixture_rails_root/Gemfile.lock index 6804b59d..ac646f1c 100644 --- a/fixture_rails_root/Gemfile.lock +++ b/fixture_rails_root/Gemfile.lock @@ -195,4 +195,8 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH +<<<<<<< HEAD 1.15.1 +======= + 1.11.2 +>>>>>>> 14e61c8... Always support tenancy diff --git a/fixture_rails_root/db/migrate/20160422044435_add_tenant_id_and_tenant_type_to_accounts.plutus.rb b/fixture_rails_root/db/migrate/20160422044435_add_tenant_id_and_tenant_type_to_accounts.plutus.rb new file mode 100644 index 00000000..5515eda9 --- /dev/null +++ b/fixture_rails_root/db/migrate/20160422044435_add_tenant_id_and_tenant_type_to_accounts.plutus.rb @@ -0,0 +1,22 @@ +# This migration comes from plutus (originally 20160422034059) +class AddTenantIdAndTenantTypeToAccounts < ActiveRecord::Migration + def up + unless Plutus.enable_tenancy + add_column :plutus_accounts, :tenant_id, :integer + end + + add_column :plutus_accounts, :tenant_type, :string + + Plutus::Account.reset_column_information + Plutus::Account.update_all(tenant_type: Plutus.tenant_class) + + add_index :plutus_accounts, [:tenant_id, :tenant_type] + end + + def down + unless Plutus.enable_tenancy + remove_column :plutus_accounts, :tenant_id, :integer + end + remove_column :plutus_accounts, :tenant_type, :string + end +end diff --git a/fixture_rails_root/db/schema.rb b/fixture_rails_root/db/schema.rb index 67d7b6fa..20f24552 100644 --- a/fixture_rails_root/db/schema.rb +++ b/fixture_rails_root/db/schema.rb @@ -18,8 +18,10 @@ t.boolean "contra" t.datetime "created_at" t.datetime "updated_at" - t.integer "tenant_id" + t.integer "tenant_id" + t.string "tenant_type" t.index ["name", "type"], name: "index_plutus_accounts_on_name_and_type" + t.index ["tenant_id", "tenant_type"], name: "index_plutus_accounts_on_tenant_name_and_tenant_type" end create_table "plutus_amounts", force: :cascade do |t|