Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

persisting trusted attributes when duplicating HashWithIndifferentAccess

  • Loading branch information...
commit 3cc297ff20dd17438b9b4f89d396a09acc4f1924 1 parent 195840d
Ryan Bates authored June 01, 2009
11  lib/trusted_params/hash_additions.rb
@@ -24,3 +24,14 @@ def trusted?(attribute_name)
24 24
 class Hash
25 25
   include TrustedParams::HashAdditions
26 26
 end
  27
+
  28
+# override "dup" method because it doesn't carry over trusted attributes
  29
+# I wish there was a better way to do this...
  30
+class HashWithIndifferentAccess
  31
+  def dup_with_trusted_attributes
  32
+    hash = dup_without_trusted_attributes
  33
+    hash.instance_variable_set("@trusted_attributes", instance_variable_get("@trusted_attributes"))
  34
+    hash
  35
+  end
  36
+  alias_method_chain :dup, :trusted_attributes
  37
+end
4  spec/spec_helper.rb
@@ -35,6 +35,10 @@ def self.columns_hash
35 35
     columns.index_by{|c| c.name.to_s}
36 36
   end
37 37
   
  38
+  def logger
  39
+    Logger.new("/dev/null")
  40
+  end
  41
+  
38 42
   def self.inspect
39 43
     "Model Mock"
40 44
   end
7  spec/trusted_params/hash_additions_spec.rb
@@ -44,4 +44,11 @@
44 44
     @hash.trust(:bar)
45 45
     @hash.should be_trusted("bar")
46 46
   end
  47
+  
  48
+  it "should persist trusted when duplicating HashWithIndifferentAccess" do
  49
+    h1 = HashWithIndifferentAccess.new(:foo => "bar")
  50
+    h1.trust
  51
+    h2 = h1.dup
  52
+    h2.should be_trusted(:foo)
  53
+  end
47 54
 end

0 notes on commit 3cc297f

Please sign in to comment.
Something went wrong with that request. Please try again.