Skip to content

Commit

Permalink
Merge #41: Add featured contents models
Browse files Browse the repository at this point in the history
  • Loading branch information
hellodhlyn committed Oct 22, 2023
2 parents 0bc29f7 + d01153d commit dd54d15
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/graphql/queries/featured_contents_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Queries::FeaturedContentsQuery < Queries::BaseQuery
type [Types::FeaturedContentType], null: false

def resolve = FeaturedContent.all
end
7 changes: 7 additions & 0 deletions app/graphql/types/featured_content_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Types::FeaturedContentType < Types::Base::Object
field :title, String, null: false
field :slug, String, null: false
field :description, String

field :posts, resolver: Resolvers::PostsResolver
end
1 change: 1 addition & 0 deletions app/graphql/types/post_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def self.authorized?(object, context)
field :blobs, [Types::Interfaces::Blob], null: false
field :visibility, Types::Enums::PostVisibility, null: false
field :reaction_summary, "Types::ReactionSummaryType", null: false
field :featured_contents, "[Types::FeaturedContentType]", null: false
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false

Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class QueryType < Types::Base::Object
field :post, resolver: Queries::PostQuery
field :site, resolver: Queries::SiteQuery
field :sites, resolver: Queries::SitesQuery
field :featured_contents, resolver: Queries::FeaturedContentsQuery
field :user, resolver: Queries::UserQuery
field :viewer, resolver: Queries::ViewerQuery
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/featured_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class FeaturedContent < ApplicationRecord
validates_uniqueness_of :slug

has_many :featured_content_posts
has_many :posts, through: :featured_content_posts
end
4 changes: 4 additions & 0 deletions app/models/featured_content_post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class FeaturedContentPost < ApplicationRecord
belongs_to :featured_content
belongs_to :post
end
2 changes: 2 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Visibilities
has_many :tags, through: :post_tags
has_many :blobs, -> { order(index: :asc) }, autosave: true, dependent: :destroy
has_many :reactions
has_many :featured_content_posts
has_many :featured_contents, through: :featured_content_posts
delegate :site, :to => :namespace

validates :visibility, inclusion: { in: Visibilities::ALL }
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20231022133724_create_featured_contents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateFeaturedContents < ActiveRecord::Migration[7.0]
def change
create_table :featured_contents do |t|
t.string :title, null: false
t.string :slug, null: false
t.string :description
t.timestamps
end

create_table :featured_content_posts do |t|
t.references :featured_content
t.references :post
t.timestamps
end
end
end
19 changes: 18 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spec/factories/featured_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :featured_content do
title { Faker::Lorem.sentence }
description { Faker::Lorem.paragraph }
slug { Faker::Internet.slug }
posts { [] }
end
end
2 changes: 1 addition & 1 deletion spec/factories/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
factory :namespace do
site
name { Faker::Internet.domain_name }
slug { Faker::Lorem.word }
slug { Faker::Internet.slug }
end
end
2 changes: 1 addition & 1 deletion spec/factories/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
factory :post do
namespace { create :namespace }
title { Faker::Lorem.sentence }
slug { Faker::Lorem.word }
slug { Faker::Internet.slug }
description { Faker::Lorem.paragraph }
author { create :user }
visibility { Post::Visibilities::PUBLIC }
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/site.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :site do
name { Faker::Internet.domain_name }
slug { Faker::Lorem.word }
slug { Faker::Internet.slug }
end
end
2 changes: 1 addition & 1 deletion spec/factories/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
factory :tag do
namespace { create :namespace }
name { Faker::Lorem.word }
slug { Faker::Lorem.word }
slug { Faker::Internet.slug }
end
end
13 changes: 13 additions & 0 deletions spec/graphql/queries/featured_contents_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "spec_helper"

RSpec.describe Queries::FeaturedContentsQuery do
subject { described_class.new(object: nil, context: nil, field: nil) }

before { create_list :featured_content, 3 }

describe "#resolve" do
it "returns all featured contents" do
expect(subject.resolve).to eq FeaturedContent.all
end
end
end
39 changes: 39 additions & 0 deletions spec/graphql/types/featured_contents_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
RSpec.describe Types::FeaturedContentType, type: :graphql do
let(:query_string) do
<<~GQL
query {
featuredContents {
title
slug
description
posts(first: 3) {
nodes {
title
}
}
}
}
GQL
end

let(:featured_content) { create(:featured_content) }

describe "#posts" do
before do
featured_content.update(posts: create_list(:post, 5))
create_list(:post, 5)
end

subject do
execute_graphql(query_string)
end

it "should return all featured posts" do
expect(subject["errors"]).to be_nil
expect(subject["data"]["featuredContents"].length).to eq 1
expect(subject["data"]["featuredContents"][0]["posts"]["nodes"].length).to eq 3
expect(subject["data"]["featuredContents"][0]["posts"]["nodes"].map { _1["title"] })
.to match_array(featured_content.posts.first(3).map(&:title))
end
end
end
18 changes: 18 additions & 0 deletions spec/graphql/types/post_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
content count
}
}
featuredContents {
title
}
}
}
GQL
Expand All @@ -35,4 +38,19 @@
]
end
end

describe "#featured_contents" do
before do
create_list(:featured_content, 3, posts: [post])
end

subject do
execute_graphql(query_string, variables: { postUuid: post.uuid })
end

it "should return featured content groups" do
expect(subject["errors"]).to be_nil
expect(subject["data"]["post"]["featuredContents"].length).to eq(3)
end
end
end

0 comments on commit dd54d15

Please sign in to comment.