Module: ActsAsObfuscated::ClassMethods
- Defined in:
- app/models/concerns/acts_as_obfuscated.rb
Instance Method Summary collapse
- #acts_as_obfuscated? ⇒ Boolean
-
#deobfuscate(original, rescue_with_original_id = true) ⇒ Object
If rescue_with_original_id is set to true the original ID will be returned when its Obfuscated Id is not found.
- #deobfuscated_maximum_id ⇒ Object
- #deobfuscator(left) ⇒ Object
- #obfuscate(original) ⇒ Object
- #relation ⇒ Object
Instance Method Details
#acts_as_obfuscated? ⇒ Boolean
60 61 62 |
# File 'app/models/concerns/acts_as_obfuscated.rb', line 60 def true end |
#deobfuscate(original, rescue_with_original_id = true) ⇒ Object
If rescue_with_original_id is set to true the original ID will be returned when its Obfuscated Id is not found
We use this as the default behaviour on everything except Class.find()
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/models/concerns/acts_as_obfuscated.rb', line 79 def deobfuscate(original, rescue_with_original_id = true) if original.kind_of?(Array) return original.map { |value| deobfuscate(value, true) } # Always rescue with original ID elsif !(original.kind_of?(Integer) || original.kind_of?(String)) return original end # Remove any non-digit formatting characters, and only consider the first 10 digits = original.to_s.delete('^0-9').first(10) # 2147483647 is PostgreSQL's Integer Max Value. If we return a value higher than this, we get weird DB errors revealed = [EffectiveObfuscation.show(, [:spin]).to_i, 2147483647].min if rescue_with_original_id && (revealed >= 2147483647 || revealed > ) original else revealed end end |
#deobfuscated_maximum_id ⇒ Object
114 115 116 |
# File 'app/models/concerns/acts_as_obfuscated.rb', line 114 def [:max_id] ||= (self.unscoped.maximum(:id) rescue 2147483647) end |
#deobfuscator(left) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/concerns/acts_as_obfuscated.rb', line 99 def deobfuscator(left) [:deobfuscators] ||= Hash.new().tap do |deobfuscators| deobfuscators['id'] = Proc.new { |right| self.deobfuscate(right) } reflect_on_all_associations(:belongs_to).each do |reflection| if (reflection.klass rescue nil).respond_to?(:deobfuscate) deobfuscators[reflection.foreign_key] = Proc.new { |right| reflection.klass.deobfuscate(right) } # Should override the foreign_object_id= method and deobfuscate it too end end end [:deobfuscators][left.to_s] end |
#obfuscate(original) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/concerns/acts_as_obfuscated.rb', line 64 def obfuscate(original) = EffectiveObfuscation.hide(original, [:spin]) if [:format] # Transform 1234567890 from ###-####-### into 123-4567-890 as per :format option [:format].dup.tap do |formatted| 10.times { |x| formatted.sub!('#', [x]) } end else end end |
#relation ⇒ Object
119 120 121 |
# File 'app/models/concerns/acts_as_obfuscated.rb', line 119 def relation super.tap { |relation| relation.extend(FinderMethods) } end |