module ActiveRecord::VettedRecord

Public Class Methods

included(base) click to toggle source
# File lib/ar_database_duplicator.rb, line 14
def self.included(base)
  class << base
    attr_accessor :field_vetting

    def field_vetting
      @field_vetting.nil? ? @field_vetting = true : @field_vetting
    end

    def mark_attribute_safe(name)
      safe_attributes << name.to_s
      safe_attributes.uniq!
    end

    def mark_attribute_temporarily_safe(name)
      temporary_safe_attributes << name.to_s
      temporary_safe_attributes.uniq!
    end

    def safe_attributes
      @safe_attributes ||= []
    end

    # These are attributes that are to be considered safe at the class level but only for a specific period of time.
    def temporary_safe_attributes
      @temporary_safe_attributes ||= []
    end

    def clear_temporary_safe_attributes
      @temporary_safe_attributes = nil
    end

    # An array of attributes not already vetted at the class level
    def unvetted_attributes
      column_names - vetted_attributes
    end

    # An array of attributes already vetted at the class level
    def vetted_attributes
      field_vetting ? (safe_attributes + temporary_safe_attributes) : column_names
    end

    def with_field_vetting(&block)
      old_state = field_vetting
      begin
        self.field_vetting = true
        yield
      ensure
        self.field_vetting = old_state
      end
    end

    def without_field_vetting(&block)
      old_state = field_vetting
      begin
        self.field_vetting = false
        yield
      ensure
        self.field_vetting = old_state
      end
    end

  end
end

Public Instance Methods

unvetted_attributes() click to toggle source
# File lib/ar_database_duplicator.rb, line 78
def unvetted_attributes
  # Start with all attributes not vetted at the class level.
  # Remove any attributes that were unchanged but marked as safe
  # Remove any attributes that were changed
  # And what you have left is unvetted attributes. Most likely a new field was added or a value was not given for an existing one.
  self.class.unvetted_attributes - vetted_attributes - changed_attributes.keys
end
vet_attribute(name) click to toggle source

If an attribute for this instance is to be considered safe without being overwritten, mark it as vetted.

# File lib/ar_database_duplicator.rb, line 91
def vet_attribute(name)
  vetted_attributes << name.to_s
  vetted_attributes.uniq!
end
vetted?() click to toggle source
# File lib/ar_database_duplicator.rb, line 86
def vetted?
  unvetted_attributes.empty?
end
vetted_attributes() click to toggle source
# File lib/ar_database_duplicator.rb, line 96
def vetted_attributes
  @vetted_attributes ||= []
end
vetted_save() click to toggle source

This will only save if there are no unvetted attributes.

# File lib/ar_database_duplicator.rb, line 101
def vetted_save
  raise UnvettedAttribute, "The following field(s) were not checked: #{unvetted_attributes.join(', ')}" unless vetted?
  save_without_validation
end