mass_insert 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.rvmrc +53 -0
- data/.travis.yml +4 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +45 -0
- data/lib/mass_insert.rb +13 -0
- data/lib/mass_insert/adapters.rb +12 -0
- data/lib/mass_insert/adapters/abstract_query.rb +47 -0
- data/lib/mass_insert/adapters/adapter.rb +56 -0
- data/lib/mass_insert/adapters/column_value.rb +107 -0
- data/lib/mass_insert/adapters/helpers.rb +8 -0
- data/lib/mass_insert/adapters/helpers/sanitizer.rb +17 -0
- data/lib/mass_insert/adapters/helpers/timestamp.rb +38 -0
- data/lib/mass_insert/adapters/mysql2_adapter.rb +21 -0
- data/lib/mass_insert/adapters/postgresql_adapter.rb +15 -0
- data/lib/mass_insert/adapters/sqlite3_adapter.rb +37 -0
- data/lib/mass_insert/adapters/sqlserver_adapter.rb +23 -0
- data/lib/mass_insert/base.rb +43 -0
- data/lib/mass_insert/process_control.rb +24 -0
- data/lib/mass_insert/query_builder.rb +42 -0
- data/lib/mass_insert/query_execution.rb +29 -0
- data/lib/mass_insert/version.rb +3 -0
- data/mass_insert.gemspec +22 -0
- data/spec/active_record_dummy/.gitignore +15 -0
- data/spec/active_record_dummy/Gemfile +40 -0
- data/spec/active_record_dummy/README.rdoc +261 -0
- data/spec/active_record_dummy/Rakefile +7 -0
- data/spec/active_record_dummy/app/assets/images/rails.png +0 -0
- data/spec/active_record_dummy/app/assets/javascripts/application.js +15 -0
- data/spec/active_record_dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/active_record_dummy/app/controllers/application_controller.rb +3 -0
- data/spec/active_record_dummy/app/helpers/application_helper.rb +2 -0
- data/spec/active_record_dummy/app/mailers/.gitkeep +0 -0
- data/spec/active_record_dummy/app/models/.gitkeep +0 -0
- data/spec/active_record_dummy/app/models/user.rb +3 -0
- data/spec/active_record_dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/active_record_dummy/config.ru +4 -0
- data/spec/active_record_dummy/config/application.rb +68 -0
- data/spec/active_record_dummy/config/boot.rb +6 -0
- data/spec/active_record_dummy/config/database.yml +54 -0
- data/spec/active_record_dummy/config/environment.rb +5 -0
- data/spec/active_record_dummy/config/environments/development.rb +37 -0
- data/spec/active_record_dummy/config/environments/mysql2.rb +37 -0
- data/spec/active_record_dummy/config/environments/postgresql.rb +37 -0
- data/spec/active_record_dummy/config/environments/production.rb +67 -0
- data/spec/active_record_dummy/config/environments/sqlite3.rb +37 -0
- data/spec/active_record_dummy/config/environments/test.rb +37 -0
- data/spec/active_record_dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/active_record_dummy/config/initializers/inflections.rb +15 -0
- data/spec/active_record_dummy/config/initializers/mime_types.rb +5 -0
- data/spec/active_record_dummy/config/initializers/secret_token.rb +7 -0
- data/spec/active_record_dummy/config/initializers/session_store.rb +8 -0
- data/spec/active_record_dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/active_record_dummy/config/locales/en.yml +5 -0
- data/spec/active_record_dummy/config/routes.rb +58 -0
- data/spec/active_record_dummy/db/migrate/20130412154541_create_users.rb +14 -0
- data/spec/active_record_dummy/db/schema.rb +27 -0
- data/spec/active_record_dummy/db/seeds.rb +7 -0
- data/spec/active_record_dummy/lib/assets/.gitkeep +0 -0
- data/spec/active_record_dummy/lib/tasks/.gitkeep +0 -0
- data/spec/active_record_dummy/log/.gitkeep +0 -0
- data/spec/active_record_dummy/public/404.html +26 -0
- data/spec/active_record_dummy/public/422.html +26 -0
- data/spec/active_record_dummy/public/500.html +25 -0
- data/spec/active_record_dummy/public/favicon.ico +0 -0
- data/spec/active_record_dummy/public/index.html +241 -0
- data/spec/active_record_dummy/public/robots.txt +5 -0
- data/spec/active_record_dummy/script/rails +6 -0
- data/spec/active_record_dummy/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/active_record_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/active_record_dummy/vendor/plugins/.gitkeep +0 -0
- data/spec/active_record_models/column_types/binary_spec.rb +60 -0
- data/spec/active_record_models/column_types/boolean_spec.rb +52 -0
- data/spec/active_record_models/column_types/decimal_spec.rb +49 -0
- data/spec/active_record_models/column_types/integer_spec.rb +49 -0
- data/spec/active_record_models/column_types/string_spec.rb +50 -0
- data/spec/active_record_models/model_spec.rb +50 -0
- data/spec/dummy_models/test.rb +5 -0
- data/spec/mass_insert/adapters/abstract_query_spec.rb +109 -0
- data/spec/mass_insert/adapters/adapter_spec.rb +117 -0
- data/spec/mass_insert/adapters/column_value_spec.rb +292 -0
- data/spec/mass_insert/adapters/helpers/sanitizer_spec.rb +39 -0
- data/spec/mass_insert/adapters/helpers/timestamp_spec.rb +79 -0
- data/spec/mass_insert/adapters/helpers_spec.rb +16 -0
- data/spec/mass_insert/adapters/mysql_adapter_spec.rb +39 -0
- data/spec/mass_insert/adapters/postgresql_adapter_spec.rb +29 -0
- data/spec/mass_insert/adapters/sqlite3_adapter_spec.rb +90 -0
- data/spec/mass_insert/adapters/sqlserver_adapter_spec.rb +56 -0
- data/spec/mass_insert/adapters_spec.rb +40 -0
- data/spec/mass_insert/base_spec.rb +98 -0
- data/spec/mass_insert/process_control_spec.rb +56 -0
- data/spec/mass_insert/query_builder_spec.rb +88 -0
- data/spec/mass_insert/query_execution_spec.rb +53 -0
- data/spec/mass_insert_spec.rb +28 -0
- data/spec/spec_helper.rb +6 -0
- metadata +254 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
module Helpers
|
4
|
+
module Sanitizer
|
5
|
+
|
6
|
+
# Update the array with the columns names according to the options
|
7
|
+
# and prepare the columns array with only valid columns.
|
8
|
+
def sanitized_columns
|
9
|
+
columns = table_columns
|
10
|
+
columns.delete(primary_key) if primary_key_mode == :auto
|
11
|
+
columns
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
module Helpers
|
4
|
+
module Timestamp
|
5
|
+
|
6
|
+
# Returns true o false if the database table has the
|
7
|
+
# timestamp columns.
|
8
|
+
def timestamp?
|
9
|
+
column_names.include?(:created_at) &&
|
10
|
+
column_names.include?(:updated_at)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns timestamp format according to the database adapter. This
|
14
|
+
# function can be overwrite in database adapters classes to put the
|
15
|
+
# correct format to that database.
|
16
|
+
def timestamp_format
|
17
|
+
"%Y-%m-%d %H:%M:%S.%6N"
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns the timestamp value with specific format according to the
|
21
|
+
# correct timestamp format to that database.
|
22
|
+
def timestamp
|
23
|
+
Time.now.strftime(timestamp_format)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the timestamp values to be merge into row values that
|
27
|
+
# will be saved in the database.
|
28
|
+
def timestamp_values
|
29
|
+
{
|
30
|
+
:created_at => timestamp,
|
31
|
+
:updated_at => timestamp
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
class Mysql2Adapter < Adapter
|
4
|
+
|
5
|
+
# This method is overwrite because the timestamp format to this
|
6
|
+
# database engine does not need precision in nanoseconds.
|
7
|
+
def timestamp_format
|
8
|
+
"%Y-%m-%d %H:%M:%S"
|
9
|
+
end
|
10
|
+
|
11
|
+
# This functions calls the necessary functions to create a complete
|
12
|
+
# mysql query to multiple insertion. The methods are in the Abstract
|
13
|
+
# Sql String module. If some method is too specific to this database
|
14
|
+
# adapter you can overwrite it.
|
15
|
+
def execute
|
16
|
+
"#{begin_string}#{string_columns}#{string_values}"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
class PostgreSQLAdapter < Adapter
|
4
|
+
|
5
|
+
# This functions calls the necessary functions to create a complete
|
6
|
+
# postgresql query to multiple insertion. The methods are in the
|
7
|
+
# AbstractQuery module. If some method is too specific to this database
|
8
|
+
# adapter you can overwrite it.
|
9
|
+
def execute
|
10
|
+
"#{begin_string}#{string_columns}#{string_values}"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
class SQLite3Adapter < Adapter
|
4
|
+
|
5
|
+
MAX_VALUES_PER_INSERTION = 500
|
6
|
+
|
7
|
+
# This method is overwrite because the query string to the Sqlite3
|
8
|
+
# adapter is different. Then the method in the AbstractQuery module
|
9
|
+
# is ignored.
|
10
|
+
def string_values
|
11
|
+
"SELECT #{string_rows_values};"
|
12
|
+
end
|
13
|
+
|
14
|
+
# This method is overwrite because the query string to complete the
|
15
|
+
# string rows values is different. The separator to sqlite adapter is
|
16
|
+
# 'UNION SELECT' instead of '), (' in other sql adapters.
|
17
|
+
def string_rows_values
|
18
|
+
values.map{ |row| string_single_row_values(row) }.join(" UNION SELECT ")
|
19
|
+
end
|
20
|
+
|
21
|
+
# This functions calls the necessary functions to create a complete
|
22
|
+
# sqlite3 query to multiple insertion. The methods are in the Abstract
|
23
|
+
# Query module. If some method is too specific to this database adapter
|
24
|
+
# you can overwrite it. The values that the user gave will be treated
|
25
|
+
# in batches of 500 items because sqlite database allows by default
|
26
|
+
# batches of 500.and each batch will generate a query. This method will
|
27
|
+
# generate an array with batch queries.
|
28
|
+
def execute
|
29
|
+
@values.each_slice(MAX_VALUES_PER_INSERTION).map do |slice|
|
30
|
+
@values = slice
|
31
|
+
"#{begin_string}#{string_columns}#{string_values}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
class SQLServerAdapter < Adapter
|
4
|
+
|
5
|
+
MAX_VALUES_PER_INSERTION = 1000
|
6
|
+
|
7
|
+
# This functions calls the necessary functions to create a complete
|
8
|
+
# sqlserver query to multiple insertion. The methods are in the Abstract
|
9
|
+
# Query module. If some method is too specific to this database adapter
|
10
|
+
# you can overwrite it. The values that the user gave will be treated
|
11
|
+
# in batches of 500 items because sqlite database allows by default
|
12
|
+
# batches of 500.and each batch will generate a query. This method will
|
13
|
+
# generate an array with batch queries.
|
14
|
+
def execute
|
15
|
+
@values.each_slice(MAX_VALUES_PER_INSERTION).map do |slice|
|
16
|
+
@values = slice
|
17
|
+
"#{begin_string}#{string_columns}#{string_values}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Base
|
3
|
+
|
4
|
+
# This method do a mass database insertion. Example...
|
5
|
+
#
|
6
|
+
# User.mass_insert(values)
|
7
|
+
#
|
8
|
+
# The values should be an array with the object values
|
9
|
+
# in a hash. Example...
|
10
|
+
#
|
11
|
+
# values = [
|
12
|
+
# {:name => "user name", :email => "user email"},
|
13
|
+
# {:name => "user name", :email => "user email"},
|
14
|
+
# {:name => "user name", :email => "user email"}
|
15
|
+
# ]
|
16
|
+
#
|
17
|
+
def mass_insert values, args = {}
|
18
|
+
options = mass_insert_options(args)
|
19
|
+
process = MassInsert::ProcessControl.new(values, options)
|
20
|
+
process.execute
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def mass_insert_options args = {}
|
26
|
+
# prepare default options that come in the class that invokes the
|
27
|
+
# mass_insert function.
|
28
|
+
args[:class_name] ||= self
|
29
|
+
args[:table_name] ||= self.table_name
|
30
|
+
|
31
|
+
# prepare attributes options that were configured by the user and
|
32
|
+
# if the options weren't passed, they would be initialized with the
|
33
|
+
# default values.
|
34
|
+
args[:primary_key] ||= :id
|
35
|
+
args[:primary_key_mode] ||= :auto
|
36
|
+
|
37
|
+
# Returns the arguments but sanitized and ready to be used. This
|
38
|
+
# args will be passed by params to ProcessControl class.
|
39
|
+
args
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module MassInsert
|
2
|
+
class ProcessControl
|
3
|
+
|
4
|
+
attr_accessor :values, :options
|
5
|
+
|
6
|
+
def initialize values, options
|
7
|
+
@values = values
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
# This method is responsible to call all the necessary process to
|
12
|
+
# complete the mass insertion process.
|
13
|
+
def execute
|
14
|
+
QueryExecution.new(query).execute
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns the correct query string according to database adapter
|
18
|
+
# previosly configured usually in database.yml in Rails project.
|
19
|
+
def query
|
20
|
+
QueryBuilder.new(values, options).build
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module MassInsert
|
2
|
+
class QueryBuilder
|
3
|
+
|
4
|
+
attr_accessor :values, :options
|
5
|
+
|
6
|
+
def initialize values, options
|
7
|
+
@values = values
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
# This function calls the correct adapter class and returns the
|
12
|
+
# sql string ready to be executed and returns it to be execute in
|
13
|
+
# the QueryExecution class.
|
14
|
+
def build
|
15
|
+
adapter_instance_class.execute
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns a string that contains the adapter type previosly
|
19
|
+
# configured in Rails project usually in the database.yml file.
|
20
|
+
def adapter
|
21
|
+
ActiveRecord::Base.connection.instance_values["config"][:adapter]
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns an instance of the correct database adapter and this
|
25
|
+
# instance will be called to generate the sql string. The values
|
26
|
+
# and options are passed by params when the correct adapter class
|
27
|
+
# is instanced.
|
28
|
+
def adapter_instance_class
|
29
|
+
case adapter
|
30
|
+
when "mysql2"
|
31
|
+
Adapters::Mysql2Adapter.new(values, options)
|
32
|
+
when "postgresql"
|
33
|
+
Adapters::PostgreSQLAdapter.new(values, options)
|
34
|
+
when "sqlite3"
|
35
|
+
Adapters::SQLite3Adapter.new(values, options)
|
36
|
+
when "sqlserver"
|
37
|
+
Adapters::SQLServerAdapter.new(values, options)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module MassInsert
|
2
|
+
# This class is responsible to execute the query string into the
|
3
|
+
# database. Uses the ActiveRecord::Base.connection.execute functionality
|
4
|
+
# to execute the query string directly.
|
5
|
+
class QueryExecution
|
6
|
+
|
7
|
+
attr_accessor :query_container
|
8
|
+
|
9
|
+
# The query string is usually passed by params when the ProcessControl
|
10
|
+
# class instances this class. The query can be a string or an array,
|
11
|
+
# therefore to be sure that the query_container attribute is an array
|
12
|
+
# the param passed to this class is converted to array. The query
|
13
|
+
# container attribute will be iterated in execute method in this class
|
14
|
+
# to execute each query that it contains.
|
15
|
+
def initialize query_container
|
16
|
+
@query_container = Array(query_container)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Saves queries contained in query_container attributes into database.
|
20
|
+
# Use the helper that ActiveRecord provides. Query container attribute
|
21
|
+
# is iterated to save each query that it contains.
|
22
|
+
def execute
|
23
|
+
@query_container.each do |query|
|
24
|
+
ActiveRecord::Base.connection.execute(query)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/mass_insert.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mass_insert/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mass_insert"
|
8
|
+
spec.version = MassInsert::VERSION
|
9
|
+
spec.authors = ["Alejandro Gutiérrez"]
|
10
|
+
spec.email = ["alejandrodevs@gmail.com"]
|
11
|
+
spec.description = "Mass database insertion in Rails"
|
12
|
+
spec.summary = "This gem aims to provide an easy and faster way to do single database insertions in Rails."
|
13
|
+
spec.homepage = "https://github.com/alejandrogutierrez/mass_insert"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile ~/.gitignore_global
|
6
|
+
|
7
|
+
# Ignore bundler config
|
8
|
+
/.bundle
|
9
|
+
|
10
|
+
# Ignore the default SQLite database.
|
11
|
+
/db/*.sqlite3
|
12
|
+
|
13
|
+
# Ignore all logfiles and tempfiles.
|
14
|
+
/log/*.log
|
15
|
+
/tmp
|
@@ -0,0 +1,40 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '3.2.13'
|
4
|
+
|
5
|
+
# Bundle edge Rails instead:
|
6
|
+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
7
|
+
|
8
|
+
gem 'sqlite3'
|
9
|
+
gem 'mysql2'
|
10
|
+
gem 'pg'
|
11
|
+
gem 'mass_insert', :git => 'git@github.com:alejandrogutierrez/mass_insert.git', :branch => 'master'
|
12
|
+
|
13
|
+
# Gems used only for assets and not required
|
14
|
+
# in production environments by default.
|
15
|
+
group :assets do
|
16
|
+
gem 'sass-rails', '~> 3.2.3'
|
17
|
+
gem 'coffee-rails', '~> 3.2.1'
|
18
|
+
|
19
|
+
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
|
20
|
+
# gem 'therubyracer', :platforms => :ruby
|
21
|
+
|
22
|
+
gem 'uglifier', '>= 1.0.3'
|
23
|
+
end
|
24
|
+
|
25
|
+
gem 'jquery-rails'
|
26
|
+
|
27
|
+
# To use ActiveModel has_secure_password
|
28
|
+
# gem 'bcrypt-ruby', '~> 3.0.0'
|
29
|
+
|
30
|
+
# To use Jbuilder templates for JSON
|
31
|
+
# gem 'jbuilder'
|
32
|
+
|
33
|
+
# Use unicorn as the app server
|
34
|
+
# gem 'unicorn'
|
35
|
+
|
36
|
+
# Deploy with Capistrano
|
37
|
+
# gem 'capistrano'
|
38
|
+
|
39
|
+
# To use debugger
|
40
|
+
# 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.
|