Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial caching #53

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TweetsController < ApplicationController
skip_forgery_protection only: %i[track receive_metrics] # Skip CSRF protection for Tampermonkey POST requests
caches_action :index, :show, expires_in: 5.minutes
caches_action :show, expires_in: 5.minutes
before_action :authenticate_user!, only: %i[new create track]

def index
Expand Down Expand Up @@ -37,7 +37,6 @@ def create
@tweet = current_user.tweets.new(create_tweet_params)

if @tweet.save
expire_action action: :index
redirect_to @tweet
else
respond_to do |format|
Expand All @@ -55,8 +54,7 @@ def receive_metrics

tweet.update!(tweet_params.except(:uuid)) if tweet_params[:body].present?

if tweet.tweet_metrics.count < 10
expire_action action: :index
if tweet.tweet_metrics.count < 50
expire_action action: :show, id: tweet.id
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/tweet_metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
# fk_rails_... (user_id => users.id)
#
class TweetMetric < ApplicationRecord
belongs_to :tweet
belongs_to :tweet, touch: true
belongs_to :user
end
22 changes: 22 additions & 0 deletions app/views/tweets/_combined_chart_tweet.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.card
.card-content
.media
.media-left
figure class="image is-48x48"
img src="#{tweet.author_avatar_url}" class="is-rounded"
.media-content
p class="title is-4"
= link_to tweet.author_name, tweet.author_url, target: "_blank"
p class="subtitle is-6"
= tweet.body&.truncate(280)
.content
= tweet.combined_chart
footer.card-footer
= link_to tweet_path(tweet), class: "card-footer-item primary" do
span View Metrics
span.icon
i class="fa fa-chart-line"
= link_to tweet.url, class: "card-footer-item", target: "_blank" do
span View Tweet
span.icon
i class="fa fa-twitter"
24 changes: 24 additions & 0 deletions app/views/tweets/_tweet_card.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.column.is-3.is-flex.is-flex-direction-column
.card.is-flex.is-flex-direction-column.is-flex-grow-1
.card-content.is-flex.is-flex-direction-column.is-flex-grow-1
.media
.media-left
figure class="image is-48x48"
img src="#{tweet.author_avatar_url}" class="is-rounded"
.media-content
.title.is-4
= link_to tweet.author_name, tweet.url, target: "_blank"
.content
.field.is-grouped.is-grouped-multiline.mb-1
.control
= tweet.status_tag
= tweet.body&.truncate(280)
.card-footer
= link_to tweet_path(tweet), class: "card-footer-item primary" do
span View Metrics
span.icon
i class="fa fa-chart-line"
= link_to tweet.url, class: "card-footer-item", target: "_blank" do
span View Tweet
span.icon
i class="fa fa-twitter"
50 changes: 2 additions & 48 deletions app/views/tweets/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,10 @@ section.hero.is-info
.column.is-10
.section
= link_to 'Add Tweet', new_tweet_path, class: 'button is-primary is-fullwidth is-large'
- @tweets.each do |tweet|
.card
.card-content
.media
.media-left
figure class="image is-48x48"
img src="#{tweet.author_avatar_url}" class="is-rounded"
.media-content
p class="title is-4"
= link_to tweet.author_name, tweet.author_url, target: "_blank"
p class="subtitle is-6"
= tweet.body&.truncate(280)
.content
= tweet.combined_chart
footer.card-footer
= link_to tweet_path(tweet), class: "card-footer-item primary" do
span View Metrics
span.icon
i class="fa fa-chart-line"
= link_to tweet.url, class: "card-footer-item", target: "_blank" do
span View Tweet
span.icon
i class="fa fa-twitter"
= render partial: "combined_chart_tweet", collection: @tweets, as: :tweet, cached: true
.column.is-1
/ Display other tweets as cards
.columns.is-centered
.column.is-10
.columns.is-centered.is-multiline
- @other_tweets.each do |tweet|
.column.is-3.is-flex.is-flex-direction-column
.card.is-flex.is-flex-direction-column.is-flex-grow-1
.card-content.is-flex.is-flex-direction-column.is-flex-grow-1
.media
.media-left
figure class="image is-48x48"
img src="#{tweet.author_avatar_url}" class="is-rounded"
.media-content
.title.is-4
= link_to tweet.author_name, tweet.url, target: "_blank"
.content
.field.is-grouped.is-grouped-multiline.mb-1
.control
= tweet.status_tag
= tweet.body&.truncate(280)
.card-footer
= link_to tweet_path(tweet), class: "card-footer-item primary" do
span View Metrics
span.icon
i class="fa fa-chart-line"
= link_to tweet.url, class: "card-footer-item", target: "_blank" do
span View Tweet
span.icon
i class="fa fa-twitter"
= render partial: "tweet_card", collection: @other_tweets, as: :tweet
8 changes: 8 additions & 0 deletions spec/models/tweet_metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
expect(association.macro).to eq(:belongs_to)
end

it "touches the tweet" do
tweet = create(:tweet, created_at: 1.hour.ago, updated_at: 1.hour.ago)

expect {
create(:tweet_metric, tweet: tweet)
}.to change { tweet.updated_at }
end

it "belongs to a user" do
association = described_class.reflect_on_association(:user)
expect(association.macro).to eq(:belongs_to)
Expand Down
Loading