module Flatter::Extensions::ActiveRecord

Public Instance Methods

add_skip_autosave_association_extension_to(klass) click to toggle source
# File lib/flatter/extensions/active_record.rb, line 126
def add_skip_autosave_association_extension_to(klass)
  return if klass.const_defined?('SkipAutosaveAssociationExtension')

  klass.const_set('SkipAutosaveAssociationExtension', skip_autosave_association_extension_for(klass))
  klass.send(:prepend, klass::SkipAutosaveAssociationExtension)
end
apply(*) click to toggle source
Calls superclass method
# File lib/flatter/extensions/active_record.rb, line 61
def apply(*)
  return super unless ar?

  ::ActiveRecord::Base.transaction do
    super or raise ::ActiveRecord::Rollback
  end
end
ar?(object = target) click to toggle source
# File lib/flatter/extensions/active_record.rb, line 155
def ar?(object = target)
  object.class < ::ActiveRecord::Base
end
assign_foreign_keys_for_mountings() click to toggle source
# File lib/flatter/extensions/active_record.rb, line 110
def assign_foreign_keys_for_mountings
  associated_mountings(:foreign_key).each do |mounting|
    mounting.target[mounting.foreign_key] ||= target.id
  end
end
assign_foreign_keys_from_mountings() click to toggle source
# File lib/flatter/extensions/active_record.rb, line 103
def assign_foreign_keys_from_mountings
  associated_mountings(:mounter_foreign_key).each do |mounting|
    target[mounting.mounter_foreign_key] ||= mounting.target.id
  end
end
associated_mountings(key) click to toggle source
# File lib/flatter/extensions/active_record.rb, line 117
def associated_mountings(key)
  root_mountings.select do |mounting|
    mounter = mounting.mounter
    mounter = mounter.mounter if mounter.trait?
    mounting.options.key?(key) && mounter == self
  end
end
build_collection_item() click to toggle source
# File lib/flatter/extensions/active_record.rb, line 11
def build_collection_item
  return build_collection_item_without_ar unless mounter!.try(:ar?)

  mounter!.target.association(name.to_sym).try(:build) ||
    build_collection_item_without_ar
end
build_collection_item_without_ar()
delete_target_item(item) click to toggle source
Calls superclass method
# File lib/flatter/extensions/active_record.rb, line 81
def delete_target_item(item)
  item.destroy! if ar?(item)
  super
end
save() click to toggle source
Calls superclass method
# File lib/flatter/extensions/active_record.rb, line 69
def save
  ::ActiveRecord::Base.transaction do
    begin
      @ar_error = nil
      super
    rescue ::ActiveRecord::StatementInvalid => e
      @ar_error = e
      raise ::ActiveRecord::Rollback
    end
  end
end
save_target() click to toggle source
Calls superclass method
# File lib/flatter/extensions/active_record.rb, line 86
def save_target
  return super unless ar?

  assign_foreign_keys_from_mountings
  result = target.without_association_callbacks{ target.save }
  assign_foreign_keys_for_mountings if result

  result != false
end
set_target!(target) click to toggle source
Calls superclass method
# File lib/flatter/extensions/active_record.rb, line 55
def set_target!(target)
  super
  add_skip_autosave_association_extension_to(target.class) if ar?
  target
end
skip_autosave_association_extension_for(klass) click to toggle source
Calls superclass method
# File lib/flatter/extensions/active_record.rb, line 134
def skip_autosave_association_extension_for(klass)
  association_autosave_methods = klass.instance_methods.grep(/autosave_associated_records_for_/)
  association_validation_methods = klass.instance_methods.grep(/validate_associated_records_for_/)

  Module.new do
    (association_autosave_methods + association_validation_methods).each do |name|
      define_method(name) do
        @skip_association_callbacks || super()
      end
    end

    def without_association_callbacks
      @skip_association_callbacks = true
      yield
    ensure
      remove_instance_variable('@skip_association_callbacks')
    end
  end
end
target_valid?() click to toggle source
Calls superclass method
# File lib/flatter/extensions/active_record.rb, line 97
def target_valid?
  return super unless ar?
  target.without_association_callbacks{ super }
end
without_association_callbacks() { || ... } click to toggle source
# File lib/flatter/extensions/active_record.rb, line 145
def without_association_callbacks
  @skip_association_callbacks = true
  yield
ensure
  remove_instance_variable('@skip_association_callbacks')
end