Sha256: ca97895b37bbd31e1bbaa6c239a17cc6f189789438fbbfbf0c0acfbea0e112bb

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module ActiveRecord::Userstamp::Utilities
  # Removes the association methods from the model.
  #
  # @param [Class] model The model to remove methods from.
  # @param [Symbol] association The name of the association to remove.
  # @return [void]
  def self.remove_association(model, association)
    methods = [
      association,
      "#{association}=",
      "build_#{association}",
      "create_#{association}",
      "create_#{association}!"
    ]

    model.generated_association_methods.module_eval do
      methods.each do |method|
        remove_method(method) if method_defined?(method)
      end
    end
  end

  # Obtains the creator/updater/deleter columns which are present in the model.
  #
  # @param [Class] model The model to query.
  # @return [nil|Array<(bool, bool, bool)>] Nil if the model does not have a table defined.
  #   Otherwise, a tuple of booleans indicating the presence of the created, updated, and deleted
  #   columns.
  def self.available_association_columns(model)
    columns = Set[*model.column_names]
    config = ActiveRecord::Userstamp.config

    [config.creator_attribute.present? && columns.include?(config.creator_attribute.to_s),
     config.updater_attribute.present? && columns.include?(config.updater_attribute.to_s),
     config.deleter_attribute.present? && columns.include?(config.deleter_attribute.to_s)]
  rescue ActiveRecord::StatementInvalid => _
    nil
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-userstamp-3.0.1 lib/active_record/userstamp/utilities.rb