module LazyAttributes::Base::ClassMethods

Public Instance Methods

column_names_without_lazy() click to toggle source
# File lib/lazy_attributes.rb, line 40
def column_names_without_lazy
  column_names - self._lazy_attributes
end
column_symbols_without_lazy() click to toggle source
# File lib/lazy_attributes.rb, line 44
def column_symbols_without_lazy
  column_names_without_lazy.map(&:to_sym)
end
lazy_attributes(*attrs) click to toggle source
# File lib/lazy_attributes.rb, line 11
def lazy_attributes(*attrs)
  if self._lazy_attributes.empty?
    default_scope do
      select(column_names_without_lazy.map do |column_name|
        "#{table_name}.#{column_name}"
      end)
    end
  end
  attrs = attrs.map(&:to_s)
  attrs.each do |attr|
    attr = attr.to_sym
    define_method(attr) do
      unless has_attribute?(attr)
        self.class.define_attribute_method(attr)
        write_attribute(attr, self.class.where(id: self.id).pluck(attr).first)
      end
      read_attribute(attr)
    end
    define_method(:"#{attr}=") do |val|
      unless has_attribute?(attr)
        self.class.define_attribute_method(attr)
        write_attribute(attr, self.class.where(id: self.id).pluck(attr).first)
      end
      write_attribute(attr, val)
    end
  end
  self._lazy_attributes += attrs
end