Class: Clearance::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/clearance/install/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dir) ⇒ Object (private)

for generating a timestamp when using create_migration


116
117
118
# File 'lib/generators/clearance/install/install_generator.rb', line 116

def self.next_migration_number(dir)
  ActiveRecord::Generators::Base.next_migration_number(dir)
end

Instance Method Details

#copy_migration(migration_name, config = {}) ⇒ Object (private)


59
60
61
62
63
64
65
66
67
# File 'lib/generators/clearance/install/install_generator.rb', line 59

def copy_migration(migration_name, config = {})
  unless migration_exists?(migration_name)
    migration_template(
      "db/migrate/#{migration_name}",
      "db/migrate/#{migration_name}",
      config
    )
  end
end

#create_add_columns_migrationObject (private)


48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/clearance/install/install_generator.rb', line 48

def create_add_columns_migration
  if migration_needed?
    config = {
      new_columns: new_columns,
      new_indexes: new_indexes
    }

    copy_migration('add_clearance_to_users.rb', config)
  end
end

#create_clearance_initializerObject


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_migrationObject


34
35
36
37
38
39
40
# File 'lib/generators/clearance/install/install_generator.rb', line 34

def create_clearance_migration
  if users_table_exists?
    create_add_columns_migration
  else
    copy_migration 'create_users.rb'
  end
end

#create_or_inject_clearance_into_user_modelObject


22
23
24
25
26
27
28
29
30
31
32
# 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 < ActiveRecord::Base\n"
    )
  else
    copy_file 'user.rb', 'app/models/user.rb'
  end
end

#display_readme_in_terminalObject


42
43
44
# File 'lib/generators/clearance/install/install_generator.rb', line 42

def display_readme_in_terminal
  readme 'README'
end

#existing_migrationsObject (private)


93
94
95
96
97
# File 'lib/generators/clearance/install/install_generator.rb', line 93

def existing_migrations
  @existing_migrations ||= Dir.glob("db/migrate/*.rb").map do |file|
    migration_name_without_timestamp(file)
  end
end

#existing_users_columnsObject (private)


107
108
109
# File 'lib/generators/clearance/install/install_generator.rb', line 107

def existing_users_columns
  ActiveRecord::Base.connection.columns(:users).map(&:name)
end

#existing_users_indexesObject (private)


111
112
113
# File 'lib/generators/clearance/install/install_generator.rb', line 111

def existing_users_indexes
  ActiveRecord::Base.connection.indexes(:users).map(&:name)
end

#inject_clearance_into_application_controllerObject


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)

Returns:

  • (Boolean)

89
90
91
# File 'lib/generators/clearance/install/install_generator.rb', line 89

def migration_exists?(name)
  existing_migrations.include?(name)
end

#migration_name_without_timestamp(file) ⇒ Object (private)


99
100
101
# File 'lib/generators/clearance/install/install_generator.rb', line 99

def migration_name_without_timestamp(file)
  file.sub(%r{^.*(db/migrate/)(?:\d+_)?}, '')
end

#migration_needed?Boolean (private)

Returns:

  • (Boolean)

69
70
71
# File 'lib/generators/clearance/install/install_generator.rb', line 69

def migration_needed?
  new_columns.any? || new_indexes.any?
end

#new_columnsObject (private)


73
74
75
76
77
78
79
80
# File 'lib/generators/clearance/install/install_generator.rb', line 73

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_indexesObject (private)


82
83
84
85
86
87
# File 'lib/generators/clearance/install/install_generator.rb', line 82

def new_indexes
  @new_indexes ||= {
    index_users_on_email: 'add_index :users, :email',
    index_users_on_remember_token: 'add_index :users, :remember_token'
  }.reject { |index| existing_users_indexes.include?(index.to_s) }
end

#users_table_exists?Boolean (private)

Returns:

  • (Boolean)

103
104
105
# File 'lib/generators/clearance/install/install_generator.rb', line 103

def users_table_exists?
  ActiveRecord::Base.connection.table_exists?(:users)
end