Class: Ahoy::Generators::Messages::ActiverecordGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Ahoy::Generators::Messages::ActiverecordGenerator
- Includes:
- ActiveRecord::Generators::Migration
- Defined in:
- lib/generators/ahoy/messages/activerecord_generator.rb
Instance Method Summary collapse
- #adapter ⇒ Object
- #copy_migration ⇒ Object
- #copy_template ⇒ Object
- #encryption ⇒ Object
- #foreign_key_type ⇒ Object
- #key_type ⇒ Object
- #lockbox_method ⇒ Object
- #migration_version ⇒ Object
- #mysql? ⇒ Boolean
- #primary_key_type ⇒ Object
- #to_column ⇒ Object
Instance Method Details
#adapter ⇒ Object
66 67 68 |
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 66 def adapter ActiveRecord::Base.connection_db_config.adapter.to_s end |
#copy_migration ⇒ Object
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_template ⇒ Object
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 |
#encryption ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 45 def encryption case [:encryption] when "lockbox", "activerecord", "none" [:encryption] else abort "Error: encryption must be lockbox, activerecord, or none" end end |
#foreign_key_type ⇒ Object
74 75 76 |
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 74 def foreign_key_type ", type: :#{key_type}" if key_type end |
#key_type ⇒ Object
78 79 80 |
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 78 def key_type Rails.configuration.generators..dig(:active_record, :primary_key_type) end |
#lockbox_method ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 54 def lockbox_method if defined?(Lockbox::VERSION) && Lockbox::VERSION.to_i < 1 "encrypts" else "has_encrypted" end end |
#migration_version ⇒ Object
28 29 30 |
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 28 def migration_version "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]" end |
#mysql? ⇒ Boolean
62 63 64 |
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 62 def mysql? adapter =~ /mysql|trilogy/i end |
#primary_key_type ⇒ Object
70 71 72 |
# File 'lib/generators/ahoy/messages/activerecord_generator.rb', line 70 def primary_key_type ", id: :#{key_type}" if key_type end |
#to_column ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# 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 if encryption == "activerecord" && mysql? "t.string :to, limit: 510, index: true" else "t.string :to, index: true" end end end |