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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ gem 'thinking-sphinx'
gem 'dotenv'
gem 'dotenv-deployment', require: 'dotenv/deployment'
gem 'unicorn'
gem 'redis-rails'

group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
Expand Down
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ GEM
rdoc (4.2.2)
json (~> 1.4)
redis (3.3.0)
redis-actionpack (5.0.0)
actionpack (>= 4.0.0, < 6)
redis-rack (~> 2.0.0.pre)
redis-store (~> 1.2.0.pre)
redis-activesupport (5.0.1)
activesupport (>= 3, < 6)
redis-store (~> 1.2.0)
redis-rack (2.0.0.pre)
rack (> 1.5, < 3)
redis-store (~> 1.2.0.pre)
redis-rails (5.0.1)
redis-actionpack (~> 5.0.0)
redis-activesupport (~> 5.0.0)
redis-store (~> 1.2.0)
redis-store (1.2.0)
redis (>= 2.2)
remotipart (1.2.1)
responders (2.2.0)
railties (>= 4.2.0, < 5.1)
Expand Down Expand Up @@ -465,6 +481,7 @@ DEPENDENCIES
pg
private_pub
rails (= 4.2)
redis-rails
remotipart
responders
rspec-rails
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_key_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 @@ -2,7 +2,7 @@ class Answer < ActiveRecord::Base
include Voteable
default_scope { order('accepted DESC') }

belongs_to :question
belongs_to :question, touch: true
belongs_to :user
has_many :attachments, as: :attachmentable
has_many :comments, as: :commentable
Expand Down
2 changes: 1 addition & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :commentable, polymorphic: true
belongs_to :commentable, polymorphic: true, touch: true
validates :body, presence: true
end
40 changes: 20 additions & 20 deletions app/views/answers/_answer.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ div { data-answer = answer.id }
span = 'Accepted'

- if answer.persisted?
- cache answer do
= render 'questions/vote', voteable: answer
= render 'questions/comments', commentable: answer

= render 'questions/vote', voteable: answer
= render 'questions/comments', commentable: answer
ul.answer-attachments
- answer.attachments.each do |a|
li { data-id = a.id }
= link_to a.file.filename, a.file.url
= ' '
- if current_user && current_user.author_of?(answer)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Учти, что здесь закешируются ссылки и после перелогина они не изменятся. Например, если в первый раз на страницу зайдет гость или не-автор, ссылок не будет и закешируется такая страница. И даже если после этого автор зайдет на страницу, он ссылок не увидит, т.к. логика в кеше не работает, там уже отрендеренный html.

Вариантов несколько:

  • Добавлять/скрывать ссылки через js
  • Кешировать все, кроме условий. Но тут есть проблема в том, что будет очень много мелких фрагментов кеша и не факт, что это даст хороший прирост скорости
  • В ключ кеширования добавить флаг залогиненн пользователь или нет (можно еще и по автору). Но тут важно добавлять именно true/false, а не id юзера, т.к. в этом случае для каждого юзера будет отдельный кеш и смысл кеширования потеряется вообще.

= link_to 'Remove attachment', destroy_attachment_path(a), method: 'delete', remote: true

ul.answer-attachments
- answer.attachments.each do |a|
li { data-id = a.id }
= link_to a.file.filename, a.file.url
= ' '
- if current_user && current_user.author_of?(answer)
= link_to 'Remove attachment', destroy_attachment_path(a), method: 'delete', remote: true
- if current_user
- if current_user.author_of?(answer)
p = link_to 'Remove answer', question_answer_path(@question, answer), method: 'delete', remote: true
p = link_to 'Edit answer', '', class: 'edit-answer-link', data: { answer_id: answer.id }

- if current_user
- if current_user.author_of?(answer)
p = link_to 'Remove answer', question_answer_path(@question, answer), method: 'delete', remote: true
p = link_to 'Edit answer', '', class: 'edit-answer-link', data: { answer_id: answer.id }
- if current_user.author_of?(@question)
p = link_to 'Accept answer', accept_question_answer_path(@question, answer), class: 'accept-answer-link', method: 'post', remote: true

