module ColumnsOnDemand::InstanceMethods

Public Instance Methods

_has_attribute?(attr_name) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 71
def _has_attribute?(attr_name)
  super || columns_to_load_on_demand.include?(attr_name)
end
_read_attribute(attr_name, &block) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 113
def _read_attribute(attr_name, &block)
  ensure_loaded(attr_name)
  super(attr_name, &block)
end
_write_attribute(attr_name, value) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 123
def _write_attribute(attr_name, value)
  ensure_loaded(attr_name)
  super(attr_name, value)
end
attribute_changed_in_place?(attr_name) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 99
def attribute_changed_in_place?(attr_name)
  column_loaded?(attr_name) && super(attr_name)
end
attribute_method?(attr_name) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 67
def attribute_method?(attr_name)
  super || columns_to_load_on_demand.include?(attr_name)
end
attribute_names() click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 63
def attribute_names
  (super + columns_to_load_on_demand).uniq.sort
end
attributes() click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 54
def attributes
  loaded_attributes = @attributes.keys
  if loaded_attributes.size == self.class.columns_to_load_by_default.size &&
     loaded_attributes.size == (self.class.columns_to_load_by_default & loaded_attributes).size
    load_attributes(*columns_to_load_on_demand.reject {|attr_name| column_loaded?(attr_name)})
  end
  super
end
changed_in_place?(attr_name) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 95
def changed_in_place?(attr_name)
  column_loaded?(attr_name) && super(attr_name)
end
column_loaded?(attr_name) click to toggle source
# File lib/columns_on_demand.rb, line 50
def column_loaded?(attr_name)
  !columns_to_load_on_demand.include?(attr_name) || @attributes.key?(attr_name) || new_record? || columns_loaded.include?(attr_name)
end
columns_loaded() click to toggle source
# File lib/columns_on_demand.rb, line 46
def columns_loaded
  @columns_loaded ||= Set.new
end
ensure_loaded(attr_name) click to toggle source
# File lib/columns_on_demand.rb, line 91
def ensure_loaded(attr_name)
  load_attributes(attr_name.to_s) unless column_loaded?(attr_name.to_s)
end
load_attributes(*attr_names) click to toggle source
# File lib/columns_on_demand.rb, line 75
def load_attributes(*attr_names)
  return if attr_names.blank?

  values = self.class.connection.select_rows(
    "SELECT #{attr_names.collect {|attr_name| self.class.connection.quote_column_name(attr_name)}.join(", ")}" +
    "  FROM #{self.class.quoted_table_name}" +
    " WHERE #{self.class.connection.quote_column_name(self.class.primary_key)} = #{self.class.connection.quote(id)}")
  row = values.first || raise(ActiveRecord::RecordNotFound, "Couldn't find #{self.class.name} with ID=#{id}")

  attr_names.each_with_index do |attr_name, i|
    columns_loaded << attr_name
    value = row[i]
    @attributes.write_from_database(attr_name, value)
  end
end
missing_attribute(attr_name, *args) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 128
def missing_attribute(attr_name, *args)
  if columns_to_load_on_demand.include?(attr_name)
    load_attributes(attr_name)
  else
    super(attr_name, *args)
  end
end
read_attribute(attr_name, &block) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 103
def read_attribute(attr_name, &block)
  ensure_loaded(attr_name)
  super(attr_name, &block)
end
read_attribute_before_type_cast(attr_name) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 108
def read_attribute_before_type_cast(attr_name)
  ensure_loaded(attr_name)
  super(attr_name)
end
reload(*args) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 136
def reload(*args)
  super(*args).tap do
    columns_loaded.clear
    columns_to_load_on_demand.each do |attr_name|
      if @attributes.respond_to?(:reset)
        # 4.2 and above
        @attributes.reset(attr_name)
      else
        # 4.1 and earlier
        @attributes.delete(attr_name)
      end
    end
  end
end
write_attribute(attr_name, value) click to toggle source
Calls superclass method
# File lib/columns_on_demand.rb, line 118
def write_attribute(attr_name, value)
  ensure_loaded(attr_name)
  super(attr_name, value)
end