Skip to content

Commit c3e4e36

Browse files
authored
Merge pull request #1 from andreobrown/add-order
Add Order
2 parents d04d7fb + c68d322 commit c3e4e36

File tree

22 files changed

+430
-2
lines changed

22 files changed

+430
-2
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,31 @@ This project is a Rails 5.2 project created with the following command:
1212

1313
`rails new --force --database=postgresql rails_web_and_mobile`
1414

15-
I choose Rails 5.2 since that is the version that I got this approach working in, and I'm trying to avoid any surprises.
15+
I choose Rails 5.2 since that is the version that I got this approach working in, and I'm trying to avoid any surprises.
16+
17+
## Project Development
18+
19+
This section will outline the steps taken to get from a bare rails project to one that support authentication for a server rendered web frontend alongside token based authentication for a native mobile app. I made each major change on a separate branch and then merged the branch into master. I also attempted to keep each change needed to make progress isolated in a single commit.
20+
21+
The project uses [scaffolding](https://guides.rubyonrails.org/command_line.html#rails-generate).
22+
23+
The basic idea for this project is that we are building a system for a small Corner Shop where Customers can submit Orders which will be viewed and fulfilled by Shopkeepers.
24+
25+
### Adding Orders
26+
27+
All the plumbing for Orders were generated using scaffolding:
28+
29+
1. Create Orders using scaffold:
30+
31+
`rails generate scaffold Order item:string quantity:integer status:string`
32+
33+
2. Create database:
34+
35+
`rails db:create`
36+
37+
3. Run migrations
38+
39+
`rails db:migrate`
40+
41+
4. Added dropdown list for Order `status`. Status is implemented as an [enum](https://api.rubyonrails.org/v5.2.3/classes/ActiveRecord/Enum.html) with [string values](https://sipsandbits.com/2018/04/30/using-database-native-enums-with-rails/) and dropdown list is populated using the [enum attriubutes](https://stackoverflow.com/a/23686698).
42+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/

app/assets/stylesheets/orders.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Orders controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
body {
2+
background-color: #fff;
3+
color: #333;
4+
margin: 33px;
5+
font-family: verdana, arial, helvetica, sans-serif;
6+
font-size: 13px;
7+
line-height: 18px;
8+
}
9+
10+
p, ol, ul, td {
11+
font-family: verdana, arial, helvetica, sans-serif;
12+
font-size: 13px;
13+
line-height: 18px;
14+
}
15+
16+
pre {
17+
background-color: #eee;
18+
padding: 10px;
19+
font-size: 11px;
20+
}
21+
22+
a {
23+
color: #000;
24+
25+
&:visited {
26+
color: #666;
27+
}
28+
29+
&:hover {
30+
color: #fff;
31+
background-color: #000;
32+
}
33+
}
34+
35+
th {
36+
padding-bottom: 5px;
37+
}
38+
39+
td {
40+
padding: 0 5px 7px;
41+
}
42+
43+
div {
44+
&.field, &.actions {
45+
margin-bottom: 10px;
46+
}
47+
}
48+
49+
#notice {
50+
color: green;
51+
}
52+
53+
.field_with_errors {
54+
padding: 2px;
55+
background-color: red;
56+
display: table;
57+
}
58+
59+
#error_explanation {
60+
width: 450px;
61+
border: 2px solid red;
62+
padding: 7px 7px 0;
63+
margin-bottom: 20px;
64+
background-color: #f0f0f0;
65+
66+
h2 {
67+
text-align: left;
68+
font-weight: bold;
69+
padding: 5px 5px 5px 15px;
70+
font-size: 12px;
71+
margin: -7px -7px 0;
72+
background-color: #c00;
73+
color: #fff;
74+
}
75+
76+
ul li {
77+
font-size: 12px;
78+
list-style: square;
79+
}
80+
}
81+
82+
label {
83+
display: block;
84+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class OrdersController < ApplicationController
2+
before_action :set_order, only: [:show, :edit, :update, :destroy]
3+
4+
# GET /orders
5+
# GET /orders.json
6+
def index
7+
@orders = Order.all
8+
end
9+
10+
# GET /orders/1
11+
# GET /orders/1.json
12+
def show
13+
end
14+
15+
# GET /orders/new
16+
def new
17+
@order = Order.new
18+
end
19+
20+
# GET /orders/1/edit
21+
def edit
22+
end
23+
24+
# POST /orders
25+
# POST /orders.json
26+
def create
27+
@order = Order.new(order_params)
28+
29+
respond_to do |format|
30+
if @order.save
31+
format.html { redirect_to @order, notice: 'Order was successfully created.' }
32+
format.json { render :show, status: :created, location: @order }
33+
else
34+
format.html { render :new }
35+
format.json { render json: @order.errors, status: :unprocessable_entity }
36+
end
37+
end
38+
end
39+
40+
# PATCH/PUT /orders/1
41+
# PATCH/PUT /orders/1.json
42+
def update
43+
respond_to do |format|
44+
if @order.update(order_params)
45+
format.html { redirect_to @order, notice: 'Order was successfully updated.' }
46+
format.json { render :show, status: :ok, location: @order }
47+
else
48+
format.html { render :edit }
49+
format.json { render json: @order.errors, status: :unprocessable_entity }
50+
end
51+
end
52+
end
53+
54+
# DELETE /orders/1
55+
# DELETE /orders/1.json
56+
def destroy
57+
@order.destroy
58+
respond_to do |format|
59+
format.html { redirect_to orders_url, notice: 'Order was successfully destroyed.' }
60+
format.json { head :no_content }
61+
end
62+
end
63+
64+
private
65+
# Use callbacks to share common setup or constraints between actions.
66+
def set_order
67+
@order = Order.find(params[:id])
68+
end
69+
70+
# Only allow a list of trusted parameters through.
71+
def order_params
72+
params.require(:order).permit(:item, :quantity, :status)
73+
end
74+
end

app/helpers/orders_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module OrdersHelper
2+
end

app/models/order.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Order < ApplicationRecord
2+
enum status: { submitted: "submitted", ready: "ready", filled: "filled" }
3+
end

app/views/orders/_form.html.erb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<%= form_with(model: order, local: true) do |form| %>
2+
<% if order.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(order.errors.count, "error") %> prohibited this order from being saved:</h2>
5+
6+
<ul>
7+
<% order.errors.full_messages.each do |message| %>
8+
<li><%= message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="field">
15+
<%= form.label :item %>
16+
<%= form.text_field :item %>
17+
</div>
18+
19+
<div class="field">
20+
<%= form.label :quantity %>
21+
<%= form.number_field :quantity %>
22+
</div>
23+
24+
<div class="field">
25+
<%= form.label :status %>
26+
<%= form.select :status, Order.statuses.keys.map { |w| [w.humanize, w] } %>
27+
</div>
28+
29+
<div class="actions">
30+
<%= form.submit %>
31+
</div>
32+
<% end %>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
json.extract! order, :id, :item, :quantity, :status, :created_at, :updated_at
2+
json.url order_url(order, format: :json)

app/views/orders/edit.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Editing Order</h1>
2+
3+
<%= render 'form', order: @order %>
4+
5+
<%= link_to 'Show', @order %> |
6+
<%= link_to 'Back', orders_path %>

0 commit comments

Comments
 (0)