From e78b7d2a8c9fc47d37cc29c663590bab58f9603d Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Tue, 18 Oct 2016 15:17:50 -0700 Subject: [PATCH] fixes #2716: Wiki pages should be undeletable --- Gemfile.lock | 6 ++-- .../stylesheets/common/simple_form.css.scss | 1 + app/controllers/wiki_pages_controller.rb | 4 +-- app/models/wiki_page.rb | 30 +++++++++++-------- app/models/wiki_page_version.rb | 2 +- app/views/wiki_page_versions/index.html.erb | 2 ++ app/views/wiki_pages/_form.html.erb | 8 +++-- .../wiki_pages/_secondary_links.html.erb | 2 +- app/views/wiki_pages/search.html.erb | 5 ++++ app/views/wiki_pages/show.html.erb | 4 +++ ...1018221128_add_is_deleted_to_wiki_pages.rb | 7 +++++ db/structure.sql | 8 +++-- 12 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 db/migrate/20161018221128_add_is_deleted_to_wiki_pages.rb diff --git a/Gemfile.lock b/Gemfile.lock index b3bfbf37d..81a1c6c7a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -336,9 +336,9 @@ GEM faraday (~> 0.9) jwt (~> 1.5) multi_json (~> 1.10) - simple_form (3.1.0) - actionpack (~> 4.0) - activemodel (~> 4.0) + simple_form (3.3.1) + actionpack (> 4, < 5.1) + activemodel (> 4, < 5.1) simple_oauth (0.3.1) simplecov (0.10.0) docile (~> 1.1.0) diff --git a/app/assets/stylesheets/common/simple_form.css.scss b/app/assets/stylesheets/common/simple_form.css.scss index 11d44233f..ee64e96a6 100644 --- a/app/assets/stylesheets/common/simple_form.css.scss +++ b/app/assets/stylesheets/common/simple_form.css.scss @@ -5,6 +5,7 @@ form.simple_form { label { display: inline; vertical-align: middle; + margin-left: 0.5em; } } diff --git a/app/controllers/wiki_pages_controller.rb b/app/controllers/wiki_pages_controller.rb index 383f48c87..daed5b0d6 100644 --- a/app/controllers/wiki_pages_controller.rb +++ b/app/controllers/wiki_pages_controller.rb @@ -1,7 +1,7 @@ class WikiPagesController < ApplicationController respond_to :html, :xml, :json, :js before_filter :member_only, :except => [:index, :show, :show_or_new] - before_filter :moderator_only, :only => [:destroy] + before_filter :builder_only, :only => [:destroy] before_filter :normalize_search_params, :only => [:index] rescue_from ActiveRecord::StatementInvalid, :with => :rescue_exception rescue_from ActiveRecord::RecordNotFound, :with => :rescue_exception @@ -61,7 +61,7 @@ class WikiPagesController < ApplicationController def destroy @wiki_page = WikiPage.find(params[:id]) - @wiki_page.destroy + @wiki_page.update_attribute(:is_deleted, true) respond_with(@wiki_page) end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index f32699763..1021f8c36 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -6,14 +6,13 @@ class WikiPage < ActiveRecord::Base before_validation :initialize_creator, :on => :create before_validation :initialize_updater after_save :create_version - before_destroy :create_mod_action_for_destroy belongs_to :creator, :class_name => "User" belongs_to :updater, :class_name => "User" validates_uniqueness_of :title, :case_sensitive => false validates_presence_of :title - validate :validate_locker_is_moderator + validate :validate_locker_is_builder validate :validate_not_locked - attr_accessible :title, :body, :is_locked, :other_names + attr_accessible :title, :body, :is_locked, :is_deleted, :other_names has_one :tag, :foreign_key => "name", :primary_key => "title" has_one :artist, lambda {where(:is_active => true)}, :foreign_key => "name", :primary_key => "title" has_many :versions, lambda {order("wiki_page_versions.id ASC")}, :class_name => "WikiPageVersion", :dependent => :destroy @@ -23,6 +22,10 @@ class WikiPage < ActiveRecord::Base where("title = ?", title.mb_chars.downcase.tr(" ", "_")) end + def active + where("is_deleted = false") + end + def recent order("updated_at DESC").limit(25) end @@ -65,6 +68,10 @@ class WikiPage < ActiveRecord::Base q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].tr(" ", "_").mb_chars.downcase) end + if params[:hide_deleted] =~ /y/i + q = q.where("is_deleted = false") + end + if params[:other_names_present] == "yes" q = q.where("other_names is not null and other_names != ''") elsif params[:other_names_present] == "no" @@ -114,15 +121,15 @@ class WikiPage < ActiveRecord::Base titled(title).select("title, id").first end - def validate_locker_is_moderator - if is_locked_changed? && !CurrentUser.is_moderator? - errors.add(:is_locked, "can be modified by moderators only") + def validate_locker_is_builder + if is_locked_changed? && !CurrentUser.is_builder? + errors.add(:is_locked, "can be modified by builders only") return false end end def validate_not_locked - if is_locked? && !CurrentUser.is_moderator? + if is_locked? && !CurrentUser.is_builder? errors.add(:is_locked, "and cannot be updated") return false end @@ -187,12 +194,13 @@ class WikiPage < ActiveRecord::Base :title => title, :body => body, :is_locked => is_locked, + :is_deleted => is_deleted, :other_names => other_names ) end def create_version - if title_changed? || body_changed? || is_locked_changed? || other_names_changed? + if title_changed? || body_changed? || is_locked_changed? || is_deleted_changed? || other_names_changed? if merge_version? merge_version else @@ -231,12 +239,8 @@ class WikiPage < ActiveRecord::Base end.map {|x| x.mb_chars.downcase.tr(" ", "_").to_s} end - def create_mod_action_for_destroy - ModAction.create(:description => "permanently deleted wiki page [[#{title}]]") - end - def visible? - artist.blank? || !artist.is_banned? || CurrentUser.is_moderator? + artist.blank? || !artist.is_banned? || CurrentUser.is_builder? end def other_names_array diff --git a/app/models/wiki_page_version.rb b/app/models/wiki_page_version.rb index 962d13a7f..5e580061d 100644 --- a/app/models/wiki_page_version.rb +++ b/app/models/wiki_page_version.rb @@ -41,7 +41,7 @@ class WikiPageVersion < ActiveRecord::Base end def visible? - artist.blank? || !artist.is_banned? || CurrentUser.is_moderator? + artist.blank? || !artist.is_banned? || CurrentUser.is_builder? end def other_names_array diff --git a/app/views/wiki_page_versions/index.html.erb b/app/views/wiki_page_versions/index.html.erb index 3f6811454..21e352480 100644 --- a/app/views/wiki_page_versions/index.html.erb +++ b/app/views/wiki_page_versions/index.html.erb @@ -15,6 +15,7 @@ <% end %> Title + Del <% if CurrentUser.is_moderator? %> IP Address @@ -49,6 +50,7 @@ <% end %> <%= link_to wiki_page_version.title, wiki_page_version_path(wiki_page_version) %> + <%= wiki_page_version.is_deleted? ? "Y" : "" %> <%= link_to "wiki", wiki_page_path(wiki_page_version.wiki_page_id) %> <% if CurrentUser.is_moderator? %> diff --git a/app/views/wiki_pages/_form.html.erb b/app/views/wiki_pages/_form.html.erb index d7c0a3f8b..2191441d7 100644 --- a/app/views/wiki_pages/_form.html.erb +++ b/app/views/wiki_pages/_form.html.erb @@ -18,8 +18,12 @@ <%= dtext_field "wiki_page", "body" %> - <% if CurrentUser.is_moderator? %> - <%= f.input :is_locked %> + <% if CurrentUser.is_builder? && @wiki_page.is_deleted? %> + <%= f.input :is_deleted, :label => "Deleted", :hint => "Uncheck to restore this wiki page" %> + <% end %> + + <% if CurrentUser.is_builder? %> + <%= f.input :is_locked, :label => "Locked" %> <% end %> <%= f.button :submit, "Submit", :data => { :disable_with => "Submitting..." } %> diff --git a/app/views/wiki_pages/_secondary_links.html.erb b/app/views/wiki_pages/_secondary_links.html.erb index 138081219..dc7844b8a 100644 --- a/app/views/wiki_pages/_secondary_links.html.erb +++ b/app/views/wiki_pages/_secondary_links.html.erb @@ -18,7 +18,7 @@ <% if CurrentUser.is_member? %>
  • <%= link_to "Edit", edit_wiki_page_path(@wiki_page), :id => "wiki-page-edit-link" %>
  • <% end %> - <% if CurrentUser.is_moderator? %> + <% if CurrentUser.is_builder? && !@wiki_page.is_deleted? %>
  • <%= link_to "Delete", wiki_page_path(@wiki_page), :remote => true, :method => :delete, :data => {:confirm => "Are you sure you want to delete this wiki page?"} %>
  • <% end %> <% end %> diff --git a/app/views/wiki_pages/search.html.erb b/app/views/wiki_pages/search.html.erb index 92c951eca..fb312f803 100644 --- a/app/views/wiki_pages/search.html.erb +++ b/app/views/wiki_pages/search.html.erb @@ -16,6 +16,11 @@ <%= select "search", "order", ["Name", "Date"] %> +
    + + <%= select "search", "hide_deleted", ["Yes", "No"] %> +
    + <%= submit_tag "Search" %> <% end %> diff --git a/app/views/wiki_pages/show.html.erb b/app/views/wiki_pages/show.html.erb index e8d260d2d..3e5a68108 100644 --- a/app/views/wiki_pages/show.html.erb +++ b/app/views/wiki_pages/show.html.erb @@ -10,6 +10,10 @@ <% if @wiki_page.is_locked? %> (locked) <% end %> + + <% if @wiki_page.is_deleted? %> + (deleted) + <% end %>
    diff --git a/db/migrate/20161018221128_add_is_deleted_to_wiki_pages.rb b/db/migrate/20161018221128_add_is_deleted_to_wiki_pages.rb new file mode 100644 index 000000000..20e7130b1 --- /dev/null +++ b/db/migrate/20161018221128_add_is_deleted_to_wiki_pages.rb @@ -0,0 +1,7 @@ +class AddIsDeletedToWikiPages < ActiveRecord::Migration + def change + execute "set statement_timeout = 0" + add_column :wiki_pages, :is_deleted, :boolean, :null => false, :default => false + add_column :wiki_page_versions, :is_deleted, :boolean, :null => false, :default => false + end +end diff --git a/db/structure.sql b/db/structure.sql index e01eb3848..38a30dec9 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -3305,7 +3305,8 @@ CREATE TABLE wiki_page_versions ( is_locked boolean NOT NULL, created_at timestamp without time zone, updated_at timestamp without time zone, - other_names text + other_names text, + is_deleted boolean DEFAULT false NOT NULL ); @@ -3343,7 +3344,8 @@ CREATE TABLE wiki_pages ( updated_at timestamp without time zone, updater_id integer, other_names text, - other_names_index tsvector + other_names_index tsvector, + is_deleted boolean DEFAULT false NOT NULL ); @@ -7452,3 +7454,5 @@ INSERT INTO schema_migrations (version) VALUES ('20160822230752'); INSERT INTO schema_migrations (version) VALUES ('20160919234407'); +INSERT INTO schema_migrations (version) VALUES ('20161018221128'); +