Skip to content
Open
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ gem 'mini_racer'

gem 'unicorn'

gem 'redis-rails'

group :development, :test do
gem 'rspec-rails', '~> 5.0.0'
gem 'factory_bot_rails'
Expand Down
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
redis (4.1.4)
redis-actionpack (5.2.0)
actionpack (>= 5, < 7)
redis-rack (>= 2.1.0, < 3)
redis-store (>= 1.1.0, < 2)
redis-activesupport (5.2.1)
activesupport (>= 3, < 7)
redis-store (>= 1.3, < 2)
redis-rack (2.1.3)
rack (>= 2.0.8, < 3)
redis-store (>= 1.2, < 2)
redis-rails (5.0.2)
redis-actionpack (>= 5.0, < 6)
redis-activesupport (>= 5.0, < 6)
redis-store (>= 1.2, < 2)
redis-store (1.9.0)
redis (>= 4, < 5)
regexp_parser (2.1.1)
request_store (1.5.0)
rack (>= 1.4)
Expand Down Expand Up @@ -515,6 +531,7 @@ DEPENDENCIES
rack-mini-profiler (~> 2.0)
rails (~> 6.1.3, >= 6.1.3.1)
rails-controller-testing
redis-rails
rspec-rails (~> 5.0.0)
rubocop
rubocop-performance
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
module ApplicationHelper
def collection_cache_helper_for(model)
klass = model.to_s.capitalize.constantize
count = klass.count
max_updated_at = klass.maximum(:updated_at).try(:utc).try(:to_s, :number)
"#{model.to_s.pluralize}/collection-#{count}-#{max_updated_at}"
end
end
2 changes: 1 addition & 1 deletion app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Answer < ApplicationRecord
include Votable
include Commentable

belongs_to :question
belongs_to :question, touch: true
belongs_to :author, class_name: 'User', foreign_key: 'user_id'

has_many_attached :files
Expand Down
2 changes: 1 addition & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
belongs_to :commentable, polymorphic: true, touch: true

validates :body, presence: true

Expand Down
2 changes: 1 addition & 1 deletion app/models/link.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Link < ApplicationRecord
belongs_to :linkable, polymorphic: true
belongs_to :linkable, polymorphic: true, touch: true

validates :name, presence: true
validates :url, presence: true, url: { no_local: true }
Expand Down
59 changes: 30 additions & 29 deletions app/views/answers/_answer.html.slim
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
li.answer id="answer-#{answer.id}"
.answer-errors
= render 'shared/errors'
- cache answer do
li.answer id="answer-#{answer.id}"
.answer-errors
= render 'shared/errors'

div
= "Author: #{answer.author.email}"
div
= answer.body
div
= "Author: #{answer.author.email}"
div
= answer.body

.answer-links
= render answer.links
.answer-links
= render answer.links

div
- if answer.files.attached?
= render partial: 'attachments/attachment', collection: answer.files

div id="votable-answer-#{answer.id}"
= render 'votes/rating', votable: answer
- if signed_in? && can?(:create_vote, answer)
= render 'votes/vote', vote: answer.vote_of(current_user), votable: answer

- if can?(:destroy, answer)
div
= link_to 'Delete answer', answer_path(answer), method: :delete, remote: true
- if can?(:update, answer)
div
= link_to 'Edit answer', '#', class: 'edit-answer-link', data: { answer_id: answer.id }
- if answer.files.attached?
= render partial: 'attachments/attachment', collection: answer.files

div id="votable-answer-#{answer.id}"
= render 'votes/rating', votable: answer
- if signed_in? && can?(:create_vote, answer)
= render 'votes/vote', vote: answer.vote_of(current_user), votable: answer

div.hidden
= render partial: '/answers/answer_form', locals: { answer: answer, html_data: { id: "edit-answer-#{answer.id}" } }
- if can?(:destroy, answer)
div
= link_to 'Delete answer', answer_path(answer), method: :delete, remote: true
- if can?(:update, answer)
div
= link_to 'Edit answer', '#', class: 'edit-answer-link', data: { answer_id: answer.id }

-if can?(:mark_best, answer)
div
= link_to 'Best answer', mark_best_answer_path(answer), method: :patch, class: 'best-answer-link', remote: true
div.hidden
= render partial: '/answers/answer_form', locals: { answer: answer, html_data: { id: "edit-answer-#{answer.id}" } }

-if can?(:mark_best, answer)
div
= link_to 'Best answer', mark_best_answer_path(answer), method: :patch, class: 'best-answer-link', remote: true

