class ActiveRecord::Has::SparseAttributes::TableStorage

Attributes

sparse_attribute_values[RW]
sparse_attributes[RW]
updated_sparse_attributes[RW]

Public Instance Methods

after_save(*args) click to toggle source
# File lib/active_record/has/sparse_attributes/table_storage.rb, line 37
def after_save(*args)
        save()
end
get(name) click to toggle source
# File lib/active_record/has/sparse_attributes/table_storage.rb, line 82
def get(name)
        load() if @sparse_attributes.nil?
        return @sparse_attribute_values[name.to_s]
end
load() click to toggle source
# File lib/active_record/has/sparse_attributes/table_storage.rb, line 41
def load()
        @sparse_attributes = {}
        @sparse_attribute_values = {}
        @updated_sparse_attributes = []
        
        # The item has not been saved - nothing to load
        if @record.id.nil?
                return
        end
        
        unserialize = @config.serialize_values
        attributes = @config.attribute_model.where(@config.id_column => @record.id)
        attributes.each do |attr|
                @sparse_attributes[attr.name] = attr
                @sparse_attribute_values[attr.name] = unserialize ? YAML::load(attr.value) : attr.value
        end
end
save() click to toggle source
# File lib/active_record/has/sparse_attributes/table_storage.rb, line 59
def save()
        return 0 if @updated_sparse_attributes.nil?
        num_updates = 0
        klass = @config.attribute_model
        klass_id_column = @config.id_column
        serialize = @config.serialize_values
        @updated_sparse_attributes.each do |name|
                value = @sparse_attribute_values[name]
                have_attribute = @sparse_attributes.has_key?(name)
                
                # If the value is nil we will delete the attribute row
                if value.nil?
                        num_updates += delete_row(name)
                else
                        value = value.to_yaml if serialize
                        num_updates += 1 if set_row(name, value)
                end
        end

        @updated_sparse_attributes = []
        return num_updates
end
set(name, value) click to toggle source
# File lib/active_record/has/sparse_attributes/table_storage.rb, line 87
def set(name, value)
        load() if @sparse_attributes.nil?

        name = name.to_s
        if value.nil?
                @sparse_attribute_values[name] = nil
        else
                value = value.to_s unless @config.serialize_values
                @sparse_attribute_values[name] = value
        end
        @updated_sparse_attributes << name
end

Protected Instance Methods

delete_row(name) click to toggle source
# File lib/active_record/has/sparse_attributes/table_storage.rb, line 102
def delete_row(name)
        klass = @config.attribute_model
        have_attribute = @sparse_attributes.has_key?(name)
                        
        # If we have the attribute ActiveRecord instance
        # we may be able to just delete the row
        deleted = klass.delete(@sparse_attributes[name].id) if have_attribute

        # If the attribute couldn't be deleted by id
        # we just scan for the record
        if !have_attribute || deleted == 0
                deleted = klass.where(["#{@config.id_column} = ? AND name = ?", @record.id, name]).delete_all
        end
        return deleted
end
set_row(name, value) click to toggle source
# File lib/active_record/has/sparse_attributes/table_storage.rb, line 118
def set_row(name, value)
        klass = @config.attribute_model
        klass_id_column = @config.id_column
        have_attribute = @sparse_attributes.has_key?(name)

        if have_attribute                                  
                # We already have the attribute so we should be able to just update
                # the row based on the id, unless it has been deleted
                attribute = @sparse_attributes[name]
                attribute.value = value
                updated = attribute.save
        end
                
        if !have_attribute || updated == false
                if klass.respond_to?(:find_or_create_by)
                        attribute = klass.create_with(:value => value).find_or_create_by(klass_id_column => @record.id, :name => name)
                else
                        method_name = ('find_or_create_by_' + klass_id_column.to_s + '_and_name').to_sym
                        attribute = klass.send(method_name, klass_id_column => @record.id, :name => name, :value => value)
                end
                if attribute.value != value
                        attribute.value = value
                        updated = attribute.save
                else
                        updated = 1
                end
                
                @sparse_attributes[name] = attribute
        end
        return updated
end