wiselinks 0.2.0 → 0.3.0
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.
- data/Gemfile +7 -0
- data/Gemfile.lock +71 -0
- data/README.md +29 -4
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/assets/javascripts/wiselinks.js.coffee +22 -8
- data/lib/wiselinks.rb +17 -9
- data/lib/wiselinks/headers.rb +19 -1
- data/lib/wiselinks/helpers.rb +9 -0
- data/lib/wiselinks/logger.rb +4 -10
- data/lib/wiselinks/rails.rb +21 -0
- data/test/cases/application_controller_test.rb +35 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/javascripts/wisepdf.js +1 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/assets/stylesheets/wisepdf.css +1 -0
- data/test/dummy/app/controllers/application_controller.rb +7 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.gitkeep +0 -0
- data/test/dummy/app/models/.gitkeep +0 -0
- data/test/dummy/app/views/application/index.pdf.erb +1 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/app/views/layouts/pdf.html.erb +14 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +57 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +7 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +3 -0
- data/test/dummy/lib/assets/.gitkeep +0 -0
- data/test/dummy/log/.gitkeep +0 -0
- data/test/dummy/log/development.log +27917 -0
- data/test/dummy/log/test.log +52308 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/javascripts/wisepdf.js +1 -0
- data/test/dummy/public/stylesheets/wisepdf.css +1 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/tmp/cache/assets/CA9/590/sprockets%2F260d19b0714b39b217abfe83309458b7 +0 -0
- data/test/dummy/tmp/cache/assets/D13/4A0/sprockets%2Fc857f4fea90e731182fa7000ea6833e9 +0 -0
- data/test/dummy/tmp/cache/assets/D1C/0F0/sprockets%2F13dc05c787589dd73a669e0ad23d54e8 +0 -0
- data/test/dummy/tmp/cache/assets/D6E/B20/sprockets%2F2669d77f5dd55e82ba092accac21871a +0 -0
- data/test/dummy/tmp/cache/assets/D93/BA0/sprockets%2Fe162e2a148480db4edf41c7ca8a527cb +0 -0
- data/test/dummy/tmp/cache/assets/E1B/1A0/sprockets%2Fbdc3a3ccd7d2f02dddd41712ed4c8e31 +0 -0
- data/test/helper.rb +7 -11
- data/travis.yml +3 -0
- data/wiselinks.gemspec +52 -3
- metadata +53 -4
- data/test/test_wiselinks.rb +0 -7
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,23 +1,80 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
+
actionmailer (3.2.9)
|
5
|
+
actionpack (= 3.2.9)
|
6
|
+
mail (~> 2.4.4)
|
7
|
+
actionpack (3.2.9)
|
8
|
+
activemodel (= 3.2.9)
|
9
|
+
activesupport (= 3.2.9)
|
10
|
+
builder (~> 3.0.0)
|
11
|
+
erubis (~> 2.7.0)
|
12
|
+
journey (~> 1.0.4)
|
13
|
+
rack (~> 1.4.0)
|
14
|
+
rack-cache (~> 1.2)
|
15
|
+
rack-test (~> 0.6.1)
|
16
|
+
sprockets (~> 2.2.1)
|
17
|
+
activemodel (3.2.9)
|
18
|
+
activesupport (= 3.2.9)
|
19
|
+
builder (~> 3.0.0)
|
20
|
+
activerecord (3.2.9)
|
21
|
+
activemodel (= 3.2.9)
|
22
|
+
activesupport (= 3.2.9)
|
23
|
+
arel (~> 3.0.2)
|
24
|
+
tzinfo (~> 0.3.29)
|
25
|
+
activeresource (3.2.9)
|
26
|
+
activemodel (= 3.2.9)
|
27
|
+
activesupport (= 3.2.9)
|
4
28
|
activesupport (3.2.9)
|
5
29
|
i18n (~> 0.6)
|
6
30
|
multi_json (~> 1.0)
|
31
|
+
arel (3.0.2)
|
7
32
|
bourne (1.1.2)
|
8
33
|
mocha (= 0.10.5)
|
34
|
+
builder (3.0.4)
|
35
|
+
erubis (2.7.0)
|
9
36
|
git (1.2.5)
|
37
|
+
hike (1.2.1)
|
10
38
|
i18n (0.6.1)
|
11
39
|
jeweler (1.8.4)
|
12
40
|
bundler (~> 1.0)
|
13
41
|
git (>= 1.2.5)
|
14
42
|
rake
|
15
43
|
rdoc
|
44
|
+
journey (1.0.4)
|
16
45
|
json (1.7.5)
|
46
|
+
mail (2.4.4)
|
47
|
+
i18n (>= 0.4.0)
|
48
|
+
mime-types (~> 1.16)
|
49
|
+
treetop (~> 1.4.8)
|
17
50
|
metaclass (0.0.1)
|
51
|
+
mime-types (1.19)
|
18
52
|
mocha (0.10.5)
|
19
53
|
metaclass (~> 0.0.1)
|
20
54
|
multi_json (1.5.0)
|
55
|
+
polyglot (0.3.3)
|
56
|
+
rack (1.4.1)
|
57
|
+
rack-cache (1.2)
|
58
|
+
rack (>= 0.4)
|
59
|
+
rack-ssl (1.3.2)
|
60
|
+
rack
|
61
|
+
rack-test (0.6.2)
|
62
|
+
rack (>= 1.0)
|
63
|
+
rails (3.2.9)
|
64
|
+
actionmailer (= 3.2.9)
|
65
|
+
actionpack (= 3.2.9)
|
66
|
+
activerecord (= 3.2.9)
|
67
|
+
activeresource (= 3.2.9)
|
68
|
+
activesupport (= 3.2.9)
|
69
|
+
bundler (~> 1.0)
|
70
|
+
railties (= 3.2.9)
|
71
|
+
railties (3.2.9)
|
72
|
+
actionpack (= 3.2.9)
|
73
|
+
activesupport (= 3.2.9)
|
74
|
+
rack-ssl (~> 1.3.2)
|
75
|
+
rake (>= 0.8.7)
|
76
|
+
rdoc (~> 3.4)
|
77
|
+
thor (>= 0.14.6, < 2.0)
|
21
78
|
rake (10.0.3)
|
22
79
|
rdoc (3.12)
|
23
80
|
json (~> 1.4)
|
@@ -28,6 +85,18 @@ GEM
|
|
28
85
|
shoulda-matchers (1.4.2)
|
29
86
|
activesupport (>= 3.0.0)
|
30
87
|
bourne (~> 1.1.2)
|
88
|
+
sprockets (2.2.2)
|
89
|
+
hike (~> 1.2)
|
90
|
+
multi_json (~> 1.0)
|
91
|
+
rack (~> 1.0)
|
92
|
+
tilt (~> 1.1, != 1.3.0)
|
93
|
+
sqlite3 (1.3.6)
|
94
|
+
thor (0.16.0)
|
95
|
+
tilt (1.3.3)
|
96
|
+
treetop (1.4.12)
|
97
|
+
polyglot
|
98
|
+
polyglot (>= 0.3.1)
|
99
|
+
tzinfo (0.3.35)
|
31
100
|
|
32
101
|
PLATFORMS
|
33
102
|
ruby
|
@@ -35,5 +104,7 @@ PLATFORMS
|
|
35
104
|
DEPENDENCIES
|
36
105
|
bundler
|
37
106
|
jeweler
|
107
|
+
rails (>= 3.2.0)
|
38
108
|
rdoc (~> 3.12)
|
39
109
|
shoulda
|
110
|
+
sqlite3
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[](https://travis-ci.org/igor-alexandrov/wiselinks)
|
2
|
+
[](https://gemnasium.com/igor-alexandrov/wiselinks)
|
3
|
+
[](https://codeclimate.com/github/igor-alexandrov/wiselinks)
|
4
|
+
|
1
5
|
#Wiselinks
|
2
6
|
|
3
7
|
Wiselinks makes following links and submitting some forms in your web application faster.
|
@@ -22,17 +26,17 @@ Wiselinks works in all major browsers including browsers that do not support HTM
|
|
22
26
|
</thead>
|
23
27
|
<tbody>
|
24
28
|
<tr>
|
25
|
-
<td>
|
29
|
+
<td>Work in browsers with History API</td>
|
26
30
|
<td>Yes</td>
|
27
31
|
<td><strong>Yes</strong></td>
|
28
32
|
</tr>
|
29
33
|
<tr>
|
30
|
-
<td>
|
34
|
+
<td>Work in browsers without History API</td>
|
31
35
|
<td>No, degrades to normal request processing.</td>
|
32
36
|
<td><strong>Yes</strong></td>
|
33
37
|
</tr>
|
34
38
|
<tr>
|
35
|
-
<td>
|
39
|
+
<td>Work without JavaScript</td>
|
36
40
|
<td>No, degrades to normal request processing.</td>
|
37
41
|
<td>No, degrades to normal request processing.</td>
|
38
42
|
</tr>
|
@@ -46,6 +50,11 @@ Wiselinks works in all major browsers including browsers that do not support HTM
|
|
46
50
|
<td>No</td>
|
47
51
|
<td><strong>Yes</strong></td>
|
48
52
|
</tr>
|
53
|
+
<tr>
|
54
|
+
<td>Asset change detection</td>
|
55
|
+
<td>Yes, by parsing document head on every request.</td>
|
56
|
+
<td><strong>Yes</strong>, by calculating assets MD5 hash on boot.</td>
|
57
|
+
</tr>
|
49
58
|
</tbody>
|
50
59
|
</table>
|
51
60
|
|
@@ -221,7 +230,9 @@ Event is triggered if the request fails.
|
|
221
230
|
* *status* – a string describing the type of error that occurred;
|
222
231
|
* *error* – optional exception object, if one occurred;
|
223
232
|
|
224
|
-
|
233
|
+
So if you wanted to have a client-side spinner, you could listen for `page:loading` to start it and `page:success` or `page:error` to stop it.
|
234
|
+
|
235
|
+
## ActionDispatch::Request extensions
|
225
236
|
|
226
237
|
Wiselinks adds a couple of methods to `ActionDispatch::Request`. These methods are mostly syntax sugar and don't have any complex logic, so you can use them or not.
|
227
238
|
|
@@ -234,6 +245,20 @@ Method returns `true` if current request is initiated by Wiselinks and client wa
|
|
234
245
|
### #wiselinks_partial? ###
|
235
246
|
Method returns `true` if current request is initiated by Wiselinks and client want to render partial, `false` otherwise.
|
236
247
|
|
248
|
+
## Title handling
|
249
|
+
|
250
|
+
Wiselinks handles page titles by passing `X-Title` header with response. To do this you can use `wiselinks_title` helper.
|
251
|
+
|
252
|
+
```html
|
253
|
+
<% wiselinks_title("Wiselinks is awesome") %>
|
254
|
+
|
255
|
+
<div>
|
256
|
+
<!-- your content -->
|
257
|
+
...
|
258
|
+
</div>
|
259
|
+
```
|
260
|
+
|
261
|
+
Of course you can use `wiselinks_title` helper in your own helpers too.
|
237
262
|
|
238
263
|
##Example
|
239
264
|
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
@@ -5,7 +5,7 @@ String.prototype.ends_with = (suffix) ->
|
|
5
5
|
|
6
6
|
class Wiselinks
|
7
7
|
constructor: (@$target = $('body'), options = {}) ->
|
8
|
-
#
|
8
|
+
# Check that JQuery is available
|
9
9
|
throw "Load JQuery to use Wiselinks" unless window.jQuery?
|
10
10
|
|
11
11
|
self = this
|
@@ -34,14 +34,16 @@ class Wiselinks
|
|
34
34
|
$(document).on(
|
35
35
|
"click", "a[data-push], a[data-replace]"
|
36
36
|
(event) ->
|
37
|
-
if (event.
|
37
|
+
if self._cross_origin_link(event.target) || self._non_standard_click(event)
|
38
38
|
return true;
|
39
|
-
|
39
|
+
|
40
40
|
self._process_link($(this))
|
41
41
|
|
42
42
|
event.preventDefault()
|
43
43
|
return false
|
44
44
|
)
|
45
|
+
|
46
|
+
@assets_digest = $("meta[name='assets-digest']").attr("content")
|
45
47
|
|
46
48
|
load: (url, target, render = 'template') ->
|
47
49
|
History.ready = true
|
@@ -62,12 +64,15 @@ class Wiselinks
|
|
62
64
|
url: url
|
63
65
|
headers:
|
64
66
|
'X-Render': render
|
65
|
-
success: (data, status, xhr) ->
|
66
|
-
|
67
|
-
|
68
|
-
|
67
|
+
success: (data, status, xhr) ->
|
68
|
+
if self._assets_changed(xhr.getResponseHeader('X-Assets-Digest'))
|
69
|
+
window.location.reload(true)
|
70
|
+
else
|
71
|
+
document.title = xhr.getResponseHeader('X-Title')
|
72
|
+
|
73
|
+
$target.html(data)
|
69
74
|
|
70
|
-
|
75
|
+
$(document).trigger('page:success', [data, status])
|
71
76
|
error: (xhr, status, error)->
|
72
77
|
$(document).trigger('page:error', [status, error])
|
73
78
|
dataType: "html"
|
@@ -115,4 +120,13 @@ class Wiselinks
|
|
115
120
|
|
116
121
|
self.load($link.attr("href"), $link.attr("data-target"), type)
|
117
122
|
|
123
|
+
_cross_origin_link: (link) ->
|
124
|
+
(location.protocol != link.protocol) || (location.host != link.host)
|
125
|
+
|
126
|
+
_non_standard_click: (event) ->
|
127
|
+
event.metaKey || event.ctrlKey || event.shiftKey || event.altKey
|
128
|
+
|
129
|
+
_assets_changed: (digest) ->
|
130
|
+
digest? && @assets_digest != digest
|
131
|
+
|
118
132
|
window.Wiselinks = Wiselinks
|
data/lib/wiselinks.rb
CHANGED
@@ -1,16 +1,24 @@
|
|
1
|
-
require 'wiselinks/headers'
|
2
1
|
require 'wiselinks/request'
|
2
|
+
require 'wiselinks/headers'
|
3
|
+
require 'wiselinks/helpers'
|
4
|
+
|
3
5
|
require 'wiselinks/logger'
|
4
6
|
|
7
|
+
require 'wiselinks/rails' if defined?(::Rails)
|
8
|
+
|
5
9
|
module Wiselinks
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
+
extend Logger
|
11
|
+
|
12
|
+
DEFAULTS = {
|
13
|
+
:assets_digest => nil,
|
14
|
+
:logger => nil
|
15
|
+
}
|
16
|
+
|
17
|
+
def self.options
|
18
|
+
@options ||= DEFAULTS.dup
|
19
|
+
end
|
10
20
|
|
11
|
-
|
12
|
-
|
13
|
-
ActionDispatch::Request.send :include, Request
|
14
|
-
end
|
21
|
+
def self.options=(value)
|
22
|
+
@options = value
|
15
23
|
end
|
16
24
|
end
|
data/lib/wiselinks/headers.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module Wiselinks
|
2
2
|
module Headers
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.helper_method :wiselinks_title
|
6
|
+
end
|
3
7
|
|
4
8
|
protected
|
5
9
|
|
@@ -8,17 +12,31 @@ module Wiselinks
|
|
8
12
|
end
|
9
13
|
|
10
14
|
def render(options = {}, *args, &block)
|
11
|
-
if self.request.wiselinks?
|
15
|
+
if self.request.wiselinks?
|
12
16
|
if self.request.wiselinks_partial?
|
17
|
+
Wiselinks.log("processing partial request")
|
13
18
|
options[:partial] ||= action_name
|
14
19
|
else
|
20
|
+
Wiselinks.log("processing template request")
|
15
21
|
options[:layout] = self.wiselinks_layout
|
16
22
|
end
|
23
|
+
|
24
|
+
if Wiselinks.options[:assets_digest].present?
|
25
|
+
Wiselinks.log("assets digest #{Wiselinks.options[:assets_digest]}")
|
26
|
+
|
27
|
+
self.headers['X-Assets-Digest'] = Wiselinks.options[:assets_digest]
|
28
|
+
end
|
17
29
|
end
|
18
30
|
|
19
31
|
super
|
20
32
|
end
|
21
33
|
|
34
|
+
def wiselinks_title(value)
|
35
|
+
Wiselinks.log("title: #{value}")
|
36
|
+
|
37
|
+
self.headers['X-Title'] = value if self.request.wiselinks?
|
38
|
+
end
|
39
|
+
|
22
40
|
def wiselinks_request?
|
23
41
|
Wiselinks::Logger.log "DEPRECATION WARNING: Method `wiselinks_request?` is deprecated. Please use `request.wiselinks?` instead."
|
24
42
|
|
data/lib/wiselinks/logger.rb
CHANGED
@@ -1,17 +1,11 @@
|
|
1
1
|
module Wiselinks
|
2
|
-
module Logger
|
3
|
-
|
4
|
-
|
5
|
-
def self.logger
|
6
|
-
@logger ||= ::Logger.new(STDOUT)
|
7
|
-
end
|
8
|
-
|
9
|
-
def logger=(logger)
|
10
|
-
@logger = logger
|
2
|
+
module Logger
|
3
|
+
def logger
|
4
|
+
@logger ||= Wiselinks.options[:logger] || ::Logger.new(STDOUT)
|
11
5
|
end
|
12
6
|
|
13
7
|
def log(message)
|
14
|
-
logger.info("[wiselinks] #{message}")
|
8
|
+
self.logger.info("[wiselinks] #{message}")
|
15
9
|
end
|
16
10
|
end
|
17
11
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Wiselinks
|
2
|
+
module Rails
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
initializer 'wiselinks.register_logger' do
|
5
|
+
Wiselinks.options[:logger] = ::Rails.logger
|
6
|
+
end
|
7
|
+
|
8
|
+
initializer "wiselinks.register_extensions" do
|
9
|
+
ActionDispatch::Request.send :include, Request
|
10
|
+
ActionController::Base.send :include, Headers
|
11
|
+
ActionView::Base.send :include, Helpers
|
12
|
+
end
|
13
|
+
|
14
|
+
initializer "wiselinks.register_assets_digest" do
|
15
|
+
if ::Rails.application.config.assets.digest && ::Rails.application.config.assets.digests.present?
|
16
|
+
Wiselinks.options[:assets_digest] ||= Digest::MD5.hexdigest(::Rails.application.config.assets.digests.values.join)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ApplicationControllerTest < ActionController::TestCase
|
4
|
+
tests ApplicationController
|
5
|
+
|
6
|
+
context "Request" do
|
7
|
+
setup do
|
8
|
+
@request = @controller.request
|
9
|
+
end
|
10
|
+
|
11
|
+
should "respond to #wiselinks?" do
|
12
|
+
assert_respond_to @request, :wiselinks?
|
13
|
+
end
|
14
|
+
|
15
|
+
should "respond to #wiselinks_partial?" do
|
16
|
+
assert_respond_to @request, :wiselinks_partial?
|
17
|
+
end
|
18
|
+
|
19
|
+
should "respond to #wiselinks_template?" do
|
20
|
+
assert_respond_to @request, :wiselinks_template?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "Layout" do
|
25
|
+
should "respond to #wiselinks_layout" do
|
26
|
+
assert_respond_to @controller, :wiselinks_layout
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "Title" do
|
31
|
+
should "respond to #wiselinks_title" do
|
32
|
+
assert_respond_to @controller, :wiselinks_title
|
33
|
+
end
|
34
|
+
end
|
35
|
+
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.
|