Skip to content

Instantly share code, notes, and snippets.

@akitaonrails
Forked from rmoriz/Gemfile
Created April 23, 2011 00:04
Show Gist options
  • Save akitaonrails/937995 to your computer and use it in GitHub Desktop.
Save akitaonrails/937995 to your computer and use it in GitHub Desktop.

Revisions

  1. @rmoriz rmoriz revised this gist Apr 22, 2011. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions uuid.rb
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,9 @@ module UUID

    included do
    set_primary_key 'uuid'

    def before_create
    before_create :generate_uuid

    def generate_uuid
    self.id = UUIDTools::UUID.random_create.to_s
    end
    end
  2. @rmoriz rmoriz revised this gist Apr 22, 2011. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # Gemfile
    gem 'uuidtools'
    1 change: 1 addition & 0 deletions migrations.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # db/migrate/20110422210841_create_sites.rb
    # 1. :id => false
    # 2. :uuid
    #
  3. @rmoriz rmoriz created this gist Apr 22, 2011.
    3 changes: 3 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    gem 'uuidtools'
    15 changes: 15 additions & 0 deletions migrations.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # 1. :id => false
    # 2. :uuid
    #
    class CreateSites < ActiveRecord::Migration
    def self.up
    create_table(:sites, :id => false) do |t|
    t.string :uuid, :limit => 36, :primary => true
    t.timestamps
    end
    end

    def self.down
    drop_table :sites
    end
    end
    4 changes: 4 additions & 0 deletions site.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # app/models/site.rb
    class Site < ActiveRecord::Base
    include Extensions::UUID
    end
    15 changes: 15 additions & 0 deletions uuid.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # app/models/extensions/uuid.rb
    #
    module Extensions
    module UUID
    extend ActiveSupport::Concern

    included do
    set_primary_key 'uuid'

    def before_create
    self.id = UUIDTools::UUID.random_create.to_s
    end
    end
    end
    end