Module: Spider::Model::ActiveRecordModel::MapperMethods

Defined in:
lib/spiderfw/model/active_record.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) before_save(obj, mode)



259
260
261
262
263
# File 'lib/spiderfw/model/active_record.rb', line 259

def before_save(obj, mode)
    obj.obj_created = DateTime.now if obj.respond_to?(:obj_created=) && mode == :insert
    obj.obj_modified = DateTime.now if obj.respond_to?(:obj_modified=) && obj.modified?
    super
end

- (Object) generate_schema(schema = nil)



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/spiderfw/model/active_record.rb', line 265

def generate_schema(schema=nil)
    return super unless @model.ar_schema
    schema = Spider::Model::Storage::Db::DbSchema.new
    schema.table = @model.ar_schema[:table]
    @model.ar_schema[:columns].each do |name, field|
        element = @model.elements[name]
        if (element.model?)
            storage_type = Spider::Model.base_type(element.type.primary_keys[0].type)
        else
            storage_type = Spider::Model.base_type(element.type)
        end
        column_type = @storage.column_type(storage_type, element.attributes)
        db_attributes = @storage.column_attributes(storage_type, element.attributes)
        if (element.model?)
            schema.set_foreign_key(element.name, element.type.primary_keys[0].name, 
            :name => field,
            :type => column_type,
            :attributes => db_attributes
            )
        else
            schema.set_column(name,
            :name => field,
            :type => column_type,
            :attributes => db_attributes
            )
        end
    end
    return schema
end