mirror of
https://github.com/iritmaximus/brewery.git
synced 2026-10-12 19:13:49 +03:00
upto 3.3
This commit is contained in:
@@ -10,6 +10,8 @@ class RatingsController < ApplicationController
|
||||
|
||||
def create
|
||||
Rating.create params.require(:rating).permit(:score, :beer_id)
|
||||
session[:last_rating] = "#{rating.beer.name} #{rating.score} points"
|
||||
|
||||
redirect_to ratings_path
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
class SessionsController < ApplicationController
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
user = User.find_by username: params[:username]
|
||||
session[:user_id] = user.id if user
|
||||
redirect_to user
|
||||
end
|
||||
|
||||
def destroy
|
||||
session[:user_id] = nil
|
||||
redirect_to :root
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,70 @@
|
||||
class UsersController < ApplicationController
|
||||
before_action :set_user, only: %i[ show edit update destroy ]
|
||||
|
||||
# GET /users or /users.json
|
||||
def index
|
||||
@users = User.all
|
||||
end
|
||||
|
||||
# GET /users/1 or /users/1.json
|
||||
def show
|
||||
end
|
||||
|
||||
# GET /users/new
|
||||
def new
|
||||
@user = User.new
|
||||
end
|
||||
|
||||
# GET /users/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /users or /users.json
|
||||
def create
|
||||
@user = User.new(user_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @user.save
|
||||
format.html { redirect_to user_url(@user), notice: "User was successfully created." }
|
||||
format.json { render :show, status: :created, location: @user }
|
||||
else
|
||||
format.html { render :new, status: :unprocessable_entity }
|
||||
format.json { render json: @user.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /users/1 or /users/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @user.update(user_params)
|
||||
format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
|
||||
format.json { render :show, status: :ok, location: @user }
|
||||
else
|
||||
format.html { render :edit, status: :unprocessable_entity }
|
||||
format.json { render json: @user.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /users/1 or /users/1.json
|
||||
def destroy
|
||||
@user.destroy
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to users_url, notice: "User was successfully destroyed." }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_user
|
||||
@user = User.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def user_params
|
||||
params.require(:user).permit(:username)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,2 @@
|
||||
module UsersHelper
|
||||
end
|
||||
@@ -0,0 +1,2 @@
|
||||
class User < ApplicationRecord
|
||||
end
|
||||
@@ -12,5 +12,13 @@
|
||||
|
||||
<body>
|
||||
<%= yield %>
|
||||
<% if not session[:user_id].nil? %>
|
||||
<p><%= User.find(session[:user_id]).username %> signed in</p>
|
||||
<% end %>
|
||||
<% if session[:last_rating].nil? %>
|
||||
<p>no ratings given</p>
|
||||
<% else %>
|
||||
<p>previous rating: <%= session[:last_rating] %></p>
|
||||
<% end %>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<h1>Sign in</h1>
|
||||
|
||||
<%= form_with url: session_path, method: :post do |form| %>
|
||||
<%= form.text_field :username %>
|
||||
<%= form.submit "Log in" %>
|
||||
<% end %>
|
||||
@@ -0,0 +1,22 @@
|
||||
<%= form_with(model: user) do |form| %>
|
||||
<% if user.errors.any? %>
|
||||
<div style="color: red">
|
||||
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
|
||||
|
||||
<ul>
|
||||
<% user.errors.each do |error| %>
|
||||
<li><%= error.full_message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= form.label :username, style: "display: block" %>
|
||||
<%= form.text_field :username %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.submit %>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -0,0 +1,7 @@
|
||||
<div id="<%= dom_id user %>">
|
||||
<p>
|
||||
<strong>Username:</strong>
|
||||
<%= user.username %>
|
||||
</p>
|
||||
|
||||
</div>
|
||||
@@ -0,0 +1,2 @@
|
||||
json.extract! user, :id, :username, :created_at, :updated_at
|
||||
json.url user_url(user, format: :json)
|
||||
@@ -0,0 +1,10 @@
|
||||
<h1>Editing user</h1>
|
||||
|
||||
<%= render "form", user: @user %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Show this user", @user %> |
|
||||
<%= link_to "Back to users", users_path %>
|
||||
</div>
|
||||
@@ -0,0 +1,15 @@
|
||||
<p style="color: green"><%= notice %></p>
|
||||
|
||||
<h1>Users</h1>
|
||||
|
||||
<div id="users">
|
||||
<% @users.each do |user| %>
|
||||
<%= render user %>
|
||||
<p>
|
||||
<%= link_to "Show this user", user %>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= link_to "New user", new_user_path %>
|
||||
<%= link_to "List of breweries", breweries_path %>
|
||||
@@ -0,0 +1 @@
|
||||
json.array! @users, partial: "users/user", as: :user
|
||||
@@ -0,0 +1,9 @@
|
||||
<h1>New user</h1>
|
||||
|
||||
<%= render "form", user: @user %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Back to users", users_path %>
|
||||
</div>
|
||||
@@ -0,0 +1,10 @@
|
||||
<p style="color: green"><%= notice %></p>
|
||||
|
||||
<%= render @user %>
|
||||
|
||||
<div>
|
||||
<%= link_to "Edit this user", edit_user_path(@user) %> |
|
||||
<%= link_to "Back to users", users_path %>
|
||||
|
||||
<%= button_to "Destroy this user", @user, method: :delete %>
|
||||
</div>
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! "users/user", user: @user
|
||||
@@ -1,4 +1,5 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :users
|
||||
resources :beers
|
||||
resources :breweries
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
@@ -6,5 +7,7 @@ Rails.application.routes.draw do
|
||||
# Defines the root path route ("/")
|
||||
# root "articles#index"
|
||||
get '/', to: 'breweries#index'
|
||||
get 'signup', to: 'users#new'
|
||||
resources :ratings, only: [:index, :new, :create, :destroy]
|
||||
resource :session, only: [:new, :create, :destroy]
|
||||
end
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class CreateUsers < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :users do |t|
|
||||
t.string :username
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
Generated
+7
-1
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.0].define(version: 2023_06_04_140719) do
|
||||
ActiveRecord::Schema[7.0].define(version: 2023_08_08_143510) do
|
||||
create_table "beers", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.string "style"
|
||||
@@ -33,4 +33,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_04_140719) do
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.string "username"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
require "test_helper"
|
||||
|
||||
class UsersControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@user = users(:one)
|
||||
end
|
||||
|
||||
test "should get index" do
|
||||
get users_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get new" do
|
||||
get new_user_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create user" do
|
||||
assert_difference("User.count") do
|
||||
post users_url, params: { user: { username: @user.username } }
|
||||
end
|
||||
|
||||
assert_redirected_to user_url(User.last)
|
||||
end
|
||||
|
||||
test "should show user" do
|
||||
get user_url(@user)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get edit" do
|
||||
get edit_user_url(@user)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should update user" do
|
||||
patch user_url(@user), params: { user: { username: @user.username } }
|
||||
assert_redirected_to user_url(@user)
|
||||
end
|
||||
|
||||
test "should destroy user" do
|
||||
assert_difference("User.count", -1) do
|
||||
delete user_url(@user)
|
||||
end
|
||||
|
||||
assert_redirected_to users_url
|
||||
end
|
||||
end
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
username: MyString
|
||||
|
||||
two:
|
||||
username: MyString
|
||||
@@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class UserTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
@@ -0,0 +1,41 @@
|
||||
require "application_system_test_case"
|
||||
|
||||
class UsersTest < ApplicationSystemTestCase
|
||||
setup do
|
||||
@user = users(:one)
|
||||
end
|
||||
|
||||
test "visiting the index" do
|
||||
visit users_url
|
||||
assert_selector "h1", text: "Users"
|
||||
end
|
||||
|
||||
test "should create user" do
|
||||
visit users_url
|
||||
click_on "New user"
|
||||
|
||||
fill_in "Username", with: @user.username
|
||||
click_on "Create User"
|
||||
|
||||
assert_text "User was successfully created"
|
||||
click_on "Back"
|
||||
end
|
||||
|
||||
test "should update User" do
|
||||
visit user_url(@user)
|
||||
click_on "Edit this user", match: :first
|
||||
|
||||
fill_in "Username", with: @user.username
|
||||
click_on "Update User"
|
||||
|
||||
assert_text "User was successfully updated"
|
||||
click_on "Back"
|
||||
end
|
||||
|
||||
test "should destroy User" do
|
||||
visit user_url(@user)
|
||||
click_on "Destroy this user", match: :first
|
||||
|
||||
assert_text "User was successfully destroyed"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user