class ActivePreview::Preview

Constants

COLLECTION
SINGULAR

~ used in place of association names

Public Class Methods

new(record) click to toggle source
Calls superclass method
# File lib/active_preview/preview.rb, line 8
def initialize(record)
  redefine_associations(record)
  super(record)
end

Public Instance Methods

ignored_associations() click to toggle source

Override in model-specific preview classes to disable previewing of certain associations

# File lib/active_preview/preview.rb, line 19
def ignored_associations
  %w().freeze
end
model_object() click to toggle source
# File lib/active_preview/preview.rb, line 13
def model_object
  __getobj__
end

Private Instance Methods

redefine_associations(record) click to toggle source
# File lib/active_preview/preview.rb, line 29
def redefine_associations(record)
  associations(record.class).each do |a|
    next if ignored_associations.include?(a) || respond_to?("#{a}=")
    methods = singular?(a) ? SINGULAR : COLLECTION
    methods.each do |method|
      self.class.send(:define_method, method.gsub("~", a)) {}
    end
    self.class.send(:define_method, "#{a}=") do |associated|
      instance_variable_set("@#{a}", associated)
      unless instance_variable_get("@#{a}_defined")
        define_singleton_method(a) { instance_variable_get "@#{a}" }
        instance_variable_set("@#{a}_defined", true)
      end
    end
  end
end