Module: ActiveAdminCsvImport::Convenience::ClassMethods

Defined in:
app/models/concerns/active_admin_csv_import/convenience.rb

Instance Method Summary collapse

Instance Method Details

#lookup_belongs_to(name, options) ⇒ Object

Look up a belongs_to association by name. E.g. lookup_belongs_to :state, by: :name Adds state_name as an ivar.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/concerns/active_admin_csv_import/convenience.rb', line 9

def lookup_belongs_to(name, options)

  lookup_by = options[:by]

  code = <<-eoruby
  attr_accessor :#{name}_#{lookup_by}

  before_validation :lookup_#{name}_by_#{lookup_by}

  def lookup_#{name}_by_#{lookup_by}
    return if self.#{name}_#{lookup_by}.blank?
    self.#{name} = '#{name}'.camelize.constantize.where(#{lookup_by}: self.#{name}_#{lookup_by}).first
  end
  eoruby
  class_eval(code)
end