-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,14 @@ | ||
require 'uuidable/version' | ||
require 'uuidable/migration' | ||
|
||
require 'active_support' | ||
|
||
# Main module | ||
module Uuidable | ||
extend ActiveSupport::Concern | ||
|
||
class UuidChangeError < Exception; end | ||
|
||
# ClassMethods | ||
module ClassMethods | ||
def uuidable | ||
after_initialize { self.uuid = self.class.generate_uuid if uuid.blank? } | ||
validates :uuid, presence: true, uniqueness: true | ||
|
||
define_method :to_param do | ||
uuid | ||
end | ||
|
||
define_method :uuid= do |val| | ||
raise UuidChangeError, 'Uuid changing is bad idea!' unless new_record? || uuid.blank? || uuid == val | ||
module_function | ||
|
||
super(val) | ||
end | ||
end | ||
end | ||
|
||
def short_uuid | ||
UUIDTools::UUID.parse(uuid).hexdigest | ||
end | ||
|
||
def self.generate_uuid | ||
def generate_uuid | ||
SecureRandom.uuid | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:active_record) do | ||
ActiveRecord::Base.send(:include, Uuidable) | ||
end | ||
require 'uuidable/migration' | ||
require 'uuidable/active_record' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Uuidable | ||
# ActiveRecord mixin | ||
module ActiveRecord | ||
extend ActiveSupport::Concern | ||
|
||
class UuidChangeError < Exception; end | ||
|
||
# ClassMethods | ||
module ClassMethods | ||
def uuidable | ||
after_initialize { self.uuid = Uuidable.generate_uuid if uuid.blank? } | ||
validates :uuid, presence: true, uniqueness: true | ||
|
||
define_method :to_param do | ||
uuid | ||
end | ||
|
||
define_method :uuid= do |val| | ||
raise UuidChangeError, 'Uuid changing is bad idea!' unless new_record? || uuid.blank? || uuid == val | ||
|
||
super(val) | ||
end | ||
end | ||
end | ||
|
||
def short_uuid | ||
UUIDTools::UUID.parse(uuid).hexdigest | ||
end | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:active_record) do | ||
ActiveRecord::Base.send(:include, Uuidable::ActiveRecord) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Uuidable | ||
VERSION = '0.0.1'.freeze | ||
VERSION = '0.0.2'.freeze | ||
end |