Module: ActiveRecord::Associations::ClassMethods

Defined in:
lib/acts_as_tableless.rb

Instance Method Summary collapse

Instance Method Details

#active_record_belongs_toObject



102
# File 'lib/acts_as_tableless.rb', line 102

alias :active_record_belongs_to :belongs_to

#active_record_has_and_belongs_to_manyObject



103
# File 'lib/acts_as_tableless.rb', line 103

alias :active_record_has_and_belongs_to_many :has_and_belongs_to_many

#active_record_has_manyObject



100
# File 'lib/acts_as_tableless.rb', line 100

alias :active_record_has_many :has_many

#active_record_has_oneObject



101
# File 'lib/acts_as_tableless.rb', line 101

alias :active_record_has_one :has_one

#belongs_to(association_id, options = {}) ⇒ Object



265
266
267
268
269
270
271
272
273
274
# File 'lib/acts_as_tableless.rb', line 265

def belongs_to(association_id, options = {})
  active_record_belongs_to(association_id, options)
  class_variable_name = association_id.to_s.singularize
  if class_variable_name.camelize.constantize.send(:included_modules).include?(ActsAsTableless) # || ActsAsTableless.class_variables.include?(:"@@#{class_variable_name}")
    association_class = class_variable_name.camelize.constantize rescue nil
    define_method(association_id.to_s) do
      association_class.all.select{|record| record.id == self.send("#{association_class.name.underscore}_id")}.first
    end
  end
end

#has_and_belongs_to_many(association_id, options = {}, &extension) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
# File 'lib/acts_as_tableless.rb', line 276

def has_and_belongs_to_many(association_id, options = {}, &extension)
  active_record_has_and_belongs_to_many(association_id, options, &extension)
  class_variable_name = association_id.to_s.singularize
  if class_variable_name.camelize.constantize.send(:included_modules).include?(ActsAsTableless) # || ActsAsTableless.class_variables.include?(:"@@#{class_variable_name}")
    association_class = class_variable_name.camelize.constantize rescue nil
    # not yet implemented, and may never be; use has_many
    define_method(association_id.to_s) do
      []
    end
  end
end

#has_many(association_id, options = {}, &extension) ⇒ Object



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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/acts_as_tableless.rb', line 105

