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

#configured_key_typeObject (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.options[: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_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", 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"
  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)



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|
    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)

Returns:

  • (Boolean)


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 migration_name_without_timestamp(file)
  file.sub(%r{^.*(db/migrate/)(?:\d+_)?}, '')
end

#migration_needed?Boolean (private)

Returns:

  • (Boolean)


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_stringObject (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_versionObject (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_fromObject (private)



142
143
144
# File 'lib/generators/clearance/install/install_generator.rb', line 142

def models_inherit_from
  "ApplicationRecord"
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
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)

Returns:

  • (Boolean)


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