stocktrade-money-rails 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +60 -0
- data/LICENSE +22 -0
- data/README.md +359 -0
- data/Rakefile +60 -0
- data/lib/generators/money_rails/initializer_generator.rb +15 -0
- data/lib/generators/templates/money.rb +41 -0
- data/lib/money-rails.rb +12 -0
- data/lib/money-rails/active_model/validator.rb +37 -0
- data/lib/money-rails/active_record/monetizable.rb +138 -0
- data/lib/money-rails/configuration.rb +47 -0
- data/lib/money-rails/helpers/action_view_extension.rb +36 -0
- data/lib/money-rails/hooks.rb +30 -0
- data/lib/money-rails/mongoid/three.rb +42 -0
- data/lib/money-rails/mongoid/two.rb +25 -0
- data/lib/money-rails/railtie.rb +7 -0
- data/lib/money-rails/version.rb +3 -0
- data/money-rails.gemspec +30 -0
- data/spec/active_record/monetizable_spec.rb +308 -0
- data/spec/configuration_spec.rb +20 -0
- 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/dummy_product.rb +10 -0
- data/spec/dummy/app/models/priceable.rb +7 -0
- data/spec/dummy/app/models/product.rb +26 -0
- data/spec/dummy/app/models/service.rb +8 -0
- data/spec/dummy/app/models/transaction.rb +13 -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 +61 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -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/money.rb +31 -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/mongoid.yml +23 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20120331190108_create_products.rb +10 -0
- data/spec/dummy/db/migrate/20120402080348_add_bonus_cents_to_product.rb +6 -0
- data/spec/dummy/db/migrate/20120524052716_create_services.rb +10 -0
- data/spec/dummy/db/migrate/20120528181002_create_transactions.rb +11 -0
- data/spec/dummy/db/migrate/20120528210103_create_dummy_products.rb +10 -0
- data/spec/dummy/db/migrate/20120607210247_add_column_that_allows_nil.rb +5 -0
- data/spec/dummy/db/migrate/20120712202655_add_sale_price_cents_to_product.rb +7 -0
- data/spec/dummy/db/schema.rb +49 -0
- data/spec/dummy/db/structure.sql +17 -0
- data/spec/dummy/db/test.sqlite3 +0 -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/helpers/action_view_extension_spec.rb +38 -0
- data/spec/helpers/form_helper_spec.rb +19 -0
- data/spec/mongoid/three_spec.rb +31 -0
- data/spec/mongoid/two_spec.rb +25 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/support/database_cleaner.rb +14 -0
- metadata +272 -0
data/money-rails.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/money-rails/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "stocktrade-money-rails"
|
6
|
+
s.version = MoneyRails::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.license = "MIT"
|
9
|
+
s.authors = ["Andreas Loupasakis", "Shane Emmons", "Simone Carletti", "Pablo Merino"]
|
10
|
+
s.email = ["alup.rubymoney@gmail.com"]
|
11
|
+
s.description = "This library provides integration of RubyMoney - Money gem with Rails - StockTrade Special"
|
12
|
+
s.summary = "Money gem integration with Rails"
|
13
|
+
s.homepage = "https://github.com/RubyMoney/money-rails"
|
14
|
+
|
15
|
+
s.files = Dir.glob("{lib,spec}/**/*")
|
16
|
+
s.files += %w(CHANGELOG.md LICENSE README.md)
|
17
|
+
s.files += %w(Rakefile money-rails.gemspec)
|
18
|
+
|
19
|
+
s.test_files = s.files.grep(/^spec\//)
|
20
|
+
|
21
|
+
s.require_path = "lib"
|
22
|
+
|
23
|
+
s.add_dependency "money", "~> 5.0.0"
|
24
|
+
s.add_dependency "activesupport", "~> 3.0"
|
25
|
+
s.add_dependency "railties", "~> 3.0"
|
26
|
+
|
27
|
+
s.add_development_dependency "rails", "~> 3.0"
|
28
|
+
s.add_development_dependency "rspec-rails", "~> 2.10"
|
29
|
+
s.add_development_dependency 'database_cleaner', ['>= 0.8.0']
|
30
|
+
end
|
@@ -0,0 +1,308 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? ActiveRecord
|
4
|
+
describe MoneyRails::ActiveRecord::Monetizable do
|
5
|
+
|
6
|
+
describe "monetize" do
|
7
|
+
before :each do
|
8
|
+
@product = Product.create(:price_cents => 3000, :discount => 150,
|
9
|
+
:bonus_cents => 200, :optional_price => 100,
|
10
|
+
:sale_price_amount => 1200)
|
11
|
+
@service = Service.create(:charge_cents => 2000, :discount_cents => 120)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "attaches a Money object to model field" do
|
15
|
+
@product.price.should be_an_instance_of(Money)
|
16
|
+
@product.discount_value.should be_an_instance_of(Money)
|
17
|
+
@product.bonus.should be_an_instance_of(Money)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns the expected money amount as a Money object" do
|
21
|
+
@product.price.should == Money.new(3000, "USD")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "assigns the correct value from a Money object" do
|
25
|
+
@product.price = Money.new(3210, "USD")
|
26
|
+
@product.save.should be_true
|
27
|
+
@product.price_cents.should == 3210
|
28
|
+
end
|
29
|
+
|
30
|
+
it "assigns the correct value from a Money object using create" do
|
31
|
+
@product = Product.create(:price => Money.new(3210, "USD"), :discount => 150,
|
32
|
+
:bonus_cents => 200, :optional_price => 100)
|
33
|
+
@product.valid?.should be_true
|
34
|
+
@product.price_cents.should == 3210
|
35
|
+
end
|
36
|
+
|
37
|
+
it "updates correctly from a Money object using update_attributes" do
|
38
|
+
@product.update_attributes(:price => Money.new(215, "USD")).should be_true
|
39
|
+
@product.price_cents.should == 215
|
40
|
+
end
|
41
|
+
|
42
|
+
it "respects :as argument" do
|
43
|
+
@product.discount_value.should == Money.new(150, "USD")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "uses numericality validation" do
|
47
|
+
@product.price_cents = "foo"
|
48
|
+
@product.save.should be_false
|
49
|
+
|
50
|
+
@product.price_cents = 2000
|
51
|
+
@product.save.should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "respects numericality validation when using update_attributes" do
|
55
|
+
@product.update_attributes(:price_cents => "some text").should be_false
|
56
|
+
@product.update_attributes(:price_cents => 2000).should be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
it "uses numericality validation on money attribute" do
|
60
|
+
@product.price = "some text"
|
61
|
+
@product.save.should be_false
|
62
|
+
|
63
|
+
@product.price = Money.new(320, "USD")
|
64
|
+
@product.save.should be_true
|
65
|
+
end
|
66
|
+
|
67
|
+
it "respects numericality validation when using update_attributes on money attribute" do
|
68
|
+
@product.update_attributes(:price => "some text").should be_false
|
69
|
+
@product.update_attributes(:price => Money.new(320, 'USD')).should be_true
|
70
|
+
end
|
71
|
+
|
72
|
+
it "doesn't allow nil by default" do
|
73
|
+
@product.price_cents = nil
|
74
|
+
@product.save.should be_false
|
75
|
+
end
|
76
|
+
|
77
|
+
it "allows nil if optioned" do
|
78
|
+
@product.optional_price = nil
|
79
|
+
@product.save.should be_true
|
80
|
+
@product.optional_price.should be_nil
|
81
|
+
end
|
82
|
+
|
83
|
+
it "doesn't raise exception if validation is used and nil is not allowed" do
|
84
|
+
expect { @product.price = nil }.to_not raise_error
|
85
|
+
end
|
86
|
+
|
87
|
+
it "doesn't save nil values if validation is used and nil is not allowed" do
|
88
|
+
@product.price = nil
|
89
|
+
@product.save
|
90
|
+
@product.price_cents.should_not be_nil
|
91
|
+
end
|
92
|
+
|
93
|
+
it "resets money_before_type_cast attr every time a save operation occurs" do
|
94
|
+
v = Money.new(100, :usd)
|
95
|
+
@product.price = v
|
96
|
+
@product.price_money_before_type_cast.should == v
|
97
|
+
@product.save
|
98
|
+
@product.price_money_before_type_cast.should be_nil
|
99
|
+
@product.price = 10
|
100
|
+
@product.price_money_before_type_cast.should == 10
|
101
|
+
@product.save
|
102
|
+
@product.price_money_before_type_cast.should be_nil
|
103
|
+
@product.bonus = ""
|
104
|
+
@product.bonus_money_before_type_cast.should == ""
|
105
|
+
@product.save.should be_false
|
106
|
+
@product.bonus_money_before_type_cast.should be_nil
|
107
|
+
end
|
108
|
+
|
109
|
+
it "uses Money default currency if :with_currency has not been used" do
|
110
|
+
@service.discount.currency.should == Money::Currency.find(:eur)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "overrides default currency with the currency registered for the model" do
|
114
|
+
@product.price.currency.should == Money::Currency.find(:usd)
|
115
|
+
end
|
116
|
+
|
117
|
+
it "overrides default currency with the value of :with_currency argument" do
|
118
|
+
@service.charge.currency.should == Money::Currency.find(:usd)
|
119
|
+
@product.bonus.currency.should == Money::Currency.find(:gbp)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "assigns correctly Money objects to the attribute" do
|
123
|
+
@product.price = Money.new(2500, :USD)
|
124
|
+
@product.save.should be_true
|
125
|
+
@product.price.cents.should == 2500
|
126
|
+
@product.price.currency_as_string.should == "USD"
|
127
|
+
end
|
128
|
+
|
129
|
+
it "assigns correctly Fixnum objects to the attribute" do
|
130
|
+
@product.price = 25
|
131
|
+
@product.save.should be_true
|
132
|
+
@product.price.cents.should == 2500
|
133
|
+
@product.price.currency_as_string.should == "USD"
|
134
|
+
|
135
|
+
@service.discount = 2
|
136
|
+
@service.save.should be_true
|
137
|
+
@service.discount.cents.should == 200
|
138
|
+
@service.discount.currency_as_string.should == "EUR"
|
139
|
+
end
|
140
|
+
|
141
|
+
it "assigns correctly String objects to the attribute" do
|
142
|
+
@product.price = "25"
|
143
|
+
@product.save.should be_true
|
144
|
+
@product.price.cents.should == 2500
|
145
|
+
@product.price.currency_as_string.should == "USD"
|
146
|
+
|
147
|
+
@service.discount = "2"
|
148
|
+
@service.save.should be_true
|
149
|
+
@service.discount.cents.should == 200
|
150
|
+
@service.discount.currency_as_string.should == "EUR"
|
151
|
+
end
|
152
|
+
|
153
|
+
it "overrides default, model currency with the value of :with_currency in fixnum assignments" do
|
154
|
+
@product.bonus = 25
|
155
|
+
@product.save.should be_true
|
156
|
+
@product.bonus.cents.should == 2500
|
157
|
+
@product.bonus.currency_as_string.should == "GBP"
|
158
|
+
|
159
|
+
@service.charge = 2
|
160
|
+
@service.save.should be_true
|
161
|
+
@service.charge.cents.should == 200
|
162
|
+
@service.charge.currency_as_string.should == "USD"
|
163
|
+
end
|
164
|
+
|
165
|
+
it "overrides default, model currency with the value of :with_currency in string assignments" do
|
166
|
+
@product.bonus = "25"
|
167
|
+
@product.save.should be_true
|
168
|
+
@product.bonus.cents.should == 2500
|
169
|
+
@product.bonus.currency_as_string.should == "GBP"
|
170
|
+
|
171
|
+
@service.charge = "2"
|
172
|
+
@service.save.should be_true
|
173
|
+
@service.charge.cents.should == 200
|
174
|
+
@service.charge.currency_as_string.should == "USD"
|
175
|
+
end
|
176
|
+
|
177
|
+
it "overrides default currency with model currency, in fixnum assignments" do
|
178
|
+
@product.discount_value = 5
|
179
|
+
@product.save.should be_true
|
180
|
+
@product.discount_value.cents.should == 500
|
181
|
+
@product.discount_value.currency_as_string.should == "USD"
|
182
|
+
end
|
183
|
+
|
184
|
+
it "overrides default currency with model currency, in string assignments" do
|
185
|
+
@product.discount_value = "5"
|
186
|
+
@product.save.should be_true
|
187
|
+
@product.discount_value.cents.should == 500
|
188
|
+
@product.discount_value.currency_as_string.should == "USD"
|
189
|
+
end
|
190
|
+
|
191
|
+
it "falls back to default currency, in fixnum assignments" do
|
192
|
+
@service.discount = 5
|
193
|
+
@service.save.should be_true
|
194
|
+
@service.discount.cents.should == 500
|
195
|
+
@service.discount.currency_as_string.should == "EUR"
|
196
|
+
end
|
197
|
+
|
198
|
+
it "falls back to default currency, in string assignments" do
|
199
|
+
@service.discount = "5"
|
200
|
+
@service.save.should be_true
|
201
|
+
@service.discount.cents.should == 500
|
202
|
+
@service.discount.currency_as_string.should == "EUR"
|
203
|
+
end
|
204
|
+
|
205
|
+
it "sets field to nil, in nil assignments if allow_nil is set" do
|
206
|
+
@product.optional_price = nil
|
207
|
+
@product.save.should be_true
|
208
|
+
@product.optional_price.should be_nil
|
209
|
+
end
|
210
|
+
|
211
|
+
it "sets field to nil, in instantiation if allow_nil is set" do
|
212
|
+
pr = Product.new(:optional_price => nil, :price_cents => 5320,
|
213
|
+
:discount => 350, :bonus_cents => 320)
|
214
|
+
pr.optional_price.should be_nil
|
215
|
+
pr.save.should be_true
|
216
|
+
pr.optional_price.should be_nil
|
217
|
+
end
|
218
|
+
|
219
|
+
it "sets field to nil, in blank assignments if allow_nil is set" do
|
220
|
+
@product.optional_price = ""
|
221
|
+
@product.save.should be_true
|
222
|
+
@product.optional_price.should be_nil
|
223
|
+
end
|
224
|
+
|
225
|
+
context "for column with currency:" do
|
226
|
+
it "is overridden by instance currency" do
|
227
|
+
product = Product.create(:price_cents => 5320, :discount => 350, :bonus_cents => 320)
|
228
|
+
product.stub(:currency) { "EUR" }
|
229
|
+
product.bonus.currency_as_string.should == "EUR"
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
context "for column with model currency:" do
|
234
|
+
it "has default currency if not specified" do
|
235
|
+
product = Product.create(:sale_price_amount => 1234)
|
236
|
+
product.sale_price.currency_as_string == 'USD'
|
237
|
+
end
|
238
|
+
it "is overridden by instance currency column" do
|
239
|
+
product = Product.create(:sale_price_amount => 1234,
|
240
|
+
:sale_price_currency_code => 'CAD')
|
241
|
+
product.sale_price.currency_as_string.should == 'CAD'
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
context "for model with currency column:" do
|
246
|
+
before :each do
|
247
|
+
@transaction = Transaction.create(:amount_cents => 2400, :tax_cents => 600,
|
248
|
+
:currency => :usd)
|
249
|
+
@dummy_product1 = DummyProduct.create(:price_cents => 2400, :currency => :usd)
|
250
|
+
@dummy_product2 = DummyProduct.create(:price_cents => 2600) # nil currency
|
251
|
+
end
|
252
|
+
|
253
|
+
it "serializes correctly the currency to a new instance of model" do
|
254
|
+
d = DummyProduct.new
|
255
|
+
d.price = Money.new(10, "EUR")
|
256
|
+
d.save!
|
257
|
+
d.reload
|
258
|
+
d.currency.should == "EUR"
|
259
|
+
end
|
260
|
+
|
261
|
+
it "overrides default currency with the value of row currency" do
|
262
|
+
@transaction.amount.currency.should == Money::Currency.find(:usd)
|
263
|
+
end
|
264
|
+
|
265
|
+
it "overrides default currency with the currency registered for the model" do
|
266
|
+
@dummy_product2.price.currency.should == Money::Currency.find(:gbp)
|
267
|
+
end
|
268
|
+
|
269
|
+
it "overrides default and model currency with the row currency" do
|
270
|
+
@dummy_product1.price.currency.should == Money::Currency.find(:usd)
|
271
|
+
end
|
272
|
+
|
273
|
+
it "constructs the money attribute from the stored mapped attribute values" do
|
274
|
+
@transaction.amount.should == Money.new(2400, :usd)
|
275
|
+
end
|
276
|
+
|
277
|
+
it "instantiates correctly Money objects from the mapped attributes" do
|
278
|
+
t = Transaction.new(:amount_cents => 2500, :currency => "CAD")
|
279
|
+
t.amount.should == Money.new(2500, "CAD")
|
280
|
+
end
|
281
|
+
|
282
|
+
it "assigns correctly Money objects to the attribute" do
|
283
|
+
@transaction.amount = Money.new(2500, :eur)
|
284
|
+
@transaction.save.should be_true
|
285
|
+
@transaction.amount.cents.should == Money.new(2500, :eur).cents
|
286
|
+
@transaction.amount.currency_as_string.should == "EUR"
|
287
|
+
end
|
288
|
+
|
289
|
+
it "uses default currency if a non Money object is assigned to the attribute" do
|
290
|
+
@transaction.amount = 234
|
291
|
+
@transaction.amount.currency_as_string.should == "USD"
|
292
|
+
end
|
293
|
+
|
294
|
+
it "constructs the money object from the mapped method value" do
|
295
|
+
@transaction.total.should == Money.new(3000, :usd)
|
296
|
+
end
|
297
|
+
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
describe "register_currency" do
|
302
|
+
it "attaches currency at model level" do
|
303
|
+
Product.currency.should == Money::Currency.find(:usd)
|
304
|
+
DummyProduct.currency.should == Money::Currency.find(:gbp)
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "configuration" do
|
4
|
+
|
5
|
+
describe "initializer" do
|
6
|
+
|
7
|
+
it "sets default currency" do
|
8
|
+
Money.default_currency.should == Money::Currency.new(:eur)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "registers a custom currency" do
|
12
|
+
Money::Currency.table.should include(:eu4)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "adds exchange rates given in config initializer" do
|
16
|
+
Money.us_dollar(100).exchange_to("CAD").should == Money.new(124, "CAD")
|
17
|
+
Money.ca_dollar(100).exchange_to("USD").should == Money.new(80, "USD")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
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.
|