obfuscated 0.1.5 → 0.1.6

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.
Files changed (2) hide show
  1. data/lib/obfuscated.rb +19 -15
  2. metadata +4 -4
@@ -6,13 +6,26 @@ module Obfuscated
6
6
 
7
7
  def self.append_features(base)
8
8
  super
9
- base.extend(ClassMethods)
9
+ base.extend(ClassMethods) if base.to_s == 'ActiveRecord::Base'
10
+ base.extend(Finder)
10
11
  end
11
12
 
12
13
  def self.supported?
13
14
  @@mysql_support ||= ActiveRecord::Base.connection.class.to_s.downcase.include?('mysql') ? true : false
14
15
  end
15
16
 
17
+ module Finder
18
+ def find( *primary_key )
19
+ if primary_key.is_a?(String) && primary_key.length == 12
20
+ find_by_hashed_id( primary_key )
21
+ elsif primary_key.is_a?(Array) && primary_key.length == 1 && primary_key[0].is_a?(String) && primary_key[0].length == 12
22
+ find_by_hashed_id( primary_key[0] )
23
+ else
24
+ super
25
+ end
26
+ end
27
+ end
28
+
16
29
  module ClassMethods
17
30
  def has_obfuscated_id( options={} )
18
31
  class_eval do
@@ -33,18 +46,9 @@ module Obfuscated
33
46
  # Find it!
34
47
  first(options) or raise ActiveRecord::RecordNotFound, "Couldn't find #{self.class.to_s} with Hashed ID=#{hash}"
35
48
  end
36
-
37
49
  end
38
50
  end
39
51
 
40
- def find( primary_key )
41
- if primary_key.is_a?(String) && primary_key.length == 12
42
- find_by_hashed_id( primary_key )
43
- else
44
- super
45
- end
46
- end
47
-
48
52
  end
49
53
 
50
54
  module InstanceMethods
@@ -64,11 +68,11 @@ module Obfuscated
64
68
  def to_param
65
69
  hashed_id
66
70
  end
67
-
68
71
  end
69
-
70
72
  end
71
73
 
72
- ActiveRecord::Base.class_eval do
73
- include Obfuscated
74
- end
74
+ ActiveRecord::Base.class_eval { include Obfuscated }
75
+
76
+ # ActiveRelation also needs to get hacked to enable queries like:
77
+ # Sale.includes(:store).find( '7e2d2c4da1b0' )
78
+ ActiveRecord::Relation.class_eval { include Obfuscated::Finder }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obfuscated
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jon Collier
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  requirements: []
73
73
 
74
74
  rubyforge_project:
75
- rubygems_version: 1.8.24
75
+ rubygems_version: 1.8.5
76
76
  signing_key:
77
77
  specification_version: 3
78
78
  summary: Primary Key Obfuscation