-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #41: Add featured contents models
- Loading branch information
Showing
17 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters