Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always on tenancy #79

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export PATH=./bin:$PATH
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# Not Released
# Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [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
45 changes: 9 additions & 36 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -269,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:

- Generate the migration which will add `tenant_id` to the plutus accounts table

```sh
bundle exec rails g plutus:tenancy
```

- Run the migration

```sh
rake db:migrate
```
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.

- 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.*
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.

```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
===============
Expand All @@ -326,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)
Expand Down
7 changes: 2 additions & 5 deletions app/models/plutus/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions app/models/plutus/no_tenancy.rb

This file was deleted.

15 changes: 0 additions & 15 deletions app/models/plutus/tenancy.rb

This file was deleted.

12 changes: 12 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions fixture_rails_root/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,8 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
<<<<<<< HEAD
1.15.1
=======
1.11.2
>>>>>>> 14e61c8... Always support tenancy
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion fixture_rails_root/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
6 changes: 0 additions & 6 deletions lib/generators/plutus/templates/add_date_migration.rb

This file was deleted.

6 changes: 0 additions & 6 deletions lib/generators/plutus/templates/tenant_migration.rb

This file was deleted.

17 changes: 0 additions & 17 deletions lib/generators/plutus/templates/update_migration.rb

This file was deleted.

7 changes: 2 additions & 5 deletions lib/plutus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,3 +16,5 @@ def self.config
yield(self)
end
end

require "plutus/engine"
5 changes: 5 additions & 0 deletions lib/plutus/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Plutus
class Engine < Rails::Engine
isolate_namespace Plutus
end
end