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

#adapterObject



78
79
80
# File 'lib/generators/authtrail/install_generator.rb', line 78

def adapter
  ActiveRecord::Base.connection_db_config.adapter.to_s
end

#copy_migrationObject



11
12
13
14
# File 'lib/generators/authtrail/install_generator.rb', line 11

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



16
17
18
# File 'lib/generators/authtrail/install_generator.rb', line 16

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

#encryptionObject



57
58
59
60
61
62
63
64
# File 'lib/generators/authtrail/install_generator.rb', line 57

def encryption
  case options[:encryption]
  when "lockbox", "activerecord", "none"
    options[:encryption]
  else
    abort "Error: encryption must be lockbox, activerecord, or none"
  end
end

#generate_modelObject



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

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



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

def identity_column
  case encryption
  when "lockbox"
    "t.text :identity_ciphertext\n      t.string :identity_bidx, index: true"
  else
    if encryption == "activerecord" && mysql?
      "t.string :identity, limit: 510, index: true"
    else
      "t.string :identity, index: true"
    end
  end
end

#ip_columnObject



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

def ip_column
  case encryption
  when "lockbox"
    "t.text :ip_ciphertext\n      t.string :ip_bidx, index: true"
  else
    "t.string :ip, index: true"
  end
end

#lockbox_methodObject



66
67
68
69
70
71
72
# File 'lib/generators/authtrail/install_generator.rb', line 66

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

#migration_versionObject



31
32
33
# File 'lib/generators/authtrail/install_generator.rb', line 31

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

#mysql?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/generators/authtrail/install_generator.rb', line 74

def mysql?
  adapter =~ /mysql|trilogy/i
end