rails_lite 0.1.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.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +8 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/railslite +4 -0
- data/lib/.DS_Store +0 -0
- data/lib/rails_lite.rb +6 -0
- data/lib/rails_lite/.DS_Store +0 -0
- data/lib/rails_lite/cli.rb +65 -0
- data/lib/rails_lite/version.rb +3 -0
- data/lib/scaffold/.DS_Store +0 -0
- data/lib/scaffold/Gemfile +12 -0
- data/lib/scaffold/app/controllers/albums_controller.rb +55 -0
- data/lib/scaffold/app/controllers/application_controller.rb +45 -0
- data/lib/scaffold/app/controllers/bands_controller.rb +62 -0
- data/lib/scaffold/app/controllers/notes_controller.rb +26 -0
- data/lib/scaffold/app/controllers/sessions_controller.rb +34 -0
- data/lib/scaffold/app/controllers/tracks_controller.rb +55 -0
- data/lib/scaffold/app/controllers/users_controller.rb +41 -0
- data/lib/scaffold/app/models/album.rb +14 -0
- data/lib/scaffold/app/models/application_model.rb +5 -0
- data/lib/scaffold/app/models/band.rb +7 -0
- data/lib/scaffold/app/models/note.rb +7 -0
- data/lib/scaffold/app/models/track.rb +20 -0
- data/lib/scaffold/app/models/user.rb +42 -0
- data/lib/scaffold/app/views/albums_controller/edit.html.erb +51 -0
- data/lib/scaffold/app/views/albums_controller/new.html.erb +50 -0
- data/lib/scaffold/app/views/albums_controller/show.html.erb +25 -0
- data/lib/scaffold/app/views/application.html.erb +41 -0
- data/lib/scaffold/app/views/bands_controller/edit.html.erb +16 -0
- data/lib/scaffold/app/views/bands_controller/index.html.erb +12 -0
- data/lib/scaffold/app/views/bands_controller/json.json.jbuilder +3 -0
- data/lib/scaffold/app/views/bands_controller/new.html.erb +15 -0
- data/lib/scaffold/app/views/bands_controller/show.html.erb +27 -0
- data/lib/scaffold/app/views/sessions_controller/new.html.erb +22 -0
- data/lib/scaffold/app/views/tracks_controller/edit.html.erb +60 -0
- data/lib/scaffold/app/views/tracks_controller/new.html.erb +59 -0
- data/lib/scaffold/app/views/tracks_controller/show.html.erb +56 -0
- data/lib/scaffold/app/views/users_controller/new.html.erb +22 -0
- data/lib/scaffold/app/views/users_controller/show.html.erb +4 -0
- data/lib/scaffold/bin/pry +13 -0
- data/lib/scaffold/bin/routes +10 -0
- data/lib/scaffold/bin/server +32 -0
- data/lib/scaffold/config/routes.rb +26 -0
- data/lib/scaffold/db/database.db +0 -0
- data/lib/scaffold/db/database.sql +48 -0
- data/lib/scaffold/lib/.DS_Store +0 -0
- data/lib/scaffold/lib/controller/controller_base.rb +183 -0
- data/lib/scaffold/lib/controller/controller_callbacks.rb +17 -0
- data/lib/scaffold/lib/controller/cookies/flash.rb +33 -0
- data/lib/scaffold/lib/controller/cookies/flash_now.rb +15 -0
- data/lib/scaffold/lib/controller/cookies/session.rb +29 -0
- data/lib/scaffold/lib/controller/strong_params.rb +40 -0
- data/lib/scaffold/lib/middleware/file_server.rb +42 -0
- data/lib/scaffold/lib/middleware/show_exceptions.rb +62 -0
- data/lib/scaffold/lib/middleware/static.rb +31 -0
- data/lib/scaffold/lib/middleware/templates/rescue.html.erb +49 -0
- data/lib/scaffold/lib/model/associations/assoc_options.rb +16 -0
- data/lib/scaffold/lib/model/associations/associatable.rb +120 -0
- data/lib/scaffold/lib/model/associations/belongs_to_options.rb +18 -0
- data/lib/scaffold/lib/model/associations/has_many_options.rb +17 -0
- data/lib/scaffold/lib/model/db_connection.rb +59 -0
- data/lib/scaffold/lib/model/model_base.rb +183 -0
- data/lib/scaffold/lib/model/model_callbacks.rb +46 -0
- data/lib/scaffold/lib/model/relations/relation.rb +151 -0
- data/lib/scaffold/lib/model/relations/searchable.rb +27 -0
- data/lib/scaffold/lib/model/validations/validations.rb +31 -0
- data/lib/scaffold/lib/model/validations/validator.rb +81 -0
- data/lib/scaffold/lib/router/route.rb +24 -0
- data/lib/scaffold/lib/router/router.rb +192 -0
- data/lib/scaffold/lib/utils/url_helpers.rb +96 -0
- data/lib/scaffold/public/main.css +165 -0
- data/rails_lite.gemspec +41 -0
- metadata +168 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
<h1 class='page-header'>New Band!</h1>
|
2
|
+
|
3
|
+
<form class='form' action="<%= bands_url %>" method="POST">
|
4
|
+
<input
|
5
|
+
type="hidden"
|
6
|
+
name="authenticity_token"
|
7
|
+
value="<%= form_authenticity_token %>">
|
8
|
+
|
9
|
+
<p>
|
10
|
+
<label for='band-name'>Name</label>
|
11
|
+
<input id='band-name' type="text" name="band[name]" value="<%= @band.name %>">
|
12
|
+
</p>
|
13
|
+
|
14
|
+
<input type="submit">
|
15
|
+
</form>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
<h1 class='page-header'><%= @band.name %></h1>
|
2
|
+
|
3
|
+
<h4 class='sub-header'>Albums</h4>
|
4
|
+
|
5
|
+
<% if @band.albums.length > 0 %>
|
6
|
+
<ul class='main-list'>
|
7
|
+
<% @band.albums.each do |album| %>
|
8
|
+
<li><a href="<%= album_url(album) %>"><p><%= album.name %></p></a></li>
|
9
|
+
<% end %>
|
10
|
+
</ul>
|
11
|
+
<% else %>
|
12
|
+
<p>This band has no albums... yet!</p>
|
13
|
+
<% end %>
|
14
|
+
|
15
|
+
<h4 class='sub-header'>Links</h4>
|
16
|
+
<ul class='page-links'>
|
17
|
+
<li><a class='button' href="<%= new_band_albums_url(@band) %>">New album</a></li>
|
18
|
+
<li><a class='button' href="<%= edit_band_url(@band) %>">Edit Band</a></li>
|
19
|
+
<li>
|
20
|
+
<form action="<%= band_url(@band) %>" method="POST">
|
21
|
+
<input type="hidden" name="_method" value="DELETE">
|
22
|
+
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>" >
|
23
|
+
<input type="submit" value="Destroy Band">
|
24
|
+
</form>
|
25
|
+
</li>
|
26
|
+
<li><a class='button' href="<%= bands_url %>">All bands</a></li>
|
27
|
+
</ul>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<h2 class='page-header'>Sign-in</h2>
|
2
|
+
|
3
|
+
<a class='button' href=<%= new_users_url %> >Go to Sign-up!</a>
|
4
|
+
|
5
|
+
<form class='form' action="<%= sessions_url %>" method="post">
|
6
|
+
<input
|
7
|
+
type="hidden"
|
8
|
+
name="authenticity_token"
|
9
|
+
value="<%= form_authenticity_token %>">
|
10
|
+
|
11
|
+
<p>
|
12
|
+
<label for='new-user-email'>Username</label>
|
13
|
+
<input id='new-user-email' type="text" name="user[username]">
|
14
|
+
</p>
|
15
|
+
|
16
|
+
<p>
|
17
|
+
<label for='new-user-password'>Password</label>
|
18
|
+
<input id='new-user-password' type="password" name="user[password]">
|
19
|
+
</p>
|
20
|
+
|
21
|
+
<input type="submit">
|
22
|
+
</form>
|
@@ -0,0 +1,60 @@
|
|
1
|
+
<h1 class='page-header'>Edit <%= @track.name %></h1>
|
2
|
+
|
3
|
+
<form class='form' action="<%= track_url(@track) %>" method="POST" %>
|
4
|
+
<input type="hidden" name="_method" value="PATCH">
|
5
|
+
<input
|
6
|
+
type="hidden"
|
7
|
+
name="authenticity_token"
|
8
|
+
value="<%= form_authenticity_token %>">
|
9
|
+
|
10
|
+
<p>
|
11
|
+
<label for='track-name'>Name</label>
|
12
|
+
<input id='track-name' type="text" name="track[name]" value="<%= @track.name %>">
|
13
|
+
</p>
|
14
|
+
|
15
|
+
<p>
|
16
|
+
<label for='track-album'>Album</label>
|
17
|
+
<select id='track-album' name="track[album_id]">
|
18
|
+
<% Album.all.each do |album| %>
|
19
|
+
<option
|
20
|
+
<%= "selected" if @track.album_id == album.id %>
|
21
|
+
value="<%= album.id %>">
|
22
|
+
<%= album.name %>
|
23
|
+
</option>
|
24
|
+
<% end %>
|
25
|
+
</select>
|
26
|
+
</p>
|
27
|
+
|
28
|
+
<p>
|
29
|
+
<label>Ord
|
30
|
+
<input type="number" name="track[ord]" value="<%= @track.ord %>">
|
31
|
+
</label>
|
32
|
+
</p>
|
33
|
+
|
34
|
+
<p>
|
35
|
+
<label for="track_bonus_f">Standard</label>
|
36
|
+
<input
|
37
|
+
type="radio"
|
38
|
+
name="track[bonus]"
|
39
|
+
value="f"
|
40
|
+
id="track_bonus_f"
|
41
|
+
<%= "checked" if !@track.bonus %>>
|
42
|
+
<br>
|
43
|
+
<label for="track_bonus_t">Bonus</label>
|
44
|
+
<input
|
45
|
+
type="radio"
|
46
|
+
name="track[bonus]"
|
47
|
+
value="t"
|
48
|
+
id="track_bonus_t"
|
49
|
+
<%= "checked" if @track.bonus %>>
|
50
|
+
</p>
|
51
|
+
|
52
|
+
<p>
|
53
|
+
<label for="track_lyrics">Lyrics</label><br>
|
54
|
+
<textarea id="track_lyrics"
|
55
|
+
rows="10" cols="40"
|
56
|
+
name="track[lyrics]"><%= @track.lyrics %></textarea>
|
57
|
+
</p>
|
58
|
+
|
59
|
+
<input type="submit">
|
60
|
+
</form>
|
@@ -0,0 +1,59 @@
|
|
1
|
+
<h1 class='page-header'>New <%= @album.name %> Track</h1>
|
2
|
+
|
3
|
+
<form class='form' action="<%= tracks_url %>" method="POST" %>
|
4
|
+
<input
|
5
|
+
type="hidden"
|
6
|
+
name="authenticity_token"
|
7
|
+
value="<%= form_authenticity_token %>">
|
8
|
+
|
9
|
+
<p>
|
10
|
+
<label for='track-name'>Name</label>
|
11
|
+
<input id='track-name' type="text" name="track[name]" value="<%= @track.name %>">
|
12
|
+
</p>
|
13
|
+
|
14
|
+
<p>
|
15
|
+
<label for='track-album'>Album</label>
|
16
|
+
<select id='track-album' name="track[album_id]">
|
17
|
+
<% Album.all.each do |album| %>
|
18
|
+
<option
|
19
|
+
<%= "selected" if @track.album_id == album.id %>
|
20
|
+
value="<%= album.id %>">
|
21
|
+
<%= album.name %>
|
22
|
+
</option>
|
23
|
+
<% end %>
|
24
|
+
</select>
|
25
|
+
</p>
|
26
|
+
|
27
|
+
<p>
|
28
|
+
<label>Ord
|
29
|
+
<input type="number" name="track[ord]" value="<%= @track.ord %>">
|
30
|
+
</label>
|
31
|
+
</p>
|
32
|
+
|
33
|
+
<p>
|
34
|
+
<label for="track_bonus_f">Standard</label>
|
35
|
+
<input
|
36
|
+
type="radio"
|
37
|
+
name="track[bonus]"
|
38
|
+
value="f"
|
39
|
+
id="track_bonus_f"
|
40
|
+
<%= "checked" if !@track.bonus %>>
|
41
|
+
<br>
|
42
|
+
<label for="track_bonus_t">Bonus</label>
|
43
|
+
<input
|
44
|
+
type="radio"
|
45
|
+
name="track[bonus]"
|
46
|
+
value="t"
|
47
|
+
id="track_bonus_t"
|
48
|
+
<%= "checked" if @track.bonus %>>
|
49
|
+
</p>
|
50
|
+
|
51
|
+
<p>
|
52
|
+
<label for="track_lyrics">Lyrics</label><br>
|
53
|
+
<textarea id="track_lyrics"
|
54
|
+
rows="10" cols="40"
|
55
|
+
name="track[lyrics]"><%= @track.lyrics %></textarea>
|
56
|
+
</p>
|
57
|
+
|
58
|
+
<input type="submit">
|
59
|
+
</form>
|
@@ -0,0 +1,56 @@
|
|
1
|
+
<h1 class='page-header'><%= @track.name %></h1>
|
2
|
+
|
3
|
+
<%= @track.lyrics %>
|
4
|
+
|
5
|
+
<h4 class='sub-header'>New Note</h4>
|
6
|
+
|
7
|
+
<form class='form' action="<%= notes_url %>" method="POST">
|
8
|
+
<input
|
9
|
+
type="hidden"
|
10
|
+
name="authenticity_token"
|
11
|
+
value="<%= form_authenticity_token %>">
|
12
|
+
|
13
|
+
<input type="hidden" name="note[track_id]" value="<%= @track.id %>">
|
14
|
+
|
15
|
+
<p>
|
16
|
+
<label for="note_content">Content</label>
|
17
|
+
<textarea id="note_content"
|
18
|
+
rows="7" cols="30"
|
19
|
+
name="note[content]"></textarea>
|
20
|
+
</p>
|
21
|
+
|
22
|
+
<input type="submit">
|
23
|
+
</form>
|
24
|
+
|
25
|
+
|
26
|
+
<h4 class='sub-header'>Notes</h4>
|
27
|
+
|
28
|
+
<% @track.notes.each do |note| %>
|
29
|
+
<div class='note-item'>
|
30
|
+
<p><%= note.content %></p>
|
31
|
+
<div class='note-item-info'>
|
32
|
+
<p>Authored by: <%= note.user.username %></p>
|
33
|
+
|
34
|
+
<% if note.user_id == current_user.id %>
|
35
|
+
<form action="<%= note_url(note) %>" method="POST">
|
36
|
+
<input type="hidden" name="_method" value="DELETE">
|
37
|
+
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>" >
|
38
|
+
<input type="submit" value="Destroy Note">
|
39
|
+
</form>
|
40
|
+
<% end %>
|
41
|
+
</div>
|
42
|
+
</div>
|
43
|
+
<% end %>
|
44
|
+
|
45
|
+
<h4 class='sub-header'>Links</h4>
|
46
|
+
<ul class='page-links'>
|
47
|
+
<li><a class='button' href="<%= edit_track_url(@track) %>">Edit Track</a></li>
|
48
|
+
<li>
|
49
|
+
<form action="<%= track_url(@track) %>" method="POST">
|
50
|
+
<input type="hidden" name="_method" value="DELETE">
|
51
|
+
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>" >
|
52
|
+
<input type="submit" value="Destroy Track">
|
53
|
+
</form>
|
54
|
+
</li>
|
55
|
+
<li><a class='button' href="<%= album_url(@track.album) %>"><%= @track.album.name %></a></li>
|
56
|
+
</ul>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<h1 class='page-header'>New User</h1>
|
2
|
+
|
3
|
+
<a class='button' href=<%= new_sessions_url %> >Go to sign-in</a>
|
4
|
+
|
5
|
+
<form class='form' action="<%= users_url %>" method="post">
|
6
|
+
<input
|
7
|
+
type="hidden"
|
8
|
+
name="authenticity_token"
|
9
|
+
value="<%= form_authenticity_token %>">
|
10
|
+
|
11
|
+
<p>
|
12
|
+
<label for='new-user-email'>Username</label>
|
13
|
+
<input id='new-user-email' type="text" name="user[username]">
|
14
|
+
</p>
|
15
|
+
|
16
|
+
<p>
|
17
|
+
<label for='new-user-password'>Password</label>
|
18
|
+
<input id='new-user-password' type="password" name="user[password]">
|
19
|
+
</p>
|
20
|
+
|
21
|
+
<input type="submit" value="Submit">
|
22
|
+
</form>
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rack'
|
3
|
+
require 'byebug'
|
4
|
+
require_relative '../lib/router/router'
|
5
|
+
require_relative '../lib/middleware/show_exceptions'
|
6
|
+
require_relative '../lib/middleware/static'
|
7
|
+
require_relative '../config/routes'
|
8
|
+
require_relative '../app/controllers/application_controller'
|
9
|
+
require_relative '../app/models/application_model'
|
10
|
+
Dir[File.join(__dir__, '..', 'app', 'controllers', '*.rb')].each { |file| require file }
|
11
|
+
Dir[File.join(__dir__, '..', 'app', 'models', '*.rb')].each { |file| require file }
|
12
|
+
|
13
|
+
Pry.start(__FILE__)
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/router/router'
|
4
|
+
require_relative '../config/routes'
|
5
|
+
Dir[File.join(__dir__, '..', 'app', 'controllers', '*.rb')].each { |file| require file }
|
6
|
+
Dir[File.join(__dir__, '..', 'app', 'models', '*.rb')].each { |file| require file }
|
7
|
+
|
8
|
+
router = Router.new
|
9
|
+
create_routes(router)
|
10
|
+
router.display_routes
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rack'
|
3
|
+
require 'byebug'
|
4
|
+
require_relative '../lib/router/router'
|
5
|
+
require_relative '../lib/middleware/show_exceptions'
|
6
|
+
require_relative '../lib/middleware/static'
|
7
|
+
require_relative '../config/routes'
|
8
|
+
require_relative '../app/controllers/application_controller'
|
9
|
+
require_relative '../app/models/application_model'
|
10
|
+
Dir[File.join(__dir__, '..', 'app', 'controllers', '*.rb')].each { |file| require file }
|
11
|
+
Dir[File.join(__dir__, '..', 'app', 'models', '*.rb')].each { |file| require file }
|
12
|
+
|
13
|
+
router = Router.new
|
14
|
+
create_routes(router)
|
15
|
+
|
16
|
+
app = Proc.new do |env|
|
17
|
+
req = Rack::Request.new(env)
|
18
|
+
res = Rack::Response.new
|
19
|
+
router.run(req, res)
|
20
|
+
res.finish
|
21
|
+
end
|
22
|
+
|
23
|
+
app = Rack::Builder.new do
|
24
|
+
use Static
|
25
|
+
# use ShowExceptions
|
26
|
+
run app
|
27
|
+
end.to_app
|
28
|
+
|
29
|
+
Rack::Server.start(
|
30
|
+
app: app,
|
31
|
+
Port: 3000
|
32
|
+
)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
def create_routes(router)
|
2
|
+
router.draw do
|
3
|
+
|
4
|
+
root to: 'bands#index'
|
5
|
+
|
6
|
+
resource :sessions, only: [:new, :create, :destroy]
|
7
|
+
|
8
|
+
resources :users, only: [:show, :new, :create]
|
9
|
+
|
10
|
+
resources :bands do
|
11
|
+
collection do
|
12
|
+
get :json
|
13
|
+
end
|
14
|
+
resources :albums, only: [:new]
|
15
|
+
end
|
16
|
+
|
17
|
+
resources :albums, only: [:show, :create, :edit, :update, :destroy] do
|
18
|
+
resources :tracks, only: [:new]
|
19
|
+
end
|
20
|
+
|
21
|
+
resources :tracks, only: [:show, :create, :edit, :update, :destroy,]
|
22
|
+
|
23
|
+
resources :notes, only: [:create, :destroy]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
Binary file
|
@@ -0,0 +1,48 @@
|
|
1
|
+
CREATE TABLE albums (
|
2
|
+
id INTEGER PRIMARY KEY,
|
3
|
+
name VARCHAR(255) NOT NULL,
|
4
|
+
band_id INTEGER NOT NULL,
|
5
|
+
year INTEGER NOT NULL,
|
6
|
+
live BOOLEAN NOT NULL DEFAULT false,
|
7
|
+
|
8
|
+
FOREIGN KEY(band_id) REFERENCES bands(id)
|
9
|
+
);
|
10
|
+
|
11
|
+
CREATE TABLE bands (
|
12
|
+
id INTEGER PRIMARY KEY,
|
13
|
+
name VARCHAR(255) NOT NULL
|
14
|
+
);
|
15
|
+
|
16
|
+
CREATE TABLE notes (
|
17
|
+
id INTEGER PRIMARY KEY,
|
18
|
+
content TEXT NOT NULL,
|
19
|
+
track_id INTEGER NOT NULL,
|
20
|
+
user_id INTEGER NOT NULL,
|
21
|
+
|
22
|
+
FOREIGN KEY(track_id) REFERENCES tracks(id),
|
23
|
+
FOREIGN KEY(user_id) REFERENCES users(id)
|
24
|
+
);
|
25
|
+
|
26
|
+
CREATE TABLE tracks (
|
27
|
+
id INTEGER PRIMARY KEY,
|
28
|
+
name VARCHAR(255) NOT NULL,
|
29
|
+
album_id INTEGER NOT NULL,
|
30
|
+
ord INTEGER NOT NULL,
|
31
|
+
bonus BOOLEAN NOT NULL DEFAULT false,
|
32
|
+
lyrics TEXT NOT NULL,
|
33
|
+
|
34
|
+
FOREIGN KEY(album_id) REFERENCES albums(id)
|
35
|
+
);
|
36
|
+
|
37
|
+
CREATE TABLE users (
|
38
|
+
id INTEGER PRIMARY KEY,
|
39
|
+
username VARCHAR(255) NOT NULL,
|
40
|
+
password_digest VARCHAR(255),
|
41
|
+
session_token VARCHAR(255)
|
42
|
+
);
|
43
|
+
|
44
|
+
|
45
|
+
INSERT INTO
|
46
|
+
bands (id, name )
|
47
|
+
VALUES
|
48
|
+
(1, "the little a's"), (2, "THE BIG A's");
|
Binary file
|
@@ -0,0 +1,183 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'erb'
|
4
|
+
require 'active_support/inflector'
|
5
|
+
require 'json'
|
6
|
+
require 'jbuilder'
|
7
|
+
require_relative './cookies/session'
|
8
|
+
require_relative './cookies/flash'
|
9
|
+
require_relative './strong_params'
|
10
|
+
require_relative './controller_callbacks'
|
11
|
+
require_relative '../utils/url_helpers'
|
12
|
+
|
13
|
+
class ControllerBase
|
14
|
+
extend ControllerCallbacks
|
15
|
+
include UrlHelpers
|
16
|
+
|
17
|
+
attr_reader :req, :res, :params
|
18
|
+
|
19
|
+
def self.protect_from_forgery
|
20
|
+
@@protect_from_forgery = true
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(req, res, route_params = {}, patterns)
|
24
|
+
@req = req
|
25
|
+
@res = res
|
26
|
+
@params = StrongParams.new_syms(req.params.merge(route_params))
|
27
|
+
@already_built_response = false
|
28
|
+
self.class.make_helpers(patterns)
|
29
|
+
end
|
30
|
+
|
31
|
+
def invoke_action(name)
|
32
|
+
if protect_from_forgery? && req.request_method != "GET"
|
33
|
+
check_authenticity_token
|
34
|
+
else
|
35
|
+
form_authenticity_token
|
36
|
+
end
|
37
|
+
|
38
|
+
self.send(name)
|
39
|
+
render name unless already_built_response?
|
40
|
+
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def form_authenticity_token
|
45
|
+
@form_authenticity_token ||= SecureRandom::urlsafe_base64
|
46
|
+
cookie = { path: '/', value: @form_authenticity_token }
|
47
|
+
res.set_cookie("#{@form_authenticity_token[0..5]}authenticity_token", cookie)
|
48
|
+
@form_authenticity_token
|
49
|
+
end
|
50
|
+
|
51
|
+
def link_to(name, path)
|
52
|
+
"<a href=\"#{path}\">#{name}</a>"
|
53
|
+
end
|
54
|
+
|
55
|
+
def root_url
|
56
|
+
'/'
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
|
61
|
+
def redirect_to(url)
|
62
|
+
prepare_render_or_redirect
|
63
|
+
|
64
|
+
res.status = 302
|
65
|
+
res['Location'] = url
|
66
|
+
|
67
|
+
nil
|
68
|
+
end
|
69
|
+
|
70
|
+
def render(options)
|
71
|
+
if options.is_a?(Symbol)
|
72
|
+
if File.exist?(html_view_path(options))
|
73
|
+
render_template(options)
|
74
|
+
else
|
75
|
+
render_json_template(options)
|
76
|
+
end
|
77
|
+
else
|
78
|
+
render_json(options[:json])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def session
|
83
|
+
@session ||= Session.new(req)
|
84
|
+
end
|
85
|
+
|
86
|
+
def flash
|
87
|
+
@flash ||= Flash.new(req)
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def render_json(obj)
|
93
|
+
if obj.is_a?(Array)
|
94
|
+
content = Jbuilder.encode do |json|
|
95
|
+
json.array! obj
|
96
|
+
end
|
97
|
+
else
|
98
|
+
|
99
|
+
content = obj.attributes.to_json
|
100
|
+
end
|
101
|
+
|
102
|
+
render_content(content, 'application/json')
|
103
|
+
end
|
104
|
+
|
105
|
+
def render_json_template(template_name)
|
106
|
+
path = json_view_path(template_name)
|
107
|
+
file_content = "<%= #{File.read(path)} %>"
|
108
|
+
content = ERB.new(file_content).result(binding)
|
109
|
+
|
110
|
+
render_content(content, 'application/json')
|
111
|
+
end
|
112
|
+
|
113
|
+
def render_template(template_name)
|
114
|
+
path = html_view_path(template_name)
|
115
|
+
content = ERB.new(File.read(path)).result(binding)
|
116
|
+
app_content = build_content { content }
|
117
|
+
|
118
|
+
render_content(app_content, 'text/html')
|
119
|
+
end
|
120
|
+
|
121
|
+
def already_built_response?
|
122
|
+
@already_built_response
|
123
|
+
end
|
124
|
+
|
125
|
+
def render_content(content, content_type)
|
126
|
+
prepare_render_or_redirect
|
127
|
+
|
128
|
+
res['Content-Type'] = content_type
|
129
|
+
res.write(content)
|
130
|
+
|
131
|
+
nil
|
132
|
+
end
|
133
|
+
|
134
|
+
def prepare_render_or_redirect
|
135
|
+
raise "double render error" if already_built_response?
|
136
|
+
@already_built_response = true
|
137
|
+
session.store_session(@res)
|
138
|
+
flash.store_flash(@res)
|
139
|
+
end
|
140
|
+
|
141
|
+
def build_content(&prc)
|
142
|
+
directory = File.dirname(__FILE__)
|
143
|
+
path = File.join(
|
144
|
+
directory, '..', '..',
|
145
|
+
'app', 'views', "application.html.erb"
|
146
|
+
)
|
147
|
+
|
148
|
+
app_content = ERB.new(File.read(path)).result(binding)
|
149
|
+
end
|
150
|
+
|
151
|
+
def check_authenticity_token
|
152
|
+
param_token = params['authenticity_token']
|
153
|
+
cookie = req.cookies["#{param_token[0..5]}authenticity_token"]
|
154
|
+
unless param_token && cookie == param_token
|
155
|
+
raise 'Invalid authenticity token'
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def protect_from_forgery?
|
160
|
+
@@protect_from_forgery ||= false
|
161
|
+
end
|
162
|
+
|
163
|
+
def json_view_path(name)
|
164
|
+
directory = File.dirname(__FILE__)
|
165
|
+
controller_name = self.class.to_s.underscore
|
166
|
+
File.join(
|
167
|
+
directory, "..", '..',
|
168
|
+
'app', 'views', controller_name,
|
169
|
+
"#{name}.json.jbuilder"
|
170
|
+
)
|
171
|
+
end
|
172
|
+
|
173
|
+
def html_view_path(name)
|
174
|
+
directory = File.dirname(__FILE__)
|
175
|
+
controller_name = self.class.to_s.underscore
|
176
|
+
File.join(
|
177
|
+
directory, "..", '..',
|
178
|
+
'app', 'views', controller_name,
|
179
|
+
"#{name}.html.erb"
|
180
|
+
)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|