rails_info 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +58 -0
- data/Rakefile +40 -0
- data/app/assets/javascripts/rails_info/application.js +6 -0
- data/app/assets/javascripts/rails_info/base.js.coffee +6 -0
- data/app/assets/javascripts/rails_info/bootstrap.js.coffee +4 -0
- data/app/assets/stylesheets/rails_info/application.css +14 -0
- data/app/assets/stylesheets/rails_info/base.css.scss +22 -0
- data/app/assets/stylesheets/rails_info/bootstrap_and_overrides.css.less +63 -0
- data/app/assets/stylesheets/rails_info/exception.css +17 -0
- data/app/assets/stylesheets/rails_info/exception/base.css.scss +31 -0
- data/app/assets/stylesheets/rails_info/exception/pygments.css.erb +5 -0
- data/app/controllers/rails_info/data_controller.rb +9 -0
- data/app/controllers/rails_info/logs/server_controller.rb +10 -0
- data/app/controllers/rails_info/logs/test/rspec_controller.rb +16 -0
- data/app/controllers/rails_info/model_controller.rb +5 -0
- data/app/controllers/rails_info/properties_controller.rb +4 -0
- data/app/controllers/rails_info/routes_controller.rb +4 -0
- data/app/controllers/rails_info/stack_traces_controller.rb +11 -0
- data/app/controllers/rails_info_controller.rb +16 -0
- data/app/helpers/rails_info/resources_helper.rb +194 -0
- data/app/presenters/rails_info/code_presenter.rb +63 -0
- data/app/presenters/rails_info/data/object_presenter.rb +34 -0
- data/app/presenters/rails_info/data/row_set_presenter.rb +38 -0
- data/app/presenters/rails_info/data_presenter.rb +21 -0
- data/app/presenters/rails_info/logs/server/action_presenter.rb +109 -0
- data/app/presenters/rails_info/logs/server_presenter.rb +57 -0
- data/app/presenters/rails_info/logs/test/rspec/file_presenter.rb +87 -0
- data/app/presenters/rails_info/logs/test/rspec_presenter.rb +32 -0
- data/app/presenters/rails_info/model_presenter.rb +68 -0
- data/app/presenters/rails_info/presenter.rb +19 -0
- data/app/presenters/rails_info/stack_trace_presenter.rb +98 -0
- data/app/views/layouts/_layout.html.erb +19 -0
- data/app/views/layouts/rails_info.html.erb +1 -0
- data/app/views/layouts/rails_info/exception.html.erb +1 -0
- data/app/views/layouts/shared/_navigation.html.erb +7 -0
- data/app/views/rails_info/data/index.html.erb +23 -0
- data/app/views/rails_info/logs/server/_table.html.erb +16 -0
- data/app/views/rails_info/logs/server/new.html.erb +19 -0
- data/app/views/rails_info/logs/test/rspec/new.html.erb +10 -0
- data/app/views/rails_info/model/index.html.erb +14 -0
- data/app/views/rails_info/properties/index.html.erb +1 -0
- data/app/views/rails_info/routes/index.html.erb +18 -0
- data/app/views/rails_info/stack_traces/_accordion.html.erb +1 -0
- data/app/views/rails_info/stack_traces/_form.html.erb +11 -0
- data/app/views/rails_info/stack_traces/_with_request_and_response.html.erb +16 -0
- data/app/views/rails_info/stack_traces/new.html.erb +10 -0
- data/config/navigation.rb +20 -0
- data/config/routes.rb +20 -0
- data/lib/rails_info.rb +19 -0
- data/lib/rails_info/controller.rb +2 -0
- data/lib/rails_info/controller/exception_diagnostics.rb +28 -0
- data/lib/rails_info/data.rb +55 -0
- data/lib/rails_info/engine.rb +9 -0
- data/lib/rails_info/logs.rb +2 -0
- data/lib/rails_info/logs/server.rb +230 -0
- data/lib/rails_info/logs/test.rb +2 -0
- data/lib/rails_info/logs/test/rspec.rb +160 -0
- data/lib/rails_info/model.rb +221 -0
- data/lib/rails_info/stack_trace.rb +155 -0
- data/lib/rails_info/version.rb +3 -0
- data/lib/tasks/rails_info_tasks.rake +4 -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/user.rb +3 -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 +59 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +42 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +39 -0
- data/spec/dummy/config/environments/production.rb +69 -0
- data/spec/dummy/config/environments/test.rb +39 -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 +2 -0
- data/spec/dummy/log/development.log +4876 -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
- metadata +274 -0
@@ -0,0 +1,221 @@
|
|
1
|
+
class RailsInfo::Model
|
2
|
+
# TODO: Live reload of ruby code as maybe seen here with require_or_load path https://github.com/Tho85/sunspot/blob/f9f6f9666e6df1a0e92c0d8070f7007075f75071/sunspot_rails/lib/sunspot/rails/tasks.rb
|
3
|
+
def classes
|
4
|
+
return @classes if @classes
|
5
|
+
|
6
|
+
#model_names = Dir.chdir(File.join(Rails.root, 'app', 'models')) { Dir["*.rb"] }
|
7
|
+
#model_names = Dir.chdir(File.expand_path('../../../../../../../reqorder2/app/models', __FILE__)) { Dir["*.rb"] }
|
8
|
+
@classes = []
|
9
|
+
|
10
|
+
# Dir['app/models/**/*.*'].map{|f| f.gsub('app/models/', '')}
|
11
|
+
Dir["#{Rails.root}/app/models/**/*.*"].each do |class_name|
|
12
|
+
@classes << class_name.split('/').last.sub(/\.rb$/,'').camelize.constantize
|
13
|
+
end
|
14
|
+
|
15
|
+
#select { |k| k.ancestors.include?(ActiveRecord::Base) && k.connection.table_exists?(k.table_name) }
|
16
|
+
@classes.select! { |c| c.respond_to?('table_name') }
|
17
|
+
|
18
|
+
@classes
|
19
|
+
end
|
20
|
+
|
21
|
+
def list
|
22
|
+
models = {}
|
23
|
+
|
24
|
+
classes.each do |klass|
|
25
|
+
ar_ancestors = active_record_ancestors(klass)
|
26
|
+
|
27
|
+
path_until_here = []
|
28
|
+
|
29
|
+
klass_path = ar_ancestors.any? ? ar_ancestors.reverse : []
|
30
|
+
|
31
|
+
if ar_ancestors.any?
|
32
|
+
if ar_ancestors.first.match('::') && ar_ancestors.length == 1
|
33
|
+
klass_path = (ar_ancestors.first + "::" + klass.to_s.split('::').last).split('::')
|
34
|
+
elsif ar_ancestors.first.match('::')
|
35
|
+
raise 'unimplemented'
|
36
|
+
else
|
37
|
+
klass_path += klass.to_s.split('::')
|
38
|
+
end
|
39
|
+
else
|
40
|
+
klass_path += klass.to_s.split('::')
|
41
|
+
end
|
42
|
+
|
43
|
+
klass_path.each do |element|
|
44
|
+
path_until_here << element
|
45
|
+
eval("models['" + path_until_here.join("']['") + "'] ||= {}")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
models
|
50
|
+
end
|
51
|
+
|
52
|
+
def associations(key_constant, options = { parent: ''})
|
53
|
+
options.assert_valid_keys(:parent)
|
54
|
+
|
55
|
+
key_constant = key_constant(key_constant, options[:parent]) if key_constant.is_a?(String)
|
56
|
+
|
57
|
+
association_collection = []
|
58
|
+
columns = columns_for(key_constant)
|
59
|
+
columns.any? and association_collection << "belongs_to: #{columns.join(', ')}"
|
60
|
+
super_active_record_ancestor_column = "#{get_super_active_record_ancestor(key_constant).split('::').last.underscore}_id"
|
61
|
+
has_many = {}
|
62
|
+
|
63
|
+
if models_with_columns[super_active_record_ancestor_column]
|
64
|
+
has_many[:standard] = models_with_columns[super_active_record_ancestor_column].uniq
|
65
|
+
else
|
66
|
+
has_many[:alias] = {}
|
67
|
+
|
68
|
+
models_with_columns.keys.select{|c| c.match("#{super_active_record_ancestor_column}")}.each do |column|
|
69
|
+
has_many[:alias][column] = models_with_columns[column]
|
70
|
+
end
|
71
|
+
|
72
|
+
has_many[:alias].empty? and has_many = {}
|
73
|
+
end
|
74
|
+
|
75
|
+
has_many_polymorphs = {}
|
76
|
+
|
77
|
+
if key_constant.respond_to?('reflections')
|
78
|
+
key_constant.reflections.values.select{|r| [:has_one, :has_many].include?(r.macro) && r.options.has_key?(:as) }.each do |reflection|
|
79
|
+
association = reflection.options[:class_name] ? reflection.options[:class_name] : reflection.name.to_s.classify
|
80
|
+
has_many_polymorphs[association] = reflection.options[:as].to_s
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
!has_many_polymorphs.empty? and has_many[:polymorphic] = has_many_polymorphs
|
85
|
+
|
86
|
+
!has_many.empty? and association_collection << "has_many: #{has_many.inspect}"
|
87
|
+
|
88
|
+
association_collection.any? ? " (#{association_collection.join(';')})" : ""
|
89
|
+
end
|
90
|
+
|
91
|
+
def columns_for(key_constant, options = {})
|
92
|
+
options.assert_valid_keys(:parent)
|
93
|
+
|
94
|
+
key_constant = key_constant(key_constant, options[:parent]) if key_constant.is_a?(String)
|
95
|
+
|
96
|
+
columns = []
|
97
|
+
|
98
|
+
begin
|
99
|
+
columns = key_constant.columns.map(&:name).select{|c| c.match('_id') || c.match('type')}.sort
|
100
|
+
rescue NoMethodError
|
101
|
+
columns = []
|
102
|
+
end
|
103
|
+
|
104
|
+
columns
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def models_with_columns
|
110
|
+
return @models_with_columns if @models_with_columns
|
111
|
+
|
112
|
+
@models_with_columns = {}
|
113
|
+
#result = []
|
114
|
+
|
115
|
+
classes.each do |klass|
|
116
|
+
columns_for(klass).each do |column|
|
117
|
+
@models_with_columns[column] ||= []
|
118
|
+
@models_with_columns[column] << klass.to_s
|
119
|
+
end
|
120
|
+
|
121
|
+
=begin
|
122
|
+
klass.reflections.values.select{|r| r.macro == :has_many && r.options.has_key?(:as) }.each do |reflection|
|
123
|
+
[
|
124
|
+
"#{reflection.options[:as].to_s}_type"#, "#{reflection.options[:as].to_s}_id"
|
125
|
+
].each do |column|
|
126
|
+
@models_with_columns[column] ||= []
|
127
|
+
@models_with_columns[column] << reflection.options[:class_name] ? reflection.options[:class_name] : reflection.name.classify
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
result << [klass.to_s, klass.table_name, ar_ancestors].inspect
|
132
|
+
=end
|
133
|
+
end
|
134
|
+
|
135
|
+
#raise @models_with_columns.inspect
|
136
|
+
|
137
|
+
#raise Customer.reflections.values.select{|r| r.macro == :has_many && r.options[:as] == :owner }.first.inspect
|
138
|
+
#raise BaseValue.reflections.values.select{|r| r.macro == :belongs_to && r.options[:polymorphic] == true }.first.inspect
|
139
|
+
|
140
|
+
#render :text => result.join('<br/>') and return
|
141
|
+
|
142
|
+
@models_with_columns
|
143
|
+
end
|
144
|
+
|
145
|
+
def get_super_active_record_ancestor(key_constant)
|
146
|
+
ancestors = active_record_ancestors(key_constant)
|
147
|
+
ancestors.none? ? key_constant.name : ancestors.last
|
148
|
+
end
|
149
|
+
|
150
|
+
# TODO: move to ActiveRecord::Base ClassMethods
|
151
|
+
def active_record_ancestors(klass)
|
152
|
+
ar_ancestors = []
|
153
|
+
|
154
|
+
klass.ancestors.map(&:to_s).uniq.each do |ancestor|
|
155
|
+
next if (ancestor == klass.to_s || ancestor == 'ActiveRecord::Base')
|
156
|
+
|
157
|
+
ancestor_constant = ancestor.constantize rescue nil
|
158
|
+
|
159
|
+
ancestor_constant == nil and next
|
160
|
+
|
161
|
+
!ancestor_constant.ancestors.map(&:to_s).include?('ActiveRecord::Base') and next
|
162
|
+
|
163
|
+
ar_ancestors << ancestor
|
164
|
+
end
|
165
|
+
|
166
|
+
ar_ancestors
|
167
|
+
end
|
168
|
+
|
169
|
+
def key_constant(key, parent = '')
|
170
|
+
key_was = key
|
171
|
+
key_constant = nil
|
172
|
+
|
173
|
+
begin
|
174
|
+
key_constant = "#{parent}::#{key}".constantize
|
175
|
+
rescue
|
176
|
+
begin
|
177
|
+
key_constant = key.constantize
|
178
|
+
|
179
|
+
!key_constant.ancestors.map(&:to_s).include?('ActiveRecord::Base') and raise key + 'is not a ActiveRecord instance.'
|
180
|
+
rescue
|
181
|
+
if parent.blank? && !key.match('::')
|
182
|
+
raise 'unexpected:' + key.inspect
|
183
|
+
elsif !parent.blank? && !key.match('::')
|
184
|
+
key = "#{parent}::#{key}"
|
185
|
+
elsif !parent.blank? && key.match('::')
|
186
|
+
raise "unexpected constellation of parent '#{parent}' and key '#{key}'"
|
187
|
+
end
|
188
|
+
|
189
|
+
# e.g. DynamicForm::BaseElement::CheckBox should be DynamicForm::CheckBox
|
190
|
+
key = key.split('::')
|
191
|
+
root_module = key.shift
|
192
|
+
klass = key.pop
|
193
|
+
|
194
|
+
tries = []
|
195
|
+
|
196
|
+
while key.any?
|
197
|
+
begin
|
198
|
+
key_constant_name = "#{root_module}::#{key.join('::')}::#{klass}"
|
199
|
+
tries << key_constant_name
|
200
|
+
key_constant = key_constant_name.constantize
|
201
|
+
rescue
|
202
|
+
key.pop
|
203
|
+
end
|
204
|
+
|
205
|
+
break
|
206
|
+
end
|
207
|
+
|
208
|
+
begin
|
209
|
+
key_constant_name = "#{root_module}::#{klass}"
|
210
|
+
tries << key_constant_name
|
211
|
+
key_constant = key_constant_name.constantize
|
212
|
+
rescue
|
213
|
+
end
|
214
|
+
|
215
|
+
key_constant.blank? and raise 'unexpected that class for key "' + key_was + '" (' + ok.inspect + ') not found by one of these tries: ' + tries.inspect
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
key_constant
|
220
|
+
end
|
221
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
class RailsInfo::StackTrace
|
2
|
+
def initialize(stack_trace, options = {})
|
3
|
+
@request = options[:request]
|
4
|
+
@exception = options[:exception]
|
5
|
+
@rails_root = options[:rails_root] || Rails.root.to_s
|
6
|
+
@lines_of_code_around = options[:lines_of_code_around] || 5
|
7
|
+
@show_all = options[:show_all]
|
8
|
+
|
9
|
+
if stack_trace.is_a?(String) && stack_trace.match('#')
|
10
|
+
# parse RSpec style stack trace "# ./app/observers/assignment_observer.rb:65:in `after_save'"
|
11
|
+
line_separator = if stack_trace.match(/\r\n/)
|
12
|
+
"\r\n"
|
13
|
+
elsif stack_trace.match(/\\n/)
|
14
|
+
/\\n/
|
15
|
+
else
|
16
|
+
"\n"
|
17
|
+
end
|
18
|
+
|
19
|
+
stack_trace = stack_trace.split(line_separator).map{|line|line.gsub('#', '').gsub('./', rails_root + '/')}.map(&:strip)
|
20
|
+
elsif stack_trace.is_a?(String) && !stack_trace.strip.blank?
|
21
|
+
raise NotImplementedError.new(stack_trace.inspect)
|
22
|
+
end
|
23
|
+
|
24
|
+
@stack_trace = stack_trace
|
25
|
+
end
|
26
|
+
|
27
|
+
def exception
|
28
|
+
@exception
|
29
|
+
end
|
30
|
+
|
31
|
+
def title
|
32
|
+
text = @exception.class.to_s
|
33
|
+
|
34
|
+
if @request && @request.parameters['controller']
|
35
|
+
action = @request.parameters['action'].present? ? "##{@request.parameters['action']}" : ''
|
36
|
+
text += " in #{@request.parameters['controller'].camelize}Controller#{action}"
|
37
|
+
end
|
38
|
+
|
39
|
+
text
|
40
|
+
end
|
41
|
+
|
42
|
+
def message
|
43
|
+
@exception.message
|
44
|
+
end
|
45
|
+
|
46
|
+
def lines_of_code_around
|
47
|
+
@lines_of_code_around
|
48
|
+
end
|
49
|
+
|
50
|
+
def hash
|
51
|
+
return @hash if @hash
|
52
|
+
|
53
|
+
@hash = {}
|
54
|
+
|
55
|
+
parse_stack_trace
|
56
|
+
|
57
|
+
@hash
|
58
|
+
end
|
59
|
+
|
60
|
+
def request
|
61
|
+
@request
|
62
|
+
end
|
63
|
+
|
64
|
+
def response
|
65
|
+
@response
|
66
|
+
end
|
67
|
+
|
68
|
+
def rails_root
|
69
|
+
@rails_root
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def parse_stack_trace
|
75
|
+
(@stack_trace.is_a?(Array) ? @stack_trace : []).each{|line| parse_stack_trace_line(line) }
|
76
|
+
end
|
77
|
+
|
78
|
+
# TODO: add additionally tab with a diff of separate rails_root / gemset when those parameters have been passed
|
79
|
+
def parse_stack_trace_line(line_string)
|
80
|
+
method = line_string.match(/in `((.)+)'/)[1]
|
81
|
+
|
82
|
+
line_string.gsub!(method, '')
|
83
|
+
line = line_string.split(':')
|
84
|
+
line = {file: line.first.strip, number: line.second.strip.to_i}
|
85
|
+
|
86
|
+
code = {}
|
87
|
+
|
88
|
+
if File.exist? line[:file]
|
89
|
+
code = code_with_line_numbers(line)
|
90
|
+
else
|
91
|
+
code = { text: "File #{line[:file]} not found", line_numbers: [0]}
|
92
|
+
end
|
93
|
+
|
94
|
+
path = line[:file].split('/')
|
95
|
+
file_name = path.pop
|
96
|
+
|
97
|
+
@hash["#{method} @ #{line[:number]} in #{file_name} of #{path.join('/')}"] = code
|
98
|
+
end
|
99
|
+
|
100
|
+
def code_with_line_numbers(options = {})
|
101
|
+
options.assert_valid_keys(:file, :number)
|
102
|
+
|
103
|
+
middle_number = options[:number]
|
104
|
+
|
105
|
+
lines_hash, current_number = {}, 0
|
106
|
+
|
107
|
+
File.open options[:file] do |f|
|
108
|
+
f.each_line do |line|
|
109
|
+
current_number += 1
|
110
|
+
|
111
|
+
next if line.strip == "" # remove empty lines
|
112
|
+
|
113
|
+
lines_hash[current_number] = line
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
not_empty_index, middle_index = 0, 0
|
118
|
+
|
119
|
+
unless lines_hash.keys.include?(middle_number)
|
120
|
+
alternative_middle_numbers = lines_hash.keys.select{|number| number >= middle_number - @lines_of_code_around && number <= middle_number + @lines_of_code_around }
|
121
|
+
|
122
|
+
if alternative_middle_numbers
|
123
|
+
middle_number = alternative_middle_numbers[(alternative_middle_numbers.length / 2).round - 1]
|
124
|
+
else
|
125
|
+
middle_number = nil
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
lines_hash.each do |number,line|
|
130
|
+
middle_index = not_empty_index if number == middle_number
|
131
|
+
not_empty_index += 1
|
132
|
+
end
|
133
|
+
|
134
|
+
not_empty_index, lines, line_numbers = -1, [], []
|
135
|
+
highlighted_number, visible_number = nil, 1
|
136
|
+
|
137
|
+
lines_hash.each do |number,line|
|
138
|
+
not_empty_index += 1
|
139
|
+
|
140
|
+
unless @show_all || middle_number.nil?
|
141
|
+
next unless not_empty_index >= middle_index - @lines_of_code_around && not_empty_index <= middle_index + @lines_of_code_around
|
142
|
+
end
|
143
|
+
|
144
|
+
lines << line
|
145
|
+
line_numbers << number
|
146
|
+
highlighted_number = visible_number if middle_number && number == middle_number
|
147
|
+
visible_number += 1
|
148
|
+
end
|
149
|
+
|
150
|
+
# try to free memory
|
151
|
+
lines_hash = nil
|
152
|
+
|
153
|
+
{ text: lines.join(''), number: options[:number], line_numbers: line_numbers, highlighted_line_numbers: [highlighted_number] }
|
154
|
+
end
|
155
|
+
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.
|