-
Notifications
You must be signed in to change notification settings - Fork 0
/
todos-scaffold.rb
235 lines (181 loc) · 5.06 KB
/
todos-scaffold.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
ENV['SECRET_KEY_BASE'] = 'my_secret_key_base'
ENV['DATABASE_URL'] = "sqlite3:///#{Dir.pwd}/todos-scaffold.sqlite"
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem 'uni_rails', '~> 0.5.0'
gem 'sqlite3', '~> 1.7'
gem 'jbuilder' # jbuilder allows the .jbuilder extension for views
end
require 'uni_rails'
require 'sqlite3'
ActiveRecord::Base.establish_connection
ActiveRecord::Schema.define do
create_table :todos, force: :cascade do |t|
t.string :name
t.datetime :completed_at
t.timestamps
end
end
UniRails.routes do
root 'todos#index'
resources :todos
end
# MODELS
class Todo < ActiveRecord::Base
validates :name, presence: true
def complete(at: Time.zone.now)
update(completed_at: at)
end
end
# CONTROLLERS
class ApplicationController < ActionController::Base
end
class TodosController < ApplicationController
before_action :set_todo, only: %i[ show edit update destroy ]
# GET /todos or /todos.json
def index
@todos = Todo.all
end
# GET /todos/1 or /todos/1.json
def show
end
# GET /todos/new
def new
@todo = Todo.new
end
# GET /todos/1/edit
def edit
end
# POST /todos or /todos.json
def create
@todo = Todo.new(todo_params)
respond_to do |format|
if @todo.save
format.html { redirect_to todo_url(@todo), notice: "Todo was successfully created." }
format.json { render :show, status: :created, location: @todo }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /todos/1 or /todos/1.json
def update
respond_to do |format|
if @todo.update(todo_params)
format.html { redirect_to todo_url(@todo), notice: "Todo was successfully updated." }
format.json { render :show, status: :ok, location: @todo }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todos/1 or /todos/1.json
def destroy
@todo.destroy!
respond_to do |format|
format.html { redirect_to todos_url, notice: "Todo was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todo
@todo = Todo.find(params[:id])
end
# Only allow a list of trusted parameters through.
def todo_params
params.require(:todo).permit(:name, :completed_at)
end
end
# VIEWS - HTML
UniRails.register_view "todos/_form.html.erb", <<~HTML
<%= form_with(model: todo) do |form| %>
<% if todo.errors.any? %>
<div style="color: red">
<h2><%= pluralize(todo.errors.count, "error") %> prohibited this todo from being saved:</h2>
<ul>
<% todo.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>
<div>
<%= form.label :completed_at, style: "display: block" %>
<%= form.datetime_field :completed_at %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
HTML
UniRails.register_view "todos/_todo.html.erb", <<~HTML
<div id="<%= dom_id todo %>">
<p>
<strong>Name:</strong>
<%= todo.name %>
</p>
<p>
<strong>Completed at:</strong>
<%= todo.completed_at %>
</p>
</div>
HTML
UniRails.register_view "todos/edit.html.erb", <<~HTML
<h1>Editing todo</h1>
<%= render "form", todo: @todo %>
<br>
<div>
<%= link_to "Show this todo", @todo %> |
<%= link_to "Back to todos", todos_path %>
</div>
HTML
UniRails.register_view "todos/index.html.erb", <<~HTML
<p style="color: green"><%= notice %></p>
<h1>Todos</h1>
<div id="todos">
<% @todos.each do |todo| %>
<%= render todo %>
<p>
<%= link_to "Show this todo", todo %>
</p>
<% end %>
</div>
<%= link_to "New todo", new_todo_path %>
HTML
UniRails.register_view "todos/new.html.erb", <<~HTML
<h1>New todo</h1>
<%= render "form", todo: @todo %>
<br>
<div>
<%= link_to "Back to todos", todos_path %>
</div>
HTML
UniRails.register_view "todos/show.html.erb", <<~HTML
<p style="color: green"><%= notice %></p>
<%= render @todo %>
<div>
<%= link_to "Edit this todo", edit_todo_path(@todo) %> |
<%= link_to "Back to todos", todos_path %>
<%= button_to "Destroy this todo", @todo, method: :delete %>
</div>
HTML
# VIEWS - JSON
UniRails.register_view "todos/_todo.json.jbuilder", <<~RUBY
json.extract! todo, :id, :name, :completed_at, :created_at, :updated_at
json.url todo_url(todo, format: :json)
RUBY
UniRails.register_view "todos/index.json.jbuilder", <<~RUBY
json.array! @todos, partial: "todos/todo", as: :todo
RUBY
UniRails.register_view "todos/show.json.jbuilder", <<~RUBY
json.partial! "todos/todo", todo: @todo
RUBY
UniRails.run(Port: 3000)