merge adming and guest into users
This commit is contained in:
parent
6b9d9dee8a
commit
362ed3596b
16 changed files with 80 additions and 189 deletions
|
|
@ -1,51 +0,0 @@
|
|||
class GuestsController < ApplicationController
|
||||
before_action :set_guest, only: %i[ show update destroy ]
|
||||
|
||||
# GET /guests
|
||||
def index
|
||||
@guests = Guest.all
|
||||
|
||||
render json: @guests
|
||||
end
|
||||
|
||||
# GET /guests/1
|
||||
def show
|
||||
render json: @guest
|
||||
end
|
||||
|
||||
# POST /guests
|
||||
def create
|
||||
@guest = Guest.new(guest_params)
|
||||
|
||||
if @guest.save
|
||||
render json: @guest, status: :created, location: @guest
|
||||
else
|
||||
render json: @guest.errors, status: :unprocessable_content
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /guests/1
|
||||
def update
|
||||
if @guest.update(guest_params)
|
||||
render json: @guest
|
||||
else
|
||||
render json: @guest.errors, status: :unprocessable_content
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /guests/1
|
||||
def destroy
|
||||
@guest.destroy!
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_guest
|
||||
@guest = Guest.find(params.expect(:id))
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def guest_params
|
||||
params.expect(guest: [ :username, :name ])
|
||||
end
|
||||
end
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: guests
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# name :string
|
||||
# username :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
class Guest < ApplicationRecord
|
||||
has_many :price_reports, dependent: :destroy
|
||||
has_many :moderation_logs, as: :moderatable, dependent: :destroy
|
||||
end
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
# reason :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# admin_id :integer not null
|
||||
# admin_id :bigint not null
|
||||
# moderatable_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
|
|
@ -17,9 +17,9 @@
|
|||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (admin_id => admins.id)
|
||||
# fk_rails_... (admin_id => users.id)
|
||||
#
|
||||
class ModerationLog < ApplicationRecord
|
||||
belongs_to :moderatable, polymorphic: true
|
||||
belongs_to :admin
|
||||
belongs_to :user, foreign_key: :admin_id
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,25 +10,25 @@
|
|||
# upvotes :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# guest_id :integer not null
|
||||
# product_id :integer not null
|
||||
# store_id :integer not null
|
||||
# user_id :bigint not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_price_reports_on_guest_id (guest_id)
|
||||
# index_price_reports_on_product_id (product_id)
|
||||
# index_price_reports_on_store_id (store_id)
|
||||
# index_price_reports_on_user_id (user_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (guest_id => guests.id)
|
||||
# fk_rails_... (product_id => products.id)
|
||||
# fk_rails_... (store_id => stores.id)
|
||||
# fk_rails_... (user_id => users.id)
|
||||
#
|
||||
class PriceReport < ApplicationRecord
|
||||
belongs_to :product
|
||||
belongs_to :store
|
||||
belongs_to :guest
|
||||
belongs_to :user
|
||||
has_many :moderation_logs, as: :moderatable, dependent: :destroy
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,18 +1,20 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: admins
|
||||
# Table name: users
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# email :string
|
||||
# name :string
|
||||
# password_digest :string
|
||||
# role :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_admins_on_email (email) UNIQUE
|
||||
# index_users_on_email (email) UNIQUE
|
||||
#
|
||||
class Admin < ApplicationRecord
|
||||
has_many :moderation_logs, dependent: :destroy
|
||||
class User < ApplicationRecord
|
||||
has_many :price_reports, dependent: :nullify
|
||||
has_many :moderation_logs, foreign_key: :admin_id, dependent: :nullify
|
||||
end
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
Rails.application.routes.draw do
|
||||
resources :price_reports
|
||||
resources :guests
|
||||
resources :price_reports, only: [:show, :update, :destroy]
|
||||
resources :users
|
||||
resources :stores
|
||||
resources :chains
|
||||
resources :products
|
||||
|
|
|
|||
13
db/migrate/20260530141606_create_users.rb
Normal file
13
db/migrate/20260530141606_create_users.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class CreateUsers < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
create_table :users do |t|
|
||||
t.string :email
|
||||
t.string :name
|
||||
t.string :password_digest
|
||||
t.integer :role
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
add_index :users, :email, unique: true
|
||||
end
|
||||
end
|
||||
17
db/migrate/20260530141612_drop_guests_and_admins.rb
Normal file
17
db/migrate/20260530141612_drop_guests_and_admins.rb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
class DropGuestsAndAdmins < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
remove_foreign_key :price_reports, :guests
|
||||
remove_foreign_key :moderation_logs, :admins
|
||||
|
||||
change_column :price_reports, :guest_id, :bigint
|
||||
change_column :moderation_logs, :admin_id, :bigint
|
||||
|
||||
drop_table :admins
|
||||
drop_table :guests
|
||||
|
||||
rename_column :price_reports, :guest_id, :user_id
|
||||
add_foreign_key :price_reports, :users
|
||||
|
||||
add_foreign_key :moderation_logs, :users, column: :admin_id
|
||||
end
|
||||
end
|
||||
38
db/schema.rb
generated
38
db/schema.rb
generated
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_05_17_111138) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_05_30_141612) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pg_catalog.plpgsql"
|
||||
|
||||
|
|
@ -42,33 +42,17 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_17_111138) do
|
|||
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||
end
|
||||
|
||||
create_table "admins", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.string "email"
|
||||
t.string "password_digest"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["email"], name: "index_admins_on_email", unique: true
|
||||
end
|
||||
|
||||
create_table "chains", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "guests", force: :cascade do |t|
|
||||
t.string "username"
|
||||
t.string "name"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "moderation_logs", force: :cascade do |t|
|
||||
t.string "reason"
|
||||
t.string "moderatable_type", null: false
|
||||
t.integer "moderatable_id", null: false
|
||||
t.integer "admin_id", null: false
|
||||
t.bigint "admin_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["admin_id"], name: "index_moderation_logs_on_admin_id"
|
||||
|
|
@ -83,12 +67,12 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_17_111138) do
|
|||
t.datetime "reported_at"
|
||||
t.integer "product_id", null: false
|
||||
t.integer "store_id", null: false
|
||||
t.integer "guest_id", null: false
|
||||
t.bigint "user_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["guest_id"], name: "index_price_reports_on_guest_id"
|
||||
t.index ["product_id"], name: "index_price_reports_on_product_id"
|
||||
t.index ["store_id"], name: "index_price_reports_on_store_id"
|
||||
t.index ["user_id"], name: "index_price_reports_on_user_id"
|
||||
end
|
||||
|
||||
create_table "products", force: :cascade do |t|
|
||||
|
|
@ -109,11 +93,21 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_17_111138) do
|
|||
t.index ["chain_id"], name: "index_stores_on_chain_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.string "email"
|
||||
t.string "name"
|
||||
t.string "password_digest"
|
||||
t.integer "role"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["email"], name: "index_users_on_email", unique: true
|
||||
end
|
||||
|
||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "moderation_logs", "admins"
|
||||
add_foreign_key "price_reports", "guests"
|
||||
add_foreign_key "moderation_logs", "users", column: "admin_id"
|
||||
add_foreign_key "price_reports", "products"
|
||||
add_foreign_key "price_reports", "stores"
|
||||
add_foreign_key "price_reports", "users"
|
||||
add_foreign_key "stores", "chains"
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class GuestsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@guest = guests(:one)
|
||||
end
|
||||
|
||||
test "should get index" do
|
||||
get guests_url, as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create guest" do
|
||||
assert_difference("Guest.count") do
|
||||
post guests_url, params: { guest: { name: @guest.name, username: @guest.username } }, as: :json
|
||||
end
|
||||
|
||||
assert_response :created
|
||||
end
|
||||
|
||||
test "should show guest" do
|
||||
get guest_url(@guest), as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should update guest" do
|
||||
patch guest_url(@guest), params: { guest: { name: @guest.name, username: @guest.username } }, as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should destroy guest" do
|
||||
assert_difference("Guest.count", -1) do
|
||||
delete guest_url(@guest), as: :json
|
||||
end
|
||||
|
||||
assert_response :no_content
|
||||
end
|
||||
end
|
||||
19
test/fixtures/guests.yml
vendored
19
test/fixtures/guests.yml
vendored
|
|
@ -1,19 +0,0 @@
|
|||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: guests
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# name :string
|
||||
# username :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
one:
|
||||
username: MyString
|
||||
name: MyString
|
||||
|
||||
two:
|
||||
username: MyString
|
||||
name: MyString
|
||||
8
test/fixtures/moderation_logs.yml
vendored
8
test/fixtures/moderation_logs.yml
vendored
|
|
@ -9,7 +9,7 @@
|
|||
# reason :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# admin_id :integer not null
|
||||
# admin_id :bigint not null
|
||||
# moderatable_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
|
|
@ -19,14 +19,14 @@
|
|||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (admin_id => admins.id)
|
||||
# fk_rails_... (admin_id => users.id)
|
||||
#
|
||||
one:
|
||||
reason: MyString
|
||||
moderatable: one (Chain)
|
||||
admin: one
|
||||
user: one
|
||||
|
||||
two:
|
||||
reason: MyString
|
||||
moderatable: two (Chain)
|
||||
admin: two
|
||||
user: two
|
||||
|
|
|
|||
|
|
@ -2,25 +2,28 @@
|
|||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: admins
|
||||
# Table name: users
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# email :string
|
||||
# name :string
|
||||
# password_digest :string
|
||||
# role :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_admins_on_email (email) UNIQUE
|
||||
# index_users_on_email (email) UNIQUE
|
||||
#
|
||||
one:
|
||||
name: Admin
|
||||
email: admin@test.com
|
||||
email: guest@test.com
|
||||
name: Guest
|
||||
password_digest: MyString
|
||||
role: 0
|
||||
|
||||
two:
|
||||
name: Mod
|
||||
email: mod@test.com
|
||||
email: admin@test.com
|
||||
name: Admin
|
||||
password_digest: MyString
|
||||
role: 1
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: guests
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# name :string
|
||||
# username :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
require "test_helper"
|
||||
|
||||
class GuestTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
# reason :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# admin_id :integer not null
|
||||
# admin_id :bigint not null
|
||||
# moderatable_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
|
|
@ -17,7 +17,7 @@
|
|||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (admin_id => admins.id)
|
||||
# fk_rails_... (admin_id => users.id)
|
||||
#
|
||||
require "test_helper"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +1,22 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: admins
|
||||
# Table name: users
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# email :string
|
||||
# name :string
|
||||
# password_digest :string
|
||||
# role :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_admins_on_email (email) UNIQUE
|
||||
# index_users_on_email (email) UNIQUE
|
||||
#
|
||||
require "test_helper"
|
||||
|
||||
class AdminTest < ActiveSupport::TestCase
|
||||
class UserTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
Loading…
Reference in a new issue