Sha256: ec9d631dc1277730bb83c36a1b3914a2593b006ecf18b7d00cf0d1f23f26a6c0

Contents?: true

Size: 1.61 KB

Versions: 21

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require "rails/generators/active_record"
require "generators/pay/orm_helpers"

module ActiveRecord
  module Generators
    class MerchantGenerator < ActiveRecord::Generators::Base
      include Pay::Generators::OrmHelpers
      source_root File.expand_path("../templates", __FILE__)

      def copy_pay_merchant_migration
        if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
          migration_template "merchant_migration.rb", "#{migration_path}/add_pay_merchant_to_#{table_name}.rb", migration_version: migration_version
        else
          say "#{model_path} does not exist.", :red
          say "⚠️  Make sure the #{name} model exists before running this generator."
        end
      end

      # If the file already contains the contents, the user will receive this warning:
      #
      #   File unchanged! The supplied flag value not found!
      #
      # This can be ignored as it just means the contents already exist and the file is unchanged.
      # Thor will be updated to improve this message: https://github.com/rails/thor/issues/706
      def inject_pay_merchant_content
        return unless model_exists?

        content = model_contents
        class_path = (namespaced? ? class_name.to_s.split("::") : [class_name])
        indent_depth = class_path.size - 1
        content = content.split("\n").map { |line| "  " * indent_depth + line }.join("\n") << "\n"
        inject_into_class(model_path, class_path.last, content)
      end

      private

      def model_contents
        "  include Pay::Merchant"
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
pay-3.0.19 lib/generators/active_record/merchant_generator.rb
pay-3.0.18 lib/generators/active_record/merchant_generator.rb
pay-3.0.17 lib/generators/active_record/merchant_generator.rb
pay-3.0.16 lib/generators/active_record/merchant_generator.rb
pay-3.0.15 lib/generators/active_record/merchant_generator.rb
pay-3.0.14 lib/generators/active_record/merchant_generator.rb
pay-3.0.13 lib/generators/active_record/merchant_generator.rb
pay-3.0.12 lib/generators/active_record/merchant_generator.rb
pay-3.0.11 lib/generators/active_record/merchant_generator.rb
pay-3.0.10 lib/generators/active_record/merchant_generator.rb
pay-3.0.9 lib/generators/active_record/merchant_generator.rb
pay-3.0.8 lib/generators/active_record/merchant_generator.rb
pay-3.0.7 lib/generators/active_record/merchant_generator.rb
pay-3.0.6 lib/generators/active_record/merchant_generator.rb
pay-3.0.5 lib/generators/active_record/merchant_generator.rb
pay-3.0.4 lib/generators/active_record/merchant_generator.rb
pay-3.0.2 lib/generators/active_record/merchant_generator.rb
pay-3.0.1 lib/generators/active_record/merchant_generator.rb
pay-3.0.0 lib/generators/active_record/merchant_generator.rb
pay-2.7.2 lib/generators/active_record/merchant_generator.rb