Sha256: e063a4cfad1bd7494fd359da2a4dee51c26958e0108fef760078ab2549a463ca

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

module Localtower
  module Generators
    class Relation
      def initialize(opts)
        @opts = JSON[opts.to_json]
      end

      def run
        model_one_name = @opts["model_one_name"]
        model_two_name = @opts["model_two_name"]

        if not model_one_name.present? and not model_two_name.present?
          return nil
        end

        model_name = "#{model_one_name.capitalize}#{model_two_name.capitalize}"

        data = {
          run_migrate: true,
          model_name: model_name,
          attributes: [
            {
              attribute_name: model_one_name.downcase,
              attribute_type: "references"
            },
            {
              attribute_name: model_two_name.downcase,
              attribute_type: "references"
            },
          ]
        }

        ::Localtower::Generators::Model.new(data.deep_stringify_keys).run

        # model 1:
        file = "#{Rails.root}/app/models/#{model_one_name.underscore}.rb"
        after = /class #{model_one_name} < ApplicationRecord\n/
        line1 = "  has_many :#{model_name.pluralize.underscore}\n"
        line2 = "  has_many :#{model_two_name.pluralize.underscore}, through: :#{model_name.pluralize.underscore}\n"

        ::Localtower::Tools::ThorTools.new.insert_after_word(file, after, line1)
        ::Localtower::Tools::ThorTools.new.insert_after_word(file, after, line2)

        # model 2:
        file = "#{Rails.root}/app/models/#{model_two_name.underscore}.rb"
        after = /class #{model_two_name} < ApplicationRecord\n/
        line1 = "  has_many :#{model_name.pluralize.underscore}\n"
        line2 = "  has_many :#{model_one_name.pluralize.underscore}, through: :#{model_name.pluralize.underscore}\n"

        ::Localtower::Tools::ThorTools.new.insert_after_word(file, after, line1)
        ::Localtower::Tools::ThorTools.new.insert_after_word(file, after, line2)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
localtower-0.1.4 lib/localtower/generators/relation.rb
localtower-0.1.3 lib/localtower/generators/relation.rb
localtower-0.1.2 lib/localtower/generators/relation.rb
localtower-0.1.1 lib/localtower/generators/relation.rb