Sha256: 7b78fa586cf2daa89f4321992d7418c9cad5298c35f04048950a673d26b50658

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 PayGenerator < ActiveRecord::Generators::Base
      include Pay::Generators::OrmHelpers
      source_root File.expand_path("../templates", __FILE__)

      def copy_pay_billable_migration
        if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
          migration_template "migration.rb", "#{migration_path}/add_pay_billable_to_#{table_name}.rb", migration_version: migration_version
        end
        # TODO: Throw error here that model should already exist if it doesn't
      end

      def inject_pay_billable_content
        content = model_contents

        class_path = if namespaced?
          class_name.to_s.split("::")
        else
          [class_name]
        end

        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) if model_exists?
      end

      def migration_data
        <<RUBY
      t.string :processor
      t.string :processor_id
      t.datetime :trial_ends_at
      t.string :card_type
      t.string :card_last4
      t.string :card_exp_month
      t.string :card_exp_year
      t.text :extra_billing_info
RUBY
      end

      def rails5_and_up?
        Rails::VERSION::MAJOR >= 5
      end

      def migration_version
        if rails5_and_up?
          "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
pay-2.7.0 lib/generators/active_record/pay_generator.rb
pay-2.6.11 lib/generators/active_record/pay_generator.rb
pay-2.6.10 lib/generators/active_record/pay_generator.rb
pay-2.6.9 lib/generators/active_record/pay_generator.rb
pay-2.6.8 lib/generators/active_record/pay_generator.rb
pay-2.6.7 lib/generators/active_record/pay_generator.rb
pay-2.6.6 lib/generators/active_record/pay_generator.rb
pay-2.6.5 lib/generators/active_record/pay_generator.rb
pay-2.6.4 lib/generators/active_record/pay_generator.rb
pay-2.6.3 lib/generators/active_record/pay_generator.rb
pay-2.6.2 lib/generators/active_record/pay_generator.rb
pay-2.6.1 lib/generators/active_record/pay_generator.rb
pay-2.6.0 lib/generators/active_record/pay_generator.rb
pay-2.5.0 lib/generators/active_record/pay_generator.rb
pay-2.4.4 lib/generators/active_record/pay_generator.rb
pay-2.4.3 lib/generators/active_record/pay_generator.rb
pay-2.4.2 lib/generators/active_record/pay_generator.rb
pay-2.4.0 lib/generators/active_record/pay_generator.rb
pay-2.3.1 lib/generators/active_record/pay_generator.rb
pay-2.3.0 lib/generators/active_record/pay_generator.rb