Skip to content

Commit

Permalink
Find record by uuid using default activerecord find
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmetal committed May 17, 2017
1 parent f98aa88 commit be078f4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/uuidable.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'uuidable/version'
require 'active_support'
require 'uuidtools'

# Main module
module Uuidable
Expand Down
10 changes: 9 additions & 1 deletion lib/uuidable/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ module Uuidable
module ActiveRecord
extend ActiveSupport::Concern

class UuidChangeError < Exception; end
class UuidChangeError < RuntimeError; end

# ClassMethods
module ClassMethods
def find(*args)
if args.first && args.first.is_a?(String) && args.first.match(UUIDTools::UUID_REGEXP)
find_by_uuid!(*args)
else
super
end
end

def uuidable
after_initialize { self.uuid = Uuidable.generate_uuid if uuid.blank? }
validates :uuid, presence: true, uniqueness: true
Expand Down
2 changes: 1 addition & 1 deletion lib/uuidable/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Uuidable
VERSION = '0.0.2'.freeze
VERSION = '0.1.0'.freeze
end
1 change: 1 addition & 0 deletions uuidable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'travis', '~> 1.8', '>= 1.8.2'

spec.add_dependency 'uuidtools', '>= 2.1', '< 3'
spec.add_dependency 'activerecord', '>= 4.2', '< 5.1'
end

0 comments on commit be078f4

Please sign in to comment.