Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Data model #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ GEM
nio4r (2.5.5)
nokogiri (1.11.1-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.11.1-x86_64-linux)
racc (~> 1.4)
parallel (1.20.1)
parser (3.0.0.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -272,6 +274,7 @@ GEM

PLATFORMS
x86_64-darwin-18
x86_64-linux

DEPENDENCIES
annotate
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/choiceboards.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the choiceboards controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/options.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the options controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
65 changes: 65 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
body {
background-color: #fff;
color: #333;
margin: 33px; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }

pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }

a {
color: #000; }

a:visited {
color: #666; }

a:hover {
color: #fff;
background-color: #000; }

th {
padding-bottom: 5px; }

td {
padding: 0 5px 7px; }

div.field,
div.actions {
margin-bottom: 10px; }

#notice {
color: green; }

.field_with_errors {
padding: 2px;
background-color: red;
display: table; }

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0; }

#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff; }

#error_explanation ul li {
font-size: 12px;
list-style: square; }

label {
display: block; }
69 changes: 69 additions & 0 deletions app/controllers/choiceboards_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class ChoiceboardsController < ApplicationController
before_action :set_choiceboard, only: %i[ show edit update destroy ]

# GET /choiceboards or /choiceboards.json
def index
@choiceboards = Choiceboard.all
end

# GET /choiceboards/1 or /choiceboards/1.json
def show
end

# GET /choiceboards/new
def new
@choiceboard = Choiceboard.new
end

# GET /choiceboards/1/edit
def edit
end

# POST /choiceboards or /choiceboards.json
def create
@choiceboard = Choiceboard.new(choiceboard_params)

respond_to do |format|
if @choiceboard.save
format.html { redirect_to @choiceboard, notice: "Choiceboard was successfully created." }
format.json { render :show, status: :created, location: @choiceboard }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @choiceboard.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /choiceboards/1 or /choiceboards/1.json
def update
respond_to do |format|
if @choiceboard.update(choiceboard_params)
format.html { redirect_to @choiceboard, notice: "Choiceboard was successfully updated." }
format.json { render :show, status: :ok, location: @choiceboard }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @choiceboard.errors, status: :unprocessable_entity }
end
end
end

# DELETE /choiceboards/1 or /choiceboards/1.json
def destroy
@choiceboard.destroy
respond_to do |format|
format.html { redirect_to choiceboards_url, notice: "Choiceboard was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_choiceboard
@choiceboard = Choiceboard.find(params[:id])
end

# Only allow a list of trusted parameters through.
def choiceboard_params
params.require(:choiceboard).permit(:user_id_id, :name, :image_uid, :sound_uid)
end
end
69 changes: 69 additions & 0 deletions app/controllers/options_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class OptionsController < ApplicationController
before_action :set_option, only: %i[ show edit update destroy ]

# GET /options or /options.json
def index
@options = Option.all
end

# GET /options/1 or /options/1.json
def show
end

# GET /options/new
def new
@option = Option.new
end

# GET /options/1/edit
def edit
end

# POST /options or /options.json
def create
@option = Option.new(option_params)

respond_to do |format|
if @option.save
format.html { redirect_to @option, notice: "Option was successfully created." }
format.json { render :show, status: :created, location: @option }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @option.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /options/1 or /options/1.json
def update
respond_to do |format|
if @option.update(option_params)
format.html { redirect_to @option, notice: "Option was successfully updated." }
format.json { render :show, status: :ok, location: @option }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @option.errors, status: :unprocessable_entity }
end
end
end

# DELETE /options/1 or /options/1.json
def destroy
@option.destroy
respond_to do |format|
format.html { redirect_to options_url, notice: "Option was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_option
@option = Option.find(params[:id])
end

# Only allow a list of trusted parameters through.
def option_params
params.require(:option).permit(:user_id, :name, :image_uid, :sound_uid)
end
end
2 changes: 2 additions & 0 deletions app/helpers/choiceboards_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ChoiceboardsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/options_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module OptionsHelper
end
3 changes: 3 additions & 0 deletions app/models/choice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Choice < ApplicationRecord
belongs_to :chooseable, polymorphic: true
end
5 changes: 5 additions & 0 deletions app/models/choiceboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Choiceboard < ApplicationRecord
belongs_to :user
has_many :options, through: :choices
has_many :choiceboards, through: :choices
end
4 changes: 4 additions & 0 deletions app/models/option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Option < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :choiceboards, as: :chooseable
end
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class User < ApplicationRecord
include Clearance::User

include Clearance::User
has_many :choiceboards
has_many :options
end
2 changes: 2 additions & 0 deletions app/views/choiceboards/_choiceboard.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! choiceboard, :id, :user_id_id, :name, :image_uid, :sound_uid, :created_at, :updated_at
json.url choiceboard_url(choiceboard, format: :json)
37 changes: 37 additions & 0 deletions app/views/choiceboards/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<%= form_with(model: choiceboard) do |form| %>
<% if choiceboard.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(choiceboard.errors.count, "error") %> prohibited this choiceboard from being saved:</h2>

<ul>
<% choiceboard.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :user_id_id %>
<%= form.text_field :user_id_id %>
</div>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>

<div class="field">
<%= form.label :image_uid %>
<%= form.text_field :image_uid %>
</div>

<div class="field">
<%= form.label :sound_uid %>
<%= form.text_field :sound_uid %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/choiceboards/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Choiceboard</h1>

<%= render 'form', choiceboard: @choiceboard %>

<%= link_to 'Show', @choiceboard %> |
<%= link_to 'Back', choiceboards_path %>
33 changes: 33 additions & 0 deletions app/views/choiceboards/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<p id="notice"><%= notice %></p>

<h1>Choiceboards</h1>

<table>
<thead>
<tr>
<th>User</th>
<th>Name</th>
<th>Image uid</th>
<th>Sound uid</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @choiceboards.each do |choiceboard| %>
<tr>
<td><%= choiceboard.user_id_id %></td>
<td><%= choiceboard.name %></td>
<td><%= choiceboard.image_uid %></td>
<td><%= choiceboard.sound_uid %></td>
<td><%= link_to 'Show', choiceboard %></td>
<td><%= link_to 'Edit', edit_choiceboard_path(choiceboard) %></td>
<td><%= link_to 'Destroy', choiceboard, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Choiceboard', new_choiceboard_path %>
1 change: 1 addition & 0 deletions app/views/choiceboards/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @choiceboards, partial: "choiceboards/choiceboard", as: :choiceboard
5 changes: 5 additions & 0 deletions app/views/choiceboards/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Choiceboard</h1>

<%= render 'form', choiceboard: @choiceboard %>

<%= link_to 'Back', choiceboards_path %>
24 changes: 24 additions & 0 deletions app/views/choiceboards/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<p id="notice"><%= notice %></p>

<p>
<strong>User:</strong>
<%= @choiceboard.user_id_id %>
</p>

<p>
<strong>Name:</strong>
<%= @choiceboard.name %>
</p>

<p>
<strong>Image uid:</strong>
<%= @choiceboard.image_uid %>
</p>

<p>
<strong>Sound uid:</strong>
<%= @choiceboard.sound_uid %>
</p>

<%= link_to 'Edit', edit_choiceboard_path(@choiceboard) %> |
<%= link_to 'Back', choiceboards_path %>
1 change: 1 addition & 0 deletions app/views/choiceboards/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "choiceboards/choiceboard", choiceboard: @choiceboard
Loading