activerecord_uuid_as_pk 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +1 -0
- data/activerecord_uuid_as_pk.gemspec +26 -0
- data/lib/activerecord_uuid_as_pk.rb +58 -0
- data/lib/activerecord_uuid_as_pk/railtie.rb +17 -0
- data/lib/activerecord_uuid_as_pk/version.rb +3 -0
- data/spec/activerecord_uuid_as_pk/activerecord_uuid_as_pk_spec.rb +124 -0
- data/spec/spec_helper.rb +20 -0
- data/test_app/Gemfile +34 -0
- data/test_app/README.rdoc +261 -0
- data/test_app/Rakefile +7 -0
- data/test_app/app/assets/images/rails.png +0 -0
- data/test_app/app/assets/javascripts/application.js +15 -0
- data/test_app/app/assets/stylesheets/application.css +13 -0
- data/test_app/app/controllers/application_controller.rb +3 -0
- data/test_app/app/helpers/application_helper.rb +2 -0
- data/test_app/app/models/account.rb +7 -0
- data/test_app/app/views/layouts/application.html.erb +14 -0
- data/test_app/config.ru +4 -0
- data/test_app/config/application.rb +68 -0
- data/test_app/config/boot.rb +6 -0
- data/test_app/config/environment.rb +5 -0
- data/test_app/config/environments/development.rb +37 -0
- data/test_app/config/environments/production.rb +67 -0
- data/test_app/config/environments/test.rb +37 -0
- data/test_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test_app/config/initializers/inflections.rb +15 -0
- data/test_app/config/initializers/mime_types.rb +5 -0
- data/test_app/config/initializers/secret_token.rb +7 -0
- data/test_app/config/initializers/session_store.rb +8 -0
- data/test_app/config/initializers/wrap_parameters.rb +14 -0
- data/test_app/config/locales/en.yml +5 -0
- data/test_app/config/routes.rb +58 -0
- data/test_app/db/migrate/20130409010425_create_accounts.rb +12 -0
- data/test_app/db/schema.rb +21 -0
- data/test_app/db/seeds.rb +7 -0
- data/test_app/public/404.html +26 -0
- data/test_app/public/422.html +26 -0
- data/test_app/public/500.html +25 -0
- data/test_app/public/favicon.ico +0 -0
- data/test_app/public/index.html +241 -0
- data/test_app/public/robots.txt +5 -0
- data/test_app/script/rails +6 -0
- metadata +196 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Shou Takenaka
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# ActiveRecordUUIDAsPK
|
2
|
+
|
3
|
+
`activerecord_uuid_as_pk` is a extension for ActiveRecord to enable to use uuid for `id` attribute (**v1.0.0 tested only MySQL**).
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'activerecord_uuid_as_pk'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install activerecord_uuid_as_pk
|
19
|
+
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
1. In a migration file, prevent generate id column and add uuid column (tinyblob).
|
24
|
+
|
25
|
+
class CreateUsers < ActiveRecord::Migration
|
26
|
+
def up
|
27
|
+
create_table :users, :id => false do |t|
|
28
|
+
t.column :uuid, :tinyblob
|
29
|
+
t.string :user_name
|
30
|
+
end
|
31
|
+
execute "ALTER TABLE users ADD PRIMARY KEY (`uuid`(16))"
|
32
|
+
end
|
33
|
+
def down
|
34
|
+
drop_table :users
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
2. Call `uuid_as_primary_key` method in a model class.
|
39
|
+
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
uuid_as_primary_key
|
42
|
+
end
|
43
|
+
|
44
|
+
3. The `uuid_as_primary_key` method hooks `before_create` callback to set automatically generated uuid as 16 byte binary data to id attirbute. And this method adds `#formatted_id` to the model to refer formatted uuid.
|
45
|
+
|
46
|
+
user = User.create(:user_name => "foo")
|
47
|
+
user.id #=> "3~\x05v\xCBfAQ\xA2\xE1\xE0\xFC\x04\xFB3\xD1"
|
48
|
+
user.formatted_id #=> "337e0576-cb66-4151-a2e1-e0fc04fb33d1"
|
49
|
+
|
50
|
+
4. Of course you can set arbitrary uuid if you set 16 byte binary, a formatted uuid string or UUIDTools::UUID object to `id` attribute. A formatted uuid string and UUIDTools::UUID object will be converted 16 byte binary data when you save a model.
|
51
|
+
|
52
|
+
user1 = User.create(:id => UUIDTools::UUID.parse("f311427a-954c-46f5-92df-3d37f56b5b41").raw, :user_name => "user1")
|
53
|
+
user1.formatted_id
|
54
|
+
#=> "f311427a-954c-46f5-92df-3d37f56b5b41"
|
55
|
+
user2 = User.create(:id => "206b38d7-3b5f-4645-b5b2-5e7efea1d3ca", :user_name => "user2")
|
56
|
+
user2.formatted_id
|
57
|
+
#=> "206b38d7-3b5f-4645-b5b2-5e7efea1d3ca"
|
58
|
+
user3 = User.create(:id => UUIDTools::UUID.parse("c81836c5-309a-4608-bf1a-5fe343081a92"), :user_name => "user3")
|
59
|
+
user3.formatted_id
|
60
|
+
#=> "c81836c5-309a-4608-bf1a-5fe343081a92"
|
61
|
+
|
62
|
+
5. The `uuid_as_primary_key` method also adds `find_by_formatted_id` class method to retrive model from data store with a formatted uuid string.
|
63
|
+
|
64
|
+
User.find_by_formatted_id("337e0576-cb66-4151-a2e1-e0fc04fb33d1")
|
65
|
+
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord_uuid_as_pk/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "activerecord_uuid_as_pk"
|
8
|
+
gem.version = ActiveRecordUUIDAsPK::VERSION
|
9
|
+
gem.authors = ["Shou Takenaka"]
|
10
|
+
gem.email = ["shou_takenaka@guihua.jp"]
|
11
|
+
gem.description = %q{activerecord_uuid_as_pk is a extension for ActiveRecord to enable to use uuid for id attribute.}
|
12
|
+
gem.summary = %q{ActiveRecord extension to use uuid for id attribute}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "uuidtools"
|
21
|
+
gem.add_development_dependency "rails"
|
22
|
+
gem.add_development_dependency "mysql2"
|
23
|
+
gem.add_development_dependency "rspec"
|
24
|
+
gem.add_development_dependency "rspec-rails"
|
25
|
+
gem.add_development_dependency "simplecov"
|
26
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require "uuidtools"
|
2
|
+
require "activerecord_uuid_as_pk/version"
|
3
|
+
|
4
|
+
|
5
|
+
module ActiveRecordUUIDAsPK
|
6
|
+
|
7
|
+
require "activerecord_uuid_as_pk/railtie" if defined?(Rails)
|
8
|
+
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
base.send :extend, ClassMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
def uuid_as_primary_key()
|
18
|
+
self.primary_key = "uuid"
|
19
|
+
|
20
|
+
define_method "formatted_id" do
|
21
|
+
return self.id.present? ? UUIDTools::UUID.parse_raw(self.id).to_s : nil
|
22
|
+
end
|
23
|
+
|
24
|
+
define_method "hex_id" do
|
25
|
+
return self.id.present? ? UUIDTools::UUID.parse_raw(self.id).hexdigest : nil
|
26
|
+
end
|
27
|
+
|
28
|
+
before_create do
|
29
|
+
if self.id.nil?
|
30
|
+
self.id = UUIDTools::UUID.timestamp_create.raw
|
31
|
+
elsif self.id.is_a?(UUIDTools::UUID)
|
32
|
+
self.id = self.id.raw
|
33
|
+
elsif self.id.is_a?(String)
|
34
|
+
if self.id.size != 16
|
35
|
+
if self.id =~ /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$/
|
36
|
+
self.id = UUIDTools::UUID.parse(self.id).raw
|
37
|
+
else
|
38
|
+
self.id = UUIDTools::UUID.timestamp_create.raw
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
self.id = UUIDTools::UUID.timestamp_create.raw
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
(class << self; self end).class_eval do
|
47
|
+
|
48
|
+
define_method "find_by_formatted_id" do |uuid|
|
49
|
+
raw_uuid = UUIDTools::UUID.parse(uuid).raw
|
50
|
+
return self.find_by_uuid(raw_uuid)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "activerecord_uuid_as_pk"
|
2
|
+
require "rails"
|
3
|
+
|
4
|
+
|
5
|
+
module ActiveRecordUUIDAsPK
|
6
|
+
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
|
9
|
+
initializer "activerecord_uuid_as_pk.initialize" do
|
10
|
+
ActiveSupport.on_load(:active_record) do
|
11
|
+
ActiveRecord::Base.send :include, ActiveRecordUUIDAsPK
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
|
4
|
+
describe ActiveRecordUUIDAsPK, ".find_by_formatted_id" do
|
5
|
+
|
6
|
+
before do
|
7
|
+
uuid = UUIDTools::UUID.parse("488995f0-a0b4-11e2-9e96-0800200c9a66")
|
8
|
+
Account.create!(:id => uuid.raw, :name => "user1")
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
it "returns a found model" do
|
13
|
+
o = Account.find_by_formatted_id("488995f0-a0b4-11e2-9e96-0800200c9a66")
|
14
|
+
o.name.should == "user1"
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
it "returns nil when no model found" do
|
19
|
+
o = Account.find_by_formatted_id("dba654cd-c798-483b-9eff-a0e68ce10846")
|
20
|
+
o.should be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
describe ActiveRecordUUIDAsPK, "#before_create" do
|
27
|
+
|
28
|
+
context "when uuid is set UUIDTools::UUID" do
|
29
|
+
it "converts the uuid to raw" do
|
30
|
+
uuid = UUIDTools::UUID.parse("28316607-02d0-4069-917a-8c29b25674d4")
|
31
|
+
o = Account.create!(:id => uuid, :name => "user1")
|
32
|
+
o.id.should == uuid.raw
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
context "when uuid is set raw uuid string" do
|
38
|
+
it "does nothing" do
|
39
|
+
uuid = UUIDTools::UUID.parse("ec9621be-5293-4f35-aa27-f993c754ec5c")
|
40
|
+
o = Account.create!(:id => uuid.raw, :name => "user1")
|
41
|
+
o.id.should == uuid.raw
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
context "when uuid is set uuid format string" do
|
47
|
+
it "converts the uuid string to raw" do
|
48
|
+
o = Account.create!(:id => "5864d76a-2738-415d-9e76-87326ae9d73d", :name => "user1")
|
49
|
+
o.id.should == UUIDTools::UUID.parse("5864d76a-2738-415d-9e76-87326ae9d73d").raw
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
context "when uuid is set other string" do
|
55
|
+
it "generates a new uuid" do
|
56
|
+
uuid = UUIDTools::UUID.parse("a7b62e8f-02ea-4ee2-adbd-ce579e2a1ab2")
|
57
|
+
UUIDTools::UUID.stub(:timestamp_create).and_return(uuid)
|
58
|
+
o = Account.create!(:id => "other_string", :name => "user1")
|
59
|
+
o.id.should == uuid.raw
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
context "when uuid is neither Stirng nor UUIDTools::UUID" do
|
65
|
+
it "generates a new uuid" do
|
66
|
+
uuid = UUIDTools::UUID.parse("3acab61b-1dbc-4636-8aa5-726a777b64b7")
|
67
|
+
UUIDTools::UUID.stub(:timestamp_create).and_return(uuid)
|
68
|
+
o = Account.create!(:id => 1, :name => "user1")
|
69
|
+
o.id.should == uuid.raw
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
context "when uuid is nil" do
|
75
|
+
it "generates a new uuid" do
|
76
|
+
uuid = UUIDTools::UUID.parse("a7b62e8f-02ea-4ee2-adbd-ce579e2a1ab2")
|
77
|
+
UUIDTools::UUID.stub(:timestamp_create).and_return(uuid)
|
78
|
+
o = Account.create!(:id => nil, :name => "user1")
|
79
|
+
o.id.should == uuid.raw
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
describe ActiveRecordUUIDAsPK, "#formatted_id" do
|
87
|
+
|
88
|
+
context "when uuid is set value" do
|
89
|
+
it "returns a uuid string" do
|
90
|
+
o = Account.create!(:id => "71366243-a184-418f-bce1-3d1f5c28d669")
|
91
|
+
o.formatted_id.should == "71366243-a184-418f-bce1-3d1f5c28d669"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
context "when uuid is nil" do
|
97
|
+
it "returns nil" do
|
98
|
+
o = Account.new
|
99
|
+
o.formatted_id.should be_nil
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
describe ActiveRecordUUIDAsPK, "#hex_id" do
|
107
|
+
|
108
|
+
context "when uuid is set value" do
|
109
|
+
it "returns a uuid string" do
|
110
|
+
uuid = UUIDTools::UUID.parse("a3ea6c43-3971-426f-b488-99c5d141bce5")
|
111
|
+
o = Account.create!(:id => uuid.to_s)
|
112
|
+
o.hex_id.should == uuid.hexdigest
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
context "when uuid is nil" do
|
118
|
+
it "returns nil" do
|
119
|
+
o = Account.new
|
120
|
+
o.hex_id.should be_nil
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
require "simplecov"
|
5
|
+
SimpleCov.start do
|
6
|
+
add_filter "spec"
|
7
|
+
add_filter "test_app"
|
8
|
+
add_filter "vendor"
|
9
|
+
end
|
10
|
+
|
11
|
+
$: << File.join(File.dirname(__FILE__), "..", "lib")
|
12
|
+
|
13
|
+
ENV["RAILS_ENV"] ||= "test"
|
14
|
+
require "#{File.dirname(__FILE__)}/../test_app/config/environment"
|
15
|
+
require "rspec/rails"
|
16
|
+
require "rspec/autorun"
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
config.use_transactional_fixtures = true
|
20
|
+
end
|
data/test_app/Gemfile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '3.2.13'
|
4
|
+
gem 'mysql2'
|
5
|
+
|
6
|
+
|
7
|
+
# Gems used only for assets and not required
|
8
|
+
# in production environments by default.
|
9
|
+
#group :assets do
|
10
|
+
# gem 'sass-rails', '~> 3.2.3'
|
11
|
+
# gem 'coffee-rails', '~> 3.2.1'
|
12
|
+
#
|
13
|
+
# # See https://github.com/sstephenson/execjs#readme for more supported runtimes
|
14
|
+
# # gem 'therubyracer', :platforms => :ruby
|
15
|
+
#
|
16
|
+
# gem 'uglifier', '>= 1.0.3'
|
17
|
+
#end
|
18
|
+
#
|
19
|
+
#gem 'jquery-rails'
|
20
|
+
|
21
|
+
# To use ActiveModel has_secure_password
|
22
|
+
# gem 'bcrypt-ruby', '~> 3.0.0'
|
23
|
+
|
24
|
+
# To use Jbuilder templates for JSON
|
25
|
+
# gem 'jbuilder'
|
26
|
+
|
27
|
+
# Use unicorn as the app server
|
28
|
+
# gem 'unicorn'
|
29
|
+
|
30
|
+
# Deploy with Capistrano
|
31
|
+
# gem 'capistrano'
|
32
|
+
|
33
|
+
# To use debugger
|
34
|
+
# gem 'debugger'
|
@@ -0,0 +1,261 @@
|
|
1
|
+
== Welcome to Rails
|
2
|
+
|
3
|
+
Rails is a web-application framework that includes everything needed to create
|
4
|
+
database-backed web applications according to the Model-View-Control pattern.
|
5
|
+
|
6
|
+
This pattern splits the view (also called the presentation) into "dumb"
|
7
|
+
templates that are primarily responsible for inserting pre-built data in between
|
8
|
+
HTML tags. The model contains the "smart" domain objects (such as Account,
|
9
|
+
Product, Person, Post) that holds all the business logic and knows how to
|
10
|
+
persist themselves to a database. The controller handles the incoming requests
|
11
|
+
(such as Save New Account, Update Product, Show Post) by manipulating the model
|
12
|
+
and directing data to the view.
|
13
|
+
|
14
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
15
|
+
layer entitled Active Record. This layer allows you to present the data from
|
16
|
+
database rows as objects and embellish these data objects with business logic
|
17
|
+
methods. You can read more about Active Record in
|
18
|
+
link:files/vendor/rails/activerecord/README.html.
|
19
|
+
|
20
|
+
The controller and view are handled by the Action Pack, which handles both
|
21
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
22
|
+
are bundled in a single package due to their heavy interdependence. This is
|
23
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
24
|
+
more separate. Each of these packages can be used independently outside of
|
25
|
+
Rails. You can read more about Action Pack in
|
26
|
+
link:files/vendor/rails/actionpack/README.html.
|
27
|
+
|
28
|
+
|
29
|
+
== Getting Started
|
30
|
+
|
31
|
+
1. At the command prompt, create a new Rails application:
|
32
|
+
<tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
|
33
|
+
|
34
|
+
2. Change directory to <tt>myapp</tt> and start the web server:
|
35
|
+
<tt>cd myapp; rails server</tt> (run with --help for options)
|
36
|
+
|
37
|
+
3. Go to http://localhost:3000/ and you'll see:
|
38
|
+
"Welcome aboard: You're riding Ruby on Rails!"
|
39
|
+
|
40
|
+
4. Follow the guidelines to start developing your application. You can find
|
41
|
+
the following resources handy:
|
42
|
+
|
43
|
+
* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
|
44
|
+
* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
|
45
|
+
|
46
|
+
|
47
|
+
== Debugging Rails
|
48
|
+
|
49
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
50
|
+
will help you debug it and get it back on the rails.
|
51
|
+
|
52
|
+
First area to check is the application log files. Have "tail -f" commands
|
53
|
+
running on the server.log and development.log. Rails will automatically display
|
54
|
+
debugging and runtime information to these files. Debugging info will also be
|
55
|
+
shown in the browser on requests from 127.0.0.1.
|
56
|
+
|
57
|
+
You can also log your own messages directly into the log file from your code
|
58
|
+
using the Ruby logger class from inside your controllers. Example:
|
59
|
+
|
60
|
+
class WeblogController < ActionController::Base
|
61
|
+
def destroy
|
62
|
+
@weblog = Weblog.find(params[:id])
|
63
|
+
@weblog.destroy
|
64
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
The result will be a message in your log file along the lines of:
|
69
|
+
|
70
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
|
71
|
+
|
72
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
73
|
+
|
74
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
|
75
|
+
several books available online as well:
|
76
|
+
|
77
|
+
* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
|
78
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
79
|
+
|
80
|
+
These two books will bring you up to speed on the Ruby language and also on
|
81
|
+
programming in general.
|
82
|
+
|
83
|
+
|
84
|
+
== Debugger
|
85
|
+
|
86
|
+
Debugger support is available through the debugger command when you start your
|
87
|
+
Mongrel or WEBrick server with --debugger. This means that you can break out of
|
88
|
+
execution at any point in the code, investigate and change the model, and then,
|
89
|
+
resume execution! You need to install ruby-debug to run the server in debugging
|
90
|
+
mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
|
91
|
+
|
92
|
+
class WeblogController < ActionController::Base
|
93
|
+
def index
|
94
|
+
@posts = Post.all
|
95
|
+
debugger
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
So the controller will accept the action, run the first line, then present you
|
100
|
+
with a IRB prompt in the server window. Here you can do things like:
|
101
|
+
|
102
|
+
>> @posts.inspect
|
103
|
+
=> "[#<Post:0x14a6be8
|
104
|
+
@attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
|
105
|
+
#<Post:0x14a6620
|
106
|
+
@attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
|
107
|
+
>> @posts.first.title = "hello from a debugger"
|
108
|
+
=> "hello from a debugger"
|
109
|
+
|
110
|
+
...and even better, you can examine how your runtime objects actually work:
|
111
|
+
|
112
|
+
>> f = @posts.first
|
113
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
114
|
+
>> f.
|
115
|
+
Display all 152 possibilities? (y or n)
|
116
|
+
|
117
|
+
Finally, when you're ready to resume execution, you can enter "cont".
|
118
|
+
|
119
|
+
|
120
|
+
== Console
|
121
|
+
|
122
|
+
The console is a Ruby shell, which allows you to interact with your
|
123
|
+
application's domain model. Here you'll have all parts of the application
|
124
|
+
configured, just like it is when the application is running. You can inspect
|
125
|
+
domain models, change values, and save to the database. Starting the script
|
126
|
+
without arguments will launch it in the development environment.
|
127
|
+
|
128
|
+
To start the console, run <tt>rails console</tt> from the application
|
129
|
+
directory.
|
130
|
+
|
131
|
+
Options:
|
132
|
+
|
133
|
+
* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
|
134
|
+
made to the database.
|
135
|
+
* Passing an environment name as an argument will load the corresponding
|
136
|
+
environment. Example: <tt>rails console production</tt>.
|
137
|
+
|
138
|
+
To reload your controllers and models after launching the console run
|
139
|
+
<tt>reload!</tt>
|
140
|
+
|
141
|
+
More information about irb can be found at:
|
142
|
+
link:http://www.rubycentral.org/pickaxe/irb.html
|
143
|
+
|
144
|
+
|
145
|
+
== dbconsole
|
146
|
+
|
147
|
+
You can go to the command line of your database directly through <tt>rails
|
148
|
+
dbconsole</tt>. You would be connected to the database with the credentials
|
149
|
+
defined in database.yml. Starting the script without arguments will connect you
|
150
|
+
to the development database. Passing an argument will connect you to a different
|
151
|
+
database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
|
152
|
+
PostgreSQL and SQLite 3.
|
153
|
+
|
154
|
+
== Description of Contents
|
155
|
+
|
156
|
+
The default directory structure of a generated Ruby on Rails application:
|
157
|
+
|
158
|
+
|-- app
|
159
|
+
| |-- assets
|
160
|
+
| | |-- images
|
161
|
+
| | |-- javascripts
|
162
|
+
| | `-- stylesheets
|
163
|
+
| |-- controllers
|
164
|
+
| |-- helpers
|
165
|
+
| |-- mailers
|
166
|
+
| |-- models
|
167
|
+
| `-- views
|
168
|
+
| `-- layouts
|
169
|
+
|-- config
|
170
|
+
| |-- environments
|
171
|
+
| |-- initializers
|
172
|
+
| `-- locales
|
173
|
+
|-- db
|
174
|
+
|-- doc
|
175
|
+
|-- lib
|
176
|
+
| |-- assets
|
177
|
+
| `-- tasks
|
178
|
+
|-- log
|
179
|
+
|-- public
|
180
|
+
|-- script
|
181
|
+
|-- test
|
182
|
+
| |-- fixtures
|
183
|
+
| |-- functional
|
184
|
+
| |-- integration
|
185
|
+
| |-- performance
|
186
|
+
| `-- unit
|
187
|
+
|-- tmp
|
188
|
+
| `-- cache
|
189
|
+
| `-- assets
|
190
|
+
`-- vendor
|
191
|
+
|-- assets
|
192
|
+
| |-- javascripts
|
193
|
+
| `-- stylesheets
|
194
|
+
`-- plugins
|
195
|
+
|
196
|
+
app
|
197
|
+
Holds all the code that's specific to this particular application.
|
198
|
+
|
199
|
+
app/assets
|
200
|
+
Contains subdirectories for images, stylesheets, and JavaScript files.
|
201
|
+
|
202
|
+
app/controllers
|
203
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
204
|
+
automated URL mapping. All controllers should descend from
|
205
|
+
ApplicationController which itself descends from ActionController::Base.
|
206
|
+
|
207
|
+
app/models
|
208
|
+
Holds models that should be named like post.rb. Models descend from
|
209
|
+
ActiveRecord::Base by default.
|
210
|
+
|
211
|
+
app/views
|
212
|
+
Holds the template files for the view that should be named like
|
213
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use
|
214
|
+
eRuby syntax by default.
|
215
|
+
|
216
|
+
app/views/layouts
|
217
|
+
Holds the template files for layouts to be used with views. This models the
|
218
|
+
common header/footer method of wrapping views. In your views, define a layout
|
219
|
+
using the <tt>layout :default</tt> and create a file named default.html.erb.
|
220
|
+
Inside default.html.erb, call <% yield %> to render the view using this
|
221
|
+
layout.
|
222
|
+
|
223
|
+
app/helpers
|
224
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are
|
225
|
+
generated for you automatically when using generators for controllers.
|
226
|
+
Helpers can be used to wrap functionality for your views into methods.
|
227
|
+
|
228
|
+
config
|
229
|
+
Configuration files for the Rails environment, the routing map, the database,
|
230
|
+
and other dependencies.
|
231
|
+
|
232
|
+
db
|
233
|
+
Contains the database schema in schema.rb. db/migrate contains all the
|
234
|
+
sequence of Migrations for your schema.
|
235
|
+
|
236
|
+
doc
|
237
|
+
This directory is where your application documentation will be stored when
|
238
|
+
generated using <tt>rake doc:app</tt>
|
239
|
+
|
240
|
+
lib
|
241
|
+
Application specific libraries. Basically, any kind of custom code that
|
242
|
+
doesn't belong under controllers, models, or helpers. This directory is in
|
243
|
+
the load path.
|
244
|
+
|
245
|
+
public
|
246
|
+
The directory available for the web server. Also contains the dispatchers and the
|
247
|
+
default HTML files. This should be set as the DOCUMENT_ROOT of your web
|
248
|
+
server.
|
249
|
+
|
250
|
+
script
|
251
|
+
Helper scripts for automation and generation.
|
252
|
+
|
253
|
+
test
|
254
|
+
Unit and functional tests along with fixtures. When using the rails generate
|
255
|
+
command, template test files will be generated for you and placed in this
|
256
|
+
directory.
|
257
|
+
|
258
|
+
vendor
|
259
|
+
External libraries that the application depends on. Also includes the plugins
|
260
|
+
subdirectory. If the app has frozen rails, those gems also go here, under
|
261
|
+
vendor/rails/. This directory is in the load path.
|