Class: Authtrail::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#copy_migrationObject



13
14
15
16
# File 'lib/generators/authtrail/install_generator.rb', line 13

def copy_migration
  encryption # ensure valid
  migration_template "login_activities_migration.rb", "db/migrate/create_login_activities.rb", migration_version: migration_version
end

#copy_templatesObject



18
19
20
# File 'lib/generators/authtrail/install_generator.rb', line 18

def copy_templates
  template "initializer.rb", "config/initializers/authtrail.rb"
end

#encryptionObject

TODO remove default



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/authtrail/install_generator.rb', line 58

def encryption
  case options[:encryption]
  when "lockbox", "activerecord", "none"
    options[:encryption]
  when nil
    if options[:lockbox]
      # TODO deprecation warning
      "lockbox"
    else
      "none"
    end
  else
    abort "Error: encryption must be lockbox, activerecord, or none"
  end
end

#generate_modelObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/authtrail/install_generator.rb', line 22

def generate_model
  case encryption
  when "lockbox"
    template "model_lockbox.rb", "app/models/login_activity.rb", lockbox_method: lockbox_method
  when "activerecord"
    template "model_activerecord.rb", "app/models/login_activity.rb"
  else
    template "model_none.rb", "app/models/login_activity.rb"
  end
end

#identity_columnObject



37
38
39
40
41
42
43
44
45
# File 'lib/generators/authtrail/install_generator.rb', line 37

def identity_column
  case encryption
  when "lockbox"
    "t.text :identity_ciphertext\n      t.string :identity_bidx, index: true"
  else
    # TODO add limit: 510 for Active Record encryption + MySQL?
    "t.string :identity, index: true"
  end
end

#ip_columnObject



47
48
49
50
51
52
53
54
55
# File 'lib/generators/authtrail/install_generator.rb', line 47

def ip_column
  case encryption
  when "lockbox"
    "t.text :ip_ciphertext\n      t.string :ip_bidx, index: true"
  else
    # TODO add limit: 510 for Active Record encryption + MySQL?
    "t.string :ip, index: true"
  end
end

#lockbox_methodObject



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

def lockbox_method
  if defined?(Lockbox::VERSION) && Lockbox::VERSION.to_i < 1
    "encrypts"
  else
    "has_encrypted"
  end
end

#migration_versionObject



33
34
35
# File 'lib/generators/authtrail/install_generator.rb', line 33

def migration_version
  "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
end