From b2d27929d16fd258b0156b3eaec0ddada2075519 Mon Sep 17 00:00:00 2001 From: Connor Mullen Date: Thu, 9 Mar 2017 16:03:53 -0800 Subject: [PATCH 1/4] Use new ScatterSwap implementation to only initialize the Hasher once --- Gemfile | 1 + Gemfile.lock | 11 +++++++++-- lib/obfuscate_id.rb | 17 +++++++++++------ obfuscate_id.gemspec | 2 +- spec/lib/obfuscate_id_spec.rb | 20 ++++++++++++++++++-- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index 3f861b7..768cfc2 100644 --- a/Gemfile +++ b/Gemfile @@ -12,3 +12,4 @@ gemspec # To use debugger # gem 'ruby-debug19', :require => 'ruby-debug' +gem 'scatter_swap', git: 'https://github.com/TiempoLabs/scatter_swap.git', branch: 'feature/lazy_loaded_spinner_map' diff --git a/Gemfile.lock b/Gemfile.lock index 23731cd..f826507 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,9 +1,16 @@ +GIT + remote: https://github.com/TiempoLabs/scatter_swap.git + revision: de0d114c1bf29fd669fc58d68354e42ea339bd59 + branch: feature/lazy_loaded_spinner_map + specs: + scatter_swap (0.0.2) + PATH remote: . specs: obfuscate_id (0.2.0) rails (~> 4.2.0) - scatter_swap (~> 0.0.3) + scatter_swap GEM remote: http://rubygems.org/ @@ -156,7 +163,6 @@ GEM rspec-mocks (~> 3.1.0) rspec-support (~> 3.1.0) rspec-support (3.1.2) - scatter_swap (0.0.3) slop (3.6.0) spork (0.9.2) sprockets (2.12.3) @@ -191,4 +197,5 @@ DEPENDENCIES pry rb-inotify rspec-rails + scatter_swap! sqlite3 diff --git a/lib/obfuscate_id.rb b/lib/obfuscate_id.rb index 319f8fd..9cfc6db 100644 --- a/lib/obfuscate_id.rb +++ b/lib/obfuscate_id.rb @@ -4,8 +4,9 @@ def obfuscate_id(options = {}) extend ClassMethods include InstanceMethods - cattr_accessor :obfuscate_id_spin + cattr_accessor :obfuscate_id_spin, :obfuscater # Obfuscater, aka the ScatterSwapper for this spin self.obfuscate_id_spin = (options[:spin] || obfuscate_id_default_spin) + self.obfuscater = ScatterSwap::Hasher.new(self.obfuscate_id_spin) end def self.hide(id, spin) @@ -22,9 +23,9 @@ def find(*args) options = args.slice!(0) || {} if has_obfuscated_id? && !options[:no_obfuscated_id] if scope.is_a?(Array) - scope.map! {|a| deobfuscate_id(a).to_i} + scope.map! {|a| deobfuscated_id(a).to_i} else - scope = deobfuscate_id(scope) + scope = deobfuscated_id(scope) end end super(scope) @@ -34,8 +35,12 @@ def has_obfuscated_id? true end - def deobfuscate_id(obfuscated_id) - ObfuscateId.show(obfuscated_id, self.obfuscate_id_spin) + def deobfuscated_id(obfuscated_id) + self.obfuscater.reverse_hash(obfuscated_id) + end + + def obfuscated_id(plain_id) + self.obfuscater.hash(plain_id) end # Generate a default spin from the Model name @@ -53,7 +58,7 @@ def obfuscate_id_default_spin module InstanceMethods def to_param - ObfuscateId.hide(self.id, self.class.obfuscate_id_spin) + self.class.obfuscated_id(self.id) end # Override ActiveRecord::Persistence#reload diff --git a/obfuscate_id.gemspec b/obfuscate_id.gemspec index f675277..af0d7b2 100644 --- a/obfuscate_id.gemspec +++ b/obfuscate_id.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |s| s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"] - s.add_dependency "scatter_swap", "~> 0.0.3" + s.add_dependency "scatter_swap" s.add_dependency "rails", "~> 4.2.0" s.add_development_dependency "sqlite3" diff --git a/spec/lib/obfuscate_id_spec.rb b/spec/lib/obfuscate_id_spec.rb index 254f878..d54954e 100644 --- a/spec/lib/obfuscate_id_spec.rb +++ b/spec/lib/obfuscate_id_spec.rb @@ -67,7 +67,7 @@ def self.column(name, sql_type = nil, default = nil, null = true) end end - describe "#deobfuscate_id" do + describe "#deobfuscated_id" do before do class User < ActiveRecord::Base obfuscate_id @@ -76,10 +76,26 @@ class User < ActiveRecord::Base let(:user) { User.create(id: 1) } - subject(:deobfuscated_id) { User.deobfuscate_id(user.to_param).to_i } + subject(:deobfuscated_id) { User.deobfuscated_id(user.to_param).to_i } it "reverses the obfuscated id" do should eq(user.id) end end + + describe "#obfuscated_id" do + before do + class User < ActiveRecord::Base + obfuscate_id + end + end + + let(:user) { User.create(id: 1) } + + subject(:obfuscated_id) { User.obfuscated_id(user.id).to_i } + + it "obfuscates the id" do + should eq(user.to_param.to_i) + end + end end From 6192d3843568c2a0b9dc5e1bc1e9039eda7c11f4 Mon Sep 17 00:00:00 2001 From: Connor Mullen Date: Thu, 9 Mar 2017 19:15:27 -0800 Subject: [PATCH 2/4] PR feedback --- lib/obfuscate_id.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/obfuscate_id.rb b/lib/obfuscate_id.rb index 9cfc6db..e8e4e4a 100644 --- a/lib/obfuscate_id.rb +++ b/lib/obfuscate_id.rb @@ -38,6 +38,7 @@ def has_obfuscated_id? def deobfuscated_id(obfuscated_id) self.obfuscater.reverse_hash(obfuscated_id) end + alias_method :deobfuscate_id, :deobfuscated_id def obfuscated_id(plain_id) self.obfuscater.hash(plain_id) @@ -81,9 +82,10 @@ def reload(options = nil) self end - def deobfuscate_id(obfuscated_id) - self.class.deobfuscate_id(obfuscated_id) + def deobfuscated_id(obfuscated_id) + self.class.deobfuscated_id(obfuscated_id) end + alias_method :deobfuscate_id, :deobfuscated_id end end From ae0e4989d690cc0c4b454ea99c065b33b5b983fb Mon Sep 17 00:00:00 2001 From: Connor Mullen Date: Fri, 10 Mar 2017 09:18:43 -0800 Subject: [PATCH 3/4] point to master scatter_swap --- Gemfile | 2 +- Gemfile.lock | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 768cfc2..dc53d1a 100644 --- a/Gemfile +++ b/Gemfile @@ -12,4 +12,4 @@ gemspec # To use debugger # gem 'ruby-debug19', :require => 'ruby-debug' -gem 'scatter_swap', git: 'https://github.com/TiempoLabs/scatter_swap.git', branch: 'feature/lazy_loaded_spinner_map' +gem 'scatter_swap', git: 'https://github.com/TiempoLabs/scatter_swap.git' diff --git a/Gemfile.lock b/Gemfile.lock index f826507..bf6c607 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,6 @@ GIT remote: https://github.com/TiempoLabs/scatter_swap.git - revision: de0d114c1bf29fd669fc58d68354e42ea339bd59 - branch: feature/lazy_loaded_spinner_map + revision: c73c7e39391df13158815c4537083787f70298fd specs: scatter_swap (0.0.2) @@ -199,3 +198,6 @@ DEPENDENCIES rspec-rails scatter_swap! sqlite3 + +BUNDLED WITH + 1.14.3 From 50b0701b65e12feb15409cc6691f4b6fab20f580 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 23 Jun 2017 16:46:30 -0700 Subject: [PATCH 4/4] switch to rails 5 for testing --- .travis.yml | 1 + Gemfile.lock | 264 ++++++++++++++++++++++--------------------- obfuscate_id.gemspec | 2 +- 3 files changed, 139 insertions(+), 128 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6f1fd69..f3b4a28 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,3 +2,4 @@ language: ruby rvm: - 1.9.3 - 2.1.1 + - 2.2.7 diff --git a/Gemfile.lock b/Gemfile.lock index bf6c607..bccb9f9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/TiempoLabs/scatter_swap.git - revision: c73c7e39391df13158815c4537083787f70298fd + revision: 9240bbb0ca9c912adba6fbe57bf9431184604b38 specs: scatter_swap (0.0.2) @@ -8,75 +8,81 @@ PATH remote: . specs: obfuscate_id (0.2.0) - rails (~> 4.2.0) + rails (~> 5.1.1) scatter_swap GEM remote: http://rubygems.org/ specs: - actionmailer (4.2.0) - actionpack (= 4.2.0) - actionview (= 4.2.0) - activejob (= 4.2.0) + actioncable (5.1.1) + actionpack (= 5.1.1) + nio4r (~> 2.0) + websocket-driver (~> 0.6.1) + actionmailer (5.1.1) + actionpack (= 5.1.1) + actionview (= 5.1.1) + activejob (= 5.1.1) mail (~> 2.5, >= 2.5.4) - rails-dom-testing (~> 1.0, >= 1.0.5) - actionpack (4.2.0) - actionview (= 4.2.0) - activesupport (= 4.2.0) - rack (~> 1.6.0) - rack-test (~> 0.6.2) - rails-dom-testing (~> 1.0, >= 1.0.5) - rails-html-sanitizer (~> 1.0, >= 1.0.1) - actionview (4.2.0) - activesupport (= 4.2.0) + rails-dom-testing (~> 2.0) + actionpack (5.1.1) + actionview (= 5.1.1) + activesupport (= 5.1.1) + rack (~> 2.0) + rack-test (~> 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.1.1) + activesupport (= 5.1.1) builder (~> 3.1) - erubis (~> 2.7.0) - rails-dom-testing (~> 1.0, >= 1.0.5) - rails-html-sanitizer (~> 1.0, >= 1.0.1) - activejob (4.2.0) - activesupport (= 4.2.0) - globalid (>= 0.3.0) - activemodel (4.2.0) - activesupport (= 4.2.0) - builder (~> 3.1) - activerecord (4.2.0) - activemodel (= 4.2.0) - activesupport (= 4.2.0) - arel (~> 6.0) - activesupport (4.2.0) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.1.1) + activesupport (= 5.1.1) + globalid (>= 0.3.6) + activemodel (5.1.1) + activesupport (= 5.1.1) + activerecord (5.1.1) + activemodel (= 5.1.1) + activesupport (= 5.1.1) + arel (~> 8.0) + activesupport (5.1.1) + concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) - json (~> 1.7, >= 1.7.7) minitest (~> 5.1) - thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) - addressable (2.3.6) - arel (6.0.0) - builder (3.2.2) - capybara (2.4.4) + addressable (2.5.1) + public_suffix (~> 2.0, >= 2.0.2) + arel (8.0.0) + builder (3.2.3) + capybara (2.14.3) + addressable mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - celluloid (0.16.0) - timers (~> 4.0.0) - childprocess (0.5.5) + childprocess (0.7.0) ffi (~> 1.0, >= 1.0.11) - coderay (1.1.0) - diff-lcs (1.2.5) - erubis (2.7.0) - ffi (1.9.6) + coderay (1.1.1) + concurrent-ruby (1.0.5) + diff-lcs (1.3) + erubi (1.6.0) + ffi (1.9.18) formatador (0.2.5) - globalid (0.3.2) - activesupport (>= 4.1.0) - guard (2.10.2) + globalid (0.4.0) + activesupport (>= 4.2.0) + guard (2.14.1) formatador (>= 0.2.4) - listen (~> 2.7) + listen (>= 2.7, < 4.0) lumberjack (~> 1.0) + nenv (~> 0.1) + notiffany (~> 0.0) pry (>= 0.9.12) + shellany (~> 0.0) thor (>= 0.18.1) - guard-compat (1.1.0) - guard-rspec (4.5.0) + guard-compat (1.2.1) + guard-rspec (4.7.3) guard (~> 2.1) guard-compat (~> 1.1) rspec (>= 2.99.0, < 4.0) @@ -85,103 +91,107 @@ GEM guard (~> 2.0) guard-compat (~> 1.0) spork (>= 0.8.4) - hike (1.2.3) - hitimes (1.2.2) - i18n (0.7.0) - json (1.8.2) + i18n (0.8.4) launchy (2.4.3) addressable (~> 2.3) - listen (2.8.3) - celluloid (>= 0.15.2) - rb-fsevent (>= 0.9.3) - rb-inotify (>= 0.9) - loofah (2.0.1) + listen (3.1.5) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + ruby_dep (~> 1.2) + loofah (2.0.3) nokogiri (>= 1.5.9) - lumberjack (1.0.9) - mail (2.6.3) - mime-types (>= 1.16, < 3) + lumberjack (1.0.12) + mail (2.6.6) + mime-types (>= 1.16, < 4) method_source (0.8.2) - mime-types (2.4.3) - mini_portile (0.6.1) - minitest (5.5.1) - multi_json (1.10.1) - nokogiri (1.6.5) - mini_portile (~> 0.6.0) - pry (0.10.1) + mime-types (3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2016.0521) + mini_portile2 (2.2.0) + minitest (5.10.2) + nenv (0.3.0) + nio4r (2.1.0) + nokogiri (1.8.0) + mini_portile2 (~> 2.2.0) + notiffany (0.1.1) + nenv (~> 0.1) + shellany (~> 0.0) + pry (0.10.4) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - rack (1.6.0) + public_suffix (2.0.5) + rack (2.0.3) rack-test (0.6.3) rack (>= 1.0) - rails (4.2.0) - actionmailer (= 4.2.0) - actionpack (= 4.2.0) - actionview (= 4.2.0) - activejob (= 4.2.0) - activemodel (= 4.2.0) - activerecord (= 4.2.0) - activesupport (= 4.2.0) + rails (5.1.1) + actioncable (= 5.1.1) + actionmailer (= 5.1.1) + actionpack (= 5.1.1) + actionview (= 5.1.1) + activejob (= 5.1.1) + activemodel (= 5.1.1) + activerecord (= 5.1.1) + activesupport (= 5.1.1) bundler (>= 1.3.0, < 2.0) - railties (= 4.2.0) - sprockets-rails - rails-deprecated_sanitizer (1.0.3) - activesupport (>= 4.2.0.alpha) - rails-dom-testing (1.0.5) - activesupport (>= 4.2.0.beta, < 5.0) - nokogiri (~> 1.6.0) - rails-deprecated_sanitizer (>= 1.0.1) - rails-html-sanitizer (1.0.1) + railties (= 5.1.1) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.0.3) loofah (~> 2.0) - railties (4.2.0) - actionpack (= 4.2.0) - activesupport (= 4.2.0) + railties (5.1.1) + actionpack (= 5.1.1) + activesupport (= 5.1.1) + method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (10.4.2) - rb-fsevent (0.9.4) - rb-inotify (0.9.5) - ffi (>= 0.5.0) - rspec (3.1.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-core (3.1.7) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) + rake (12.0.0) + rb-fsevent (0.9.8) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + rspec (3.6.0) + rspec-core (~> 3.6.0) + rspec-expectations (~> 3.6.0) + rspec-mocks (~> 3.6.0) + rspec-core (3.6.0) + rspec-support (~> 3.6.0) + rspec-expectations (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-mocks (3.6.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.1.0) - rspec-mocks (3.1.3) - rspec-support (~> 3.1.0) - rspec-rails (3.1.0) + rspec-support (~> 3.6.0) + rspec-rails (3.6.0) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-support (~> 3.1.0) - rspec-support (3.1.2) + rspec-core (~> 3.6.0) + rspec-expectations (~> 3.6.0) + rspec-mocks (~> 3.6.0) + rspec-support (~> 3.6.0) + rspec-support (3.6.0) + ruby_dep (1.5.0) + shellany (0.0.1) slop (3.6.0) spork (0.9.2) - sprockets (2.12.3) - hike (~> 1.2) - multi_json (~> 1.0) - rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) - sprockets-rails (2.2.4) - actionpack (>= 3.0) - activesupport (>= 3.0) - sprockets (>= 2.8, < 4.0) - sqlite3 (1.3.10) - thor (0.19.1) - thread_safe (0.3.4) - tilt (1.4.1) - timers (4.0.1) - hitimes - tzinfo (1.2.2) + sprockets (3.7.1) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.0) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + sqlite3 (1.3.13) + thor (0.19.4) + thread_safe (0.3.6) + tzinfo (1.2.3) thread_safe (~> 0.1) - xpath (2.0.0) + websocket-driver (0.6.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.2) + xpath (2.1.0) nokogiri (~> 1.3) PLATFORMS @@ -200,4 +210,4 @@ DEPENDENCIES sqlite3 BUNDLED WITH - 1.14.3 + 1.15.1 diff --git a/obfuscate_id.gemspec b/obfuscate_id.gemspec index af0d7b2..b53451b 100644 --- a/obfuscate_id.gemspec +++ b/obfuscate_id.gemspec @@ -17,7 +17,7 @@ Gem::Specification.new do |s| s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"] s.add_dependency "scatter_swap" - s.add_dependency "rails", "~> 4.2.0" + s.add_dependency "rails", "~> 5.1.1" s.add_development_dependency "sqlite3" s.add_development_dependency "rspec-rails"