Class: Helene::Sdb::Base::Association::BelongsTo

Inherits:
Helene::Sdb::Base::Association show all
Defined in:
lib/helene/sdb/base/associations.rb

Instance Attribute Summary collapse

Attributes inherited from Helene::Sdb::Base::Association

#base, #class_name, #conditions, #dependent, #foreign_key, #foreign_keys, #name, #options

Instance Method Summary collapse

Methods inherited from Helene::Sdb::Base::Association

#associated_class, attr, #conditions_for, #initialize_record

Constructor Details

#initialize(base, name, options = {}, &block) ⇒ BelongsTo

Returns a new instance of BelongsTo.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/helene/sdb/base/associations.rb', line 301

def initialize(base, name, options = {}, &block)
  super

  @polymorphic ||= options[:polymorphic]

  if @polymorphic
    @foreign_type ||= "#{ @polymorphic }_type"
    @foreign_key ||= "#{ @polymorphic }_id"
  end

  @foreign_key ||= options[:foreign_key] || class_name.foreign_key

  lineno, code = __LINE__ + 1, <<-__
    def #{ name }(*args, &block)
      @#{ name }_record ||= nil
      options = args.extract_options!.to_options!
      forcing = options.delete(:force)
      @#{ name }_record =   nil if forcing
      @#{ name }_record ||= #{ name }_association.get(self, *args, &block)
    end

    def #{ name }=(value)
      record =
        case value
          when Hash
            build_#{ name }(value)
          when Base
            value
        end
      #{ name }_association.set(self, record)
    end

    def build_#{ name }(attributes = {})
      record         = #{ class_name }.new(attributes)
      self.#{ name } = record
      record
    end

    def create_#{ name }(attributes = {})
      record = build_#{ name }(attributes)
      record.save
      record
    end
  __
  filename = __FILE__
  eval code, @base.module_eval('binding'), filename, lineno
end

Instance Attribute Details

#foreign_typeObject (readonly)

Returns the value of attribute foreign_type.



299
300
301
# File 'lib/helene/sdb/base/associations.rb', line 299

def foreign_type
  @foreign_type
end

#polymorphicObject (readonly)

Returns the value of attribute polymorphic.



298
299
300
# File 'lib/helene/sdb/base/associations.rb', line 298

def polymorphic
  @polymorphic
end

Instance Method Details

#find_associated_object_for(record) ⇒ Object



353
354
355
356
357
358
359
360
361
# File 'lib/helene/sdb/base/associations.rb', line 353

def find_associated_object_for(record)
  return nil unless record[foreign_key]
  conditions = {}
  if foreign_type
    conditions[foreign_type] = class_name
  end
  conditions[:id] = record[foreign_key]
  associated = associated_class.find(:first, :conditions => conditions_for(conditions))
end

#get(record, *args, &block) ⇒ Object



349
350
351
# File 'lib/helene/sdb/base/associations.rb', line 349

def get(record, *args, &block)
  find_associated_object_for(record)
end

#set(record, value) ⇒ Object



363
364
365
366
# File 'lib/helene/sdb/base/associations.rb', line 363

def set(record, value)
  record[foreign_key] = value.is_a?(Base) ? value.id : value
  value
end