Forked from emad-elsaid/OnlyAdminsAuthorization.rb
Last active
August 29, 2015 14:17
-
-
Save kivanio/265d57ef7a8d4bc193bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1- add to gemfile | |
gem 'apartment' | |
2- execute | |
bundle install | |
rails g scaffold account url:string name:string phone:string email:string address:text ssn:string valid_until:date | |
rake db:migrate | |
3- replace app/models/account.rb | |
4- use the authorization adapter if you were using active admin | |
5- create the apartment.rb initializer | |
6- add the line below to config/application.rb | |
config.middleware.use 'Apartment::Elevators::FirstSubdomain' | |
7- modify your database.yml to use a user who can create and drop databases | |
8- create a new rake task db.rake to use "rake db:migrateall" to migrate all available databases | |
Have Fun! | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/account.rb | |
class Account < ActiveRecord::Base | |
attr_accessible :address, :email, :name, :phone, :ssn, :url, :valid_until | |
attr_readonly :url | |
validates_presence_of :url, :valid_until | |
validates_uniqueness_of :url | |
after_create :after_create_record | |
before_destroy :before_destroy_record | |
def after_create_record | |
Apartment::Database.create(url) | |
end | |
def before_destroy_record | |
Apartment::Database.drop(url) | |
end | |
def self.current | |
find_by_url(Apartment::Database.current_database) | |
end | |
def expired? | |
valid_until < Date.today | |
end | |
def expire_within?(days) | |
valid_until < Date.today + days | |
end | |
def grace? | |
expired? and valid_until+7.days >= Date.today | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/apartment.rb | |
Apartment.configure do |config| | |
config.seed_after_create = true | |
config.excluded_models = ['Account'] | |
config.default_schema = '<your default database name here>' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/tasks/db.rake | |
namespace :db do | |
desc "migrate all databases using Apartment migrator" | |
task :migrateall => :environment do | |
puts "Started migration for all databases" | |
dbs = Account.pluck(:url) | |
puts "Found #{dbs.size} to migrate" | |
dbs.each do |db| | |
puts "Migrating database #{db}..." | |
Apartment::Migrator.migrate db | |
end | |
puts "Databases migrated successfully" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment