Module: Skyline::BelongsToReferable

Defined in:
lib/skyline/belongs_to_referable.rb

Overview

Use this Module in in a class that references a Page/MediaFile/URL as an association. It adds the class method ‘belongs_to_referable`. With Referable::ClassMethods#belongs_to_referable you create a `belongs_to` association to an Page/MediaFile/URL through a RefObject.

class Model < ActiveRecord::Base

include Skyline::BelongsToReferable

belongs_to_referable :teaser      # column teaser_id must be available

end

class Model < ActiveRecord::Base

after_save :possibly_destroy_previous_referables
after_destroy :possibly_destroy_referables
before_save :set_refering_type_and_id
after_create :set_refering_id

belongs_to :teaser, :class_name => "Skyline::ObjectRef", :foreign_key => "teaser_id", :dependent => :destroy
accepts_nested_attributes_for :teaser, :reject_if => proc {|attributes| attributes['referable_type'].blank?}, :allow_destroy => true
validates_presence_of :teaser   # only if options[:allow_nil] is not set

def teaser_with_passthrough=(obj) # obj can be an ObjectRef or a Teaser, in which case it will be passed through
  # ...
end
alias_method_chain :teaser=, :passthrough

end

Examples:

Usage

Gives your Model the following interface:

See Also:

Defined Under Namespace

Modules: ClassMethods