= render "comments/comments", commentable: answer
= render "comments/comments", commentable: answer
25 changes: 13 additions & 12 deletions app/views/answers/_answer_form.html.slim
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
= form_with model: answer, html: try(:html_data) || {}, local: false do |f|
div
= f.label :body
div
= f.text_area :body
- cache answer do
= form_with model: answer, html: try(:html_data) || {}, local: false do |f|
div
= f.label :body
div
= f.text_area :body

= render 'links/links_form', f: f
= render 'links/links_form', f: f

div
= f.label :files
div
= f.file_field :files, multiple: true, direct_upload: true
div
= f.label :files
div
= f.file_field :files, multiple: true, direct_upload: true

div
= f.submit 'Answer'
div
= f.submit 'Answer'
5 changes: 3 additions & 2 deletions app/views/comments/_comment.html.slim
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
div id="comment-#{comment.id}"
= comment.body
- cache comment do
div id="comment-#{comment.id}"
= comment.body
17 changes: 9 additions & 8 deletions app/views/links/_link_fields.html.slim
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.nested-fields
.field
div = f.label :name
div = f.text_field :name
.field
div = f.label :url
div = f.text_field :url
= link_to_remove_association "Remove link", f
- cache f do
.nested-fields
.field
div = f.label :name
div = f.text_field :name
.field
div = f.label :url
div = f.text_field :url
= link_to_remove_association "Remove link", f
27 changes: 14 additions & 13 deletions app/views/questions/_question_form.html.slim
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
= form_with model: question, local: local do |f|
div = f.label :title
div = f.text_field :title
- cache question do
= form_with model: question, local: local do |f|
div = f.label :title
div = f.text_field :title

div = f.label :body
div = f.text_area :body
div = f.label :body
div = f.text_area :body

= render 'links/links_form', f: f
= render 'links/links_form', f: f

div = f.label :files
div = f.file_field :files, multiple: true, direct_upload: true
div = f.label :files
div = f.file_field :files, multiple: true, direct_upload: true

.award
h3 Award for the best answer
= f.fields_for :award do |award_f|
= render 'awards/award_fields', f: award_f
.award
h3 Award for the best answer
= f.fields_for :award do |award_f|
= render 'awards/award_fields', f: award_f

div = f.submit 'Ask'
div = f.submit 'Ask'
75 changes: 38 additions & 37 deletions app/views/questions/_question_full.html.slim
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
div.question id="question-#{question.id}"
.question-errors
= render 'shared/errors'

h4 = "Title: #{question.title}"
div = "Author: #{question.author.email}"
div = question.body

.question-links
= render question.links

-if question.files.attached?
= render partial: 'attachments/attachment', collection: question.files

div id="votable-question-#{question.id}"
= render 'votes/rating', votable: question
- if signed_in? && can?(:create_vote, question)
= render 'votes/vote', vote: question.vote_of(current_user), votable: question

- if signed_in?
= render 'subscriptions/subscription', question: question

- if question.award.present?
.question-award
h3 = "For the best answer you will receive #{question.award.name}"
.question-award-image
= image_tag question.award.image, size:"250x250"

- if can?(:destroy, question)
div = link_to 'Delete question', question_path(question), method: :delete
- if can?(:update, question)
div = link_to 'Edit question', '#', class: 'edit-question-link', data: { question_id: question.id }

div.hidden
= render 'question_form', question: question, local: false

= render "comments/comments", commentable: question
- cache question do
div.question id="question-#{question.id}"
.question-errors
= render 'shared/errors'

h4 = "Title: #{question.title}"
div = "Author: #{question.author.email}"
div = question.body

.question-links
= render question.links

-if question.files.attached?
= render partial: 'attachments/attachment', collection: question.files

div id="votable-question-#{question.id}"
= render 'votes/rating', votable: question
- if signed_in? && can?(:create_vote, question)
= render 'votes/vote', vote: question.vote_of(current_user), votable: question

- if signed_in?
= render 'subscriptions/subscription', question: question

- if question.award.present?
.question-award
h3 = "For the best answer you will receive #{question.award.name}"
.question-award-image
= image_tag question.award.image, size:"250x250"

- if can?(:destroy, question)
div = link_to 'Delete question', question_path(question), method: :delete
- if can?(:update, question)
div = link_to 'Edit question', '#', class: 'edit-question-link', data: { question_id: question.id }

div.hidden
= render 'question_form', question: question, local: false

= render "comments/comments", commentable: question
5 changes: 3 additions & 2 deletions app/views/questions/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ h1 Questions
p= link_to 'Ask question', new_question_path

ul.questions
- @questions.each do |question|
li = render question
- cache collection_cache_helper_for(:question)
- @questions.each do |question|
li = render question
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Application < Rails::Application
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { expires_in: 90.minutes }

config.generators do |g|
g.test_framework :rspec,
Expand Down
3 changes: 2 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
'Cache-Control' => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true

config.cache_store = :null_store
end
Expand Down