Sha256: 2c62e7b10e3cba463a30df83033ae5dfe33f536bcb0708cd7339d5ae203a45c6

Contents?: true

Size: 1.65 KB

Versions: 103

Compression:

Stored size: 1.65 KB

Contents

# ATTRIBUTION NOTE: This module has been mostly copied from the
# lazy_columns gem. The original code can be found at:
# https://github.com/jorgemanrubia/lazy_columns

module Marty
  module LazyColumnLoader
    extend ActiveSupport::Concern

    module ClassMethods
      def lazy_load(*columns)
        return unless table_exists?
        columns = columns.collect(&:to_s)
        exclude_columns_from_default_scope columns
        define_lazy_accessors_for columns

        # allow introspection of lazy-loaded column list
        const_set(:LAZY_LOADED, columns)
      end

    private
      def exclude_columns_from_default_scope(columns)
        default_scope {
          select((column_names - columns).map {
                   |column_name|
                   "#{table_name}.#{column_name}"
                 })
        }
      end

      def define_lazy_accessors_for(columns)
        columns.each { |column| define_lazy_accessor_for column }
      end

      def define_lazy_accessor_for(column)
        define_method column do
          unless has_attribute?(column)
            changes_before_reload = self.changes.clone
            self.reload
            changes_before_reload.each{
              |attribute_name, values|
              self.send("#{attribute_name}=", values[1])
            }
          end
          read_attribute column
        end
      end
    end
  end
end

if ActiveRecord::Base.respond_to?(:lazy_load)
  $stderr.puts "ERROR: Method `.lazy_load` already defined in " +
    "`ActiveRecord::Base`. This is incompatible with LazyColumnLoader " +
    "and the module will be disabled."
else
  ActiveRecord::Base.send :include, Marty::LazyColumnLoader
end

Version data entries

103 entries across 103 versions & 1 rubygems

Version Path
marty-2.0.6 lib/marty/lazy_column_loader.rb
marty-2.0.5 lib/marty/lazy_column_loader.rb
marty-2.0.4 lib/marty/lazy_column_loader.rb
marty-2.0.3 lib/marty/lazy_column_loader.rb
marty-2.0.2 lib/marty/lazy_column_loader.rb
marty-2.0.1 lib/marty/lazy_column_loader.rb
marty-2.0.0 lib/marty/lazy_column_loader.rb
marty-1.2.9 lib/marty/lazy_column_loader.rb
marty-1.2.8 lib/marty/lazy_column_loader.rb
marty-1.2.7 lib/marty/lazy_column_loader.rb
marty-1.2.6 lib/marty/lazy_column_loader.rb
marty-1.2.5 lib/marty/lazy_column_loader.rb
marty-1.2.4 lib/marty/lazy_column_loader.rb
marty-1.2.3 lib/marty/lazy_column_loader.rb
marty-1.2.2 lib/marty/lazy_column_loader.rb
marty-1.2.1 lib/marty/lazy_column_loader.rb
marty-1.2.0 lib/marty/lazy_column_loader.rb
marty-1.1.9 lib/marty/lazy_column_loader.rb
marty-1.1.8 lib/marty/lazy_column_loader.rb
marty-1.1.7 lib/marty/lazy_column_loader.rb