Class: Clearance::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Clearance::Generators::InstallGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/clearance/install/install_generator.rb
Class Method Summary collapse
-
.next_migration_number(dir) ⇒ Object
private
for generating a timestamp when using
create_migration
.
Instance Method Summary collapse
- #configured_key_type ⇒ Object private
- #copy_migration(migration_name, config = {}) ⇒ Object private
- #create_add_columns_migration ⇒ Object private
- #create_clearance_initializer ⇒ Object
- #create_clearance_migration ⇒ Object
- #create_or_inject_clearance_into_user_model ⇒ Object
- #display_readme_in_terminal ⇒ Object
- #existing_migrations ⇒ Object private
- #existing_users_columns ⇒ Object private
- #existing_users_indexes ⇒ Object private
- #inject_clearance_into_application_controller ⇒ Object
- #migration_exists?(name) ⇒ Boolean private
- #migration_name_without_timestamp(file) ⇒ Object private
- #migration_needed? ⇒ Boolean private
- #migration_primary_key_type_string ⇒ Object private
- #migration_version ⇒ Object private
- #models_inherit_from ⇒ Object private
- #new_columns ⇒ Object private
- #new_indexes ⇒ Object private
- #users_table_exists? ⇒ Boolean private
Class Method Details
.next_migration_number(dir) ⇒ Object (private)
for generating a timestamp when using create_migration
121 122 123 |
# File 'lib/generators/clearance/install/install_generator.rb', line 121 def self.next_migration_number(dir) ActiveRecord::Generators::Base.next_migration_number(dir) end |
Instance Method Details
#configured_key_type ⇒ Object (private)
135 136 137 138 139 140 |
# File 'lib/generators/clearance/install/install_generator.rb', line 135 def configured_key_type active_record = Rails.configuration.generators.active_record active_record ||= Rails.configuration.generators.[:active_record] active_record[:primary_key_type] end |
#copy_migration(migration_name, config = {}) ⇒ Object (private)
60 61 62 63 64 65 66 67 68 |
# File 'lib/generators/clearance/install/install_generator.rb', line 60 def copy_migration(migration_name, config = {}) unless migration_exists?(migration_name) migration_template( "db/migrate/#{migration_name}.rb.erb", "db/migrate/#{migration_name}.rb", config.merge(migration_version: migration_version), ) end end |
#create_add_columns_migration ⇒ Object (private)
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/generators/clearance/install/install_generator.rb', line 49 def create_add_columns_migration if migration_needed? config = { new_columns: new_columns, new_indexes: new_indexes } copy_migration("add_clearance_to_users", config) end end |
#create_clearance_initializer ⇒ Object
10 11 12 |
# File 'lib/generators/clearance/install/install_generator.rb', line 10 def create_clearance_initializer copy_file 'clearance.rb', 'config/initializers/clearance.rb' end |
#create_clearance_migration ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/generators/clearance/install/install_generator.rb', line 35 def create_clearance_migration if users_table_exists? create_add_columns_migration else copy_migration "create_users" end end |
#create_or_inject_clearance_into_user_model ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/generators/clearance/install/install_generator.rb', line 22 def create_or_inject_clearance_into_user_model if File.exist? "app/models/user.rb" inject_into_file( "app/models/user.rb", " include Clearance::User\n\n", after: "class User < #{models_inherit_from}\n", ) else @inherit_from = models_inherit_from template("user.rb.erb", "app/models/user.rb") end end |
#display_readme_in_terminal ⇒ Object
43 44 45 |
# File 'lib/generators/clearance/install/install_generator.rb', line 43 def display_readme_in_terminal readme 'README' end |
#existing_migrations ⇒ Object (private)
98 99 100 101 102 |
# File 'lib/generators/clearance/install/install_generator.rb', line 98 def existing_migrations @existing_migrations ||= Dir.glob("db/migrate/*.rb").map do |file| (file) end end |
#existing_users_columns ⇒ Object (private)
112 113 114 |
# File 'lib/generators/clearance/install/install_generator.rb', line 112 def existing_users_columns ActiveRecord::Base.connection.columns(:users).map(&:name) end |
#existing_users_indexes ⇒ Object (private)
116 117 118 |
# File 'lib/generators/clearance/install/install_generator.rb', line 116 def existing_users_indexes ActiveRecord::Base.connection.indexes(:users).map(&:name) end |
#inject_clearance_into_application_controller ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/generators/clearance/install/install_generator.rb', line 14 def inject_clearance_into_application_controller inject_into_class( "app/controllers/application_controller.rb", ApplicationController, " include Clearance::Controller\n" ) end |
#migration_exists?(name) ⇒ Boolean (private)
94 95 96 |
# File 'lib/generators/clearance/install/install_generator.rb', line 94 def migration_exists?(name) existing_migrations.include?(name) end |
#migration_name_without_timestamp(file) ⇒ Object (private)
104 105 106 |
# File 'lib/generators/clearance/install/install_generator.rb', line 104 def (file) file.sub(%r{^.*(db/migrate/)(?:\d+_)?}, '') end |
#migration_needed? ⇒ Boolean (private)
70 71 72 |
# File 'lib/generators/clearance/install/install_generator.rb', line 70 def migration_needed? new_columns.any? || new_indexes.any? end |
#migration_primary_key_type_string ⇒ Object (private)
129 130 131 132 133 |
# File 'lib/generators/clearance/install/install_generator.rb', line 129 def migration_primary_key_type_string if configured_key_type ", id: :#{configured_key_type}" end end |
#migration_version ⇒ Object (private)
125 126 127 |
# File 'lib/generators/clearance/install/install_generator.rb', line 125 def migration_version "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" end |
#models_inherit_from ⇒ Object (private)
142 143 144 |
# File 'lib/generators/clearance/install/install_generator.rb', line 142 def models_inherit_from "ApplicationRecord" end |
#new_columns ⇒ Object (private)
74 75 76 77 78 79 80 81 |
# File 'lib/generators/clearance/install/install_generator.rb', line 74 def new_columns @new_columns ||= { email: "t.string :email", encrypted_password: "t.string :encrypted_password, limit: 128", confirmation_token: "t.string :confirmation_token, limit: 128", remember_token: "t.string :remember_token, limit: 128", }.reject { |column| existing_users_columns.include?(column.to_s) } end |
#new_indexes ⇒ Object (private)
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/generators/clearance/install/install_generator.rb', line 83 def new_indexes @new_indexes ||= { index_users_on_email: "add_index :users, :email", index_users_on_confirmation_token: "add_index :users, :confirmation_token, unique: true", index_users_on_remember_token: "add_index :users, :remember_token, unique: true", }.reject { |index| existing_users_indexes.include?(index.to_s) } end |
#users_table_exists? ⇒ Boolean (private)
108 109 110 |
# File 'lib/generators/clearance/install/install_generator.rb', line 108 def users_table_exists? ActiveRecord::Base.connection.data_source_exists?(:users) end |