Skip to content

Commit

Permalink
Redefine association's finder to search by uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmetal committed Jun 5, 2017
1 parent bc5a223 commit 8e1390f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Metrics/MethodLength:

Metrics/AbcSize:
Max: 25

Style/Documentation:
Enabled: False
9 changes: 7 additions & 2 deletions lib/uuidable/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ module ActiveRecord

class UuidChangeError < RuntimeError; end

# ClassMethods
module ClassMethods
module Finder
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
end

# ClassMethods
module ClassMethods
include Finder

def uuidable(as_param: true)
after_initialize { self.uuid = Uuidable.generate_uuid if attributes.keys.include?('uuid') && uuid.blank? }
Expand Down Expand Up @@ -41,4 +45,5 @@ def short_uuid

ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.send(:include, Uuidable::ActiveRecord)
ActiveRecord::Relation.send(:prepend, Uuidable::ActiveRecord::Finder)
end
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.1.2'.freeze
VERSION = '0.1.3'.freeze
end

0 comments on commit 8e1390f

Please sign in to comment.