Skip to content
This repository was archived by the owner on Jul 25, 2025. It is now read-only.

Commit 17ced09

Browse files
committed
Add author association to articles, FAQs, and terms
- Introduced `authorship` relationship in Article, Faq, and Term models to link authors. - Updated controllers to include author data in queries for articles, FAQs, and terms. - Modified views to display author information where applicable. - Added new routes and sitemap entries for authors and the About Us page. - Created a new Author model with relevant fields and associations. This enhances content attribution and improves user navigation to author profiles.
1 parent 285426f commit 17ced09

29 files changed

Lines changed: 586 additions & 10 deletions

app/avo/resources/article.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ def fields
1414
field :slug, as: :text
1515
field :content, as: :easy_mde
1616
field :publish_at, as: :date_time
17-
field :author_name, as: :text
17+
field :author_name, as: :text, hide_on: [:forms]
18+
19+
tabs do
20+
tab "Author" do
21+
field :authorship, as: :has_one
22+
field :author, as: :has_one, through: :authorship
23+
end
24+
end
1825
end
1926
end

app/avo/resources/author.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Avo::Resources::Author < Avo::BaseResource
2+
self.find_record_method = -> {
3+
query.find_by_slug(id)
4+
}
5+
6+
self.includes = []
7+
self.search = {
8+
query: -> { query.ransack(name_cont: params[:q], bio_cont: params[:q], email_cont: params[:q], m: "or").result(distinct: false) }
9+
}
10+
11+
def fields
12+
field :id, as: :id
13+
field :name, as: :text, required: true
14+
field :slug, as: :text, required: true
15+
field :bio, as: :textarea
16+
field :avatar_url, as: :text
17+
field :position, as: :text
18+
field :email, as: :text
19+
field :social_links, as: :code, language: 'javascript'
20+
21+
tabs do
22+
tab "Content" do
23+
field :articles, as: :has_many
24+
field :terms, as: :has_many
25+
field :faqs, as: :has_many
26+
end
27+
end
28+
end
29+
end

app/avo/resources/authorship.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Avo::Resources::Authorship < Avo::BaseResource
2+
self.includes = [:author, :authorable]
3+
4+
def fields
5+
field :id, as: :id
6+
field :author, as: :belongs_to, searchable: true
7+
field :authorable, as: :belongs_to, polymorphic_as: :authorable,
8+
types: [::Article, ::Term, ::Faq]
9+
field :role, as: :select, options: {
10+
primary: "Primary",
11+
contributor: "Contributor",
12+
editor: "Editor",
13+
reviewer: "Reviewer"
14+
}, default: "primary"
15+
field :position, as: :number, default: 0
16+
end
17+
end

app/avo/resources/faq.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@ def fields
1414
field :answer, as: :easy_mde
1515
field :slug, as: :text
1616
field :category, as: :select, options: Faq::CATEGORIES
17+
18+
tabs do
19+
tab "Author" do
20+
field :authorship, as: :has_one
21+
field :author, as: :has_one, through: :authorship
22+
end
23+
end
1724
end
1825
end

app/avo/resources/term.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@ def fields
1818
field :video_description, as: :textarea
1919
field :video_upload_date, as: :date
2020
field :video_duration, as: :text
21+
22+
tabs do
23+
tab "Author" do
24+
field :authorship, as: :has_one
25+
field :author, as: :has_one, through: :authorship
26+
end
27+
end
2128
end
2229
end

app/controllers/articles_controller.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ class ArticlesController < ApplicationController
1010
# @example
1111
# GET /articles
1212
def index
13-
@featured_article = Article.published.latest.first
13+
@featured_article = Article.published.latest.includes(:authorship => :author).first
1414
@pagy, @articles = pagy(
1515
Article.published
1616
.where.not(id: @featured_article&.id)
17-
.latest,
17+
.latest
18+
.includes(:authorship => :author),
1819
items: 6,
1920
limit: 6
2021
)
@@ -28,7 +29,7 @@ def index
2829
# @example
2930
# GET /articles/my-first-article
3031
def show
31-
@article = Article.find_by(slug: params[:id])
32+
@article = Article.includes(:authorship => :author).find_by(slug: params[:id])
3233
redirect_to articles_path unless @article
3334
end
3435
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class AuthorsController < ApplicationController
2+
before_action :set_author, only: [:show]
3+
4+
def index
5+
@authors = Author.includes(:authorships).order(:name)
6+
end
7+
8+
def show
9+
@articles = @author.articles.published.latest.includes(:authorship => :author)
10+
@terms = @author.terms.includes(:authorship => :author)
11+
@faqs = @author.faqs.includes(:authorship => :author)
12+
13+
@content_count = @articles.count + @terms.count + @faqs.count
14+
end
15+
16+
private
17+
18+
def set_author
19+
@author = Author.find_by!(slug: params[:slug])
20+
end
21+
end

app/controllers/faqs_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def index
4141
# @example
4242
# GET /faqs/what-is-compound-interest
4343
def show
44-
@faq = Faq.find_by(slug: params[:id])
44+
@faq = Faq.includes(:authorship => :author).find_by(slug: params[:id])
4545

4646
unless @faq
4747
redirect_to faqs_path, alert: "FAQ not found"

app/controllers/pages_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def tos
2727
def privacy
2828
end
2929

30+
# GET /about
31+
# Renders the About Us page.
32+
def about
33+
@stars = Rails.cache.fetch("stargazers_count", expires_in: 72.hours) do
34+
fetch_stars_count
35+
end
36+
end
37+
3038

3139
# GET /sitemap.xml
3240
# Generates a sitemap index file with links to multiple sitemaps.
@@ -37,6 +45,7 @@ def sitemap_index
3745
@articles = Article.all.order(publish_at: :desc).where("publish_at <= ?", Time.now)
3846
@faqs = Faq.all.order(:question)
3947
@tools = Tool.all
48+
@authors = Author.all
4049

4150
@exchange_rate_currencies = Tool::Presenter::ExchangeRateCalculator.new.currency_options
4251
respond_to do |format|

app/controllers/terms_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def index
2626
# @example
2727
# GET /terms/ruby-on-rails
2828
def show
29-
@term = Term.find_by(slug: params[:id])
29+
@term = Term.includes(:authorship => :author).find_by(slug: params[:id])
3030

3131
if @term.nil?
3232
redirect_to terms_path, alert: "The financial term you're looking for could not be found."

0 commit comments

Comments
 (0)