Module: DutyFree::SuggestTemplate::ClassMethods

Defined in:
lib/duty_free/suggest_template.rb

Instance Method Summary collapse

Instance Method Details

#suggest_template(hops = 0, do_has_many = false, show_output = true, this_klass = self) ⇒ Object

Helpful suggestions to get started creating a template Pass in -1 for hops if you want to traverse all possible links



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/duty_free/suggest_template.rb', line 9

def suggest_template(hops = 0, do_has_many = false, show_output = true, this_klass = self)
  ::DutyFree.instance_variable_set(:@errored_columns, [])
  uniques, _required = ::DutyFree::SuggestTemplate._suggest_unique_column(this_klass, nil, '')
  template, required = ::DutyFree::SuggestTemplate._suggest_template(hops, do_has_many, this_klass)
  template = {
    uniques: uniques,
    required: required.map(&:to_sym),
    all: template,
    as: {}
  }

  if show_output
    path = this_klass.name.split('::').map(&:underscore).join('/')
    puts "\n# Place the following into app/models/#{path}.rb:"
    arguments = method(__method__).parameters[0..2].map { |_, name| binding.local_variable_get(name).to_s }
    puts "# Generated by:  #{this_klass.name}.suggest_template(#{arguments.join(', ')})"
    ::DutyFree::SuggestTemplate._template_pretty_print(template)
    puts '# ------------------------------------------'
    puts
  end
  template
end