From 3d4e82f94f5b59cc1ee8c4ca58f9bf7044235807 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 21 Oct 2015 13:34:37 +0200 Subject: [PATCH 1/3] Adds erased_at to user via a migration --- db/migrate/20151021113348_add_erased_at_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20151021113348_add_erased_at_to_users.rb diff --git a/db/migrate/20151021113348_add_erased_at_to_users.rb b/db/migrate/20151021113348_add_erased_at_to_users.rb new file mode 100644 index 000000000..9431c3861 --- /dev/null +++ b/db/migrate/20151021113348_add_erased_at_to_users.rb @@ -0,0 +1,5 @@ +class AddErasedAtToUsers < ActiveRecord::Migration + def change + add_column :users, :erased_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 8b03adc9e..703ddd0d7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151020112354) do +ActiveRecord::Schema.define(version: 20151021113348) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -294,6 +294,7 @@ ActiveRecord::Schema.define(version: 20151020112354) do t.integer "failed_census_calls_count", default: 0 t.datetime "level_two_verified_at" t.string "erase_reason" + t.datetime "erased_at" end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree From 6c5d1faa9062a60b1dba9876b05ba60ac0fdc641 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 21 Oct 2015 14:28:21 +0200 Subject: [PATCH 2/3] Uses the new field, erased_at, for erased users --- app/models/user.rb | 10 +++-- app/views/comments/_comment.html.erb | 4 +- app/views/debates/_debate.html.erb | 2 +- app/views/debates/show.html.erb | 2 +- app/views/proposals/_proposal.html.erb | 2 +- app/views/proposals/show.html.erb | 2 +- spec/features/comments/debates_spec.rb | 12 ++++++ spec/features/comments/proposals_spec.rb | 12 ++++++ spec/features/debates_spec.rb | 12 ++++++ spec/features/proposals_spec.rb | 12 ++++++ spec/models/comment_spec.rb | 5 +++ spec/models/debate_spec.rb | 5 +++ spec/models/proposal_spec.rb | 51 ++++++++++++++++++++++++ spec/models/user_spec.rb | 2 +- 14 files changed, 123 insertions(+), 10 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 177befd5c..b6ee626d6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -147,8 +147,8 @@ class User < ActiveRecord::Base end def erase(erase_reason = nil) - self.hide self.update( + erased_at: Time.now, erase_reason: erase_reason, username: nil, email: nil, @@ -162,6 +162,10 @@ class User < ActiveRecord::Base ) end + def erased? + erased_at.present? + end + def email_provided? !!(email && email !~ OMNIAUTH_EMAIL_REGEX) || !!(unconfirmed_email && unconfirmed_email !~ OMNIAUTH_EMAIL_REGEX) @@ -189,11 +193,11 @@ class User < ActiveRecord::Base end def username_required? - !organization? && !hidden? + !organization? && !erased? end def email_required? - !hidden? + !erased? end private diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 5a87dde7d..ac61483fc 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -19,7 +19,7 @@ <% else %> <%= avatar_image(comment.user, seed: comment.user_id, size: 32, class: "left") %> <% end %> - <% if comment.user.hidden? %> + <% if comment.user.hidden? || comment.user.erased? %> <% end %> <% end %> @@ -33,7 +33,7 @@ <%= t("comments.comment.moderator") %> #<%= comment.moderator_id%> <% else %> - <% if comment.user.hidden? %> + <% if comment.user.hidden? || comment.user.erased? %> <%= t("comments.comment.user_deleted") %> <% else %> <%= comment.user.name %> diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index d9a4fe7d5..6148630d3 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -14,7 +14,7 @@  •  <%= l debate.created_at.to_date %> - <% if debate.author.hidden? %> + <% if debate.author.hidden? || debate.author.erased? %>  •  <%= t("debates.show.author_deleted") %> diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 8adc9ce27..371260550 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -23,7 +23,7 @@
<%= avatar_image(@debate.author, seed: @debate.author_id, size: 32, class: 'author-photo') %> - <% if @debate.author.hidden? %> + <% if @debate.author.hidden? || @debate.author.erased? %> <%= t("debates.show.author_deleted") %> diff --git a/app/views/proposals/_proposal.html.erb b/app/views/proposals/_proposal.html.erb index c536c897f..ac2f0ec2f 100644 --- a/app/views/proposals/_proposal.html.erb +++ b/app/views/proposals/_proposal.html.erb @@ -13,7 +13,7 @@  •  <%= l proposal.created_at.to_date %> - <% if proposal.author.hidden? %> + <% if proposal.author.hidden? || proposal.author.erased? %>  •  <%= t("proposals.show.author_deleted") %> diff --git a/app/views/proposals/show.html.erb b/app/views/proposals/show.html.erb index 06e61937a..95adf93aa 100644 --- a/app/views/proposals/show.html.erb +++ b/app/views/proposals/show.html.erb @@ -29,7 +29,7 @@
<%= avatar_image(@proposal.author, seed: @proposal.author_id, size: 32, class: 'author-photo') %> - <% if @proposal.author.hidden? %> + <% if @proposal.author.hidden? || @proposal.author.erased? %> <%= t("proposals.show.author_deleted") %> diff --git a/spec/features/comments/debates_spec.rb b/spec/features/comments/debates_spec.rb index 4b8b23ce8..1fad7affe 100644 --- a/spec/features/comments/debates_spec.rb +++ b/spec/features/comments/debates_spec.rb @@ -193,6 +193,18 @@ feature 'Commenting debates' do end end + scenario "Erasing a comment's author" do + debate = create(:debate) + comment = create(:comment, commentable: debate, body: 'this should be visible') + comment.user.erase + + visit debate_path(debate) + within "#comment_#{comment.id}" do + expect(page).to have_content('Deleted user') + expect(page).to have_content('this should be visible') + end + end + feature "Moderators" do scenario "can create comment as a moderator", :js do moderator = create(:moderator) diff --git a/spec/features/comments/proposals_spec.rb b/spec/features/comments/proposals_spec.rb index 9c6b99a6a..ad3c26be9 100644 --- a/spec/features/comments/proposals_spec.rb +++ b/spec/features/comments/proposals_spec.rb @@ -193,6 +193,18 @@ feature 'Commenting proposals' do end end + scenario "Erasing a comment's author" do + proposal = create(:proposal) + comment = create(:comment, commentable: proposal, body: "this should be visible") + comment.user.erase + + visit proposal_path(proposal) + within "#comment_#{comment.id}" do + expect(page).to have_content('Deleted user') + expect(page).to have_content('this should be visible') + end + end + feature "Moderators" do scenario "can create comment as a moderator", :js do moderator = create(:moderator) diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index c633f1b2f..717f02c20 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -523,4 +523,16 @@ feature 'Debates' do visit debate_path(good_debate) expect(page).to_not have_content "This debate has been flag as innapropiate for some users." end + + scenario 'Erased author' do + user = create(:user) + debate = create(:debate, author: user) + user.erase + + visit debates_path + expect(page).to have_content('Deleted user') + + visit debate_path(debate) + expect(page).to have_content('Deleted user') + end end diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 7bb1146ec..d15ade63b 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -605,4 +605,16 @@ feature 'Proposals' do expect(Flag.flagged?(user, proposal)).to_not be end + + scenario 'Erased author' do + user = create(:user) + proposal = create(:proposal, author: user) + user.erase + + visit proposals_path + expect(page).to have_content('Deleted user') + + visit proposal_path(proposal) + expect(page).to have_content('Deleted user') + end end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index fdb632986..6d37eb71b 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -57,6 +57,11 @@ describe Comment do .to change { [comment.reload.updated_at, comment.author.updated_at] } end + it "expires cache when the author is erased" do + expect { comment.user.erase } + .to change { [comment.reload.updated_at, comment.author.updated_at] } + end + it "expires cache when the author changes" do expect { comment.user.update(username: "Isabel") } .to change { [comment.reload.updated_at, comment.author.updated_at] } diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index a9ef3892a..159ebdbb1 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -380,6 +380,11 @@ describe Debate do .to change { [debate.reload.updated_at, debate.author.updated_at] } end + it "should expire cache when the author is erased" do + expect { debate.author.erase } + .to change { [debate.reload.updated_at, debate.author.updated_at] } + end + it "should expire cache when its author changes" do expect { debate.author.update(username: "Eva") } .to change { [debate.reload.updated_at, debate.author.updated_at] } diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index 964f8f057..bb13f0769 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -304,4 +304,55 @@ describe Proposal do end end + + describe "cache" do + let(:proposal) { create(:proposal) } + + it "should expire cache when it has a new comment" do + expect { create(:comment, commentable: proposal) } + .to change { proposal.updated_at } + end + + it "should expire cache when it has a new vote" do + expect { create(:vote, votable: proposal) } + .to change { proposal.updated_at } + end + + it "should expire cache when it has a new flag" do + expect { create(:flag, flaggable: proposal) } + .to change { proposal.reload.updated_at } + end + + it "should expire cache when it has a new tag" do + expect { proposal.update(tag_list: "new tag") } + .to change { proposal.updated_at } + end + + it "should expire cache when hidden" do + expect { proposal.hide } + .to change { proposal.updated_at } + end + + it "should expire cache when the author is hidden" do + expect { proposal.author.hide } + .to change { [proposal.reload.updated_at, proposal.author.updated_at] } + end + + it "should expire cache when the author is erased" do + expect { proposal.author.erase } + .to change { [proposal.reload.updated_at, proposal.author.updated_at] } + end + + it "should expire cache when its author changes" do + expect { proposal.author.update(username: "Eva") } + .to change { [proposal.reload.updated_at, proposal.author.updated_at] } + end + + it "should expire cache when the author's organization get verified" do + create(:organization, user: proposal.author) + expect { proposal.author.organization.verify } + .to change { [proposal.reload.updated_at, proposal.author.updated_at] } + end + end + end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2e92ced35..9b045a7ed 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -317,7 +317,7 @@ describe User do user.reload expect(user.erase_reason).to eq('a test') - expect(user.hidden_at).to be + expect(user.erased_at).to be expect(user.username).to be_nil From d4bbf0d26eb4b297697ec882988480dcb1bdf8d5 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 21 Oct 2015 14:47:17 +0200 Subject: [PATCH 3/3] refactors shared part of debate & proposal views --- app/views/debates/show.html.erb | 26 +------------------------- app/views/proposals/show.html.erb | 26 +------------------------- app/views/shared/_author_info.html.erb | 25 +++++++++++++++++++++++++ config/locales/en.yml | 2 ++ config/locales/es.yml | 2 ++ 5 files changed, 31 insertions(+), 50 deletions(-) create mode 100644 app/views/shared/_author_info.html.erb diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 371260550..a4613fd3b 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -21,31 +21,7 @@ <% end %>
- <%= avatar_image(@debate.author, seed: @debate.author_id, size: 32, class: 'author-photo') %> - - <% if @debate.author.hidden? || @debate.author.erased? %> - - - <%= t("debates.show.author_deleted") %> - - <% else %> - - <%= @debate.author.name %> - - <% if @debate.author.official? %> -  •  - - <%= @debate.author.official_position %> - - <% end %> - <% end %> - - <% if @debate.author.verified_organization? %> -  •  - - <%= t("shared.collective") %> - - <% end %> + <%= render '/shared/author_info', resource: @debate %>  •  <%= l @debate.created_at.to_date %> diff --git a/app/views/proposals/show.html.erb b/app/views/proposals/show.html.erb index 95adf93aa..4cc5f5e17 100644 --- a/app/views/proposals/show.html.erb +++ b/app/views/proposals/show.html.erb @@ -27,31 +27,7 @@ <% end %>
- <%= avatar_image(@proposal.author, seed: @proposal.author_id, size: 32, class: 'author-photo') %> - - <% if @proposal.author.hidden? || @proposal.author.erased? %> - - - <%= t("proposals.show.author_deleted") %> - - <% else %> - - <%= @proposal.author.name %> - - <% if @proposal.author.official? %> -  •  - - <%= @proposal.author.official_position %> - - <% end %> - <% end %> - - <% if @proposal.author.verified_organization? %> -  •  - - <%= t("shared.collective") %> - - <% end %> + <%= render '/shared/author_info', resource: @proposal %>  •  <%= l @proposal.created_at.to_date %> diff --git a/app/views/shared/_author_info.html.erb b/app/views/shared/_author_info.html.erb new file mode 100644 index 000000000..ee5c1f749 --- /dev/null +++ b/app/views/shared/_author_info.html.erb @@ -0,0 +1,25 @@ +<%= avatar_image(resource.author, seed: resource.author_id, size: 32, class: 'author-photo') %> + +<% if resource.author.hidden? || resource.author.erased? %> + + + <%= t("shared.author_info.author_deleted") %> + +<% else %> + + <%= resource.author.name %> + + <% if resource.author.official? %> +  •  + + <%= resource.author.official_position %> + + <% end %> +<% end %> + +<% if resource.author.verified_organization? %> +  •  + + <%= t("shared.collective") %> + +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index e2d604c72..bd0a6ce3a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -310,6 +310,8 @@ en: debate: "secret code did not match with the image" proposal: "secret code did not match with the image" shared: + author_info: + author_deleted: Deleted user tags_cloud: tags: Trend print: diff --git a/config/locales/es.yml b/config/locales/es.yml index c20524f47..a31572432 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -310,6 +310,8 @@ es: debate: "el código secreto no coincide con la imagen" proposal: "el código secreto no coincide con la imagen" shared: + author_info: + author_deleted: Usuario eliminado tags_cloud: tags: Tendencias print: