Skip to content
Browse files

First version

  • Loading branch information...
1 parent 114df09 commit e183f7a581e3f6eaf696898b4f90c921513593c5 @gabynaiman committed
View
40 README.md
@@ -18,7 +18,45 @@ Or install it yourself as:
## Usage
-TODO: Write usage instructions here
+### Migrations
+
+Define attribute as UUID
+
+ class CreateCountries < ActiveRecord::Migration
+ def change
+ create_table :countries do |t|
+ t.uuid :key
+ end
+ end
+ end
+
+Define primary key as UUID
+
+ class CreateLanguages < ActiveRecord::Migration
+ def change
+ create_table :languages do |t|
+ t.uuid :id, primary_key: true
+ end
+ end
+ end
+
+### Models
+
+For automatic generation of UUID before creation
+
+ class Country < ActiveRecord::Base
+ attr_uuid :key
+ end
+
+For change primary key to UUID
+
+ class Language < ActiveRecord::Base
+ pk_uuid
+ end
+
+### Manually creation of UUIDs
+
+ ActiveRecordUUID.random
## Contributing
View
1 activerecord_uuid.gemspec
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_dependency 'rails', '>= 3.2.0'
+ s.add_dependency 'uuidtools'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec'
View
14 lib/activerecord_uuid.rb
@@ -1,5 +1,17 @@
+require 'active_record'
+require 'uuidtools'
+require 'logger'
+
require 'activerecord_uuid/version'
+require 'activerecord_uuid/active_record_extension'
+
+ActiveRecord::Base.send :extend, ActiveRecordUUID::ActiveRecord
+ActiveRecord::ConnectionAdapters::TableDefinition.send :include, ActiveRecordUUID::TableDefinition
module ActiveRecordUUID
- # Your code goes here...
+
+ def self.random
+ UUIDTools::UUID.random_create.to_s
+ end
+
end
View
26 lib/activerecord_uuid/active_record_extension.rb
@@ -0,0 +1,26 @@
+module ActiveRecordUUID
+
+ module ActiveRecord
+ def pk_uuid
+ self.primary_key = :id
+ self.sequence_name = nil
+ before_create do
+ self.id = ActiveRecordUUID.random unless self.id
+ end
+ end
+
+ def attr_uuid(name)
+ before_create do
+ self.send("#{name}=", ActiveRecordUUID.random) unless self.send("#{name}")
+ end
+ end
+ end
+
+ module TableDefinition
+ def uuid(name, options={})
+ @columns.delete @columns_hash.delete('id') if options[:primary_key]
+ column(name, :string, limit: 36, null: options[:null] || !options[:primary_key], primary: options[:primary_key])
+ end
+ end
+
+end
View
48 spec/active_record_extension_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe 'ActiveRecord extension' do
+
+ before :all do
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ":memory:"
+ ActiveRecord::Base.connection
+ ActiveRecord::Migrator.migrate ActiveRecord::Migrator.migrations_path
+ end
+
+ context 'Attribute uuid' do
+
+ it 'Migration type' do
+ Country.primary_key.should eq 'id'
+ uuid_column = Country.columns_hash['key']
+ uuid_column.type.should eq :string
+ uuid_column.limit.should eq 36
+ uuid_column.null.should be_true
+ end
+
+ it 'ActiveRecord generation' do
+ model = Country.create!
+ model.key.should be_a String
+ model.key.size.should eq 36
+ end
+
+ end
+
+ context 'Primary key uuid' do
+
+ it 'Migration type' do
+ Language.primary_key.should eq 'id'
+ uuid_column = Language.columns_hash['id']
+ uuid_column.type.should eq :string
+ uuid_column.limit.should eq 36
+ uuid_column.primary.should be_true
+ uuid_column.null.should be_false
+ end
+
+ it 'ActiveRecord generation' do
+ model = Language.create!
+ model.id.should be_a String
+ model.id.size.should eq 36
+ end
+
+ end
+
+end
View
7 spec/migrations/20121004135939_create_countries.rb
@@ -0,0 +1,7 @@
+class CreateCountries < ActiveRecord::Migration
+ def change
+ create_table :countries do |t|
+ t.uuid :key
+ end
+ end
+end
View
7 spec/migrations/20121004135940_create_languages.rb
@@ -0,0 +1,7 @@
+class CreateLanguages < ActiveRecord::Migration
+ def change
+ create_table :languages do |t|
+ t.uuid :id, primary_key: true
+ end
+ end
+end
View
11 spec/spec_helper.rb
@@ -0,0 +1,11 @@
+require 'activerecord_uuid'
+
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+TEMP_PATH = ENV['TMP'].gsub("\\", '/')
+
+ActiveRecord::Base.logger = Logger.new($stdout)
+ActiveRecord::Migrator.migrations_path = "#{File.dirname(__FILE__)}/migrations"
+
+RSpec.configure do |config|
+end
View
3 spec/support/models/country.rb
@@ -0,0 +1,3 @@
+class Country < ActiveRecord::Base
+ attr_uuid :key
+end
View
3 spec/support/models/language.rb
@@ -0,0 +1,3 @@
+class Language < ActiveRecord::Base
+ pk_uuid
+end

0 comments on commit e183f7a

Please sign in to comment.
Something went wrong with that request. Please try again.