Module: HasInherited::ClassMethods

Defined in:
lib/has_inherited.rb

Overview

self.included

Instance Method Summary collapse

Instance Method Details

#has_inherited(*opts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/has_inherited.rb', line 24

def has_inherited(*opts)
  options = opts.extract_options!
  attr = opts.shift || 'inheritable'
  assoc = "_#{attr}"
  class_name = options[:inherit_class] || 'Seo'

  has_many assoc.to_sym, :class_name => class_name, :as => :inheritable, :dependent => :destroy

  define_method attr do
    instance_variable_get(:"@#{attr.to_s}") || instance_variable_set(:"@#{attr.to_s}", InheritAccessor.new(self, assoc.to_sym, options[:from]))
  end
end

#is_inheritable(*name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/has_inherited.rb', line 7

def is_inheritable(*name)
  attr = name.shift || :global
  assoc = "_#{attr}"

  scope assoc.to_sym,
      :conditions => 'inheritable_id IS NULL AND inheritable_type IS NULL'

  (class << self; self; end).instance_eval do
    define_method attr do
      instance_variable_get(:"@#{attr.to_s}") || instance_variable_set(:"@#{attr.to_s}", InheritAccessor.new(self, assoc.to_sym, nil))
    end
  end

  belongs_to :inheritable, :polymorphic => true
  include HasInherited::InstanceMethods
end