module DutyFree::SuggestTemplate::ClassMethods

Public Instance Methods

suggest_template(hops = 0, do_has_many = false, show_output = true, this_klass = self) click to toggle source

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

# 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_assocs, [])
  ::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: {}
  }
  # puts "Errors: #{::DutyFree.instance_variable_get(:@errored_assocs).inspect}"

  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