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 aa908b19..bb709539 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::Acount.where(:name => "Cash", :tenant => my_tenant).last -credit_account = Plutus::Acount.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](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 b7f7cadf..00000000 --- a/app/models/plutus/tenancy.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Plutus - module Tenancy - extend ActiveSupport::Concern - - included do - validates :name, presence: true, uniqueness: { scope: :tenant_id } - - belongs_to :tenant, class_name: Plutus.tenant_class - 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..0069bb2c --- /dev/null +++ b/db/migrate/20160422034059_add_tenant_id_and_tenant_type_to_accounts.rb @@ -0,0 +1,39 @@ +class AddTenantIdAndTenantTypeToAccounts < ActiveRecord::Migration + def up + if Plutus.enable_tenancy + rename_column :plutus_accounts, :tenant_id, :old_tenant_id + Plutus::Account.reset_column_information + end + + add_column :plutus_accounts, :tenant_id, :string + add_column :plutus_accounts, :tenant_type, :string + + Plutus::Account.update_all(tenant_type: Plutus.tenant_class) + + if Plutus.enable_tenancy + Plutus::Account.find_each do |account| + account.update(tenant_id: account.old_tenant_id.to_s) + end + remove_column :plutus_accounts, :old_tenant_id + end + + add_index :plutus_accounts, [:tenant_id, :tenant_type] + end + + def down + if Plutus.enable_tenancy + rename_column :plutus_accounts, :tenant_id, :new_tenant_id + Plutus::Account.reset_column_information + add_column :plutus_accounts, :tenant_id, :integer, index: true + + Plutus::Account.find_each do |account| + account.update(tenant_id: account.new_tenant_id.to_i) + end + + remove_column :plutus_accounts, :new_tenant_id + else + remove_column :plutus_accounts, :tenant_id, :string + 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 061bdfd6..c103fafd 100644 --- a/fixture_rails_root/Gemfile.lock +++ b/fixture_rails_root/Gemfile.lock @@ -151,4 +151,4 @@ DEPENDENCIES web-console (~> 2.0) BUNDLED WITH - 1.10.6 + 1.11.2 diff --git a/fixture_rails_root/db/migrate/20150722204422_tenant_plutus_tables.rb b/fixture_rails_root/db/migrate/20150722204422_tenant_plutus_tables.rb deleted file mode 100644 index 9790d59e..00000000 --- a/fixture_rails_root/db/migrate/20150722204422_tenant_plutus_tables.rb +++ /dev/null @@ -1,6 +0,0 @@ -class TenantPlutusTables < ActiveRecord::Migration - def change - # add a tenant column to plutus accounts table. - add_column :plutus_accounts, :tenant_id, :integer, index: true - end -end 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..8652d30f --- /dev/null +++ b/fixture_rails_root/db/migrate/20160422044435_add_tenant_id_and_tenant_type_to_accounts.plutus.rb @@ -0,0 +1,40 @@ +# This migration comes from plutus (originally 20160422034059) +class AddTenantIdAndTenantTypeToAccounts < ActiveRecord::Migration + def up + if Plutus.enable_tenancy + rename_column :plutus_accounts, :tenant_id, :old_tenant_id + Plutus::Account.reset_column_information + end + + add_column :plutus_accounts, :tenant_id, :string + add_column :plutus_accounts, :tenant_type, :string + + Plutus::Account.update_all(tenant_type: Plutus.tenant_class) + + if Plutus.enable_tenancy + Plutus::Account.find_each do |account| + account.update(tenant_id: account.old_tenant_id.to_s) + end + remove_column :plutus_accounts, :old_tenant_id + end + + add_index :plutus_accounts, [:tenant_id, :tenant_type] + end + + def down + if Plutus.enable_tenancy + rename_column :plutus_accounts, :tenant_id, :new_tenant_id + Plutus::Account.reset_column_information + add_column :plutus_accounts, :tenant_id, :integer, index: true + + Plutus::Account.find_each do |account| + account.update(tenant_id: account.new_tenant_id.to_i) + end + + remove_column :plutus_accounts, :new_tenant_id + else + remove_column :plutus_accounts, :tenant_id, :string + 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 a8561aad..fd4fa448 100644 --- a/fixture_rails_root/db/schema.rb +++ b/fixture_rails_root/db/schema.rb @@ -9,33 +9,35 @@ # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # -# It's strongly recommended to check this file into your version control system. +# It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(:version => 20150722204422) do +ActiveRecord::Schema.define(version: 20160422044435) do - create_table "plutus_accounts", :force => true do |t| + create_table "plutus_accounts", force: :cascade do |t| t.string "name" t.string "type" t.boolean "contra" t.datetime "created_at" t.datetime "updated_at" - t.integer "tenant_id" + t.string "tenant_id" + t.string "tenant_type" end - add_index "plutus_accounts", ["name", "type"], :name => "index_plutus_accounts_on_name_and_type" + add_index "plutus_accounts", ["name", "type"], name: "index_plutus_accounts_on_name_and_type" + add_index "plutus_accounts", ["tenant_id", "tenant_type"], name: "index_plutus_accounts_on_tenant_id_and_tenant_type" - create_table "plutus_amounts", :force => true do |t| + create_table "plutus_amounts", force: :cascade do |t| t.string "type" t.integer "account_id" t.integer "entry_id" - t.decimal "amount", :precision => 20, :scale => 10 + t.decimal "amount", precision: 20, scale: 10 end - add_index "plutus_amounts", ["account_id", "entry_id"], :name => "index_plutus_amounts_on_account_id_and_entry_id" - add_index "plutus_amounts", ["entry_id", "account_id"], :name => "index_plutus_amounts_on_entry_id_and_account_id" - add_index "plutus_amounts", ["type"], :name => "index_plutus_amounts_on_type" + add_index "plutus_amounts", ["account_id", "entry_id"], name: "index_plutus_amounts_on_account_id_and_entry_id" + add_index "plutus_amounts", ["entry_id", "account_id"], name: "index_plutus_amounts_on_entry_id_and_account_id" + add_index "plutus_amounts", ["type"], name: "index_plutus_amounts_on_type" - create_table "plutus_entries", :force => true do |t| + create_table "plutus_entries", force: :cascade do |t| t.string "description" t.date "date" t.integer "commercial_document_id" @@ -44,7 +46,7 @@ t.datetime "updated_at" end - add_index "plutus_entries", ["commercial_document_id", "commercial_document_type"], :name => "index_entries_on_commercial_doc" - add_index "plutus_entries", ["date"], :name => "index_plutus_entries_on_date" + add_index "plutus_entries", ["commercial_document_id", "commercial_document_type"], name: "index_entries_on_commercial_doc" + add_index "plutus_entries", ["date"], name: "index_plutus_entries_on_date" end