Class: CompanyScope::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/company_scope/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



11
12
13
# File 'lib/generators/company_scope/install_generator.rb', line 11

def self.source_root
  File.expand_path("../templates", __FILE__)
end

Instance Method Details

#add_scoping_to_application_controllerObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/generators/company_scope/install_generator.rb', line 80

def add_scoping_to_application_controller
  controller_file = 'app/controllers/application_controller.rb'
  line = 'class ApplicationController < ActionController::Base'
  insert_company_scope = <<-RUBY
  company_setup\n
  set_scoping_class :company\n
  acts_as_company_filter\n
  RUBY
  if File.readlines(controller_file).grep(/company_setup/).size == 0
    gsub_file controller_file, /(#{Regexp.escape(line)})/mi do |match|
      match << "\n#{insert_company_scope}"
    end
  end
end

#generate_company_migrationObject



29
30
31
32
33
34
# File 'lib/generators/company_scope/install_generator.rb', line 29

def generate_company_migration
  unless options.no_migrations?
    # - generate a company model and migration with a 50 char max on the company name and unique index
    generate(:model, :company, 'company_name:string{50}:uniq' )
  end
end

#generate_user_migrationObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/company_scope/install_generator.rb', line 36

def generate_user_migration
  unless options.no_migrations?
    # - generate a user model and migration with a company_id reference a few basic user auth fields
    migrate_user_model = <<-RUBY
      company_id:references
      password_hash:string
      password_salt:string
      first_name:string{50}
      last_name:string{50}
      user_name:string{50}:uniq
      email_address:string{100}:uniq
    RUBY
    generate(:model, :user, "#{migrate_user_model.gsub("\n", " ")}")
  end
end

#make_company_the_guardianObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/company_scope/install_generator.rb', line 52

def make_company_the_guardian
  # - add the acts_as_guardian company_scope module into the company model
  unless options.no_migrations?
    config_file = 'app/models/company.rb'
    line = "class Company < ActiveRecord::Base"
    insert_guardian_scope = "acts_as_guardian"
    if File.readlines(config_file).grep(/acts_as_guardian/).size == 0
      gsub_file config_file, /(#{Regexp.escape(line)})/mi do |match|
        match << "\n\t#{insert_guardian_scope}"
      end
    end
  end
end

#make_user_a_tenantObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/company_scope/install_generator.rb', line 66

def make_user_a_tenant
  # - add the acts_as_company company_scope module into the user model
  unless options.no_migrations?
    config_file = 'app/models/user.rb'
    line = "class User < ActiveRecord::Base"
    insert_tenant_scope = "acts_as_company"
    if File.readlines(config_file).grep(/acts_as_company/).size == 0
      gsub_file config_file, /(#{Regexp.escape(line)})/mi do |match|
        match << "\n\t#{insert_tenant_scope}"
      end
    end
  end
end

#modify_application_rbObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/company_scope/install_generator.rb', line 15

def modify_application_rb
  # add the company_scope configuration enabler into config/application.rb
  config_file = 'config/application.rb'
  line = "class Application < Rails::Application"
  insert_config = <<-RUBY
  config.company_scope[:configured] = false
  RUBY
  if File.readlines(config_file).grep(/config.company_scope\[:configured\] = false/).size == 0
    gsub_file config_file, /(#{Regexp.escape(line)})/mi do |match|
      match << "\n#{insert_config}"
    end
  end
end