Last active
October 30, 2018 12:56
-
-
Save Lyubomyr/2a0b37833a0aaf1a49f38e298c8d7b48 to your computer and use it in GitHub Desktop.
Code sample from my last project
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
class Company::DestroyService | |
def initialize(company) | |
@company = company | |
end | |
def destroy | |
Company.transaction do | |
Apartment::Tenant.drop @company.db_schema | |
Apartment::Tenant.switch! 'public' | |
@company.destroy | |
end | |
@company | |
end | |
end | |
module CoveredPeriodCondition | |
def covered_period_condition(start_date = nil, end_date = nil, attr_name = :incurred_at) | |
if start_date.blank? || end_date.blank? | |
{} | |
else | |
{ attr_name => (start_date..end_date) } | |
end | |
end | |
end | |
class Company < ApplicationRecord | |
audited | |
has_and_belongs_to_many :global_users, -> { order :name, :created_at } | |
belongs_to :creator, class_name: 'GlobalUser', foreign_key: :creator_id | |
validates :name, presence: true, length: { maximum: 100 }, | |
uniqueness: { | |
case_sensitive: false, | |
message: 'has already been taken. Since company name is going to be part of URL (as subdomain) it must be unique.' | |
} | |
validates :db_schema, uniqueness: { case_sensitive: false } | |
after_validation :generate_db_schema_name | |
def to_s | |
name | |
end | |
private | |
def generate_db_schema_name | |
if errors.empty? && new_record? | |
self.db_schema = name.parameterize | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment