From 362ed3596b0f8b0af31f9d0ca03ecc7fcb6746ac Mon Sep 17 00:00:00 2001 From: Mohammad Kanaan Date: Sat, 30 May 2026 17:54:26 +0300 Subject: [PATCH] merge adming and guest into users --- app/controllers/guests_controller.rb | 51 ------------------- app/models/guest.rb | 14 ----- app/models/moderation_log.rb | 6 +-- app/models/price_report.rb | 8 +-- app/models/{admin.rb => user.rb} | 10 ++-- config/routes.rb | 4 +- db/migrate/20260530141606_create_users.rb | 13 +++++ .../20260530141612_drop_guests_and_admins.rb | 17 +++++++ db/schema.rb | 38 ++++++-------- test/controllers/guests_controller_test.rb | 38 -------------- test/fixtures/guests.yml | 19 ------- test/fixtures/moderation_logs.yml | 8 +-- test/fixtures/{admins.yml => users.yml} | 15 +++--- test/models/guest_test.rb | 17 ------- test/models/moderation_log_test.rb | 4 +- test/models/{admin_test.rb => user_test.rb} | 7 +-- 16 files changed, 80 insertions(+), 189 deletions(-) delete mode 100644 app/controllers/guests_controller.rb delete mode 100644 app/models/guest.rb rename app/models/{admin.rb => user.rb} (53%) create mode 100644 db/migrate/20260530141606_create_users.rb create mode 100644 db/migrate/20260530141612_drop_guests_and_admins.rb delete mode 100644 test/controllers/guests_controller_test.rb delete mode 100644 test/fixtures/guests.yml rename test/fixtures/{admins.yml => users.yml} (76%) delete mode 100644 test/models/guest_test.rb rename test/models/{admin_test.rb => user_test.rb} (73%) diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb deleted file mode 100644 index ea60a8b..0000000 --- a/app/controllers/guests_controller.rb +++ /dev/null @@ -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 diff --git a/app/models/guest.rb b/app/models/guest.rb deleted file mode 100644 index 760dbd0..0000000 --- a/app/models/guest.rb +++ /dev/null @@ -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 diff --git a/app/models/moderation_log.rb b/app/models/moderation_log.rb index 0bfd8f5..3d020fc 100644 --- a/app/models/moderation_log.rb +++ b/app/models/moderation_log.rb @@ -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 diff --git a/app/models/price_report.rb b/app/models/price_report.rb index 9d9942e..b81230b 100644 --- a/app/models/price_report.rb +++ b/app/models/price_report.rb @@ -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 diff --git a/app/models/admin.rb b/app/models/user.rb similarity index 53% rename from app/models/admin.rb rename to app/models/user.rb index 0d797bd..12a5d4f 100644 --- a/app/models/admin.rb +++ b/app/models/user.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 38b830c..c73bc0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20260530141606_create_users.rb b/db/migrate/20260530141606_create_users.rb new file mode 100644 index 0000000..6fb8ca4 --- /dev/null +++ b/db/migrate/20260530141606_create_users.rb @@ -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 diff --git a/db/migrate/20260530141612_drop_guests_and_admins.rb b/db/migrate/20260530141612_drop_guests_and_admins.rb new file mode 100644 index 0000000..bd18ea3 --- /dev/null +++ b/db/migrate/20260530141612_drop_guests_and_admins.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 8b02474..e7e1e9f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/test/controllers/guests_controller_test.rb b/test/controllers/guests_controller_test.rb deleted file mode 100644 index fe67f10..0000000 --- a/test/controllers/guests_controller_test.rb +++ /dev/null @@ -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 diff --git a/test/fixtures/guests.yml b/test/fixtures/guests.yml deleted file mode 100644 index 596094d..0000000 --- a/test/fixtures/guests.yml +++ /dev/null @@ -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 diff --git a/test/fixtures/moderation_logs.yml b/test/fixtures/moderation_logs.yml index 38f2fba..e980a21 100644 --- a/test/fixtures/moderation_logs.yml +++ b/test/fixtures/moderation_logs.yml @@ -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 diff --git a/test/fixtures/admins.yml b/test/fixtures/users.yml similarity index 76% rename from test/fixtures/admins.yml rename to test/fixtures/users.yml index eb79181..ff5aabf 100644 --- a/test/fixtures/admins.yml +++ b/test/fixtures/users.yml @@ -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 \ No newline at end of file diff --git a/test/models/guest_test.rb b/test/models/guest_test.rb deleted file mode 100644 index 827a504..0000000 --- a/test/models/guest_test.rb +++ /dev/null @@ -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 diff --git a/test/models/moderation_log_test.rb b/test/models/moderation_log_test.rb index 77d9d76..814d9bb 100644 --- a/test/models/moderation_log_test.rb +++ b/test/models/moderation_log_test.rb @@ -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" diff --git a/test/models/admin_test.rb b/test/models/user_test.rb similarity index 73% rename from test/models/admin_test.rb rename to test/models/user_test.rb index e0dd841..d782a16 100644 --- a/test/models/admin_test.rb +++ b/test/models/user_test.rb @@ -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