Sha256: 7f7f661340fb50df6cab4652bec6e47d0e493ab1d92805384d08e18fdb3d997f

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 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"
          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
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ahoy_email-2.1.2 lib/generators/ahoy/messages/activerecord_generator.rb
ahoy_email-2.1.1 lib/generators/ahoy/messages/activerecord_generator.rb