extjs_scaffold 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. data/.gitignore +9 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +4 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +80 -0
  6. data/Rakefile +28 -0
  7. data/extjs_scaffold.gemspec +27 -0
  8. data/features/scaffold_generator.feature +124 -0
  9. data/features/step_definitions/aruba_ext_steps.rb +6 -0
  10. data/features/support/setup.rb +5 -0
  11. data/lib/extjs_scaffold.rb +1 -0
  12. data/lib/extjs_scaffold/version.rb +3 -0
  13. data/lib/generators/extjs_scaffold.rb +15 -0
  14. data/lib/generators/extjs_scaffold/install/USAGE +28 -0
  15. data/lib/generators/extjs_scaffold/install/install_generator.rb +82 -0
  16. data/lib/generators/extjs_scaffold/install/templates/Actionable.js +27 -0
  17. data/lib/generators/extjs_scaffold/install/templates/App.js +11 -0
  18. data/lib/generators/extjs_scaffold/install/templates/EditWindow.js +135 -0
  19. data/lib/generators/extjs_scaffold/install/templates/FormPanel.js +27 -0
  20. data/lib/generators/extjs_scaffold/install/templates/Format.js +34 -0
  21. data/lib/generators/extjs_scaffold/install/templates/GridPanel.js +289 -0
  22. data/lib/generators/extjs_scaffold/install/templates/ParentComboField.js +39 -0
  23. data/lib/generators/extjs_scaffold/install/templates/Rails.js +162 -0
  24. data/lib/generators/extjs_scaffold/install/templates/ScrollingToolbar.js +170 -0
  25. data/lib/generators/extjs_scaffold/install/templates/SearchField.js +104 -0
  26. data/lib/generators/extjs_scaffold/install/templates/UpdateWindow.js +112 -0
  27. data/lib/generators/extjs_scaffold/install/templates/Updateable.js +31 -0
  28. data/lib/generators/extjs_scaffold/install/templates/ext_auth.html.erb +3 -0
  29. data/lib/generators/extjs_scaffold/install/templates/extjs_scaffold.css.scss +9 -0
  30. data/lib/generators/extjs_scaffold/install/templates/images/README.txt +1 -0
  31. data/lib/generators/extjs_scaffold/install/templates/images/add.gif +0 -0
  32. data/lib/generators/extjs_scaffold/install/templates/images/application_form_edit.png +0 -0
  33. data/lib/generators/extjs_scaffold/install/templates/images/delete.gif +0 -0
  34. data/lib/generators/extjs_scaffold/install/templates/images/tick.png +0 -0
  35. data/lib/generators/extjs_scaffold/scaffold/USAGE +24 -0
  36. data/lib/generators/extjs_scaffold/scaffold/scaffold_generator.rb +19 -0
  37. data/lib/generators/extjs_scaffold/scaffold_controller/USAGE +20 -0
  38. data/lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb +296 -0
  39. data/lib/generators/extjs_scaffold/scaffold_controller/templates/controller.rb +191 -0
  40. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/Controller.js +53 -0
  41. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/EditForm.js +17 -0
  42. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/EditWindow.js +27 -0
  43. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/Grid.js +26 -0
  44. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/Model.js +43 -0
  45. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/ReferenceStore.js +27 -0
  46. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/Store.js +28 -0
  47. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/UpdateForm.js +17 -0
  48. data/lib/generators/extjs_scaffold/scaffold_controller/templates/js/UpdateWindow.js +21 -0
  49. data/lib/generators/extjs_scaffold/scaffold_controller/templates/model.rb +57 -0
  50. data/lib/generators/extjs_scaffold/scaffold_controller/templates/tests/controller_spec.rb +197 -0
  51. data/lib/generators/extjs_scaffold/scaffold_controller/templates/tests/controller_test.rb +85 -0
  52. data/lib/generators/extjs_scaffold/scaffold_controller/templates/views/erb/index.html.erb +3 -0
  53. data/lib/generators/extjs_scaffold/scaffold_controller/templates/views/haml/index.html.haml +3 -0
  54. data/spec/dummy/.gitignore +15 -0
  55. data/spec/dummy/README +261 -0
  56. data/spec/dummy/Rakefile +7 -0
  57. data/spec/dummy/app/assets/images/rails.png +0 -0
  58. data/spec/dummy/app/assets/javascripts/application.js +7 -0
  59. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  60. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  61. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  62. data/spec/dummy/app/mailers/.gitkeep +0 -0
  63. data/spec/dummy/app/models/.gitkeep +0 -0
  64. data/spec/dummy/app/views/layouts/application.html.erb +16 -0
  65. data/spec/dummy/config.ru +4 -0
  66. data/spec/dummy/config/application.rb +48 -0
  67. data/spec/dummy/config/boot.rb +7 -0
  68. data/spec/dummy/config/database.yml +25 -0
  69. data/spec/dummy/config/environment.rb +5 -0
  70. data/spec/dummy/config/environments/development.rb +30 -0
  71. data/spec/dummy/config/environments/production.rb +60 -0
  72. data/spec/dummy/config/environments/test.rb +39 -0
  73. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  74. data/spec/dummy/config/initializers/inflections.rb +10 -0
  75. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  76. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  77. data/spec/dummy/config/initializers/session_store.rb +8 -0
  78. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  79. data/spec/dummy/config/locales/en.yml +5 -0
  80. data/spec/dummy/config/routes.rb +58 -0
  81. data/spec/dummy/db/schema.rb +23 -0
  82. data/spec/dummy/db/seeds.rb +7 -0
  83. data/spec/dummy/doc/README_FOR_APP +2 -0
  84. data/spec/dummy/lib/assets/.gitkeep +0 -0
  85. data/spec/dummy/lib/tasks/.gitkeep +0 -0
  86. data/spec/dummy/log/.gitkeep +0 -0
  87. data/spec/dummy/public/404.html +26 -0
  88. data/spec/dummy/public/422.html +26 -0
  89. data/spec/dummy/public/500.html +26 -0
  90. data/spec/dummy/public/favicon.ico +0 -0
  91. data/spec/dummy/public/index.html +241 -0
  92. data/spec/dummy/public/robots.txt +5 -0
  93. data/spec/dummy/script/rails +6 -0
  94. data/spec/dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  95. data/spec/dummy/vendor/plugins/.gitkeep +0 -0
  96. data/spec/generators/install_spec.rb +140 -0
  97. data/spec/generators/scaffold_spec.rb +244 -0
  98. data/spec/spec_helper.rb +26 -0
  99. data/spec/support/App.js +12 -0
  100. data/spec/support/rails_routes.rb +4 -0
  101. metadata +290 -0
