Skip to content

Commit b4bc17f

Browse files
committed
Scaffold artist and run migrations
1 parent db10e31 commit b4bc17f

22 files changed

+770
-6
lines changed

app/controllers/art_controller.rb

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class ArtController < ApplicationController
2+
before_action :set_art, only: %i[ show edit update destroy ]
3+
4+
# GET /art
5+
def index
6+
@art = Art.all
7+
end
8+
9+
# GET /art/1
10+
def show
11+
end
12+
13+
# GET /art/new
14+
def new
15+
@art = Art.new
16+
end
17+
18+
# GET /art/1/edit
19+
def edit
20+
end
21+
22+
# POST /art
23+
def create
24+
@art = Art.new(art_params)
25+
26+
if @art.save
27+
redirect_to @art, notice: "Art was successfully created."
28+
else
29+
render :new, status: :unprocessable_entity
30+
end
31+
end
32+
33+
# PATCH/PUT /art/1
34+
def update
35+
if @art.update(art_params)
36+
redirect_to @art, notice: "Art was successfully updated.", status: :see_other
37+
else
38+
render :edit, status: :unprocessable_entity
39+
end
40+
end
41+
42+
# DELETE /art/1
43+
def destroy
44+
@art.destroy!
45+
redirect_to art_index_url, notice: "Art was successfully destroyed.", status: :see_other
46+
end
47+
48+
private
49+
# Use callbacks to share common setup or constraints between actions.
50+
def set_art
51+
@art = Art.find(params[:id])
52+
end
53+
54+
# Only allow a list of trusted parameters through.
55+
def art_params
56+
params.require(:art).permit(:title, :description, :artist_name, :last_editor_id, :art_created_date, :created_location, :url, :image, :classification, :is_published, :location_id, :artist_id)
57+
end
58+
end

app/helpers/art_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ArtHelper
2+
end

app/models/art.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Art < ApplicationRecord
2+
belongs_to :last_editor
3+
belongs_to :location
4+
belongs_to :artist
5+
has_rich_text :description
6+
has_one_attached :image
7+
end

app/views/art/_art.html.erb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<div id="<%= dom_id art %>">
2+
<p>
3+
<strong>Title:</strong>
4+
<%= art.title %>
5+
</p>
6+
7+
<p>
8+
<strong>Description:</strong>
9+
<%= art.description %>
10+
</p>
11+
12+
<p>
13+
<strong>Artist name:</strong>
14+
<%= art.artist_name %>
15+
</p>
16+
17+
<p>
18+
<strong>Last editor:</strong>
19+
<%= art.last_editor_id %>
20+
</p>
21+
22+
<p>
23+
<strong>Art created date:</strong>
24+
<%= art.art_created_date %>
25+
</p>
26+
27+
<p>
28+
<strong>Created location:</strong>
29+
<%= art.created_location %>
30+
</p>
31+
32+
<p>
33+
<strong>Url:</strong>
34+
<%= art.url %>
35+
</p>
36+
37+
<p>
38+
<strong>Image:</strong>
39+
<%= link_to art.image.filename, art.image if art.image.attached? %>
40+
</p>
41+
42+
<p>
43+
<strong>Classification:</strong>
44+
<%= art.classification %>
45+
</p>
46+
47+
<p>
48+
<strong>Is published:</strong>
49+
<%= art.is_published %>
50+
</p>
51+
52+
<p>
53+
<strong>Location:</strong>
54+
<%= art.location_id %>
55+
</p>
56+
57+
<p>
58+
<strong>Artist:</strong>
59+
<%= art.artist_id %>
60+
</p>
61+
62+
</div>

