Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_owner.rb

Class Method Summary collapse

Class Method Details

.acts_as_owner(options = {}, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/acts_as_owner.rb', line 35

def self.acts_as_owner(options = {}, &block)
  configuration = {
    :max_depth => 50,
    :owner_id  => name.tableize.singularize.to_sym }
  configuration.update(options) if options.is_a?(Hash)

  write_inheritable_hash(:ownership_options, configuration)
  write_inheritable_hash(:ownable_ids, {})

  class << self
    def owns_many(association_id, options = {})
      read_inheritable_attribute(:ownable_ids).update({
        association_id.to_s.singularize.to_sym =>
          read_inheritable_attribute(:ownership_options).merge(options) })
    end

    def owns_one(association_id, options = {})
      read_inheritable_attribute(:ownable_ids).update({
        association_id.to_sym =>
          read_inheritable_attribute(:ownership_options).merge(options) })
    end

    def could_own_a?(association_id)
      read_inheritable_attribute(:ownable_ids).keys.
        include?(association_id.to_sym)
    end

    alias_method :could_own_an?, :could_own_a?
  end

  instance_eval &block

  include ActsAsOwner
end