encrypted_id 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1 +1 @@
1
- --color
1
+ --color --format d
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # encrypted_id
1
+ # encrypted_id [![Build Status](https://secure.travis-ci.org/pencil/encrypted_id.png)](http://travis-ci.org/pencil/encrypted_id)
2
2
 
3
3
  Sometimes you don't want your users to see the actual ID of your databases entries. This gem allows you to hide the ID.
4
4
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "encrypted_id"
8
+ s.version = "1.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nils Caspar"]
12
+ s.date = "2012-10-15"
13
+ s.description = "Sometimes you don't want your users to see the actual ID of your databases entries. This gem allows you to hide the ID."
14
+ s.email = "ncaspar@me.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "encrypted_id.gemspec",
30
+ "lib/encrypted_id.rb",
31
+ "spec/encrypted_id_spec.rb",
32
+ "spec/spec_helper.rb",
33
+ "spec/support/models/animal.rb",
34
+ "spec/support/models/user.rb"
35
+ ]
36
+ s.homepage = "http://github.com/pencil/encrypted_id"
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = "1.8.24"
40
+ s.summary = "Allows you to encrypt the ID of your ActiveRecord model."
41
+
42
+ if s.respond_to? :specification_version then
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
47
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.2.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
50
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
53
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.2.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
56
+ s.add_dependency(%q<sqlite3>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
60
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
61
+ s.add_dependency(%q<bundler>, ["~> 1.2.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
63
+ s.add_dependency(%q<sqlite3>, [">= 0"])
64
+ end
65
+ end
66
+
data/lib/encrypted_id.rb CHANGED
@@ -11,6 +11,7 @@ module EncryptedId
11
11
  include InstanceMethods
12
12
  cattr_accessor :encrypted_id_key
13
13
  self.encrypted_id_key = Digest::SHA256.digest(options[:key] || encrypted_id_default_key)
14
+ self.define_singleton_method(:find_single, lambda { puts "foo" })
14
15
  end
15
16
 
16
17
  def self.decrypt(key, id)
@@ -29,14 +30,17 @@ module EncryptedId
29
30
 
30
31
  module ClassMethods
31
32
  def find(*args)
32
- if has_encrypted_id?
33
+ scope = args.slice!(0)
34
+ options = args.slice!(0) || {}
35
+ if !(scope.is_a? Symbol) && has_encrypted_id? && !options[:no_encrypted_id]
33
36
  begin
34
- args[0] = EncryptedId.decrypt(encrypted_id_key, "#{args[0]}")
37
+ scope = EncryptedId.decrypt(encrypted_id_key, "#{scope}")
35
38
  rescue OpenSSL::Cipher::CipherError
36
39
  raise ActiveRecord::RecordNotFound.new("Could not decrypt ID #{args[0]}")
37
40
  end
38
41
  end
39
- super(*args)
42
+ options.delete(:no_encrypted_id)
43
+ super(scope, options)
40
44
  end
41
45
 
42
46
  def has_encrypted_id?
@@ -52,6 +56,11 @@ module EncryptedId
52
56
  def to_param
53
57
  EncryptedId.encrypt(self.class.encrypted_id_key, self.id)
54
58
  end
59
+
60
+ def reload(options = nil)
61
+ options = (options || {}).merge(:no_encrypted_id => true)
62
+ super(options)
63
+ end
55
64
  end
56
65
  end
57
66
 
@@ -1,49 +1,51 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe 'EncryptedId' do
4
- describe 'in the User model' do
5
- before(:each) do
6
- @user = User.new
7
- end
8
-
9
- it 'should give us the encrypted ID via to_param' do
10
- @user.id = 15
11
- @user.to_param.should == '1e8644f924812bec506b116ff14368c8'
12
- end
13
-
14
- it 'should be possible to find an entry by the encrypred id' do
15
- @user.id = 8
16
- @user.save!
17
- User.find('da9f98cd7c3eb2b0f0e88cc8daeb222c').id.should == 8
18
- end
19
-
20
- it 'should throw an exception if we try to find an entry by the real ID' do
21
- @user.id = 1
22
- @user.save!
23
- expect { User.find 1 }.to raise_error(ActiveRecord::RecordNotFound)
24
- end
25
- end
26
-
27
- describe 'in the Animal model' do
28
- before(:each) do
29
- @animal = Animal.new
30
- end
31
-
32
- it 'should give us the encrypted ID via to_param' do
33
- @animal.id = 15
34
- @animal.to_param.should == '2f71ba22a8e6975db0f13b7e3db6d9cd'
35
- end
36
-
37
- it 'should be possible to find an entry by the encrypred id' do
38
- @animal.id = 8
39
- @animal.save!
40
- Animal.find('6916a1adba452d3fffde6e444ae1ad3a').id.should == 8
41
- end
42
-
43
- it 'should throw an exception if we try to find an entry by the real ID' do
44
- @animal.id = 1
45
- @animal.save!
46
- expect { Animal.find 1 }.to raise_error(ActiveRecord::RecordNotFound)
4
+ ModelDescription = Struct.new(:model, :test_ids)
5
+
6
+ [
7
+ ModelDescription.new(User,
8
+ { 15 => '1e8644f924812bec506b116ff14368c8', 8 => 'da9f98cd7c3eb2b0f0e88cc8daeb222c' }
9
+ ),
10
+ ModelDescription.new(Animal,
11
+ { 15 => '2f71ba22a8e6975db0f13b7e3db6d9cd', 8 => '6916a1adba452d3fffde6e444ae1ad3a' }
12
+ )
13
+ ].each do |description|
14
+ describe "in the #{description.model} model" do
15
+ before(:each) do
16
+ @model = description.model
17
+ @test_ids = description.test_ids
18
+ @entity = @model.new
19
+ end
20
+
21
+ it 'should give us the encrypted ID via to_param' do
22
+ @entity.id = 15
23
+ @entity.to_param.should == @test_ids[15]
24
+ end
25
+
26
+ it 'should be possible to find an entry by the encrypted id' do
27
+ @entity.id = 8
28
+ @entity.save!
29
+ @model.find(@test_ids[8]).id.should == 8
30
+ end
31
+
32
+ it 'should throw an exception if we try to find an entry by the real ID' do
33
+ @entity.id = 1
34
+ @entity.save!
35
+ expect { @model.find 1 }.to raise_error(ActiveRecord::RecordNotFound)
36
+ end
37
+
38
+ it 'should allow us to reload an entity' do
39
+ @entity.id = 7
40
+ @entity.save!
41
+ @entity.reload.should == @entity
42
+ end
43
+
44
+ it 'should allow us to use find with :all' do
45
+ @entity.id = 99
46
+ @entity.save!
47
+ @model.find(:all).should include(@entity)
48
+ end
47
49
  end
48
50
  end
49
51
  end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,7 @@ require 'encrypted_id'
8
8
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
9
 
10
10
  RSpec.configure do |config|
11
+ config.order = 'random'
11
12
  c = ActiveRecord::Base.establish_connection(
12
13
  :adapter => 'sqlite3',
13
14
  :database => 'spec/test.sqlite3'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encrypted_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-14 00:00:00.000000000 Z
12
+ date: 2012-10-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -109,6 +109,7 @@ files:
109
109
  - README.md
110
110
  - Rakefile
111
111
  - VERSION
112
+ - encrypted_id.gemspec
112
113
  - lib/encrypted_id.rb
113
114
  - spec/encrypted_id_spec.rb
114
115
  - spec/spec_helper.rb
@@ -129,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
130
  version: '0'
130
131
  segments:
131
132
  - 0
132
- hash: -4444389274485543368
133
+ hash: 4307408867672767625
133
134
  required_rubygems_version: !ruby/object:Gem::Requirement
134
135
  none: false
135
136
  requirements: