Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmetal committed Mar 24, 2016
0 parents commit 646865e
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Metrics/LineLength:
Max: 120

Metrics/AbcSize:
Max: 25
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: ruby
rvm:
- 2.2.4
before_install: gem install bundler -v 1.11.2

script:
- bundle exec rubocop
- bundle exec rspec

deploy:
provider: rubygems
api_key:
secure: IS8mKByRG8lT2TkQJ/M+Dlia+UE1ridJQ4E704h5SmGtmU9A6yzeB1rZF4CuEV768StTcMNzvGSthFF8SByw+krA7vW4muQPFcNPswbA0d+0VRdCfO6oPJS5AFjVCN3H+zqY2PAqUiABE7O9hyYSNzxXOEsimKAUUYlkuXEqvFOsIHtG7vhZ0jNYSpsH1ABm6RlUwAWXODyHFR/Fte0MSA9BT4NQwTUT9rLAeiCU1aLvq3PT1OZttPAso5Npjj5RslAWiC+2CP/g+zJgqegPuopVfdgotY9GMB99+naONk37JPlHOTSDSI5dpxcAr3ka92cnOHGS5f6dADb+lZ4b/qgPHqANy8cByVIOJYJx4fwtMZzuiNE7XGOqVAPZTwtpGUeFdB4j1ct+Y2PyKS8lbdl9d/s8bKXHK03KDef0/mUsW8X8C1iyw49gupY8YvJy+0I6q1EVsPY3318/HxUGgZvTJq9DFzTO+ZDbHqz54z2WgYDA96a4cnSqDZ/gysPK2L56+2eAK+2VeAWZFoe311WK5/Vnw+TGNM08pTwE80QuzZdBHPe0dOVtn56EuF1WoKR2PAirqoMYxo74maRg/3Un97GYTAGXYKq/IdYBLwyJFuFu6j1y/ucwG7dOvtlIep0zDfc17+/Ql3ZGa9Q9Y5acKtwXbb7Af6SEMEQqrQE=
gem: uuidable
on:
tags: true
repo: flant/uuidable
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in uuidable.gemspec
gemspec

gem 'rubocop', require: false
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Sergey Gnuskov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Uuidable

With this gem you can use UUID instead of id in routes. But id is still primary key.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'uuidable'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install uuidable

## Usage

Simply add `uuidable` in your ActiveRecord model:

```ruby
class Project < ActiveRecord::Base
uuidable

# Rest of the code
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/flant/uuidable.


## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'uuidable'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
41 changes: 41 additions & 0 deletions lib/uuidable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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

super(val)
end
end
end

def short_uuid
UUIDTools::UUID.parse(uuid).hexdigest
end

def self.generate_uuid
SecureRandom.uuid
end
end

ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.send(:include, Uuidable)
end
24 changes: 24 additions & 0 deletions lib/uuidable/migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Uuidable
COLUMN_NAME = :uuid
COLUMN_TYPE = :binary
COLUMN_OPTIONS = { limit: 36, null: false, index: true }.freeze

# Module adds method to table definition
module TableDefinition
def uuid(opts = {})
column COLUMN_NAME, COLUMN_TYPE, opts.merge(COLUMN_OPTIONS)
end
end

# Module adds method to alter table migration
module Migration
def add_uuid_column(table_name, opts = {})
add_column table_name, COLUMN_NAME, COLUMN_TYPE, opts.merge(COLUMN_OPTIONS)
end
end
end
if defined? ActiveRecord::ConnectionAdapters::TableDefinition
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Uuidable::TableDefinition
end

ActiveRecord::Migration.send :include, Uuidable::Migration if defined? ActiveRecord::Migration
3 changes: 3 additions & 0 deletions lib/uuidable/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Uuidable
VERSION = '0.1.0'.freeze
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'uuidable'
7 changes: 7 additions & 0 deletions spec/uuidable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

describe Uuidable do
it 'has a version number' do
expect(Uuidable::VERSION).not_to be nil
end
end
27 changes: 27 additions & 0 deletions uuidable.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'uuidable/version'

Gem::Specification.new do |spec|
spec.name = 'uuidable'
spec.version = Uuidable::VERSION
spec.authors = ['Sergey Gnuskov']
spec.email = ['[email protected]']

spec.summary = 'Helps using uuid everywhere in routes instead of id for ActiveRecord models.'
spec.description = ''
spec.homepage = 'https://github.com/flant/uuidable'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '~> 1.11'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'

spec.add_dependency 'activerecord', '>= 4.2'
end

0 comments on commit 646865e

Please sign in to comment.