Sha256: 77a5d43d75965ca8c04de0d1855630255b2926acf81e0990e8096d7e43021ca8
Contents?: true
Size: 671 Bytes
Versions: 1
Compression:
Stored size: 671 Bytes
Contents
module Convenience extend ActiveSupport::Concern module ClassMethods # Look up a belongs_to association by name. # E.g. # lookup_belongs_to :state, by: :name # Adds state_name as an ivar. 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}'.capitalize.constantize.where(#{lookup_by}: self.#{name}_#{lookup_by}).first end eoruby class_eval(code) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_admin_csv_import-1.2.0 | app/models/concerns/convenience.rb |