Module: Skyline::ContentItem

Included in:
LinkSectionLink, Sections::MediaSection, Sections::RedirectSection
Defined in:
lib/skyline/content_item.rb

Overview

Use this Module in in a class that references a Page/MediaFile etc throug an ObjectRef

Usage: class Model < ActiveRecord::Base

include Skyline::ContentItem

referable_content :teaser      (column teaser_id must be crreated)

end

1) Gives your Model the following interface:

class Model < ActiveRecord::Base
  before_save :set_refering_type_and_id
  after_create :set_refering_id
  named_scope :published             (scope to return only published items)

  def referable_content(*fields)

  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)
  alias_method_chain :teaser=, :passthrough
end

***** OR *****

it can be used to reference a Page/MediaFile etc directly when the foreign key must be serialized (or isn’t available as an association)

Usage: class Settings < ActiveRecord::Base

include Skyline::ContentItem

referable_serialized_content  :results_page      (obj.results_page_id and obj.results_page_id= must be available)

end

1) Gives your Settings the following interface:

class Settings < ActiveRecord::Base
  def referable_serialized_content(*fields)

  def results_page_attributes=       (set results_page_id to the referable_id; bypassing an ObjectRef)
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#clone_with_referable_contentObject



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/skyline/content_item.rb', line 167

def clone_with_referable_content
  returning clone_without_referable_content do |clone|      
    if self.referable_contents.any?
      self.referable_contents.each do |field|
        if self.send(field).present?
          clone.send("#{field}_id=", nil)
          clone.send("#{field}=", self.send(field).clone)
        end
      end
    end
  end
end

#possibly_destroy_previous_referablesObject



180
181
182
183
184
185
186
187
# File 'lib/skyline/content_item.rb', line 180

def possibly_destroy_previous_referables
  return unless self.previous_referables
  self.previous_referables.each do |field, previous_referable|
    if previous_referable != self.send(field).referable
      previous_referable.destroy if previous_referable.kind_of?(Skyline::ReferableUri)
    end
  end
end

#possibly_destroy_referablesObject



189
190
191
192
193
194
195
# File 'lib/skyline/content_item.rb', line 189

def possibly_destroy_referables
  self.referable_contents.each do |field|
    if object_ref = self.send(field) 
      object_ref.destroy if object_ref.kind_of?(Skyline::ReferableUri)
    end      
  end
end

#set_refering_idObject



159
160
161
162
163
164
165
# File 'lib/skyline/content_item.rb', line 159

def set_refering_id
  self.referable_contents.each do |field|
    if object_ref = self.send(field) 
      object_ref.update_attribute(:refering_id, self.id) if object_ref.refering_id.blank?
    end
  end
end

#set_refering_type_and_idObject



150
151
152
153
154
155
156
157
# File 'lib/skyline/content_item.rb', line 150

def set_refering_type_and_id
  self.referable_contents.each do |field|
    if object_ref = self.send(field)
      object_ref.refering_type = self.class.name
      object_ref.refering_id = self.id unless self.new_record?
    end
  end
end