Skip to content

Commit

Permalink
Add method for finding exclusively by hashid
Browse files Browse the repository at this point in the history
  • Loading branch information
jcypret committed Mar 30, 2016
1 parent a78718f commit dc58695
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/hashid/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def find(hashid)
model_reload? ? super(hashid) : super( decode_id(hashid) || hashid )
end

# Calls `find` with decoded hashid
def find_by_hashid(hashid)
find_by!(id: hashid_decode(hashid))
end

private

def model_reload?
Expand Down
11 changes: 11 additions & 0 deletions spec/hashid/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
expect(encoded_ids).to eq ['YznovR', 'OeVre9', 'YNAOva']
end

describe '.find_by_hashid' do
it 'calls find with decoded id' do
expect(Model).to receive(:find_by!).with({id: 4})
Model.find_by_hashid('OeVre9')
end
end

describe '.to_param' do
it 'returns the hashid' do
expect(model.to_param).to eql 'z3m059'
Expand Down Expand Up @@ -169,6 +176,10 @@ def self.column_types
columns_hash
end

def self.find_by!(attrs = {})
Model.new
end

def id
100117
end
Expand Down

0 comments on commit dc58695

Please sign in to comment.