Class: PgRls::Generators::InstallGenerator

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

Overview

Installer Generator

Constant Summary collapse

APPLICATION_LINE =
'class Application < Rails::Application'
APPLICATION_PATH =
'config/application.rb'
APPLICATION_RECORD_LINE =
'class ApplicationRecord < ActiveRecord::Base'
APPLICATION_RECORD_PATH =
'app/models/application_record.rb'
APPLICATION_CONTROLLER_LINE =
'class ApplicationController < ActionController::Base'
APPLICATION_CONTROLLER_PATH =
'app/controllers/application_controller.rb'

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ InstallGenerator

Returns a new instance of InstallGenerator.



11
12
13
14
15
16
17
18
# File 'lib/generators/pg_rls/install_generator.rb', line 11

def initialize(*args)
  tenant_model_or_table = args.first
  if tenant_model_or_table.present?
    PgRls.table_name = tenant_model_or_table.first.pluralize
    PgRls.class_name = tenant_model_or_table.first.singularize
  end
  super
end

Instance Method Details

#aplication_already_included?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/generators/pg_rls/install_generator.rb', line 86

def aplication_already_included?
  File.readlines(APPLICATION_PATH).grep(/config.active_record.schema_format = :sql/).any?
end

#aplication_controller_already_included?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/generators/pg_rls/install_generator.rb', line 78

def aplication_controller_already_included?
  File.readlines(APPLICATION_CONTROLLER_PATH).grep(/include PgRls::MultiTenancy/).any?
end

#aplication_record_already_included?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/generators/pg_rls/install_generator.rb', line 82

def aplication_record_already_included?
  File.readlines(APPLICATION_RECORD_PATH).grep(/include PgRls::SecureConnection/).any?
end

#copy_initializerObject

Raises:



45
46
47
48
49
50
51
52
# File 'lib/generators/pg_rls/install_generator.rb', line 45

def copy_initializer
  raise MissingORMError, orm_error_message unless options[:orm]

  inject_include_to_application
  inject_include_to_application_record
  inject_include_to_application_controller
  template 'pg_rls.rb.tt', 'config/initializers/pg_rls.rb'
end

#initialize_error_textObject



90
91
92
93
94
# File 'lib/generators/pg_rls/install_generator.rb', line 90

def initialize_error_text
  <<-ERROR.strip_heredoc
    TO DO
  ERROR
end

#inject_include_to_applicationObject



54
55
56
57
58
59
60
# File 'lib/generators/pg_rls/install_generator.rb', line 54

def inject_include_to_application
  return if aplication_already_included?

  gsub_file(APPLICATION_PATH, /(#{Regexp.escape(APPLICATION_LINE)})/mi) do |match|
    "#{match}\n  config.active_record.schema_format = :sql\n"
  end
end

#inject_include_to_application_controllerObject



70
71
72
73
74
75
76
# File 'lib/generators/pg_rls/install_generator.rb', line 70

def inject_include_to_application_controller
  return if aplication_controller_already_included?

  gsub_file(APPLICATION_CONTROLLER_PATH, /(#{Regexp.escape(APPLICATION_CONTROLLER_LINE)})/mi) do |match|
    "#{match}\n  include PgRls::MultiTenancy\n"
  end
end

#inject_include_to_application_recordObject



62
63
64
65
66
67
68
# File 'lib/generators/pg_rls/install_generator.rb', line 62

def inject_include_to_application_record
  return if aplication_record_already_included?

  gsub_file(APPLICATION_RECORD_PATH, /(#{Regexp.escape(APPLICATION_RECORD_LINE)})/mi) do |match|
    "#{match}\n  include PgRls::SecureConnection\n"
  end
end

#orm_error_messageObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/pg_rls/install_generator.rb', line 34

def orm_error_message
  <<-ERROR.strip_heredoc
  An ORM must be set to install PgRls in your application.
  Be sure to have an ORM like Active Record or loaded in your
  app or configure your own at `config/application.rb`.
    config.generators do |g|
      g.orm :your_orm_gem
    end
  ERROR
end

#show_readmeObject



96
97
98
# File 'lib/generators/pg_rls/install_generator.rb', line 96

def show_readme
  readme 'README' if behavior == :invoke
end