def has_many(association_id, options = {}, &extension)
  active_record_has_many(association_id, options, &extension)
  class_variable_name = association_id.to_s.singularize
  if class_variable_name.camelize.constantize.send(:included_modules).include?(ActsAsTableless) # || ActsAsTableless.class_variables.include?(:"@@#{class_variable_name}")
    association_class = class_variable_name.camelize.constantize rescue nil
    if options.include?(:through)
      define_method(association_id.to_s) do
        through_objects = self.send(options[:through])
        records = if through_objects.nil?
          []
        else
          through_objects = [through_objects] unless through_objects.is_a?(Array)
          through_association_id = [:has_one, :belongs_to].include?( options[:through].to_s.singularize.camelize.constantize.reflect_on_all_associations.select{ |associations| associations.name.to_s == class_variable_name }.first.macro ) ? association_id.to_s.singularize.to_sym : association_id
          through_objects.collect{|object| object.send(through_association_id) }.flatten
        end

        records.instance_variable_set(:@parent, self)
        records.instance_variable_set(:@options, options)
        records.instance_variable_set(:@association_class, association_class)
        def records.<<(associated_records)
          associated_records = [associated_records] unless associated_records.is_a?(Array)
          case @parent.class.reflect_on_all_associations.select{|association| association.name == @options[:through]}.first.macro
          when :has_many
            associated_records.each do |associated_record|
              raise ActiveRecord::AssociationTypeMismatch, "#{@association_class.name} expected, got #{associated_record.inspect}" unless @association_class.name == associated_record.class.name
              @options[:through].to_s.singularize.camelize.constantize.create("#{@parent.class.name.underscore}_id".to_sym => @parent.id, "#{@association_class.name.underscore}_id".to_sym => associated_record.id)
            end
          when :has_one
            # not yet implemented
            []
          when :belongs_to
            # not yet implemented
            []
          when :has_and_belongs_to_many
            # not yet implemented
            []
          end
          self
        end
        def records.create(new_records_attributes = nil, options = {})
          new_records = new_records_attributes.is_a?(Array) ? [] : nil
          new_records_attributes = [new_records_attributes] unless new_records_attributes.is_a?(Array)
          case @parent.class.reflect_on_all_associations.select{|association| association.name == @options[:through]}.first.macro
          when :has_many
            new_records_attributes = [new_records_attributes] unless new_records_attributes.is_a?(Array)
            new_records = []
            new_records_attributes.each do |attributes|
              new_record = @association_class.create(attributes)
              @options[:through].to_s.singularize.camelize.constantize.create("#{@parent.class.name.underscore}_id".to_sym => @parent.id, "#{@association_class.name.underscore}_id".to_sym => new_record.id)
              if new_records.is_a?(Array)
                new_records << new_record
              else
                return new_record
              end
            end
          when :has_one
            # not yet implemented
            []
          when :belongs_to
            # not yet implemented
            []
          when :has_and_belongs_to_many
            # not yet implemented
            []
          end
          return new_records
        end
        
        return records
      end
      
      define_method("#{association_id.to_s}=") do |associated_records|
        new_associated_records = associated_records.is_a?(Array) ? [] : nil
        associated_records = [associated_records] unless associated_records.is_a?(Array)
        through_class = options[:through].to_s.singularize.camelize.constantize
        through_class.all.select{|r|r.send("#{self.class.name.underscore}_id") == self.id}.each{|r|r.delete} # this line could be made more efficient
        case self.class.reflect_on_all_associations.select{|association| association.name == options[:through]}.first.macro
        when :has_many
          associated_records.each do |associated_record|
            new_associated_record = through_class.new
            new_associated_record.send("#{self.class.name.underscore}_id=", self.id)
            new_associated_record.send("#{association_class.name.underscore}_id=", associated_record.id)
            new_associated_record.save
            if new_associated_records.is_a?(Array)
              new_associated_records << new_associated_record
            else
              return new_associated_record
            end
          end
        when :has_one
          # not yet implemented
          []
        when :belongs_to
          # not yet implemented
          []
        when :has_and_belongs_to_many
          # not yet implemented
          []
        end
        return new_associated_records
      end
      
    else
      define_method(association_id.to_s) do
        records = association_class.all.select{|record| record.send("#{self.class.name.underscore}_id") == self.id}
        
        records.instance_variable_set(:@parent, self)
        records.instance_variable_set(:@association_class, association_class)
        def records.create(new_records_attributes = nil)
          new_records = new_records_attributes.is_a?(Array) ? [] : nil
          new_records_attributes = [new_records_attributes] unless new_records_attributes.is_a?(Array)
          new_records_attributes.each do |attributes|
            new_record = @association_class.new(attributes)
            new_record.send("#{@parent.class.name.underscore}_id=", @parent.id)
            new_record.save
            if new_records.is_a?(Array)
              new_records << new_record
            else
              return new_record
            end
          end
          return new_records
        end
        
        return records
      end
    end
  end
end

#has_one(association_id, options = {}) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/acts_as_tableless.rb', line 235

def has_one(association_id, options = {})
  active_record_has_one(association_id, options)
  class_variable_name = association_id.to_s.singularize
  if class_variable_name.camelize.constantize.send(:included_modules).include?(ActsAsTableless) # || ActsAsTableless.class_variables.include?(:"@@#{class_variable_name}")
    association_class = class_variable_name.camelize.constantize rescue nil
    if options.include?(:through)
      define_method(association_id.to_s) do
        through_object = self.send(options[:through])
        return nil if through_object.nil?
        association_class.find(through_object.send("#{association_class.name.underscore}_id"))
      end
    else
      define_method(association_id.to_s) do
        record = association_class.all.select{|r|r.send("#{self.class.name.underscore}_id") == self.id}.first
        record.instance_variable_set(:@parent, self)
        record.instance_variable_set(:@association_class, association_class)
        def record.create(attributes)
          old_record = @parent.send(@association_class.name.underscore)
          new_record = @association_class.new(attributes)
          new_record.send("#{@parent.class.name.underscore}_id=", @parent.id)
          new_record.save
          old_record.delete unless old_record.nil?
          return new_record
        end
        return record
      end
    end
  end
end