audit_trail 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +99 -0
- data/LICENSE.txt +20 -0
- data/README +0 -0
- data/README.md +19 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/app/models/change_event.rb +3 -0
- data/audit_trail.gemspec +110 -0
- data/lib/audit_trail.rb +11 -0
- data/lib/audit_trail/change_tracking.rb +64 -0
- data/lib/audit_trail/engine.rb +4 -0
- data/lib/audit_trail/model.rb +31 -0
- data/lib/generators/audit_trail/install_generator.rb +18 -0
- data/lib/generators/audit_trail/templates/migration.rb +16 -0
- data/test/audit_trail_test.rb +5 -0
- data/test/change_tracking_test.rb +242 -0
- data/test/test_app_3_1/Gemfile +8 -0
- data/test/test_app_3_1/Gemfile.lock +102 -0
- data/test/test_app_3_1/README +261 -0
- data/test/test_app_3_1/Rakefile +7 -0
- data/test/test_app_3_1/app/assets/images/rails.png +0 -0
- data/test/test_app_3_1/app/assets/javascripts/application.js +9 -0
- data/test/test_app_3_1/app/assets/stylesheets/application.css +7 -0
- data/test/test_app_3_1/app/controllers/application_controller.rb +3 -0
- data/test/test_app_3_1/app/helpers/application_helper.rb +2 -0
- data/test/test_app_3_1/app/views/layouts/application.html.erb +14 -0
- data/test/test_app_3_1/config.ru +4 -0
- data/test/test_app_3_1/config/application.rb +48 -0
- data/test/test_app_3_1/config/boot.rb +6 -0
- data/test/test_app_3_1/config/database.yml +42 -0
- data/test/test_app_3_1/config/environment.rb +5 -0
- data/test/test_app_3_1/config/environments/development.rb +30 -0
- data/test/test_app_3_1/config/environments/production.rb +60 -0
- data/test/test_app_3_1/config/environments/test.rb +42 -0
- data/test/test_app_3_1/config/initializers/backtrace_silencers.rb +7 -0
- data/test/test_app_3_1/config/initializers/inflections.rb +10 -0
- data/test/test_app_3_1/config/initializers/mime_types.rb +5 -0
- data/test/test_app_3_1/config/initializers/secret_token.rb +7 -0
- data/test/test_app_3_1/config/initializers/session_store.rb +8 -0
- data/test/test_app_3_1/config/initializers/wrap_parameters.rb +14 -0
- data/test/test_app_3_1/config/locales/en.yml +5 -0
- data/test/test_app_3_1/config/routes.rb +58 -0
- data/test/test_app_3_1/db/schema.rb +15 -0
- data/test/test_app_3_1/db/seeds.rb +7 -0
- data/test/test_app_3_1/log/development.log +3 -0
- data/test/test_app_3_1/log/test.log +17791 -0
- data/test/test_app_3_1/public/404.html +26 -0
- data/test/test_app_3_1/public/422.html +26 -0
- data/test/test_app_3_1/public/500.html +26 -0
- data/test/test_app_3_1/public/favicon.ico +0 -0
- data/test/test_app_3_1/public/index.html +241 -0
- data/test/test_app_3_1/public/robots.txt +5 -0
- data/test/test_app_3_1/script/rails +6 -0
- data/test/test_app_3_1/test/test_helper.rb +13 -0
- data/test/test_app_3_1/tmp/cache/4B5/180/experiments +0 -0
- data/test/test_helper.rb +66 -0
- metadata +198 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateAuditTrails < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table(:change_events) do |t|
|
4
|
+
t.integer :changed_object_id, :changed_by_id, :integer_value, :previous_integer_value
|
5
|
+
t.string :changed_object_type, :changed_attribute, :string_value, :previous_string_value, :additional_info, :changer_ip_address
|
6
|
+
t.date :date_value, :previous_date_value
|
7
|
+
t.datetime :created_at, :datetime_value, :previous_datetime_value
|
8
|
+
t.decimal :decimal_value, :previous_decimal_value, :precision => 14, :scale => 2
|
9
|
+
t.boolean :boolean_value, :previous_boolean_value
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :change_events
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,242 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ChangeTrackingTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup :declare_tracked_model
|
6
|
+
teardown :cleanup_tracked_model
|
7
|
+
|
8
|
+
def test_methods_defined
|
9
|
+
assert TrackedModel.respond_to?(:audit_trail_for)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_audit_trail_for
|
13
|
+
TrackedModel.class_eval do
|
14
|
+
audit_trail_for :count, :price
|
15
|
+
end
|
16
|
+
|
17
|
+
obj = TrackedModel.new
|
18
|
+
|
19
|
+
assert obj.respond_to?(:record_change_event_for_count)
|
20
|
+
assert obj.respond_to?(:record_change_event_for_price)
|
21
|
+
|
22
|
+
assert_equal :integer, TrackedModel.audit_trail_types['count']
|
23
|
+
assert_equal :decimal, TrackedModel.audit_trail_types['price']
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_record_change_events__changes_on_non_tracked_attributes
|
28
|
+
TrackedModel.class_eval do
|
29
|
+
audit_trail_for :note
|
30
|
+
end
|
31
|
+
|
32
|
+
obj = TrackedModel.new
|
33
|
+
obj.untracked_column = "foobar"
|
34
|
+
|
35
|
+
assert_no_difference 'ChangeEvent.count' do
|
36
|
+
obj.save!
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_record_change_events__changes_on_tracked_attributes
|
41
|
+
TrackedModel.class_eval do
|
42
|
+
audit_trail_for :note, :price
|
43
|
+
end
|
44
|
+
|
45
|
+
obj = TrackedModel.new(:note => "value_new")
|
46
|
+
|
47
|
+
assert_difference 'ChangeEvent.count', 1 do
|
48
|
+
obj.save!
|
49
|
+
end
|
50
|
+
|
51
|
+
assert_change_event ChangeEvent.last, 'note', nil, "value_new", :string, :changed_object => obj
|
52
|
+
|
53
|
+
assert_no_difference 'ChangeEvent.count' do
|
54
|
+
obj.save!
|
55
|
+
end
|
56
|
+
|
57
|
+
assert_difference 'ChangeEvent.count', 2 do
|
58
|
+
obj.update_attributes!(:note => "new_new_value", :price => 100.00)
|
59
|
+
end
|
60
|
+
|
61
|
+
note_change_event = ChangeEvent.where(:changed_attribute => "note").last
|
62
|
+
assert_change_event note_change_event, 'note', "value_new", "new_new_value", :string, :changed_object => obj
|
63
|
+
|
64
|
+
price_change_event = ChangeEvent.where(:changed_attribute => "price").last
|
65
|
+
assert_change_event price_change_event, 'price', nil, 100.00, :decimal, :changed_object => obj
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_record_change_events__changes_on_tracked_attributes__with_additional_info
|
69
|
+
TrackedModel.class_eval do
|
70
|
+
audit_trail_for :note, :additional_info => :note_additional_info
|
71
|
+
audit_trail_for :price, :additional_info => Proc.new { |rec| "Price: #{rec.price}"}
|
72
|
+
|
73
|
+
def note_additional_info
|
74
|
+
"note"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
obj = TrackedModel.create(:note => 'old_value', :price => 1)
|
79
|
+
obj.note = "new_note"
|
80
|
+
obj.price = 10
|
81
|
+
|
82
|
+
assert_difference 'ChangeEvent.count', 2 do
|
83
|
+
obj.save!
|
84
|
+
end
|
85
|
+
|
86
|
+
note_change_event = obj.note_change_events.first
|
87
|
+
price_change_event = obj.price_change_events.first
|
88
|
+
|
89
|
+
assert_change_event note_change_event, 'note', 'old_value', "new_note", :string, :changed_object => obj, :additional_info => 'note'
|
90
|
+
assert_change_event price_change_event, 'price', 1, 10, :decimal, :changed_object => obj, :additional_info => "Price: 10.0"
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_record_change_events__changes_on_tracked_attributes__if_block
|
94
|
+
TrackedModel.class_eval do
|
95
|
+
audit_trail_for :note, :if => :some_check
|
96
|
+
end
|
97
|
+
|
98
|
+
obj = TrackedModel.new(:note => "old_value")
|
99
|
+
obj.stubs(:some_check).returns(true)
|
100
|
+
obj.save!
|
101
|
+
|
102
|
+
obj.note = "new_note"
|
103
|
+
|
104
|
+
assert_difference 'ChangeEvent.count', 1 do
|
105
|
+
obj.save!
|
106
|
+
end
|
107
|
+
|
108
|
+
note_change_event = ChangeEvent.last
|
109
|
+
assert_change_event note_change_event, 'note', 'old_value', "new_note", :string, :changed_object => obj
|
110
|
+
|
111
|
+
obj.stubs(:some_check).returns(false)
|
112
|
+
|
113
|
+
assert_no_difference 'ChangeEvent.count' do
|
114
|
+
obj.update_attributes!(:note => 'new_new_note')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_record_change_events__changes_on_tracked_attributes__if_block_separate_statements_for_each_attr
|
119
|
+
TrackedModel.class_eval do
|
120
|
+
audit_trail_for :note, :if => :some_check_1
|
121
|
+
audit_trail_for :price, :if => Proc.new { |rec| rec.some_check_2 }
|
122
|
+
end
|
123
|
+
|
124
|
+
obj = TrackedModel.new
|
125
|
+
obj.stubs(:some_check_1).returns(true)
|
126
|
+
obj.stubs(:some_check_2).returns(false)
|
127
|
+
|
128
|
+
obj.note = "new_note"
|
129
|
+
obj.price = 1
|
130
|
+
|
131
|
+
assert_difference 'obj.note_change_events.count', 1 do
|
132
|
+
assert_no_difference 'obj.price_change_events.count' do
|
133
|
+
obj.save!
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
obj.stubs(:some_check_1).returns(false)
|
138
|
+
obj.stubs(:some_check_2).returns(true)
|
139
|
+
|
140
|
+
obj.note = "new_new_note"
|
141
|
+
obj.price = 100
|
142
|
+
|
143
|
+
assert_no_difference 'obj.note_change_events.count' do
|
144
|
+
assert_difference 'obj.price_change_events.count', 1 do
|
145
|
+
obj.save!
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_change_events__different_types
|
151
|
+
TrackedModel.class_eval do
|
152
|
+
audit_trail_for :note, :price, :yes_no, :occurred_on, :occurred_at, :count
|
153
|
+
end
|
154
|
+
|
155
|
+
obj = TrackedModel.new
|
156
|
+
obj.attributes = { :note => "new_note", :price => 10, :yes_no => true, :occurred_at => Time.now, :occurred_on => Date.today, :untracked_column => "hello", :count => 1 }
|
157
|
+
|
158
|
+
assert_difference 'ChangeEvent.count', 6 do
|
159
|
+
obj.save!
|
160
|
+
end
|
161
|
+
|
162
|
+
assert_change_event obj.note_change_events.first, 'note', nil, "new_note", :string
|
163
|
+
assert_change_event obj.price_change_events.first, 'price', nil, 10, :decimal
|
164
|
+
assert_change_event obj.count_change_events.first, 'count', nil, 1, :integer
|
165
|
+
assert_change_event obj.yes_no_change_events.first, 'yes_no', nil, true, :boolean
|
166
|
+
assert_change_event obj.occurred_on_change_events.first, 'occurred_on', nil, Date.today, :date
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_change_events__records_user
|
170
|
+
TrackedModel.class_eval do
|
171
|
+
audit_trail_for :note
|
172
|
+
end
|
173
|
+
ChangeEvent.stubs(:changer).returns(stub(:id => 5))
|
174
|
+
|
175
|
+
obj = TrackedModel.new(:note => 'note')
|
176
|
+
obj.save!
|
177
|
+
|
178
|
+
changed_event = ChangeEvent.last
|
179
|
+
assert_equal 5, changed_event.changed_by_id
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_change_events__records_ip_address
|
183
|
+
TrackedModel.class_eval do
|
184
|
+
audit_trail_for :note
|
185
|
+
end
|
186
|
+
|
187
|
+
ChangeEvent.stubs(:changer_ip_address).returns("3.4.5.6")
|
188
|
+
|
189
|
+
obj = TrackedModel.new(:note => 'note')
|
190
|
+
obj.save!
|
191
|
+
|
192
|
+
changed_event = ChangeEvent.last
|
193
|
+
assert_equal "3.4.5.6", changed_event.changer_ip_address
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_change_events__supports_different_callbacks
|
197
|
+
TrackedModel.class_eval do
|
198
|
+
audit_trail_for :note
|
199
|
+
audit_trail_for :price, :on => :update
|
200
|
+
end
|
201
|
+
|
202
|
+
obj = TrackedModel.new
|
203
|
+
|
204
|
+
assert_difference 'obj.note_change_events.count', 1 do
|
205
|
+
assert_no_difference 'obj.price_change_events.count' do
|
206
|
+
obj.update_attributes!(:note => 'note', :price => 1)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
assert_difference 'obj.note_change_events.count', 1 do
|
211
|
+
assert_difference 'obj.price_change_events.count', 1 do
|
212
|
+
obj.update_attributes!(:note => 'note2', :price => 100)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
assert_no_difference 'obj.note_change_events.count' do
|
217
|
+
assert_no_difference 'obj.price_change_events.count' do
|
218
|
+
obj.update_attributes!(:note => 'note2', :price => 100)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
private
|
224
|
+
|
225
|
+
def assert_change_event(change_event, changed_attribute, previous_value, new_value, type, options = {})
|
226
|
+
assert_equal changed_attribute, change_event.changed_attribute
|
227
|
+
assert_equal previous_value, change_event.previous_value
|
228
|
+
assert_equal new_value, change_event.value
|
229
|
+
assert_equal previous_value, change_event.send("previous_#{type}_value")
|
230
|
+
assert_equal new_value, change_event.send("#{type}_value")
|
231
|
+
if options[:additional_info]
|
232
|
+
assert_equal options[:additional_info], change_event.additional_info
|
233
|
+
else
|
234
|
+
assert change_event.additional_info.blank?
|
235
|
+
end
|
236
|
+
|
237
|
+
if options[:changed_object]
|
238
|
+
assert_equal options[:changed_object], change_event.changed_object
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../..
|
3
|
+
specs:
|
4
|
+
audit_trail (0.0.1)
|
5
|
+
rails (~> 3.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
remote: https://gems:frifeprexo@gemini.atl.appfolio.net/
|
10
|
+
specs:
|
11
|
+
actionmailer (3.1.0)
|
12
|
+
actionpack (= 3.1.0)
|
13
|
+
mail (~> 2.3.0)
|
14
|
+
actionpack (3.1.0)
|
15
|
+
activemodel (= 3.1.0)
|
16
|
+
activesupport (= 3.1.0)
|
17
|
+
builder (~> 3.0.0)
|
18
|
+
erubis (~> 2.7.0)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
rack (~> 1.3.2)
|
21
|
+
rack-cache (~> 1.0.3)
|
22
|
+
rack-mount (~> 0.8.2)
|
23
|
+
rack-test (~> 0.6.1)
|
24
|
+
sprockets (~> 2.0.0)
|
25
|
+
activemodel (3.1.0)
|
26
|
+
activesupport (= 3.1.0)
|
27
|
+
bcrypt-ruby (~> 3.0.0)
|
28
|
+
builder (~> 3.0.0)
|
29
|
+
i18n (~> 0.6)
|
30
|
+
activerecord (3.1.0)
|
31
|
+
activemodel (= 3.1.0)
|
32
|
+
activesupport (= 3.1.0)
|
33
|
+
arel (~> 2.2.1)
|
34
|
+
tzinfo (~> 0.3.29)
|
35
|
+
activeresource (3.1.0)
|
36
|
+
activemodel (= 3.1.0)
|
37
|
+
activesupport (= 3.1.0)
|
38
|
+
activesupport (3.1.0)
|
39
|
+
multi_json (~> 1.0)
|
40
|
+
arel (2.2.1)
|
41
|
+
bcrypt-ruby (3.0.1)
|
42
|
+
builder (3.0.0)
|
43
|
+
erubis (2.7.0)
|
44
|
+
hike (1.2.1)
|
45
|
+
i18n (0.6.0)
|
46
|
+
json (1.4.3)
|
47
|
+
mail (2.3.0)
|
48
|
+
i18n (>= 0.4.0)
|
49
|
+
mime-types (~> 1.16)
|
50
|
+
treetop (~> 1.4.8)
|
51
|
+
mime-types (1.17.2)
|
52
|
+
mocha (0.9.8)
|
53
|
+
rake
|
54
|
+
multi_json (1.0.4)
|
55
|
+
mysql2 (0.3.7)
|
56
|
+
polyglot (0.3.3)
|
57
|
+
rack (1.3.6)
|
58
|
+
rack-cache (1.0.3)
|
59
|
+
rack (>= 0.4)
|
60
|
+
rack-mount (0.8.3)
|
61
|
+
rack (>= 1.0.0)
|
62
|
+
rack-ssl (1.3.2)
|
63
|
+
rack
|
64
|
+
rack-test (0.6.1)
|
65
|
+
rack (>= 1.0)
|
66
|
+
rails (3.1.0)
|
67
|
+
actionmailer (= 3.1.0)
|
68
|
+
actionpack (= 3.1.0)
|
69
|
+
activerecord (= 3.1.0)
|
70
|
+
activeresource (= 3.1.0)
|
71
|
+
activesupport (= 3.1.0)
|
72
|
+
bundler (~> 1.0)
|
73
|
+
railties (= 3.1.0)
|
74
|
+
railties (3.1.0)
|
75
|
+
actionpack (= 3.1.0)
|
76
|
+
activesupport (= 3.1.0)
|
77
|
+
rack-ssl (~> 1.3.2)
|
78
|
+
rake (>= 0.8.7)
|
79
|
+
rdoc (~> 3.4)
|
80
|
+
thor (~> 0.14.6)
|
81
|
+
rake (0.9.2.2)
|
82
|
+
rdoc (3.12)
|
83
|
+
json (~> 1.4)
|
84
|
+
sprockets (2.0.3)
|
85
|
+
hike (~> 1.2)
|
86
|
+
rack (~> 1.0)
|
87
|
+
tilt (~> 1.1, != 1.3.0)
|
88
|
+
thor (0.14.6)
|
89
|
+
tilt (1.3.3)
|
90
|
+
treetop (1.4.10)
|
91
|
+
polyglot
|
92
|
+
polyglot (>= 0.3.1)
|
93
|
+
tzinfo (0.3.31)
|
94
|
+
|
95
|
+
PLATFORMS
|
96
|
+
ruby
|
97
|
+
|
98
|
+
DEPENDENCIES
|
99
|
+
audit_trail!
|
100
|
+
mocha (= 0.9.8)
|
101
|
+
mysql2 (= 0.3.7)
|
102
|
+
rails (= 3.1.0)
|
@@ -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.
|