@@ -0,0 +1,57 @@
1
+ <%
2
+ # build search query on all attributes
3
+ query = []
4
+ params = []
5
+ attributes.each do |a|
6
+ a.reference? ? query << "#{a.name.pluralize}.#{reference_field(a)} LIKE ?" : query << "#{plural_table_name}.#{a.name} LIKE ?"
7
+ params << "search"
8
+ end
9
+
10
+ virtual_fields = {}
11
+ join_tables = []
12
+ -%>
13
+ <% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
14
+
15
+ validates :<%= attribute.name %>_<%= reference_field(attribute) %>, :presence => true
16
+
17
+ def <%= attribute.name %>_<%= reference_field(attribute) %>
18
+ <%= attribute.name %>.<%= reference_field(attribute) %> if <%= attribute.name %>
19
+ end
20
+
21
+ def <%= attribute.name %>_<%= reference_field(attribute) %>=(<%= reference_field(attribute) %>)
22
+ # empty setter for update_all
23
+ end
24
+ <% virtual_fields["#{attribute.name}_#{reference_field(attribute)}"] = "#{attribute.name.pluralize}.#{reference_field(attribute)}" -%>
25
+ <% join_tables << ":#{attribute.name}" -%>
26
+ <% end -%>
27
+
28
+ # search or return all
29
+ def self.search(query, page, size, sort_column, sort_direction)
30
+ <% if virtual_fields.count > 0 -%>
31
+ # handle sort columns - prepend table name and add reference attributes
32
+ case sort_column
33
+ <% virtual_fields.each do |key, value| -%>
34
+ when '<%= key %>'
35
+ sort_column = '<%= value %>'
36
+ <% end -%>
37
+ else
38
+ sort_column = "<%= plural_table_name %>.#{sort_column}"
39
+ end
40
+ <% end -%>
41
+ <%
42
+ pagination_string = @pagination == "will_paginate" ? "paginate(:page => page, :per_page => size)" : "page(page).per(size)"
43
+ -%>
44
+ if query
45
+ search = "%#{query}%"
46
+ <% if virtual_fields.count > 0 -%>
47
+ return order(sort_column+" "+sort_direction).joins([<%= join_tables.join(',') %>]).where("<%= query.join(' OR ') %>", <%= params.join(',') %>).<%= pagination_string %>
48
+ <% else -%>
49
+ return order(sort_column+" "+sort_direction).where("<%= query.join(' OR ') %>", <%= params.join(',') %>).<%= pagination_string %>
50
+ <% end -%>
51
+ end
52
+ <% if virtual_fields.count > 0 -%>
53
+ order(sort_column+" "+sort_direction).joins([<%= join_tables.join(',') %>]).<%= pagination_string %>
54
+ <% else -%>
55
+ order(sort_column+" "+sort_direction).page(page).<%= pagination_string %>
56
+ <% end -%>
57
+ end
@@ -0,0 +1,197 @@
1
+ <%
2
+ # build reference fields
3
+ refs = attributes.select {|attr| attr.reference? }.collect{|a| a.name }
4
+ -%>
5
+ require 'spec_helper'
6
+
7
+ describe <%= controller_class_name %>Controller do
8
+
9
+ def valid_attributes
10
+ {
11
+ <% refs.each_with_index do |ref, index| -%>
12
+ <%= ", " if index > 0 %>:<%= ref %>_id => <%= ref.classify %>.create!(<%= ref %>_valid_attributes)
13
+ <% end -%>
14
+ }
15
+ end
16
+
17
+ <% refs.each_with_index do |ref| -%>
18
+ def <%= ref %>_valid_attributes
19
+ {}
20
+ end
21
+
22
+ <% end -%>
23
+ describe "GET index" do
24
+ it "assigns all <%= plural_table_name %> as @<%= plural_table_name %>" do
25
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
26
+ get :index
27
+ assigns(:<%= plural_table_name %>).should eq([<%= singular_table_name %>])
28
+ end
29
+
30
+ it "renders index template" do
31
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
32
+ get :index
33
+ render_template("index")
34
+ end
35
+
36
+ it "responds to xhr json requests" do
37
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
38
+ xhr :get, :index, :format => :json
39
+ json = ActiveSupport::JSON.decode(response.body)
40
+ json['total'].should eq(1)
41
+ json['<%= singular_table_name %>'].size.should eq(1)
42
+ json['<%= singular_table_name %>'].first['id'].should eq(<%= singular_table_name %>.id)
43
+ end
44
+ end
45
+
46
+ describe "GET show" do
47
+ it "responds to xhr json requests" do
48
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
49
+ xhr :get, :show, :id => <%= singular_table_name %>.id, :format => :json
50
+ json = ActiveSupport::JSON.decode(response.body)
51
+ json['success'].should be_true
52
+ json['data']['id'].should eq(<%= singular_table_name %>.id)
53
+ end
54
+ end
55
+
56
+ describe "GET edit" do
57
+ it "responds to xhr json requests" do
58
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
59
+ xhr :get, :edit, :id => <%= singular_table_name %>.id, :format => :json
60
+ json = ActiveSupport::JSON.decode(response.body)
61
+ json['success'].should be_true
62
+ json['data']['id'].should eq(<%= singular_table_name %>.id)
63
+ end
64
+ end
65
+
66
+ describe "POST create" do
67
+ describe "with valid params" do
68
+ it "creates a new <%= class_name %>" do
69
+ expect {
70
+ post :create, :<%= singular_table_name %> => valid_attributes
71
+ }.to change(<%= class_name %>, :count).by(1)
72
+ end
73
+
74
+ it "assigns a newly created <%= singular_table_name %> as @<%= singular_table_name %>" do
75
+ post :create, :<%= singular_table_name %> => valid_attributes
76
+ assigns(:<%= singular_table_name %>).should be_a(<%= class_name %>)
77
+ assigns(:<%= singular_table_name %>).should be_persisted
78
+ end
79
+
80
+ it "redirects to the created <%= singular_table_name %>" do
81
+ post :create, :<%= singular_table_name %> => valid_attributes
82
+ response.should redirect_to(<%= class_name %>.last)
83
+ end
84
+
85
+ it "responds to xhr json requests" do
86
+ xhr :post, :create, :<%= singular_table_name %> => valid_attributes, :format => :json
87
+ json = ActiveSupport::JSON.decode(response.body)
88
+ json['success'].should be_true
89
+ json['data']['id'].should eq(<%= class_name %>.last.id)
90
+ end
91
+ end
92
+
93
+ describe "with invalid params" do
94
+ it "responds to xhr json requests" do
95
+ # Trigger the behavior that occurs when invalid params are submitted
96
+ <%= class_name %>.any_instance.stub(:save).and_return(false)
97
+ xhr :post, :create, :<%= singular_table_name %> => valid_attributes, :format => :json
98
+ json = ActiveSupport::JSON.decode(response.body)
99
+ json['success'].should be_false
100
+ json['errors'].should_not be_nil
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "PUT update" do
106
+ describe "with valid params" do
107
+ it "responds to xhr json requests" do
108
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
109
+ xhr :put, :update, :id => <%= singular_table_name %>.id, :<%= singular_table_name %> => valid_attributes, :format => :json
110
+ json = ActiveSupport::JSON.decode(response.body)
111
+ json['success'].should be_true
112
+ json['data']['id'].should eq(<%= singular_table_name %>.id)
113
+
114
+ end
115
+ end
116
+
117
+ describe "with invalid params" do
118
+ it "responds to xhr json requests" do
119
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
120
+ # Trigger the behavior that occurs when invalid params are submitted
121
+ <%= class_name %>.any_instance.stub(:invalid?).and_return(true)
122
+ xhr :put, :update, :id => <%= singular_table_name %>.id, :<%= singular_table_name %> => {}, :format => :json
123
+ json = ActiveSupport::JSON.decode(response.body)
124
+ json['success'].should be_false
125
+ json['errors'].should_not be_nil
126
+ end
127
+ end
128
+ end
129
+
130
+ describe "DELETE destroy" do
131
+ it "destroys the requested <%= singular_table_name %>" do
132
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
133
+ expect {
134
+ delete :destroy, :id => <%= singular_table_name %>.id
135
+ }.to change(<%= class_name %>, :count).by(-1)
136
+ end
137
+
138
+ it "redirects to the <%= plural_table_name %> list" do
139
+ <%= singular_table_name %> = <%= class_name %>.create! valid_attributes
140
+ delete :destroy, :id => <%= singular_table_name %>.id
141
+ response.should redirect_to(<%= plural_table_name %>_url)
142
+ end
143
+ end
144
+
145
+ describe "POST destory_all" do
146
+ describe "with valid params" do
147
+ it "responds to xhr json requests" do
148
+ <%= singular_table_name %> = []
149
+ 2.times{|n| <%= singular_table_name %> << <%= class_name %>.create!(valid_attributes) }
150
+ expect {
151
+ xhr :post, :destroy_all, :<%= singular_table_name %> => <%= singular_table_name %>.to_json, :format => :json
152
+ }.to change(<%= class_name %>, :count).by(-2)
153
+ json = ActiveSupport::JSON.decode(response.body)
154
+ json['success'].should be_true
155
+ end
156
+ end
157
+
158
+ describe "with invalid params" do
159
+ it "responds to xhr json requests" do
160
+ <%= singular_table_name %> = []
161
+ 2.times{|n| <%= singular_table_name %> << <%= class_name %>.create!(valid_attributes) }
162
+ <%= class_name %>.find(<%= singular_table_name %>.last).destroy
163
+ expect {
164
+ xhr :post, :destroy_all, :<%= singular_table_name %> => <%= singular_table_name %>.to_json, :format => :json
165
+ }.to change(<%= class_name %>, :count).by(0)
166
+ json = ActiveSupport::JSON.decode(response.body)
167
+ json['success'].should be_false
168
+ json['errors'].should_not be_nil
169
+ end
170
+ end
171
+ end
172
+
173
+ describe "POST update_all" do
174
+ describe "with valid params" do
175
+ it "responds to xhr json requests" do
176
+ <%= singular_table_name %> = []
177
+ 2.times{|n| <%= singular_table_name %> << <%= class_name %>.create!(valid_attributes) }
178
+ xhr :post, :update_all, :<%= singular_table_name %> => <%= singular_table_name %>.to_json, :format => :json
179
+ json = ActiveSupport::JSON.decode(response.body)
180
+ json['success'].should be_true
181
+ end
182
+ end
183
+
184
+ describe "with invalid params" do
185
+ it "responds to xhr json requests" do
186
+ <%= singular_table_name %> = []
187
+ 2.times{|n| <%= singular_table_name %> << <%= class_name %>.create!(valid_attributes) }
188
+ <%= class_name %>.find(<%= singular_table_name %>.last).destroy
189
+ xhr :post, :update_all, :<%= singular_table_name %> => <%= singular_table_name %>.to_json, :format => :json
190
+ json = ActiveSupport::JSON.decode(response.body)
191
+ json['success'].should be_false
192
+ json['errors'].should_not be_nil
193
+ end
194
+ end
195
+ end
196
+
197
+ end
@@ -0,0 +1,85 @@
1
+ <%
2
+ # build reference fields
3
+ refs = attributes.select {|attr| attr.reference? }.collect{|a| a.name }
4
+ -%>
5
+ require 'test_helper'
6
+
7
+ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
8
+ setup do
9
+ @<%= singular_table_name %> = <%= plural_table_name %>(:one)
10
+ <% refs.each_with_index do |ref| -%>
11
+ @<%= singular_table_name %>.<%= ref %> = <%= ref.pluralize %>(:one)
12
+ <% end -%>
13
+ @<%= singular_table_name %>_2 = <%= plural_table_name %>(:two)
14
+ <% refs.each_with_index do |ref| -%>
15
+ @<%= singular_table_name %>_2.<%= ref %> = <%= ref.pluralize %>(:two)
16
+ <% end -%>
17
+ end
18
+
19
+ test "should get index" do
20
+ get :index
21
+ assert_response :success
22
+ assert_not_nil assigns(:<%= plural_table_name %>)
23
+ end
24
+
25
+ test "should create <%= singular_table_name %>" do
26
+ assert_difference('<%= class_name %>.count') do
27
+ post :create, <%= singular_table_name %>: @<%= singular_table_name %>.attributes
28
+ end
29
+
30
+ assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
31
+ end
32
+
33
+ test "should create <%= singular_table_name %> via xhr json requests" do
34
+ assert_difference('<%= class_name %>.count') do
35
+ xhr :post, :create, <%= singular_table_name %>: @<%= singular_table_name %>.attributes, :format => :json
36
+ end
37
+
38
+ json = ActiveSupport::JSON.decode(@response.body)
39
+ assert json['success']
40
+ assert_equal json['data']['id'], <%= class_name %>.last.id
41
+ end
42
+
43
+ test "should show <%= singular_table_name %> via xhr json" do
44
+ xhr :get, :show, id: @<%= singular_table_name %>.to_param, :format => :json
45
+ json = ActiveSupport::JSON.decode(@response.body)
46
+ assert json['success']
47
+ assert_equal json['data']['id'], @<%= singular_table_name %>.id
48
+ end
49
+
50
+ test "should get edit <%= singular_table_name %> via xhr json" do
51
+ xhr :get, :edit, id: @<%= singular_table_name %>.to_param, :format => :json
52
+ json = ActiveSupport::JSON.decode(@response.body)
53
+ assert json['success']
54
+ assert_equal json['data']['id'], @<%= singular_table_name %>.id
55
+ end
56
+
57
+ test "should update <%= singular_table_name %> via xhr json" do
58
+ xhr :put, :update, id: @<%= singular_table_name %>.to_param, <%= singular_table_name %>: @<%= singular_table_name %>.attributes, :format => :json
59
+ json = ActiveSupport::JSON.decode(@response.body)
60
+ assert json['success']
61
+ assert_equal json['data']['id'], @<%= singular_table_name %>.id
62
+ end
63
+
64
+ test "should destroy <%= singular_table_name %>" do
65
+ assert_difference('<%= class_name %>.count', -1) do
66
+ delete :destroy, id: @<%= singular_table_name %>.to_param
67
+ end
68
+
69
+ assert_redirected_to <%= plural_table_name %>_path
70
+ end
71
+
72
+ test "should destroy_all via xhr json" do
73
+ assert_difference('<%= class_name %>.count', -2) do
74
+ xhr :post, :destroy_all, <%= singular_table_name %>: [@<%= singular_table_name %>, @<%= singular_table_name %>_2].to_json, :format => :json
75
+ end
76
+ json = ActiveSupport::JSON.decode(@response.body)
77
+ assert json['success']
78
+ end
79
+
80
+ test "should update_all via xhr json" do
81
+ xhr :post, :destroy_all, <%= singular_table_name %>: [@<%= singular_table_name %>, @<%= singular_table_name %>_2].to_json, :format => :json
82
+ json = ActiveSupport::JSON.decode(@response.body)
83
+ assert json['success']
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ <%%= render "shared/ext_auth" %>
2
+
3
+ <div id="<%= plural_table_name %>_list"></div>
@@ -0,0 +1,3 @@
1
+ = render "shared/ext_auth"
2
+
3
+ #<%= plural_table_name %>_list
@@ -0,0 +1,15 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile ~/.gitignore_global
6
+
7
+ # Ignore bundler config
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+
13
+ # Ignore all logfiles and tempfiles.
14
+ /log/*.log
15
+ /tmp
data/spec/dummy/README 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.