Class: Ahoy::Generators::Messages::ActiverecordGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/ahoy/messages/activerecord_generator.rb

Instance Method Summary collapse

Instance Method Details

#copy_migrationObject



14
15
16
17
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 14

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

#copy_templateObject



19
20
21
22
23
24
25
26
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 19

def copy_template
  case encryption
  when "lockbox"
    template "model_lockbox.rb", "app/models/ahoy/message.rb", lockbox_method: lockbox_method
  when "activerecord"
    template "model_activerecord.rb", "app/models/ahoy/message.rb"
  end
end

#encryptionObject

TODO remove default



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 43

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

#lockbox_methodObject



59
60
61
62
63
64
65
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 59

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

#migration_versionObject



28
29
30
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 28

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

#to_columnObject



32
33
34
35
36
37
38
39
40
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 32

def to_column
  case encryption
  when "lockbox"
    "t.text :to_ciphertext\n      t.string :to_bidx, index: true"
  else
    # TODO add limit: 510 for Active Record encryption + MySQL?
    "t.string :to, index: true"
  end
end