draper 0.18.0 → 1.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.markdown +8 -0
- data/Gemfile +4 -0
- data/Rakefile +50 -31
- data/Readme.markdown +42 -48
- data/draper.gemspec +2 -1
- data/lib/draper.rb +42 -7
- data/lib/draper/collection_decorator.rb +88 -0
- data/lib/draper/decoratable.rb +44 -0
- data/lib/draper/decorated_association.rb +55 -0
- data/lib/draper/decorator.rb +208 -0
- data/lib/draper/finders.rb +44 -0
- data/lib/draper/helper_proxy.rb +16 -0
- data/lib/draper/railtie.rb +17 -21
- data/lib/draper/security.rb +48 -0
- data/lib/draper/tasks/tu.rake +5 -0
- data/lib/draper/test/minitest_integration.rb +1 -1
- data/lib/draper/test/test_unit_integration.rb +9 -0
- data/lib/draper/version.rb +1 -1
- data/lib/draper/view_context.rb +6 -13
- data/lib/draper/view_helpers.rb +36 -0
- data/lib/generators/decorator/decorator_generator.rb +1 -1
- data/lib/generators/decorator/templates/decorator.rb +0 -1
- data/spec/draper/collection_decorator_spec.rb +240 -0
- data/spec/draper/decoratable_spec.rb +164 -0
- data/spec/draper/decorated_association_spec.rb +130 -0
- data/spec/draper/decorator_spec.rb +497 -0
- data/spec/draper/finders_spec.rb +156 -0
- data/spec/draper/helper_proxy_spec.rb +12 -0
- data/spec/draper/security_spec.rb +158 -0
- data/spec/draper/view_helpers_spec.rb +41 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/localized_urls.rb +5 -0
- data/spec/dummy/app/controllers/posts_controller.rb +17 -0
- data/spec/dummy/app/decorators/post_decorator.rb +25 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +3 -0
- data/spec/dummy/app/mailers/post_mailer.rb +9 -0
- data/spec/dummy/app/models/post.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +11 -0
- data/spec/dummy/app/views/post_mailer/decorated_email.html.erb +1 -0
- data/spec/dummy/app/views/posts/_post.html.erb +19 -0
- data/spec/dummy/app/views/posts/show.html.erb +1 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +64 -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 +34 -0
- data/spec/dummy/config/environments/production.rb +55 -0
- data/spec/dummy/config/environments/test.rb +32 -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 +7 -0
- data/spec/dummy/db/migrate/20121019115657_create_posts.rb +8 -0
- data/spec/dummy/db/schema.rb +21 -0
- data/spec/dummy/db/seeds.rb +2 -0
- data/spec/dummy/lib/tasks/spec.rake +5 -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/dummy/spec/decorators/post_decorator_spec.rb +23 -0
- data/spec/dummy/spec/mailers/post_mailer_spec.rb +29 -0
- data/spec/dummy/spec/spec_helper.rb +9 -0
- data/spec/generators/decorator/decorator_generator_spec.rb +3 -4
- data/spec/integration/integration_spec.rb +33 -0
- data/{performance → spec/performance}/active_record.rb +0 -0
- data/{performance/bechmark.rb → spec/performance/benchmark.rb} +0 -0
- data/{performance → spec/performance}/decorators.rb +2 -4
- data/{performance → spec/performance}/models.rb +0 -0
- data/spec/spec_helper.rb +20 -41
- data/spec/support/action_controller.rb +12 -0
- data/spec/support/active_model.rb +7 -0
- data/spec/support/{samples/active_record.rb → active_record.rb} +5 -0
- data/spec/support/{samples → decorators}/decorator_with_application_helper.rb +1 -1
- data/spec/support/decorators/namespaced_product_decorator.rb +5 -0
- data/spec/support/decorators/non_active_model_product_decorator.rb +2 -0
- data/spec/support/decorators/product_decorator.rb +19 -0
- data/spec/support/{samples → decorators}/products_decorator.rb +1 -1
- data/spec/support/decorators/some_thing_decorator.rb +2 -0
- data/spec/support/{samples → decorators}/specific_product_decorator.rb +0 -2
- data/spec/support/{samples → decorators}/widget_decorator.rb +0 -0
- data/spec/support/dummy_app.rb +84 -0
- data/spec/support/matchers/have_text.rb +50 -0
- data/spec/support/{samples → models}/namespaced_product.rb +1 -3
- data/spec/support/{samples → models}/non_active_model_product.rb +1 -0
- data/spec/support/{samples → models}/product.rb +13 -2
- data/spec/support/models/some_thing.rb +5 -0
- data/spec/support/models/uninferrable_decorator_model.rb +3 -0
- data/spec/support/{samples → models}/widget.rb +0 -0
- metadata +185 -68
- data/lib/draper/active_model_support.rb +0 -27
- data/lib/draper/base.rb +0 -312
- data/lib/draper/decorated_enumerable_proxy.rb +0 -90
- data/lib/draper/model_support.rb +0 -25
- data/lib/draper/rspec_integration.rb +0 -2
- data/lib/draper/system.rb +0 -18
- data/spec/draper/base_spec.rb +0 -873
- data/spec/draper/decorated_enumerable_proxy_spec.rb +0 -45
- data/spec/draper/model_support_spec.rb +0 -48
- data/spec/support/samples/decorator.rb +0 -5
- data/spec/support/samples/decorator_with_allows.rb +0 -3
- data/spec/support/samples/decorator_with_denies.rb +0 -3
- data/spec/support/samples/decorator_with_denies_all.rb +0 -3
- data/spec/support/samples/decorator_with_multiple_allows.rb +0 -4
- data/spec/support/samples/decorator_with_special_methods.rb +0 -13
- data/spec/support/samples/enumerable_proxy.rb +0 -3
- data/spec/support/samples/namespaced_product_decorator.rb +0 -7
- data/spec/support/samples/product_decorator.rb +0 -7
- data/spec/support/samples/sequel_product.rb +0 -13
- data/spec/support/samples/some_thing.rb +0 -2
- data/spec/support/samples/some_thing_decorator.rb +0 -3
@@ -0,0 +1,156 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Draper::Finders do
|
4
|
+
describe ".find" do
|
5
|
+
it "proxies to the model class" do
|
6
|
+
Product.should_receive(:find).with(1)
|
7
|
+
ProductDecorator.find(1)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "decorates the result" do
|
11
|
+
found = Product.new
|
12
|
+
Product.stub(:find).and_return(found)
|
13
|
+
decorator = ProductDecorator.find(1)
|
14
|
+
decorator.should be_a ProductDecorator
|
15
|
+
decorator.source.should be found
|
16
|
+
end
|
17
|
+
|
18
|
+
it "passes options to the decorator" do
|
19
|
+
decorator = ProductDecorator.find(1, some: "options")
|
20
|
+
decorator.options.should == {some: "options"}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe ".find_by_(x)" do
|
25
|
+
it "proxies to the model class" do
|
26
|
+
Product.should_receive(:find_by_name).with("apples")
|
27
|
+
ProductDecorator.find_by_name("apples")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "decorates the result" do
|
31
|
+
found = Product.new
|
32
|
+
Product.stub(:find_by_name).and_return(found)
|
33
|
+
decorator = ProductDecorator.find_by_name("apples")
|
34
|
+
decorator.should be_a ProductDecorator
|
35
|
+
decorator.source.should be found
|
36
|
+
end
|
37
|
+
|
38
|
+
it "proxies complex finders" do
|
39
|
+
Product.should_receive(:find_by_name_and_size).with("apples", "large")
|
40
|
+
ProductDecorator.find_by_name_and_size("apples", "large")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "proxies find_last_by_(x) finders" do
|
44
|
+
Product.should_receive(:find_last_by_name_and_size).with("apples", "large")
|
45
|
+
ProductDecorator.find_last_by_name_and_size("apples", "large")
|
46
|
+
end
|
47
|
+
|
48
|
+
it "proxies find_or_initialize_by_(x) finders" do
|
49
|
+
Product.should_receive(:find_or_initialize_by_name_and_size).with("apples", "large")
|
50
|
+
ProductDecorator.find_or_initialize_by_name_and_size("apples", "large")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "proxies find_or_create_by_(x) finders" do
|
54
|
+
Product.should_receive(:find_or_create_by_name_and_size).with("apples", "large")
|
55
|
+
ProductDecorator.find_or_create_by_name_and_size("apples", "large")
|
56
|
+
end
|
57
|
+
|
58
|
+
it "passes options to the decorator" do
|
59
|
+
Product.should_receive(:find_by_name_and_size).with("apples", "large", {some: "options"})
|
60
|
+
decorator = ProductDecorator.find_by_name_and_size("apples", "large", some: "options")
|
61
|
+
decorator.options.should == {some: "options"}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe ".find_all_by_" do
|
66
|
+
it "proxies to the model class" do
|
67
|
+
Product.should_receive(:find_all_by_name_and_size).with("apples", "large")
|
68
|
+
ProductDecorator.find_all_by_name_and_size("apples", "large")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "decorates the result" do
|
72
|
+
found = [Product.new, Product.new]
|
73
|
+
Product.stub(:find_all_by_name).and_return(found)
|
74
|
+
decorator = ProductDecorator.find_all_by_name("apples")
|
75
|
+
decorator.should be_a Draper::CollectionDecorator
|
76
|
+
decorator.source.should be found
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe ".all" do
|
81
|
+
it "returns a decorated collection" do
|
82
|
+
collection = ProductDecorator.all
|
83
|
+
collection.should be_a Draper::CollectionDecorator
|
84
|
+
collection.first.should be_a ProductDecorator
|
85
|
+
end
|
86
|
+
|
87
|
+
it "passes options to the collection decorator" do
|
88
|
+
collection = ProductDecorator.all(some: "options")
|
89
|
+
collection.options.should == {some: "options"}
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe ".first" do
|
94
|
+
it "proxies to the model class" do
|
95
|
+
Product.should_receive(:first)
|
96
|
+
ProductDecorator.first
|
97
|
+
end
|
98
|
+
|
99
|
+
it "decorates the result" do
|
100
|
+
first = Product.new
|
101
|
+
Product.stub(:first).and_return(first)
|
102
|
+
decorator = ProductDecorator.first
|
103
|
+
decorator.should be_a ProductDecorator
|
104
|
+
decorator.source.should be first
|
105
|
+
end
|
106
|
+
|
107
|
+
it "passes options to the decorator" do
|
108
|
+
decorator = ProductDecorator.first(some: "options")
|
109
|
+
decorator.options.should == {some: "options"}
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe ".last" do
|
114
|
+
it "proxies to the model class" do
|
115
|
+
Product.should_receive(:last)
|
116
|
+
ProductDecorator.last
|
117
|
+
end
|
118
|
+
|
119
|
+
it "decorates the result" do
|
120
|
+
last = Product.new
|
121
|
+
Product.stub(:last).and_return(last)
|
122
|
+
decorator = ProductDecorator.last
|
123
|
+
decorator.should be_a ProductDecorator
|
124
|
+
decorator.source.should be last
|
125
|
+
end
|
126
|
+
|
127
|
+
it "passes options to the decorator" do
|
128
|
+
decorator = ProductDecorator.last(some: "options")
|
129
|
+
decorator.options.should == {some: "options"}
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe "scopes" do
|
134
|
+
it "proxies to the model class" do
|
135
|
+
Product.should_receive(:where).with({name: "apples"})
|
136
|
+
ProductDecorator.where(name: "apples")
|
137
|
+
end
|
138
|
+
|
139
|
+
it "doesn't decorate the result" do
|
140
|
+
found = [Product.new]
|
141
|
+
Product.stub(:where).and_return(found)
|
142
|
+
ProductDecorator.where(name: "apples").should be found
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe ".respond_to?" do
|
147
|
+
it "responds to the model's class methods" do
|
148
|
+
ProductDecorator.should respond_to :sample_class_method
|
149
|
+
end
|
150
|
+
|
151
|
+
it "responds to its own methods" do
|
152
|
+
ProductDecorator.should respond_to :my_class_method
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Draper::HelperProxy do
|
4
|
+
subject(:helper_proxy) { Draper::HelperProxy.new }
|
5
|
+
let(:view_context) { Object.new }
|
6
|
+
before { helper_proxy.stub(:view_context).and_return(view_context) }
|
7
|
+
|
8
|
+
it "proxies methods to the view context" do
|
9
|
+
view_context.should_receive(:foo).with("bar")
|
10
|
+
helper_proxy.foo("bar")
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec::Matchers.define :allow do |method|
|
4
|
+
match do |subject|
|
5
|
+
subject.allow?(method)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Draper::Security do
|
10
|
+
subject(:security) { Draper::Security.new }
|
11
|
+
|
12
|
+
context "when newly initialized" do
|
13
|
+
it "allows any method" do
|
14
|
+
security.should allow :foo
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#denies" do
|
19
|
+
it "raises an error when there are no arguments" do
|
20
|
+
expect{security.denies}.to raise_error ArgumentError
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when using denies" do
|
25
|
+
before { security.denies :foo, :bar }
|
26
|
+
|
27
|
+
it "denies the listed methods" do
|
28
|
+
security.should_not allow :foo
|
29
|
+
security.should_not allow :bar
|
30
|
+
end
|
31
|
+
|
32
|
+
it "allows other methods" do
|
33
|
+
security.should allow :baz
|
34
|
+
end
|
35
|
+
|
36
|
+
it "accepts multiple denies" do
|
37
|
+
expect{security.denies :baz}.not_to raise_error
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not accept denies_all" do
|
41
|
+
expect{security.denies_all}.to raise_error ArgumentError
|
42
|
+
end
|
43
|
+
|
44
|
+
it "does not accept allows" do
|
45
|
+
expect{security.allows :baz}.to raise_error ArgumentError
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when using mulitple denies" do
|
49
|
+
before { security.denies :baz }
|
50
|
+
|
51
|
+
it "still denies the original methods" do
|
52
|
+
security.should_not allow :foo
|
53
|
+
security.should_not allow :bar
|
54
|
+
end
|
55
|
+
|
56
|
+
it "denies the additional methods" do
|
57
|
+
security.should_not allow :baz
|
58
|
+
end
|
59
|
+
|
60
|
+
it "allows other methods" do
|
61
|
+
security.should allow :qux
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "with strings" do
|
66
|
+
before { security.denies "baz" }
|
67
|
+
|
68
|
+
it "denies the method" do
|
69
|
+
security.should_not allow :baz
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when using denies_all" do
|
75
|
+
before { security.denies_all }
|
76
|
+
|
77
|
+
it "denies all methods" do
|
78
|
+
security.should_not allow :foo
|
79
|
+
end
|
80
|
+
|
81
|
+
it "accepts multiple denies_all" do
|
82
|
+
expect{security.denies_all}.not_to raise_error
|
83
|
+
end
|
84
|
+
|
85
|
+
it "does not accept denies" do
|
86
|
+
expect{security.denies :baz}.to raise_error ArgumentError
|
87
|
+
end
|
88
|
+
|
89
|
+
it "does not accept allows" do
|
90
|
+
expect{security.allows :baz}.to raise_error ArgumentError
|
91
|
+
end
|
92
|
+
|
93
|
+
context "when using mulitple denies_all" do
|
94
|
+
before { security.denies_all }
|
95
|
+
|
96
|
+
it "still denies all methods" do
|
97
|
+
security.should_not allow :foo
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "#allows" do
|
103
|
+
it "raises an error when there are no arguments" do
|
104
|
+
expect{security.allows}.to raise_error ArgumentError
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when using allows" do
|
109
|
+
before { security.allows :foo, :bar }
|
110
|
+
|
111
|
+
it "allows the listed methods" do
|
112
|
+
security.should allow :foo
|
113
|
+
security.should allow :bar
|
114
|
+
end
|
115
|
+
|
116
|
+
it "denies other methods" do
|
117
|
+
security.should_not allow :baz
|
118
|
+
end
|
119
|
+
|
120
|
+
it "accepts multiple allows" do
|
121
|
+
expect{security.allows :baz}.not_to raise_error
|
122
|
+
end
|
123
|
+
|
124
|
+
it "does not accept denies" do
|
125
|
+
expect{security.denies :baz}.to raise_error ArgumentError
|
126
|
+
end
|
127
|
+
|
128
|
+
it "does not accept denies_all" do
|
129
|
+
expect{security.denies_all}.to raise_error ArgumentError
|
130
|
+
end
|
131
|
+
|
132
|
+
context "when using mulitple allows" do
|
133
|
+
before { security.allows :baz }
|
134
|
+
|
135
|
+
it "still allows the original methods" do
|
136
|
+
security.should allow :foo
|
137
|
+
security.should allow :bar
|
138
|
+
end
|
139
|
+
|
140
|
+
it "allows the additional methods" do
|
141
|
+
security.should allow :baz
|
142
|
+
end
|
143
|
+
|
144
|
+
it "denies other methods" do
|
145
|
+
security.should_not allow :qux
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "with strings" do
|
150
|
+
before { security.allows "baz" }
|
151
|
+
|
152
|
+
it "allows the method" do
|
153
|
+
security.should allow :baz
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Draper::ViewHelpers do
|
4
|
+
subject(:view_helpers_class) { Class.new { include Draper::ViewHelpers } }
|
5
|
+
|
6
|
+
let(:view_helpers) { view_helpers_class.new }
|
7
|
+
let(:helper_proxy) { Draper::HelperProxy.new }
|
8
|
+
let(:view_context) { Object.new }
|
9
|
+
|
10
|
+
before { view_helpers.helpers.stub(:view_context).and_return(view_context) }
|
11
|
+
|
12
|
+
describe "#helpers" do
|
13
|
+
it "returns a HelperProxy" do
|
14
|
+
view_helpers.helpers.should be_a Draper::HelperProxy
|
15
|
+
end
|
16
|
+
|
17
|
+
it "is aliased to #h" do
|
18
|
+
view_helpers.h.should be view_helpers_class.helpers
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "delegates #localize to #helpers" do
|
23
|
+
view_context.should_receive(:localize).with(Date.new)
|
24
|
+
view_helpers.localize(Date.new)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "aliases #l to #localize" do
|
28
|
+
view_context.should_receive(:localize).with(Date.new)
|
29
|
+
view_helpers.l(Date.new)
|
30
|
+
end
|
31
|
+
|
32
|
+
describe ".helpers" do
|
33
|
+
it "returns a HelperProxy" do
|
34
|
+
view_helpers_class.helpers.should be_a Draper::HelperProxy
|
35
|
+
end
|
36
|
+
|
37
|
+
it "is aliased to #h" do
|
38
|
+
view_helpers_class.h.should be view_helpers_class.helpers
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/dummy/.rspec
ADDED
@@ -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.
|