Sha256: d94de083b4cf1910dfdc8e57f43b1d8d9fd1740d089e5911e92427ffe8460dc6

Contents?: true

Size: 1.94 KB

Versions: 4

Compression:

Stored size: 1.94 KB

Contents

require "rails/generators/active_record"

module Ahoy
  module Generators
    module Messages
      class ActiverecordGenerator < Rails::Generators::Base
        include ActiveRecord::Generators::Migration
        source_root File.join(__dir__, "templates")

        class_option :encryption, type: :string
        # deprecated
        class_option :unencrypted, type: :boolean

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

        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

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

        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

        # TODO remove default
        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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ahoy_email-2.3.1 lib/generators/ahoy/messages/activerecord_generator.rb
ahoy_email-2.3.0 lib/generators/ahoy/messages/activerecord_generator.rb
ahoy_email-2.2.0 lib/generators/ahoy/messages/activerecord_generator.rb
ahoy_email-2.1.3 lib/generators/ahoy/messages/activerecord_generator.rb