Module: ActiveRecord::VettedRecord

Included in:
Base
Defined in:
lib/ar_database_duplicator.rb

Defined Under Namespace

Classes: UnvettedAttribute

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

Instance Method Details

#unvetted_attributesObject



78
79
80
81
82
83
84
# 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) ⇒ Object

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



91
92
93
94
# File 'lib/ar_database_duplicator.rb', line 91

def vet_attribute(name)
  vetted_attributes << name.to_s
  vetted_attributes.uniq!
end

#vetted?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ar_database_duplicator.rb', line 86

def vetted?
  unvetted_attributes.empty?
end

#vetted_attributesObject



96
97
98
# File 'lib/ar_database_duplicator.rb', line 96

def vetted_attributes
  @vetted_attributes ||= []
end

#vetted_saveObject

This will only save if there are no unvetted attributes.

Raises:



101
102
103
104
# 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