app/views/art/_form.html.erb

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<%= form_with(model: art) do |form| %>
2+
<% if art.errors.any? %>
3+
<div style="color: red">
4+
<h2><%= pluralize(art.errors.count, "error") %> prohibited this art from being saved:</h2>
5+
6+
<ul>
7+
<% art.errors.each do |error| %>
8+
<li><%= error.full_message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div>
15+
<%= form.label :title, style: "display: block" %>
16+
<%= form.text_field :title %>
17+
</div>
18+
19+
<div>
20+
<%= form.label :description, style: "display: block" %>
21+
<%= form.rich_text_area :description %>
22+
</div>
23+
24+
<div>
25+
<%= form.label :artist_name, style: "display: block" %>
26+
<%= form.text_area :artist_name %>
27+
</div>
28+
29+
<div>
30+
<%= form.label :last_editor_id, style: "display: block" %>
31+
<%= form.text_field :last_editor_id %>
32+
</div>
33+
34+
<div>
35+
<%= form.label :art_created_date, style: "display: block" %>
36+
<%= form.date_field :art_created_date %>
37+
</div>
38+
39+
<div>
40+
<%= form.label :created_location, style: "display: block" %>
41+
<%= form.text_field :created_location %>
42+
</div>
43+
44+
<div>
45+
<%= form.label :url, style: "display: block" %>
46+
<%= form.text_field :url %>
47+
</div>
48+
49+
<div>
50+
<%= form.label :image, style: "display: block" %>
51+
<%= form.file_field :image %>
52+
</div>
53+
54+
<div>
55+
<%= form.label :classification, style: "display: block" %>
56+
<%= form.text_field :classification %>
57+
</div>
58+
59+
<div>
60+
<%= form.label :is_published, style: "display: block" %>
61+
<%= form.check_box :is_published %>
62+
</div>
63+
64+
<div>
65+
<%= form.label :location_id, style: "display: block" %>
66+
<%= form.text_field :location_id %>
67+
</div>
68+
69+
<div>
70+
<%= form.label :artist_id, style: "display: block" %>
71+
<%= form.text_field :artist_id %>
72+
</div>
73+
74+
<div>
75+
<%= form.submit %>
76+
</div>
77+
<% end %>

app/views/art/edit.html.erb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h1>Editing art</h1>
2+
3+
<%= render "form", art: @art %>
4+
5+
<br>
6+
7+
<div>
8+
<%= link_to "Show this art", @art %> |
9+
<%= link_to "Back to art", art_index_path %>
10+
</div>

app/views/art/index.html.erb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p style="color: green"><%= notice %></p>
2+
3+
<h1>Art</h1>
4+
5+
<div id="art">
6+
<% @art.each do |art| %>
7+
<%= render art %>
8+
<p>
9+
<%= link_to "Show this art", art %>
10+
</p>
11+
<% end %>
12+
</div>
13+
14+
<%= link_to "New art", new_art_path %>

app/views/art/new.html.erb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>New art</h1>
2+
3+
<%= render "form", art: @art %>
4+
5+
<br>
6+
7+
<div>
8+
<%= link_to "Back to art", art_index_path %>
9+
</div>

app/views/art/show.html.erb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<p style="color: green"><%= notice %></p>
2+
3+
<%= render @art %>
4+
5+
<div>
6+
<%= link_to "Edit this art", edit_art_path(@art) %> |
7+
<%= link_to "Back to art", art_index_path %>
8+
9+
<%= button_to "Destroy this art", @art, method: :delete %>
10+
</div>

config/initializers/inflections.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
# Add new inflection rules using the following format. Inflections
44
# are locale specific, and you may define rules for as many different
55
# locales as you wish. All of these examples are active by default:
6-
# ActiveSupport::Inflector.inflections(:en) do |inflect|
7-
# inflect.plural /^(ox)$/i, "\\1en"
8-
# inflect.singular /^(ox)en/i, "\\1"
9-
# inflect.irregular "person", "people"
10-
# inflect.uncountable %w( fish sheep )
11-
# end
6+
ActiveSupport::Inflector.inflections(:en) do |inflect|
7+
# inflect.plural /^(ox)$/i, "\\1en"
8+
# inflect.singular /^(ox)en/i, "\\1"
9+
# inflect.irregular "person", "people"
10+
inflect.uncountable %w( art )
11+
end
1212

1313
# These inflection rules are supported but not enabled by default:
1414
# ActiveSupport::Inflector.inflections(:en) do |inflect|

config/routes.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Rails.application.routes.draw do
2+
resources :art
3+
resources :arts
24
resources :locations
35
resources :users
46
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class CreateArt < ActiveRecord::Migration[7.1]
2+
def change
3+
create_table :art do |t|
4+
t.string :title, null: false
5+
t.text :artist_name
6+
t.references :last_editor, null: false, foreign_key: { to_table: 'users' }
7+
t.date :art_created_date
8+
t.string :created_location
9+
t.string :url
10+
t.string :classification
11+
t.boolean :is_published, default: false
12+
t.references :location, null: false, foreign_key: true
13+
t.references :artist, null: true, foreign_key: { to_table: 'users' }
14+
15+
t.timestamps
16+
end
17+
18+
add_index :art, :title, unique: true
19+
end
20+
end

0 commit comments

Comments
 (0)