Class: Localtower::Generators::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/localtower/generators/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Relation

Returns a new instance of Relation.



4
5
6
# File 'lib/localtower/generators/relation.rb', line 4

def initialize(opts)
  @opts = JSON[opts.to_json]
end

Instance Method Details

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/localtower/generators/relation.rb', line 8

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

  if @opts["model_name"].present?
    model_name = @opts["model_name"].capitalize
  else
    model_name = "#{model_one_name.capitalize}#{model_two_name.capitalize}"
  end

  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