secretary-rails 1.0.0.beta4 → 1.0.0.beta5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a3fabc2aa72279b7c65cfb1437502c7828516aa
4
+ data.tar.gz: e1a90574dfafbcc326d590658fd6a7841aebfa4b
5
+ SHA512:
6
+ metadata.gz: df71ac7242fe988ee50c01f21a83ced6c0ff2d3a3ba620ca09eee8eb3b0298aec0ef6014fd8e12fc005a9c776bfd782f3bde541e341e3851b35e66e533d46779
7
+ data.tar.gz: 029dfa9765095925a73db1c3d2d2d894878731725fc7cbaf9d1522e872bbf190ac2da8d294839a00b39aef16d33780de0269280b5fd8bf3554331abf712f0093
@@ -82,7 +82,8 @@ module Secretary
82
82
  def assign_to_or_mark_for_destruction(record, *args)
83
83
  name = association_name(record)
84
84
 
85
- if self.class.reflect_on_association(name).collection?
85
+ if self.class.reflect_on_association(name).collection? &&
86
+ versioned_attribute?(name)
86
87
  previous = changed_attributes[name]
87
88
 
88
89
  # Assume it will change. It may not. We'll handle that scenario
@@ -1,3 +1,3 @@
1
1
  module Secretary
2
- GEM_VERSION = "1.0.0.beta4"
2
+ GEM_VERSION = "1.0.0.beta5"
3
3
  end
@@ -55,7 +55,20 @@ module Secretary
55
55
  raise NoAssociationError, name, self.name
56
56
  end
57
57
 
58
- self.versioned_attributes << name.to_s
58
+ # If the environment is loaded, the following line will be
59
+ # evaluated for any `tracks_association` calls. `versioned_attributes`
60
+ # calls `self.column_names`, which requires the table to exist.
61
+ #
62
+ # So the problem is that if our database or table doesn't exist,
63
+ # we can't load the environment, and the environment needs to be
64
+ # loaded in order to load in the schema.
65
+ #
66
+ # So, we rescue! And warn.
67
+ begin
68
+ self.versioned_attributes << name.to_s
69
+ rescue => e
70
+ warn "Caught an error while updating versioned attributes. #{e}"
71
+ end
59
72
 
60
73
  define_dirty_association_methods(name, reflection)
61
74
 
@@ -75,7 +88,12 @@ module Secretary
75
88
 
76
89
  def association_name(association_object)
77
90
  klass = association_object.class
78
- name = self.class.reflections.find { |r| r[1].klass == klass }[0]
91
+
92
+ reflection = self.class.reflections.find do |name, reflection|
93
+ reflection.klass >= klass
94
+ end
95
+
96
+ reflection[0] if reflection
79
97
  end
80
98
  end
81
99
  end
@@ -17,7 +17,10 @@ module Secretary
17
17
  # which attributes to ignore.
18
18
  #
19
19
  # Each takes an array of column names *as strings*.
20
- attr_writer :versioned_attributes
20
+ def versioned_attributes=(attributes)
21
+ verify_strings!(attributes)
22
+ @versioned_attributes = attributes
23
+ end
21
24
 
22
25
  def versioned_attributes
23
26
  @versioned_attributes ||=
@@ -26,8 +29,9 @@ module Secretary
26
29
  unversioned_attributes
27
30
  end
28
31
 
29
- def unversioned_attributes=(array)
30
- self.versioned_attributes -= array
32
+ def unversioned_attributes=(attributes)
33
+ verify_strings!(attributes)
34
+ self.versioned_attributes -= attributes
31
35
  end
32
36
 
33
37
  private
@@ -35,6 +39,13 @@ module Secretary
35
39
  def unversioned_attributes
36
40
  @unversioned_attributes ||= []
37
41
  end
42
+
43
+ def verify_strings!(array)
44
+ if array.any? { |e| !e.is_a?(String) }
45
+ raise ArgumentError,
46
+ "Versioned attributes must be declared as strings."
47
+ end
48
+ end
38
49
  end
39
50
  end
40
51