[deploy] Listing endorsements: models setup (#9841)
* create listing endorsement model and write test to validate it * create the classified_listing_endorsements table * create model associations with seed data and test validations * modify test and create more seed date for endorsement * modify the model naming, changed seed data to become radomized and updated the test file * remove unnecessary function
This commit is contained in:
parent
03ed8aedb1
commit
30b3592488
10 changed files with 88 additions and 0 deletions
|
|
@ -22,6 +22,11 @@ class Listing < ApplicationRecord
|
|||
after_commit :remove_from_elasticsearch, on: [:destroy]
|
||||
acts_as_taggable_on :tags
|
||||
has_many :credits, as: :purchase, inverse_of: :purchase, dependent: :nullify
|
||||
has_many :endorsements,
|
||||
foreign_key: :classified_listing_id,
|
||||
dependent: :destroy,
|
||||
inverse_of: :listing,
|
||||
class_name: "ListingEndorsement"
|
||||
|
||||
validates :user_id, presence: true
|
||||
validates :organization_id, presence: true, unless: :user_id?
|
||||
|
|
|
|||
10
app/models/listing_endorsement.rb
Normal file
10
app/models/listing_endorsement.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
class ListingEndorsement < ApplicationRecord
|
||||
self.table_name = "classified_listing_endorsements"
|
||||
|
||||
belongs_to :listing, foreign_key: :classified_listing_id, inverse_of: :endorsements
|
||||
belongs_to :user
|
||||
|
||||
validates :user_id, presence: true
|
||||
validates :classified_listing_id, presence: true
|
||||
validates :content, presence: true
|
||||
end
|
||||
|
|
@ -134,6 +134,7 @@ class User < ApplicationRecord
|
|||
has_many :chat_channel_memberships, dependent: :destroy
|
||||
has_many :chat_channels, through: :chat_channel_memberships
|
||||
has_many :listings, dependent: :destroy
|
||||
has_many :endorsements, dependent: :destroy, class_name: "ListingEndorsement"
|
||||
has_many :collections, dependent: :destroy
|
||||
has_many :comments, dependent: :destroy
|
||||
has_many :created_podcasts, class_name: "Podcast", foreign_key: :creator_id, inverse_of: :creator, dependent: :nullify
|
||||
|
|
|
|||
11
db/migrate/20200817205048_create_listing_endorsements.rb
Normal file
11
db/migrate/20200817205048_create_listing_endorsements.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class CreateListingEndorsements < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
create_table :classified_listing_endorsements do |t|
|
||||
t.string :content
|
||||
t.boolean :approved, default: false
|
||||
t.references :classified_listing, foreign_key: true
|
||||
t.references :user, foreign_key: true
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
13
db/schema.rb
13
db/schema.rb
|
|
@ -348,6 +348,17 @@ ActiveRecord::Schema.define(version: 2020_08_18_101700) do
|
|||
t.index ["slug"], name: "index_classified_listing_categories_on_slug", unique: true
|
||||
end
|
||||
|
||||
create_table "classified_listing_endorsements", force: :cascade do |t|
|
||||
t.boolean "approved", default: false
|
||||
t.bigint "classified_listing_id"
|
||||
t.string "content"
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.bigint "user_id"
|
||||
t.index ["classified_listing_id"], name: "index_classified_listing_endorsements_on_classified_listing_id"
|
||||
t.index ["user_id"], name: "index_classified_listing_endorsements_on_user_id"
|
||||
end
|
||||
|
||||
create_table "classified_listings", force: :cascade do |t|
|
||||
t.text "body_markdown"
|
||||
t.datetime "bumped_at"
|
||||
|
|
@ -1347,6 +1358,8 @@ ActiveRecord::Schema.define(version: 2020_08_18_101700) do
|
|||
add_foreign_key "badge_achievements", "users"
|
||||
add_foreign_key "chat_channel_memberships", "chat_channels"
|
||||
add_foreign_key "chat_channel_memberships", "users"
|
||||
add_foreign_key "classified_listing_endorsements", "classified_listings"
|
||||
add_foreign_key "classified_listing_endorsements", "users"
|
||||
add_foreign_key "classified_listings", "classified_listing_categories"
|
||||
add_foreign_key "classified_listings", "users", on_delete: :cascade
|
||||
add_foreign_key "display_ad_events", "display_ads", on_delete: :cascade
|
||||
|
|
|
|||
|
|
@ -477,6 +477,15 @@ seeder.create_if_none(Listing) do
|
|||
end
|
||||
end
|
||||
|
||||
seeder.create_if_none(ListingEndorsement) do
|
||||
5.times do
|
||||
ListingEndorsement.create!(
|
||||
content: Faker::Lorem.sentence,
|
||||
user: User.order(Arel.sql("RANDOM()")).first,
|
||||
listing: Listing.order(Arel.sql("RANDOM()")).first,
|
||||
)
|
||||
end
|
||||
end
|
||||
##############################################################################
|
||||
|
||||
seeder.create_if_none(Page) do
|
||||
|
|
|
|||
8
spec/factories/listing_endorsements.rb
Normal file
8
spec/factories/listing_endorsements.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
FactoryBot.define do
|
||||
factory :listing_endorsement do
|
||||
user
|
||||
listing
|
||||
content { "#{Faker::Lorem.sentence} " }
|
||||
approved { true }
|
||||
end
|
||||
end
|
||||
29
spec/models/listing_endorsement_spec.rb
Normal file
29
spec/models/listing_endorsement_spec.rb
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe ListingEndorsement, type: :model do
|
||||
let(:user) { create(:user) }
|
||||
let(:listing) { create(:listing, user: user) }
|
||||
let(:listing_endorsement) { create(:listing_endorsement, listing: listing, user: user) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:content) }
|
||||
it { is_expected.to belong_to(:listing) }
|
||||
it { is_expected.to belong_to(:user) }
|
||||
|
||||
describe "valid associations" do
|
||||
it "is not valid w/o user" do
|
||||
cl = build(:listing_endorsement, user_id: nil)
|
||||
expect(cl).not_to be_valid
|
||||
expect(cl.errors[:user_id]).to be_truthy
|
||||
end
|
||||
|
||||
it "is not valid w/o listing" do
|
||||
cl = build(:listing_endorsement, classified_listing_id: nil, user_id: user.id)
|
||||
expect(cl).not_to be_valid
|
||||
end
|
||||
|
||||
it "is valid with listing and user" do
|
||||
cl = build(:listing_endorsement, classified_listing_id: listing.id, user_id: user.id)
|
||||
expect(cl).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -12,6 +12,7 @@ RSpec.describe Listing, type: :model do
|
|||
it { is_expected.to validate_presence_of(:title) }
|
||||
it { is_expected.to validate_presence_of(:body_markdown) }
|
||||
it { is_expected.to have_many(:credits) }
|
||||
it { is_expected.to have_many(:endorsements) }
|
||||
|
||||
describe "valid associations" do
|
||||
it "is not valid w/o user and org" do
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ RSpec.describe User, type: :model do
|
|||
it { is_expected.to have_many(:reactions).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:response_templates).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:tweets).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:endorsements).dependent(:destroy) }
|
||||
|
||||
# rubocop:disable RSpec/NamedSubject
|
||||
it do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue