rails_soft_deletable 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/lib/rails_soft_deletable.rb +1 -171
- data/lib/rails_soft_deletable/callbacks.rb +119 -0
- data/lib/rails_soft_deletable/query.rb +30 -0
- data/lib/rails_soft_deletable/rails/active_record.rb +34 -0
- data/lib/rails_soft_deletable/rails/railtie.rb +12 -0
- data/lib/rails_soft_deletable/version.rb +1 -1
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/decimal_model.rb +7 -0
- data/spec/dummy/app/models/integer_model.rb +7 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +53 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +10 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/test.rb +34 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +58 -0
- data/spec/dummy/db/schema.rb +22 -0
- data/spec/dummy/lib/soft_deletable_model_callbacks.rb +57 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/spec_helper.rb +5 -4
- data/spec/support/environment.rb +3 -23
- metadata +104 -58
- data/spec/models.rb +0 -75
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NWUwMzNmMjUxZDEyOGQ0NTNkMDBhMjViYTEyOTQ1NjdiNGRlYThkMA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YjYzZjFiYTdmNzZlMjI3OWJkMzVkZjZhN2JmNTc5ZDZjNTI4MDJkYw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
N2I5YmNjZDljZmYzZjBiMzk4ZmFlNjk0OTNmNmU1ZTA5ZDkxYmI1MTdkMTNj
|
10
|
+
N2FkYmNhYjdkZGFmYzRjMDJiMzYyMmQzZDcwMTdkZjNjZmRiMzBiMTY4YjUw
|
11
|
+
NjQ4YTNhY2IwMzk2ODRlNWJhN2UyYjY1ZDNjMWUyZDVlZTZmYjA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjM2NzQzNmE5MTZiMzNlODA5ZTg0YWQzYTUzZDkwNWU5ZjViNmZmNDQxN2E3
|
14
|
+
ZDFkZGMxZjAxMzZlYjk1YmExMDNmMGMxYjgyMGY1ODQ5ZTAwZTI1NjU0NzU3
|
15
|
+
NTdjYTc4NmM0YWVhYWU4MjczNjBmZTFiNWU0OGIwMWYwZWRkZjI=
|
data/lib/rails_soft_deletable.rb
CHANGED
@@ -1,175 +1,5 @@
|
|
1
1
|
require "rails_soft_deletable/version"
|
2
|
-
require "
|
2
|
+
require "rails_soft_deletable/rails/railtie"
|
3
3
|
|
4
4
|
module RailsSoftDeletable
|
5
|
-
def self.included(base)
|
6
|
-
base.extend Query
|
7
|
-
base.extend Callbacks
|
8
|
-
end
|
9
|
-
|
10
|
-
module Query
|
11
|
-
def soft_deletable?
|
12
|
-
true
|
13
|
-
end
|
14
|
-
|
15
|
-
def with_deleted
|
16
|
-
scoped.tap { |x| x.default_scoped = false }
|
17
|
-
end
|
18
|
-
|
19
|
-
def only_deleted
|
20
|
-
with_deleted.where("#{self.table_name}.#{soft_deletable_column} > 0")
|
21
|
-
end
|
22
|
-
alias :deleted :only_deleted
|
23
|
-
|
24
|
-
def restore(id)
|
25
|
-
if id.is_a?(Array)
|
26
|
-
id.map { |one_id| restore(one_id) }
|
27
|
-
else
|
28
|
-
only_deleted.find(id).restore!
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module Callbacks
|
34
|
-
def self.extended(base)
|
35
|
-
base.define_callbacks :restore
|
36
|
-
|
37
|
-
base.define_singleton_method("before_restore") do |*args, &block|
|
38
|
-
set_callback(:restore, :before, *args, &block)
|
39
|
-
end
|
40
|
-
|
41
|
-
base.define_singleton_method("around_restore") do |*args, &block|
|
42
|
-
set_callback(:restore, :around, *args, &block)
|
43
|
-
end
|
44
|
-
|
45
|
-
base.define_singleton_method("after_restore") do |*args, &block|
|
46
|
-
set_callback(:restore, :after, *args, &block)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def soft_delete_time
|
52
|
-
value = send(soft_deletable_column)
|
53
|
-
if value.zero? || value.nil?
|
54
|
-
nil
|
55
|
-
else
|
56
|
-
Time.at(value).in_time_zone
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def destroy(destroy_mode = :soft)
|
61
|
-
if destroy_mode == :hard
|
62
|
-
_original_destroy
|
63
|
-
else
|
64
|
-
if destroyed?
|
65
|
-
delete_or_soft_delete(true)
|
66
|
-
else
|
67
|
-
run_callbacks(:destroy) { delete_or_soft_delete(true) }
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def delete(delete_mode = :soft)
|
73
|
-
if delete_mode == :hard
|
74
|
-
_original_delete
|
75
|
-
else
|
76
|
-
return if new_record?
|
77
|
-
delete_or_soft_delete
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def restore!
|
82
|
-
run_callbacks(:restore) do
|
83
|
-
# XXX: Rails >3.2.11 fixes an issue with update_column:
|
84
|
-
# https://github.com/rails/rails/commit/a3c3cfdd0ebba26bb9dfc0bfd4e23a5f336730c0
|
85
|
-
# Since we're on 3.2.11, we cannot use update_column.
|
86
|
-
# update_column(soft_deletable_column, 0)
|
87
|
-
|
88
|
-
name = soft_deletable_column.to_s
|
89
|
-
updated_count = self.class.unscoped.update_all({ name => 0 }, self.class.primary_key => id)
|
90
|
-
raw_write_attribute(name, 0)
|
91
|
-
|
92
|
-
updated_count == 1
|
93
|
-
end
|
94
|
-
end
|
95
|
-
alias :restore :restore!
|
96
|
-
|
97
|
-
def destroyed?
|
98
|
-
value = send(soft_deletable_column)
|
99
|
-
!value || value != 0
|
100
|
-
end
|
101
|
-
|
102
|
-
def persisted?
|
103
|
-
@_pretend_persistence || super
|
104
|
-
end
|
105
|
-
|
106
|
-
private
|
107
|
-
|
108
|
-
def _prepare_for_hard_delete(&block)
|
109
|
-
@_pretend_persistence = true
|
110
|
-
self.class.unscoped(&block)
|
111
|
-
ensure
|
112
|
-
@_pretend_persistence = false
|
113
|
-
end
|
114
|
-
|
115
|
-
def delete_or_soft_delete(with_transaction = false)
|
116
|
-
if destroyed?
|
117
|
-
_prepare_for_hard_delete { _original_delete }
|
118
|
-
else
|
119
|
-
touch_soft_deletable_column(with_transaction)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
def touch_soft_deletable_column(with_transaction=false)
|
124
|
-
if with_transaction
|
125
|
-
with_transaction_returning_status { touch_column }
|
126
|
-
else
|
127
|
-
touch_column
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
def touch_column
|
132
|
-
raise ActiveRecordError, "can not touch on a new record object" unless persisted?
|
133
|
-
|
134
|
-
current_time = ("%0.6f" % current_time_from_proper_timezone).to_f
|
135
|
-
changes = {}
|
136
|
-
|
137
|
-
changes[soft_deletable_column.to_s] = write_attribute(soft_deletable_column.to_s, current_time)
|
138
|
-
|
139
|
-
changes[self.class.locking_column] = increment_lock if locking_enabled?
|
140
|
-
|
141
|
-
@changed_attributes.except!(*changes.keys)
|
142
|
-
primary_key = self.class.primary_key
|
143
|
-
self.class.unscoped.where(primary_key => self[primary_key]).update_all(changes) == 1
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
class ActiveRecord::Base
|
148
|
-
def self.soft_deletable(options={})
|
149
|
-
alias :_original_destroy :destroy
|
150
|
-
alias :_original_delete :delete
|
151
|
-
|
152
|
-
private :_original_destroy
|
153
|
-
private :_original_delete
|
154
|
-
|
155
|
-
include RailsSoftDeletable
|
156
|
-
class_attribute :soft_deletable_column
|
157
|
-
|
158
|
-
self.soft_deletable_column = options[:column] || :deleted_at
|
159
|
-
default_scope { where(self.quoted_table_name + ".#{soft_deletable_column} = 0") }
|
160
|
-
end
|
161
|
-
|
162
|
-
def self.soft_deletable?
|
163
|
-
false
|
164
|
-
end
|
165
|
-
|
166
|
-
def soft_deletable?
|
167
|
-
self.class.soft_deletable?
|
168
|
-
end
|
169
|
-
|
170
|
-
private
|
171
|
-
|
172
|
-
def soft_deletable_column
|
173
|
-
self.class.soft_deletable_column
|
174
|
-
end
|
175
5
|
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
|
3
|
+
module RailsSoftDeletable
|
4
|
+
module Callbacks
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
define_callbacks :restore
|
9
|
+
|
10
|
+
define_singleton_method("before_restore") do |*args, &block|
|
11
|
+
set_callback(:restore, :before, *args, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
define_singleton_method("around_restore") do |*args, &block|
|
15
|
+
set_callback(:restore, :around, *args, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
define_singleton_method("after_restore") do |*args, &block|
|
19
|
+
set_callback(:restore, :after, *args, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def soft_delete_time
|
24
|
+
value = send(soft_deletable_column)
|
25
|
+
|
26
|
+
if value.zero? || value.nil?
|
27
|
+
nil
|
28
|
+
else
|
29
|
+
Time.at(value).in_time_zone
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def destroy(destroy_mode = :soft)
|
34
|
+
if destroy_mode == :hard
|
35
|
+
super()
|
36
|
+
else
|
37
|
+
if destroyed?
|
38
|
+
delete_or_soft_delete(true)
|
39
|
+
else
|
40
|
+
run_callbacks(:destroy) { delete_or_soft_delete(true) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete(delete_mode = :soft)
|
46
|
+
if delete_mode == :hard
|
47
|
+
super()
|
48
|
+
else
|
49
|
+
return if new_record?
|
50
|
+
delete_or_soft_delete
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def restore!
|
55
|
+
run_callbacks(:restore) do
|
56
|
+
# XXX: Rails >3.2.11 fixes an issue with update_column:
|
57
|
+
# https://github.com/rails/rails/commit/a3c3cfdd0ebba26bb9dfc0bfd4e23a5f336730c0
|
58
|
+
# Since we're on 3.2.11, we cannot use update_column.
|
59
|
+
# update_column(soft_deletable_column, 0)
|
60
|
+
|
61
|
+
name = soft_deletable_column.to_s
|
62
|
+
updated_count = self.class.unscoped.update_all({ name => 0 }, self.class.primary_key => id)
|
63
|
+
raw_write_attribute(name, 0)
|
64
|
+
|
65
|
+
updated_count == 1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
alias :restore :restore!
|
69
|
+
|
70
|
+
def destroyed?
|
71
|
+
value = send(soft_deletable_column)
|
72
|
+
!value || value != 0
|
73
|
+
end
|
74
|
+
|
75
|
+
def persisted?
|
76
|
+
@_pretend_persistence || super
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def _prepare_for_hard_delete(&block)
|
82
|
+
@_pretend_persistence = true
|
83
|
+
self.class.unscoped(&block)
|
84
|
+
ensure
|
85
|
+
@_pretend_persistence = false
|
86
|
+
end
|
87
|
+
|
88
|
+
def delete_or_soft_delete(with_transaction = false)
|
89
|
+
if destroyed?
|
90
|
+
_prepare_for_hard_delete { delete(:hard) }
|
91
|
+
else
|
92
|
+
touch_soft_deletable_column(with_transaction)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def touch_soft_deletable_column(with_transaction=false)
|
97
|
+
if with_transaction
|
98
|
+
with_transaction_returning_status { touch_column }
|
99
|
+
else
|
100
|
+
touch_column
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def touch_column
|
105
|
+
raise ActiveRecordError, "can not touch on a new record object" unless persisted?
|
106
|
+
|
107
|
+
current_time = ("%0.6f" % current_time_from_proper_timezone).to_f
|
108
|
+
changes = {}
|
109
|
+
|
110
|
+
changes[soft_deletable_column.to_s] = write_attribute(soft_deletable_column.to_s, current_time)
|
111
|
+
|
112
|
+
changes[self.class.locking_column] = increment_lock if locking_enabled?
|
113
|
+
|
114
|
+
@changed_attributes.except!(*changes.keys)
|
115
|
+
primary_key = self.class.primary_key
|
116
|
+
self.class.unscoped.where(primary_key => self[primary_key]).update_all(changes) == 1
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
|
3
|
+
module RailsSoftDeletable
|
4
|
+
module Query
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def soft_deletable?
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
def with_deleted
|
13
|
+
scoped.tap { |x| x.default_scoped = false }
|
14
|
+
end
|
15
|
+
|
16
|
+
def only_deleted
|
17
|
+
with_deleted.where("#{self.table_name}.#{soft_deletable_column} > 0")
|
18
|
+
end
|
19
|
+
alias :deleted :only_deleted
|
20
|
+
|
21
|
+
def restore(id)
|
22
|
+
if id.is_a?(Array)
|
23
|
+
id.map { |one_id| restore(one_id) }
|
24
|
+
else
|
25
|
+
only_deleted.find(id).restore!
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "rails_soft_deletable/query"
|
2
|
+
require "rails_soft_deletable/callbacks"
|
3
|
+
|
4
|
+
module RailsSoftDeletable
|
5
|
+
module ActiveRecord
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def soft_deletable?
|
9
|
+
self.class.soft_deletable?
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def soft_deletable_column
|
15
|
+
self.class.soft_deletable_column
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def soft_deletable(options={})
|
20
|
+
include RailsSoftDeletable::Query
|
21
|
+
include RailsSoftDeletable::Callbacks
|
22
|
+
|
23
|
+
class_attribute :soft_deletable_column
|
24
|
+
|
25
|
+
self.soft_deletable_column = options[:column] || :deleted_at
|
26
|
+
default_scope { where(self.quoted_table_name + ".#{soft_deletable_column} = 0") }
|
27
|
+
end
|
28
|
+
|
29
|
+
def soft_deletable?
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rails/railtie"
|
2
|
+
require "rails_soft_deletable/rails/active_record"
|
3
|
+
|
4
|
+
module RailsSoftDeletable
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "rails_soft_deletable.initialize" do |app|
|
7
|
+
ActiveSupport.on_load(:active_record) do
|
8
|
+
include(RailsSoftDeletable::ActiveRecord)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -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
|
+
| `-- 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.
|