activerecord-turntable 1.1.2 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.travis.yml +3 -5
- data/CHANGELOG.md +70 -0
- data/Guardfile +7 -5
- data/README.md +490 -0
- data/Rakefile +37 -22
- data/activerecord-turntable.gemspec +37 -34
- data/gemfiles/rails4_0.gemfile +6 -0
- data/gemfiles/rails4_1.gemfile +6 -0
- data/lib/active_record/turntable/active_record_ext/abstract_adapter.rb +14 -29
- data/lib/active_record/turntable/active_record_ext/activerecord_import_ext.rb +45 -0
- data/lib/active_record/turntable/active_record_ext/acts_as_archive_extension.rb +21 -0
- data/lib/active_record/turntable/active_record_ext/association.rb +85 -0
- data/lib/active_record/turntable/active_record_ext/association_preloader.rb +37 -0
- data/lib/active_record/turntable/active_record_ext/clever_load.rb +33 -76
- data/lib/active_record/turntable/active_record_ext/connection_handler_extension.rb +31 -0
- data/lib/active_record/turntable/active_record_ext/database_tasks.rb +81 -0
- data/lib/active_record/turntable/active_record_ext/fixtures.rb +54 -42
- data/lib/active_record/turntable/active_record_ext/locking_optimistic.rb +101 -0
- data/lib/active_record/turntable/active_record_ext/log_subscriber.rb +28 -46
- data/lib/active_record/turntable/active_record_ext/migration_proxy.rb +7 -0
- data/lib/active_record/turntable/active_record_ext/persistence.rb +96 -94
- data/lib/active_record/turntable/active_record_ext/relation.rb +31 -0
- data/lib/active_record/turntable/active_record_ext/schema_dumper.rb +18 -28
- data/lib/active_record/turntable/active_record_ext/transactions.rb +9 -3
- data/lib/active_record/turntable/active_record_ext.rb +26 -11
- data/lib/active_record/turntable/algorithm/base.rb +1 -1
- data/lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb +1 -1
- data/lib/active_record/turntable/algorithm.rb +7 -3
- data/lib/active_record/turntable/base.rb +67 -14
- data/lib/active_record/turntable/cluster.rb +46 -2
- data/lib/active_record/turntable/config.rb +1 -1
- data/lib/active_record/turntable/connection_proxy/mixable.rb +7 -29
- data/lib/active_record/turntable/connection_proxy.rb +61 -72
- data/lib/active_record/turntable/error.rb +5 -6
- data/lib/active_record/turntable/helpers.rb +5 -1
- data/lib/active_record/turntable/migration.rb +9 -49
- data/lib/active_record/turntable/mixer/fader/calculate_shards_sum_result.rb +13 -2
- data/lib/active_record/turntable/mixer/fader/select_shards_merge_result.rb +17 -6
- data/lib/active_record/turntable/mixer/fader/specified_shard.rb +3 -1
- data/lib/active_record/turntable/mixer/fader.rb +12 -10
- data/lib/active_record/turntable/mixer.rb +59 -29
- data/lib/active_record/turntable/plugin.rb +6 -0
- data/lib/active_record/turntable/pool_proxy.rb +12 -19
- data/lib/active_record/turntable/rack/query_cache.rb +20 -23
- data/lib/active_record/turntable/rack.rb +4 -2
- data/lib/active_record/turntable/railtie.rb +4 -3
- data/lib/active_record/turntable/railties/databases.rake +81 -122
- data/lib/active_record/turntable/seq_shard.rb +1 -1
- data/lib/active_record/turntable/sequencer/api.rb +1 -1
- data/lib/active_record/turntable/sequencer/barrage.rb +28 -0
- data/lib/active_record/turntable/sequencer.rb +27 -9
- data/lib/active_record/turntable/shard.rb +2 -2
- data/lib/active_record/turntable/sql_tree_patch.rb +1 -1
- data/lib/active_record/turntable/version.rb +1 -1
- data/lib/active_record/turntable.rb +26 -16
- data/lib/generators/templates/turntable.yml +9 -7
- data/spec/active_record/turntable/active_record_ext/association_preloader_spec.rb +78 -0
- data/spec/active_record/turntable/active_record_ext/association_spec.rb +72 -0
- data/spec/active_record/turntable/active_record_ext/clever_load_spec.rb +25 -46
- data/spec/active_record/turntable/active_record_ext/locking_optimistic_spec.rb +28 -0
- data/spec/active_record/turntable/active_record_ext/persistence_spec.rb +46 -25
- data/spec/active_record/turntable/algorithm/range_algorithm_spec.rb +4 -4
- data/spec/active_record/turntable/algorithm/range_bsearch_algorithm_spec.rb +35 -0
- data/spec/active_record/turntable/algorithm_spec.rb +28 -12
- data/spec/active_record/turntable/base_spec.rb +1 -1
- data/spec/active_record/turntable/cluster_spec.rb +27 -5
- data/spec/active_record/turntable/config_spec.rb +2 -2
- data/spec/active_record/turntable/connection_proxy_spec.rb +112 -45
- data/spec/active_record/turntable/finder_spec.rb +24 -11
- data/spec/active_record/turntable/mixer_spec.rb +21 -21
- data/spec/active_record/turntable/rack/query_cache_spec.rb +19 -0
- data/spec/active_record/turntable/sequencer/api_spec.rb +38 -0
- data/spec/active_record/turntable/sequencer/barrage_spec.rb +22 -0
- data/spec/active_record/turntable/sequencer/mysql_spec.rb +22 -0
- data/spec/active_record/turntable/shard_spec.rb +1 -1
- data/spec/active_record/turntable/transaction_spec.rb +35 -0
- data/spec/active_record/turntable_spec.rb +4 -4
- data/spec/config/database.yml +24 -34
- data/spec/config/turntable.yml +18 -1
- data/spec/fabricators/turntable_fabricator.rb +0 -2
- data/spec/models/card.rb +3 -0
- data/spec/models/cards_user.rb +10 -0
- data/spec/models/cards_users_histories.rb +7 -0
- data/spec/models/events_users_history.rb +7 -0
- data/spec/models/user.rb +7 -0
- data/spec/models/user_status.rb +6 -0
- data/spec/spec_helper.rb +10 -4
- data/spec/support/matchers/be_saved_to.rb +6 -0
- data/spec/support/turntable_helper.rb +29 -0
- metadata +124 -74
- data/README.rdoc +0 -294
- data/gemfiles/rails3_0.gemfile +0 -7
- data/gemfiles/rails3_1.gemfile +0 -6
- data/gemfiles/rails3_2.gemfile +0 -6
- data/lib/active_record/turntable/compatible.rb +0 -19
- data/sample_app/.gitignore +0 -16
- data/sample_app/Gemfile +0 -41
- data/sample_app/README.rdoc +0 -261
- data/sample_app/Rakefile +0 -7
- data/sample_app/app/assets/images/rails.png +0 -0
- data/sample_app/app/assets/javascripts/application.js +0 -15
- data/sample_app/app/assets/stylesheets/application.css +0 -13
- data/sample_app/app/controllers/application_controller.rb +0 -3
- data/sample_app/app/helpers/application_helper.rb +0 -2
- data/sample_app/app/mailers/.gitkeep +0 -0
- data/sample_app/app/models/.gitkeep +0 -0
- data/sample_app/app/models/user.rb +0 -4
- data/sample_app/app/views/layouts/application.html.erb +0 -14
- data/sample_app/config/application.rb +0 -65
- data/sample_app/config/boot.rb +0 -6
- data/sample_app/config/database.yml +0 -70
- data/sample_app/config/environment.rb +0 -5
- data/sample_app/config/environments/development.rb +0 -37
- data/sample_app/config/environments/production.rb +0 -67
- data/sample_app/config/environments/test.rb +0 -37
- data/sample_app/config/initializers/backtrace_silencers.rb +0 -7
- data/sample_app/config/initializers/inflections.rb +0 -15
- data/sample_app/config/initializers/mime_types.rb +0 -5
- data/sample_app/config/initializers/secret_token.rb +0 -7
- data/sample_app/config/initializers/session_store.rb +0 -8
- data/sample_app/config/initializers/wrap_parameters.rb +0 -14
- data/sample_app/config/locales/en.yml +0 -5
- data/sample_app/config/routes.rb +0 -58
- data/sample_app/config/turntable.yml +0 -64
- data/sample_app/config.ru +0 -4
- data/sample_app/db/migrate/20120316073058_create_users.rb +0 -11
- data/sample_app/db/seeds.rb +0 -7
- data/sample_app/lib/assets/.gitkeep +0 -0
- data/sample_app/lib/tasks/.gitkeep +0 -0
- data/sample_app/log/.gitkeep +0 -0
- data/sample_app/public/404.html +0 -26
- data/sample_app/public/422.html +0 -26
- data/sample_app/public/500.html +0 -25
- data/sample_app/public/favicon.ico +0 -0
- data/sample_app/public/index.html +0 -241
- data/sample_app/public/robots.txt +0 -5
- data/sample_app/script/rails +0 -6
- data/sample_app/vendor/assets/javascripts/.gitkeep +0 -0
- data/sample_app/vendor/assets/stylesheets/.gitkeep +0 -0
- data/sample_app/vendor/plugins/.gitkeep +0 -0
- data/spec/test_models.rb +0 -27
- data/spec/turntable_helper.rb +0 -29
data/README.rdoc
DELETED
@@ -1,294 +0,0 @@
|
|
1
|
-
= ActiveRecord::Turntable {<img src="https://badge.fury.io/rb/activerecord-turntable.png" alt="Gem Version" />}[http://badge.fury.io/rb/activerecord-turntable] {<img src="https://secure.travis-ci.org/drecom/activerecord-turntable.png" />}[http://travis-ci.org/drecom/activerecord-turntable] {<img src="https://gemnasium.com/drecom/activerecord-turntable.png" alt="Dependency Status" />}[https://gemnasium.com/drecom/activerecord-turntable] {<img src="https://coveralls.io/repos/drecom/activerecord-turntable/badge.png?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/drecom/activerecord-turntable]
|
2
|
-
|
3
|
-
ActiveRecord::Turntable is a database sharding extension for ActiveRecord.
|
4
|
-
|
5
|
-
== Dependencies
|
6
|
-
activerecord(>=3.0.0)
|
7
|
-
|
8
|
-
== Supported Database
|
9
|
-
Currently supports mysql only.
|
10
|
-
|
11
|
-
== Installation
|
12
|
-
|
13
|
-
Add 'activerecord-turntable' to Gemfile:
|
14
|
-
|
15
|
-
gem 'activerecord-turntable', '~>1.1.0'
|
16
|
-
|
17
|
-
Run a bundle install:
|
18
|
-
|
19
|
-
bundle install
|
20
|
-
|
21
|
-
Run install generator:
|
22
|
-
|
23
|
-
bundle exec rails g active_record:turntable:install
|
24
|
-
|
25
|
-
generator creates Rails.root/config/turntable.yml
|
26
|
-
|
27
|
-
== ActiveRecord::Turntable terms
|
28
|
-
|
29
|
-
* Shard
|
30
|
-
* Cluster - a cluster of shards.
|
31
|
-
* Master - default ActiveRecord::Base's connection
|
32
|
-
* Sequencer - a module that creates global sequence number
|
33
|
-
|
34
|
-
== Example
|
35
|
-
|
36
|
-
=== Example Databases Structure
|
37
|
-
one main database(default ActiveRecord::Base connection) and
|
38
|
-
three user databases sharded by user_id.
|
39
|
-
|
40
|
-
+-------+
|
41
|
-
| App |
|
42
|
-
+-------+
|
43
|
-
|
|
44
|
-
+---------+-----------+---------+---------+
|
45
|
-
| | | | |
|
46
|
-
+--------+ +---------+ +-------+ +-------+ +-------+
|
47
|
-
| Master | |Sequencer| |UserDB1| |UserDB2| |UserDB3|
|
48
|
-
+--------+ +---------+ +-------+ +-------+ +-------+
|
49
|
-
|
50
|
-
=== Example Configuration
|
51
|
-
|
52
|
-
Edit turntable.yml and database.yml. See below example config.
|
53
|
-
|
54
|
-
example turntable.yml:
|
55
|
-
|
56
|
-
development:
|
57
|
-
clusters:
|
58
|
-
user_cluster: # <-- cluster name
|
59
|
-
algorithm: range # <-- only range or range_bsearch sharding is supported currently.
|
60
|
-
seq:
|
61
|
-
connection: user_seq_1
|
62
|
-
shards:
|
63
|
-
- connection: user_shard_1 # <-- shard name
|
64
|
-
less_than: 100 # <-- shard range(like mysql partitioning)
|
65
|
-
- connection: user_shard_2
|
66
|
-
less_than: 200
|
67
|
-
- connection: user_shard_3
|
68
|
-
less_than: 2000000000
|
69
|
-
|
70
|
-
database.yml:
|
71
|
-
connection_spec: &spec
|
72
|
-
adapter: mysql2
|
73
|
-
encoding: utf8
|
74
|
-
reconnect: false
|
75
|
-
pool: 5
|
76
|
-
username: root
|
77
|
-
password: root
|
78
|
-
socket: /tmp/mysql.sock
|
79
|
-
|
80
|
-
development:
|
81
|
-
<<: *spec
|
82
|
-
database: sample_app_development
|
83
|
-
seq: # <-- sequence definition
|
84
|
-
user_seq_1:
|
85
|
-
<<: *spec
|
86
|
-
database: sample_app_user_seq_development
|
87
|
-
shards: # <-- shards definition
|
88
|
-
user_shard_1:
|
89
|
-
<<: *spec
|
90
|
-
database: sample_app_user1_development
|
91
|
-
user_shard_2:
|
92
|
-
<<: *spec
|
93
|
-
database: sample_app_user2_development
|
94
|
-
user_shard_3:
|
95
|
-
<<: *spec
|
96
|
-
database: sample_app_user3_development
|
97
|
-
|
98
|
-
|
99
|
-
=== Migration Example
|
100
|
-
|
101
|
-
Generate a model:
|
102
|
-
|
103
|
-
bundle exec rails g model user name:string
|
104
|
-
|
105
|
-
Edit migration file:
|
106
|
-
|
107
|
-
class CreateUsers < ActiveRecord::Migration
|
108
|
-
clusters :user_cluster # <-- the cluster that executes migration
|
109
|
-
|
110
|
-
def change
|
111
|
-
create_table :users do |t|
|
112
|
-
t.string :name
|
113
|
-
t.timestamps
|
114
|
-
end
|
115
|
-
create_sequence_for(:users)
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
then please execute rake tasks:
|
120
|
-
|
121
|
-
bundle exec rake db:create
|
122
|
-
bundle exec rake db:migrate
|
123
|
-
|
124
|
-
Those rake tasks would be executed to shards too.
|
125
|
-
|
126
|
-
=== Model Example
|
127
|
-
add turntable [shard_key_name] to the model class:
|
128
|
-
|
129
|
-
class User < ActiveRecord::Base
|
130
|
-
turntable :user_cluster, :id
|
131
|
-
sequencer
|
132
|
-
has_one :status
|
133
|
-
end
|
134
|
-
|
135
|
-
class Status < ActiveRecord::Base
|
136
|
-
turntable :user_cluster, :user_id
|
137
|
-
sequencer
|
138
|
-
belongs_to :user
|
139
|
-
end
|
140
|
-
|
141
|
-
== Usage
|
142
|
-
|
143
|
-
=== Create
|
144
|
-
> User.create(name: "hoge")
|
145
|
-
(0.0ms) [Shard: user_seq_1] BEGIN
|
146
|
-
(0.3ms) [Shard: user_seq_1] UPDATE `users_id_seq` SET id=LAST_INSERT_ID(id+1)
|
147
|
-
(0.8ms) [Shard: user_seq_1] COMMIT
|
148
|
-
(0.1ms) [Shard: user_seq_1] SELECT LAST_INSERT_ID()
|
149
|
-
(0.1ms) [Shard: user_shard_1] BEGIN
|
150
|
-
[ActiveRecord::Turntable] Sending method: insert, sql: #<Arel::InsertManager:0x007f8503685b48>, shards: ["user_shard_1"]
|
151
|
-
SQL (0.8ms) [Shard: user_shard_1] INSERT INTO `users` (`created_at`, `id`, `name`, `updated_at`) VALUES ('2012-04-10 03:59:42', 2, 'hoge', '2012-04-10 03:59:42')
|
152
|
-
(0.4ms) [Shard: user_shard_1] COMMIT
|
153
|
-
=> #<User id: 2, name: "hoge", created_at: "2012-04-10 03:59:42", updated_at: "2012-04-10 03:59:42">
|
154
|
-
|
155
|
-
=== Retrieve
|
156
|
-
> user = User.find(2)
|
157
|
-
[ActiveRecord::Turntable] Sending method: select_all, sql: #<Arel::SelectManager:0x007f850466e668>, shards: ["user_shard_1"]
|
158
|
-
User Load (0.3ms) [Shard: user_shard_1] SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
|
159
|
-
=> #<User id: 2, name: "hoge", created_at: "2012-04-10 03:59:42", updated_at: "2012-04-10 03:59:42">
|
160
|
-
|
161
|
-
=== Update
|
162
|
-
> user.update_attributes(name: "hogefoo")
|
163
|
-
(0.1ms) [Shard: user_shard_1] BEGIN
|
164
|
-
[ActiveRecord::Turntable] Sending method: update, sql: UPDATE `users` SET `name` = 'hogefoo', `updated_at` = '2012-04-10 04:07:52' WHERE `users`.`id` = 2, shards: ["user_shard_1"]
|
165
|
-
(0.3ms) [Shard: user_shard_1] UPDATE `users` SET `name` = 'hogefoo', `updated_at` = '2012-04-10 04:07:52' WHERE `users`.`id` = 2
|
166
|
-
(0.8ms) [Shard: user_shard_1] COMMIT
|
167
|
-
=> true
|
168
|
-
|
169
|
-
=== Delete
|
170
|
-
> user.destroy
|
171
|
-
(0.2ms) [Shard: user_shard_1] BEGIN
|
172
|
-
[ActiveRecord::Turntable] Sending method: delete, sql: #<Arel::DeleteManager:0x007f8503677ea8>, shards: ["user_shard_1"]
|
173
|
-
SQL (0.3ms) [Shard: user_shard_1] DELETE FROM `users` WHERE `users`.`id` = 2
|
174
|
-
(1.7ms) [Shard: user_shard_1] COMMIT
|
175
|
-
=> #<User id: 2, name: "hogefoo", created_at: "2012-04-10 03:59:42", updated_at: "2012-04-10 04:07:52">
|
176
|
-
|
177
|
-
=== Count
|
178
|
-
|
179
|
-
> User.count
|
180
|
-
[ActiveRecord::Turntable] Sending method: select_value, sql: #<Arel::SelectManager:0x007f9e82ccebb0>, shards: ["user_shard_1", "user_shard_2", "user_shard_3"]
|
181
|
-
(0.8ms) [Shard: user_shard_1] SELECT COUNT(*) FROM `users`
|
182
|
-
(0.3ms) [Shard: user_shard_2] SELECT COUNT(*) FROM `users`
|
183
|
-
(0.2ms) [Shard: user_shard_3] SELECT COUNT(*) FROM `users`
|
184
|
-
=> 1
|
185
|
-
|
186
|
-
=== Sequencer
|
187
|
-
Sequencer provides generating global IDs.
|
188
|
-
|
189
|
-
Add below to the migration:
|
190
|
-
|
191
|
-
create_sequence_for(:users) # <-- this line creates sequence table
|
192
|
-
|
193
|
-
This will creates sequence table.
|
194
|
-
|
195
|
-
Next, add sequencer to the model:
|
196
|
-
|
197
|
-
class User < ActiveRecord::Base
|
198
|
-
turntable :id
|
199
|
-
sequencer # <-- this line enables sequencer module
|
200
|
-
has_one :status
|
201
|
-
end
|
202
|
-
|
203
|
-
|
204
|
-
=== Transaction
|
205
|
-
> user = User.find(2)
|
206
|
-
> user3 = User.create(name: "hoge3")
|
207
|
-
> User.shards_transaction([user, user3]) do
|
208
|
-
> user.name = "hogehoge"
|
209
|
-
> user3.name = "hogehoge3"
|
210
|
-
> user.save!
|
211
|
-
> user3.save!
|
212
|
-
> end
|
213
|
-
|
214
|
-
=== Send query to a specific shard.
|
215
|
-
use with_shard method:
|
216
|
-
|
217
|
-
AR::Base.connection.with_shard(shard1) do
|
218
|
-
# something queries to shard1
|
219
|
-
end
|
220
|
-
|
221
|
-
To access shard objects, use below:
|
222
|
-
* AR::Base.connection.shards # \\{shard_name => shard_obj,....}
|
223
|
-
* AR::Base#turntable_shard
|
224
|
-
* AR::Base.connection.select_shard(shard_key_value) #=> shard
|
225
|
-
|
226
|
-
=== Send query to all shards
|
227
|
-
use with_all method:
|
228
|
-
|
229
|
-
User.connection.with_all do
|
230
|
-
User.order("created_at DESC").limit(3).all
|
231
|
-
end
|
232
|
-
|
233
|
-
|
234
|
-
=== Migration
|
235
|
-
If you specify cluster or shard, migration will be executed to the cluster(or shard) and master database.
|
236
|
-
|
237
|
-
to specify cluster:
|
238
|
-
|
239
|
-
class CreateUsers < ActiveRecord::Migration
|
240
|
-
clusters :user_cluster
|
241
|
-
....
|
242
|
-
end
|
243
|
-
|
244
|
-
to specify shard:
|
245
|
-
|
246
|
-
class CreateUsers < ActiveRecord::Migration
|
247
|
-
shards :user_shard_01
|
248
|
-
....
|
249
|
-
end
|
250
|
-
|
251
|
-
if you would like to execute a migration to all databases in the environments, use :all symbol:
|
252
|
-
|
253
|
-
class CreateUsers < ActiveRecord::Migration
|
254
|
-
clusters :all
|
255
|
-
....
|
256
|
-
end
|
257
|
-
|
258
|
-
== Connection Management
|
259
|
-
Rails's ConnectionManagement middleware keeps ActiveRecord's connection during the process is alive, but Turntable keeps more connections.
|
260
|
-
This may cause flooding max connections on your database. So, we made a middleware that disconnects on each request.
|
261
|
-
To use turntable's ConnectionManagement middleware, add below line to your initializer.
|
262
|
-
|
263
|
-
app.middleware.swap ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::Turntable::Rack::ConnectionManagement
|
264
|
-
|
265
|
-
== Inability
|
266
|
-
* Queries includes "ORDER BY", "GROUP BY" and "LIMIT" clauses cannot be distributed.
|
267
|
-
* "has many through" and "habtm" relationships causes wrong results. ex) User-Friend-User relation
|
268
|
-
|
269
|
-
== Thanks to
|
270
|
-
ConnectionProxy, Distributed Migration implementation is inspired by Octopus and DataFabric.
|
271
|
-
|
272
|
-
== License
|
273
|
-
activerecord-turntable is released under the MIT license:
|
274
|
-
|
275
|
-
Copyright (c) 2012 Drecom Co.,Ltd.
|
276
|
-
|
277
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
278
|
-
a copy of this software and associated documentation files (the
|
279
|
-
"Software"), to deal in the Software without restriction, including
|
280
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
281
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
282
|
-
permit persons to whom the Software is furnished to do so, subject to
|
283
|
-
the following conditions:
|
284
|
-
|
285
|
-
The above copyright notice and this permission notice shall be
|
286
|
-
included in all copies or substantial portions of the Software.
|
287
|
-
|
288
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
289
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
290
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
291
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
292
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
293
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
294
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/gemfiles/rails3_0.gemfile
DELETED
data/gemfiles/rails3_1.gemfile
DELETED
data/gemfiles/rails3_2.gemfile
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
#
|
2
|
-
#=ActiveRecord::Turntable::Compatible
|
3
|
-
#
|
4
|
-
# for ActiveRecord versions compatibility
|
5
|
-
#
|
6
|
-
module ActiveRecord::Turntable
|
7
|
-
module Compatible
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
|
10
|
-
included do
|
11
|
-
# class_attributes
|
12
|
-
unless respond_to?(:class_attribute)
|
13
|
-
class << self
|
14
|
-
alias_method :class_attribute, :class_inheritable_accessor
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/sample_app/.gitignore
DELETED
@@ -1,16 +0,0 @@
|
|
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
|
-
/db/schema*
|
13
|
-
|
14
|
-
# Ignore all logfiles and tempfiles.
|
15
|
-
/log/*.log
|
16
|
-
/tmp
|
data/sample_app/Gemfile
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gem 'rails', '3.2.2'
|
4
|
-
|
5
|
-
# Bundle edge Rails instead:
|
6
|
-
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
7
|
-
|
8
|
-
gem 'mysql2'
|
9
|
-
|
10
|
-
gem 'activerecord-turntable', '1.0.0'
|
11
|
-
|
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'
|
21
|
-
|
22
|
-
gem 'uglifier', '>= 1.0.3'
|
23
|
-
end
|
24
|
-
|
25
|
-
gem 'jquery-rails'
|
26
|
-
gem 'pry-rails'
|
27
|
-
|
28
|
-
# To use ActiveModel has_secure_password
|
29
|
-
# gem 'bcrypt-ruby', '~> 3.0.0'
|
30
|
-
|
31
|
-
# To use Jbuilder templates for JSON
|
32
|
-
# gem 'jbuilder'
|
33
|
-
|
34
|
-
# Use unicorn as the app server
|
35
|
-
# gem 'unicorn'
|
36
|
-
|
37
|
-
# Deploy with Capistrano
|
38
|
-
# gem 'capistrano'
|
39
|
-
|
40
|
-
# To use debugger
|
41
|
-
# gem 'ruby-debug19', :require => 'ruby-debug'
|
data/sample_app/README.rdoc
DELETED
@@ -1,261 +0,0 @@
|
|
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
|
-
| `-- tasks
|
177
|
-
|-- log
|
178
|
-
|-- public
|
179
|
-
|-- script
|
180
|
-
|-- test
|
181
|
-
| |-- fixtures
|
182
|
-
| |-- functional
|
183
|
-
| |-- integration
|
184
|
-
| |-- performance
|
185
|
-
| `-- unit
|
186
|
-
|-- tmp
|
187
|
-
| |-- cache
|
188
|
-
| |-- pids
|
189
|
-
| |-- sessions
|
190
|
-
| `-- sockets
|
191
|
-
`-- vendor
|
192
|
-
|-- assets
|
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.
|
data/sample_app/Rakefile
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
#!/usr/bin/env rake
|
2
|
-
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
3
|
-
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
4
|
-
|
5
|
-
require File.expand_path('../config/application', __FILE__)
|
6
|
-
|
7
|
-
SampleApp::Application.load_tasks
|
Binary file
|
@@ -1,15 +0,0 @@
|
|
1
|
-
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
-
// listed below.
|
3
|
-
//
|
4
|
-
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
-
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
-
//
|
7
|
-
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
-
// the compiled file.
|
9
|
-
//
|
10
|
-
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
11
|
-
// GO AFTER THE REQUIRES BELOW.
|
12
|
-
//
|
13
|
-
//= require jquery
|
14
|
-
//= require jquery_ujs
|
15
|
-
//= require_tree .
|