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



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

#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}",
      "db/migrate/#{migration_name}",
      config.merge(migration_version: migration_version),
    )
  end
end

#create_add_columns_migrationObject (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.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



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.rb'
  end
end

#create_or_inject_clearance_into_user_modelObject



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_terminalObject



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

def display_readme_in_terminal
  readme 'README'
end

#existing_migrationsObject (private)



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

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

#existing_users_columnsObject (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_indexesObject (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_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)



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

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

#migration_name_without_timestamp(file) ⇒ Object (private)



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

def migration_name_without_timestamp(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_versionObject (private)



125
126
127
128
129
# File 'lib/generators/clearance/install/install_generator.rb', line 125

def migration_version
  if Rails.version >= "5.0.0"
    "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
  end
end

#models_inherit_fromObject (private)



131
132
133
134
135
136
137
# File 'lib/generators/clearance/install/install_generator.rb', line 131

def models_inherit_from
  if Rails.version >= "5.0.0"
    "ApplicationRecord"
  else
    "ActiveRecord::Base"
  end
end

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



83
84
85
86
87
88
# 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_remember_token: 'add_index :users, :remember_token'
  }.reject { |index| existing_users_indexes.include?(index.to_s) }
end

#users_table_exists?Boolean (private)



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

def users_table_exists?
  if ActiveRecord::Base.connection.respond_to?(:data_source_exists?)
    ActiveRecord::Base.connection.data_source_exists?(:users)
  else
    ActiveRecord::Base.connection.table_exists?(:users)
  end
end