Class: ActiveRecord::Base

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

Constant Summary collapse

@@passes_on_to =
{}
@@inherits_many =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherits_many(source, params) ⇒ Object



64
65
66
67
68
69
# File 'lib/inherits_many.rb', line 64

def self.inherits_many(source, params)
  unless @@inherits_many.has_key? self.name
    @@inherits_many[self.name] = []
  end
  @@inherits_many[self.name] << {source: source}.merge(params)
end

.passes_on_to(target, params) ⇒ Object



6
7
8
9
10
11
# File 'lib/inherits_many.rb', line 6

def self.passes_on_to(target, params)
  unless @@passes_on_to.has_key? self.name
    @@passes_on_to[self.name] = []
  end
  @@passes_on_to[self.name] << {target: target}.merge(params)
end

Instance Method Details

#process_inherits_manyObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/inherits_many.rb', line 72

def process_inherits_many

  concrete_class = self.class

  while concrete_class.name != 'ActiveRecord::Base' do

    unless @@inherits_many[concrete_class.name].nil?

      @@inherits_many[concrete_class.name].each do |tt|

        child = tt[:source]
        parent = tt[:from]
        parent_id = "#{parent}_id"

        # if the chain has changed ..
        # if chain_id_changed?
        if self.changes.has_key? parent_id

          parent_id_was = self.changes[parent_id][0]
          parent_id_is = self.changes[parent_id][1]

          # if there was previously a chain ..
          if parent_id_was

            # remove all the menus from the old chain.
            self.send("#{child}=", self.send(child) - self.class.reflect_on_association(parent).class_name.constantize.find(parent_id_was).send(child))

          end

          # if there is a new chain ..
          if parent_id_is

            # add all the menu items from the new chain to this restaurant.
            self.send("#{child}=", self.send(child) + self.send(parent).send(child))

          end


          # if there was previously a chain ..
          if parent_id_was

            # remove all restaurants from the old chain.
            # self.restaurants = self.restaurants - Chain.find(chain_id_was).restaurants
            self.send("#{child}=", self.send(child) - self.class.reflect_on_association(parent).class_name.constantize.find(parent_id_was).send(child))

          end

          # if there is a new chain ..
          if parent_id_is

            # add all the menu items from the new chain to this restaurant.
            self.send("#{child}=", self.send(child) + self.send(parent).send(child))

          end

        end
      end

    end

    concrete_class = concrete_class.superclass

  end

end

#process_passes_on_toObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/inherits_many.rb', line 14

def process_passes_on_to

  concrete_class = self.class

  while concrete_class.name != 'ActiveRecord::Base' do

    unless @@passes_on_to[concrete_class.name].nil?

      @@passes_on_to[concrete_class.name].each do |tt|

        child = tt[:target]
        parent = tt[:of]
        parent_id = "#{parent}_id"

        # if the chain has changed ..
        # if chain_id_changed?
        if self.changes.has_key? parent_id

          parent_id_was = self.changes[parent_id][0]
          parent_id_is = self.changes[parent_id][1]

          # if there was previously a chain ..
          if parent_id_was

            # remove all restaurants from the old chain.
            # self.restaurants = self.restaurants - Chain.find(chain_id_was).restaurants
            self.send("#{child}=", self.send(child) - self.class.reflect_on_association(parent).class_name.constantize.find(parent_id_was).send(child))

          end

          # if there is a new chain ..
          if parent_id_is

            # add all the menu items from the new chain to this restaurant.
            self.send("#{child}=", self.send(child) + self.send(parent).send(child))

          end

        end
      end
    end

    concrete_class = concrete_class.superclass

  end

end