obfuscated 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/obfuscated.rb +18 -18
  2. metadata +3 -3
data/lib/obfuscated.rb CHANGED
@@ -1,37 +1,22 @@
1
1
  require 'digest/sha1'
2
2
  require 'active_record'
3
3
 
4
+ # ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new( STDOUT )
5
+
4
6
  module Obfuscated
5
7
  mattr_accessor :salt
6
8
 
7
9
  def self.append_features(base)
8
10
  super
9
11
  base.extend(ClassMethods)
10
- base.extend(Finder)
11
12
  end
12
13
 
13
14
  def self.supported?
14
15
  @@mysql_support ||= ActiveRecord::Base.connection.class.to_s.downcase.include?('mysql') ? true : false
15
16
  end
16
-
17
- module Finder
18
- def find( *primary_key )
19
- # Sale.find( '7e2d2c4da1b0' )
20
- if primary_key.is_a?(String) && primary_key.length == 12
21
- find_by_hashed_id( primary_key )
22
-
23
- # Sale.includes(:store).find( '7e2d2c4da1b0' )
24
- elsif primary_key.is_a?(Array) && primary_key.length == 1 && primary_key[0].is_a?(String) && primary_key[0].length == 12
25
- find_by_hashed_id( primary_key[0] )
26
-
27
- # Other queries
28
- else
29
- super
30
- end
31
- end
32
- end
33
17
 
34
18
  module ClassMethods
19
+
35
20
  def has_obfuscated_id( options={} )
36
21
  class_eval do
37
22
 
@@ -77,3 +62,18 @@ module Obfuscated
77
62
  end
78
63
 
79
64
  ActiveRecord::Base.class_eval { include Obfuscated }
65
+
66
+ # This will work for both ActiveRecord::Base as well as ActiveRecord::Relation.
67
+ module ActiveRecord
68
+ module FinderMethods
69
+ def find( *args )
70
+ if block_given?
71
+ to_a.find { |*block_args| yield(*block_args) }
72
+ elsif args.length == 1 && args[0].is_a?(String) && args[0].length == 12
73
+ @klass.find_by_hashed_id( args[0] )
74
+ else
75
+ find_with_ids( *args )
76
+ end
77
+ end
78
+ end
79
+ end
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: 21
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 7
10
- version: 0.1.7
9
+ - 8
10
+ version: 0.1.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jon Collier