- if current_user.author_of?(@question)
p = link_to 'Accept answer', accept_question_answer_path(@question, answer), class: 'accept-answer-link', method: 'post', remote: true

= form_for [@question, answer], remote: true, html: {id: "edit-answer-#{answer.id}"} do |f|
= f.label :body, 'Answer'
= f.text_area :body
= f.submit 'Save'
= form_for [@question, answer], remote: true, html: {id: "edit-answer-#{answer.id}"} do |f|
= f.label :body, 'Answer'
= f.text_area :body
= f.submit 'Save'
3 changes: 2 additions & 1 deletion app/views/questions/_comments.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.comments
- commentable.comments.each do |comment|
p= comment.body
- cache comment do
p= comment.body

.comments-form
= form_for [commentable, commentable.comments.build], remote: true do |f|
Expand Down
9 changes: 5 additions & 4 deletions app/views/questions/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
p = 'Is no questions'
- else
table.questions
- @questions.each do |question|
tr
td= link_to question.title, question
td= question.body
- cache collection_cache_key_for :question do
- @questions.each do |question|
tr
td= link_to question.title, question
td= question.body

= subscribe_to "/questions"
52 changes: 27 additions & 25 deletions app/views/questions/show.html.slim
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
.question
= render @question
= render 'vote', voteable: @question
= render 'comments', commentable: @question
- if current_user
- if @subscription = current_user.subscriptions.find_by(question_id: @question.id)
= link_to 'Cancel subscription', question_subscription_path(@question, @subscription), method: 'delete', remote: true, class: 'cancel_subscribe'
- else
= link_to 'Create subscription', question_subscriptions_path(@question), method: 'post', remote: true, class: 'create_subscribe'
- cache @question do
.question
= render @question
= render 'vote', voteable: @question
= render 'comments', commentable: @question
- if current_user
- if @subscription = current_user.subscriptions.find_by(question_id: @question.id)
= link_to 'Cancel subscription', question_subscription_path(@question, @subscription), method: 'delete', remote: true, class: 'cancel_subscribe'
- else
= link_to 'Create subscription', question_subscriptions_path(@question), method: 'post', remote: true, class: 'create_subscribe'

- if current_user && current_user.author_of?(@question)
= link_to 'Remove', question_path(@question), method: 'delete'
- if current_user && current_user.author_of?(@question)
= link_to 'Remove', question_path(@question), method: 'delete'

.answers data={question_id: "#{@question.id}"}
= render @question.answers
= subscribe_to "/questions/#{@question.id}/answers"
= subscribe_to "/questions/#{@question.id}/comments"
.answers data={question_id: "#{@question.id}"}
= render @question.answers

.answer_form
= form_for [@question, @answer], remote: true do |f|
= f.label :body, 'Your Answer'
.answer-errors
= f.text_area :body
ul.files
= f.fields_for :attachments, @answer.attachments.build do |a|
= render 'attachment_fields', f: a
li = link_to_add_association 'Add file', f, :attachments
= f.submit 'Post Your Answer'

.answer_form
= form_for [@question, @answer], remote: true do |f|
= f.label :body, 'Your Answer'
.answer-errors
= f.text_area :body
ul.files
= f.fields_for :attachments, @answer.attachments.build do |a|
= render 'attachment_fields', f: a
li = link_to_add_association 'Add file', f, :attachments
= f.submit 'Post Your Answer'
= subscribe_to "/questions/#{@question.id}/answers"
= subscribe_to "/questions/#{@question.id}/comments"
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Application < Rails::Application
request_specs: false,
controller_specs: true
g.fixture_replacement :factory_girl, dir: 'spec/factories'
config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { expares_in: 90.minutes }
end
end
end
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_controller.perform_caching = true

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
Expand Down