From f3df3f4fbca19ec9e5bf932ee88c79bd2add72b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 30 Oct 2019 02:26:42 +0100 Subject: [PATCH] Remove people proposal model This model isn't used anywhere, since it was created as part of a feature which couldn't be completed. This commit reverts commit 46e5d6a9. --- app/models/comment.rb | 2 +- app/models/legislation/people_proposal.rb | 152 ------------------ app/models/legislation/process.rb | 14 +- app/models/setting.rb | 1 - app/models/user.rb | 1 - db/dev_seeds.rb | 1 - db/dev_seeds/legislation_people_proposals.rb | 12 -- db/dev_seeds/legislation_processes.rb | 3 - ...proposal_phase_from_legislation_process.rb | 7 + ...11956_drop_legislation_people_proposals.rb | 9 ++ db/schema.rb | 61 ++----- spec/factories/legislations.rb | 41 ----- .../legislation/people_proposals_spec.rb | 107 ------------ 13 files changed, 28 insertions(+), 383 deletions(-) delete mode 100644 app/models/legislation/people_proposal.rb delete mode 100644 db/dev_seeds/legislation_people_proposals.rb create mode 100644 db/migrate/20191030011813_remove_people_proposal_phase_from_legislation_process.rb create mode 100644 db/migrate/20191030011956_drop_legislation_people_proposals.rb delete mode 100644 spec/models/legislation/people_proposals_spec.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 112ebb9fd..e769aa3df 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -9,7 +9,7 @@ class Comment < ApplicationRecord COMMENTABLE_TYPES = %w[Debate Proposal Budget::Investment Poll Topic Legislation::Question Legislation::Annotation - Legislation::Proposal Legislation::PeopleProposal].freeze + Legislation::Proposal].freeze acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases diff --git a/app/models/legislation/people_proposal.rb b/app/models/legislation/people_proposal.rb deleted file mode 100644 index a0a9fe461..000000000 --- a/app/models/legislation/people_proposal.rb +++ /dev/null @@ -1,152 +0,0 @@ -class Legislation::PeopleProposal < ApplicationRecord - include ActsAsParanoidAliases - include Flaggable - include Taggable - include Conflictable - include Measurable - include Sanitizable - include Searchable - include Filterable - include Followable - include Communitable - include Documentable - include Notifiable - include Imageable - include Randomizable - - accepts_nested_attributes_for :documents, allow_destroy: true - - acts_as_votable - acts_as_paranoid column: :hidden_at - - belongs_to :process, foreign_key: "legislation_process_id", inverse_of: :people_proposals - belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :people_proposals - has_many :comments, as: :commentable, inverse_of: :commentable - - validates :title, presence: true - validates :summary, presence: true - validates :author, presence: true - validates :process, presence: true - - validates :title, length: { in: 4..Legislation::PeopleProposal.title_max_length } - validates :description, length: { maximum: Legislation::PeopleProposal.description_max_length } - - validates :terms_of_service, acceptance: { allow_nil: false }, on: :create - - before_validation :set_responsible_name - - before_save :calculate_hot_score, :calculate_confidence_score - - scope :for_render, -> { includes(:tags) } - scope :sort_by_hot_score, -> { reorder(hot_score: :desc) } - scope :sort_by_confidence_score, -> { reorder(confidence_score: :desc) } - scope :sort_by_created_at, -> { reorder(created_at: :desc) } - scope :sort_by_most_commented, -> { reorder(comments_count: :desc) } - scope :sort_by_title, -> { reorder(title: :asc) } - scope :sort_by_id, -> { reorder(id: :asc) } - scope :sort_by_supports, -> { reorder(cached_votes_score: :desc) } - scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } - scope :last_week, -> { where("people_proposals.created_at >= ?", 7.days.ago) } - scope :validated, -> { where(validated: true) } - scope :selected, -> { where(selected: true) } - scope :winners, -> { selected.sort_by_confidence_score } - - def to_param - "#{id}-#{title}".parameterize - end - - def searchable_values - { title => "A", - author.username => "B", - tag_list.join(" ") => "B", - summary => "C", - description => "D" } - end - - def self.search(terms) - by_code = search_by_code(terms.strip) - by_code.presence || pg_search(terms) - end - - def self.search_by_code(terms) - matched_code = match_code(terms) - results = where(id: matched_code[1]) if matched_code - return results if results.present? && results.first.code == terms - end - - def self.match_code(terms) - /\A#{Setting["proposal_code_prefix"]}-\d\d\d\d-\d\d-(\d*)\z/.match(terms) - end - - def likes - cached_votes_up - end - - def dislikes - cached_votes_down - end - - def total_votes - cached_votes_total - end - - def votes_score - cached_votes_score - end - - def voters - User.active.where(id: votes_for.voters) - end - - def editable? - total_votes <= Setting["max_votes_for_people_proposal_edit"].to_i - end - - def editable_by?(user) - author_id == user.id && editable? - end - - def votable_by?(user) - user&.level_two_or_three_verified? - end - - def register_vote(user, vote_value) - vote_by(voter: user, vote: vote_value) if votable_by?(user) - end - - def code - "#{Setting["proposal_code_prefix"]}-#{created_at.strftime("%Y-%m")}-#{id}" - end - - def after_commented - save # update cache when it has a new comment - end - - def calculate_hot_score - self.hot_score = ScoreCalculator.hot_score(self) - end - - def calculate_confidence_score - self.confidence_score = ScoreCalculator.confidence_score(total_votes, total_votes) - end - - def after_hide - tags.each { |t| t.decrement_custom_counter_for("LegislationPeopleProposal") } - end - - def after_restore - tags.each { |t| t.increment_custom_counter_for("LegislationPeopleProposal") } - end - - def contact_info - [phone, email, website].compact - end - - protected - - def set_responsible_name - if author&.document_number? - self.responsible_name = author.document_number - end - end -end diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index e11bc3b09..bac80536c 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -20,8 +20,7 @@ class Legislation::Process < ApplicationRecord extend DownloadSettings::LegislationProcessCsv PHASES_AND_PUBLICATIONS = %i[homepage_phase draft_phase debate_phase allegations_phase - proposals_phase people_proposals_phase draft_publication - result_publication].freeze + proposals_phase draft_publication result_publication].freeze CSS_HEX_COLOR = /\A#?(?:[A-F0-9]{3}){1,2}\z/i.freeze @@ -41,10 +40,6 @@ class Legislation::Process < ApplicationRecord foreign_key: "legislation_process_id", inverse_of: :process, dependent: :destroy - has_many :people_proposals, -> { order(:id) }, - foreign_key: "legislation_process_id", - inverse_of: :process, - dependent: :destroy validates_translation :title, presence: true validates :start_date, presence: true @@ -56,8 +51,6 @@ class Legislation::Process < ApplicationRecord validates :allegations_start_date, presence: true, if: :allegations_end_date? validates :allegations_end_date, presence: true, if: :allegations_start_date? validates :proposals_phase_end_date, presence: true, if: :proposals_phase_start_date? - validates :people_proposals_phase_end_date, presence: true, - if: :people_proposals_phase_start_date? validate :valid_date_ranges validates :background_color, format: { allow_blank: true, with: CSS_HEX_COLOR } validates :font_color, format: { allow_blank: true, with: CSS_HEX_COLOR } @@ -101,11 +94,6 @@ class Legislation::Process < ApplicationRecord proposals_phase_end_date, proposals_phase_enabled) end - def people_proposals_phase - Legislation::Process::Phase.new(people_proposals_phase_start_date, - people_proposals_phase_end_date, people_proposals_phase_enabled) - end - def draft_publication Legislation::Process::Publication.new(draft_publication_date, draft_publication_enabled) end diff --git a/app/models/setting.rb b/app/models/setting.rb index a63cda9ac..d9d319784 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -143,7 +143,6 @@ class Setting < ApplicationRecord "max_ratio_anon_votes_on_debates": 50, "max_votes_for_debate_edit": 1000, "max_votes_for_proposal_edit": 1000, - "max_votes_for_people_proposal_edit": 1000, "comments_body_max_length": 1000, "proposal_code_prefix": "CONSUL", "votes_for_proposal_success": 10000, diff --git a/app/models/user.rb b/app/models/user.rb index ee618566d..c3a3663a1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,7 +23,6 @@ class User < ApplicationRecord has_many :identities, dependent: :destroy has_many :debates, -> { with_hidden }, foreign_key: :author_id, inverse_of: :author has_many :proposals, -> { with_hidden }, foreign_key: :author_id, inverse_of: :author - has_many :people_proposals, -> { with_hidden }, foreign_key: :author_id, inverse_of: :author has_many :activities has_many :budget_investments, -> { with_hidden }, class_name: "Budget::Investment", diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 1d795b79e..f3c154b64 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -39,7 +39,6 @@ require_relative "dev_seeds/notifications" require_relative "dev_seeds/widgets" require_relative "dev_seeds/admin_notifications" require_relative "dev_seeds/legislation_proposals" -require_relative "dev_seeds/legislation_people_proposals" require_relative "dev_seeds/milestones" require_relative "dev_seeds/pages" diff --git a/db/dev_seeds/legislation_people_proposals.rb b/db/dev_seeds/legislation_people_proposals.rb deleted file mode 100644 index cccd84df3..000000000 --- a/db/dev_seeds/legislation_people_proposals.rb +++ /dev/null @@ -1,12 +0,0 @@ -section "Creating legislation people proposals" do - 10.times do - Legislation::PeopleProposal.create!(title: Faker::Lorem.sentence(3).truncate(60), - description: Faker::Lorem.paragraphs.join("\n\n"), - summary: Faker::Lorem.paragraph, - author: User.all.sample, - process: Legislation::Process.all.sample, - terms_of_service: "1", - validated: rand <= 2.0 / 3, - selected: rand <= 1.0 / 3) - end -end diff --git a/db/dev_seeds/legislation_processes.rb b/db/dev_seeds/legislation_processes.rb index c718685f0..f932549f7 100644 --- a/db/dev_seeds/legislation_processes.rb +++ b/db/dev_seeds/legislation_processes.rb @@ -11,8 +11,6 @@ section "Creating collaborative legislation" do debate_end_date: Date.current + (i - 5).days, proposals_phase_start_date: Date.current + (i - 7).days, proposals_phase_end_date: Date.current + (i - 5).days, - people_proposals_phase_start_date: Date.current + (i - 7).days, - people_proposals_phase_end_date: Date.current + (i - 5).days, draft_publication_date: Date.current + (i - 3).days, allegations_start_date: Date.current + (i - 2).days, allegations_end_date: Date.current + (i - 1).days, @@ -22,7 +20,6 @@ section "Creating collaborative legislation" do draft_publication_enabled: true, result_publication_enabled: true, proposals_phase_enabled: true, - people_proposals_phase_enabled: true, published: true) end diff --git a/db/migrate/20191030011813_remove_people_proposal_phase_from_legislation_process.rb b/db/migrate/20191030011813_remove_people_proposal_phase_from_legislation_process.rb new file mode 100644 index 000000000..228f65af4 --- /dev/null +++ b/db/migrate/20191030011813_remove_people_proposal_phase_from_legislation_process.rb @@ -0,0 +1,7 @@ +class RemovePeopleProposalPhaseFromLegislationProcess < ActiveRecord::Migration[5.0] + def change + remove_column :legislation_processes, :people_proposals_phase_start_date, :date + remove_column :legislation_processes, :people_proposals_phase_end_date, :date + remove_column :legislation_processes, :people_proposals_phase_enabled, :boolean + end +end diff --git a/db/migrate/20191030011956_drop_legislation_people_proposals.rb b/db/migrate/20191030011956_drop_legislation_people_proposals.rb new file mode 100644 index 000000000..feee7ea68 --- /dev/null +++ b/db/migrate/20191030011956_drop_legislation_people_proposals.rb @@ -0,0 +1,9 @@ +class DropLegislationPeopleProposals < ActiveRecord::Migration[5.0] + def up + drop_table :legislation_people_proposals + end + + def down + fail ActiveRecord::IrreversibleMigration + end +end diff --git a/db/schema.rb b/db/schema.rb index c7be92ca8..4ff87d53c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20191024025634) do +ActiveRecord::Schema.define(version: 20191030011956) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -780,44 +780,6 @@ ActiveRecord::Schema.define(version: 20191024025634) do t.index ["status"], name: "index_legislation_draft_versions_on_status", using: :btree end - create_table "legislation_people_proposals", force: :cascade do |t| - t.integer "legislation_process_id" - t.string "title", limit: 80 - t.text "description" - t.integer "author_id" - t.datetime "hidden_at" - t.integer "flags_count", default: 0 - t.datetime "ignored_flag_at" - t.integer "cached_votes_up", default: 0 - t.integer "comments_count", default: 0 - t.datetime "confirmed_hide_at" - t.bigint "hot_score", default: 0 - t.integer "confidence_score", default: 0 - t.string "responsible_name", limit: 60 - t.text "summary" - t.string "video_url" - t.tsvector "tsv" - t.datetime "retired_at" - t.string "retired_reason" - t.text "retired_explanation" - t.integer "community_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "cached_votes_total", default: 0 - t.integer "cached_votes_down", default: 0 - t.boolean "selected" - t.boolean "validated" - t.integer "cached_votes_score", default: 0 - t.string "email" - t.string "phone" - t.string "twitter" - t.string "facebook" - t.string "instagram" - t.string "youtube" - t.string "website" - t.index ["cached_votes_score"], name: "index_legislation_people_proposals_on_cached_votes_score", using: :btree - end - create_table "legislation_process_translations", force: :cascade do |t| t.integer "legislation_process_id", null: false t.string "locale", null: false @@ -848,27 +810,24 @@ ActiveRecord::Schema.define(version: 20191024025634) do t.date "allegations_end_date" t.date "result_publication_date" t.datetime "hidden_at" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.text "summary" - t.boolean "debate_phase_enabled", default: false - t.boolean "allegations_phase_enabled", default: false - t.boolean "draft_publication_enabled", default: false - t.boolean "result_publication_enabled", default: false - t.boolean "published", default: true + t.boolean "debate_phase_enabled", default: false + t.boolean "allegations_phase_enabled", default: false + t.boolean "draft_publication_enabled", default: false + t.boolean "result_publication_enabled", default: false + t.boolean "published", default: true t.date "proposals_phase_start_date" t.date "proposals_phase_end_date" t.boolean "proposals_phase_enabled" t.text "proposals_description" t.date "draft_start_date" t.date "draft_end_date" - t.boolean "draft_phase_enabled", default: false - t.boolean "homepage_enabled", default: false + t.boolean "draft_phase_enabled", default: false + t.boolean "homepage_enabled", default: false t.text "background_color" t.text "font_color" - t.date "people_proposals_phase_start_date" - t.date "people_proposals_phase_end_date" - t.boolean "people_proposals_phase_enabled" t.index ["allegations_end_date"], name: "index_legislation_processes_on_allegations_end_date", using: :btree t.index ["allegations_start_date"], name: "index_legislation_processes_on_allegations_start_date", using: :btree t.index ["debate_end_date"], name: "index_legislation_processes_on_debate_end_date", using: :btree diff --git a/spec/factories/legislations.rb b/spec/factories/legislations.rb index 22c904ffb..94693417b 100644 --- a/spec/factories/legislations.rb +++ b/spec/factories/legislations.rb @@ -13,13 +13,10 @@ FactoryBot.define do allegations_end_date { Date.current + 3.days } proposals_phase_start_date { Date.current } proposals_phase_end_date { Date.current + 2.days } - people_proposals_phase_start_date { Date.current } - people_proposals_phase_end_date { Date.current + 2.days } result_publication_date { Date.current + 5.days } debate_phase_enabled { true } allegations_phase_enabled { true } proposals_phase_enabled { true } - people_proposals_phase_enabled { true } draft_publication_enabled { true } result_publication_enabled { true } published { true } @@ -66,18 +63,6 @@ FactoryBot.define do proposals_phase_enabled { true } end - trait :in_people_proposals_phase do - people_proposals_phase_start_date { Date.current - 1.day } - people_proposals_phase_end_date { Date.current + 2.days } - people_proposals_phase_enabled { true } - end - - trait :upcoming_people_proposals_phase do - people_proposals_phase_start_date { Date.current + 1.day } - people_proposals_phase_end_date { Date.current + 2.days } - people_proposals_phase_enabled { true } - end - trait :published do published { true } end @@ -101,13 +86,10 @@ FactoryBot.define do allegations_end_date { nil } proposals_phase_start_date { nil } proposals_phase_end_date { nil } - people_proposals_phase_start_date { nil } - people_proposals_phase_end_date { nil } result_publication_date { nil } debate_phase_enabled { false } allegations_phase_enabled { false } proposals_phase_enabled { false } - people_proposals_phase_enabled { false } draft_publication_enabled { false } result_publication_enabled { false } published { true } @@ -183,27 +165,4 @@ FactoryBot.define do process factory: :legislation_process author factory: :user end - - factory :legislation_people_proposal, class: "Legislation::PeopleProposal" do - sequence(:title) { |n| "People and group #{n} for a legislation" } - summary { "This law should be implemented by..." } - terms_of_service { "1" } - process factory: :legislation_process - author factory: :user - validated { false } - - trait :with_contact_info do - email { "proposal@test.com" } - website { "https://proposal.io" } - phone { "666666666" } - facebook { "facebook.id" } - twitter { "TwitterId" } - youtube { "youtubechannelid" } - instagram { "instagramid" } - end - - trait :validated do - validated { true } - end - end end diff --git a/spec/models/legislation/people_proposals_spec.rb b/spec/models/legislation/people_proposals_spec.rb deleted file mode 100644 index f03ea952d..000000000 --- a/spec/models/legislation/people_proposals_spec.rb +++ /dev/null @@ -1,107 +0,0 @@ -require "rails_helper" - -describe Legislation::PeopleProposal do - let(:people_proposal) { build(:legislation_people_proposal) } - - it "is valid" do - expect(people_proposal).to be_valid - end - - it "is not valid without a process" do - people_proposal.process = nil - expect(people_proposal).not_to be_valid - end - - it "is not valid without an author" do - people_proposal.author = nil - expect(people_proposal).not_to be_valid - end - - it "is not valid without a title" do - people_proposal.title = nil - expect(people_proposal).not_to be_valid - end - - it "is not valid without a summary" do - people_proposal.summary = nil - expect(people_proposal).not_to be_valid - end - - describe "#hot_score" do - let(:now) { Time.current } - - it "period is correctly calculated to get exact votes per day" do - new_people_proposal = create(:legislation_people_proposal, created_at: 23.hours.ago) - 2.times { new_people_proposal.vote_by(voter: create(:user), vote: "yes") } - expect(new_people_proposal.hot_score).to be 2 - - old_people_proposal = create(:legislation_people_proposal, created_at: 25.hours.ago) - 2.times { old_people_proposal.vote_by(voter: create(:user), vote: "yes") } - expect(old_people_proposal.hot_score).to be 1 - - older_people_proposal = create(:legislation_people_proposal, created_at: 49.hours.ago) - 3.times { older_people_proposal.vote_by(voter: create(:user), vote: "yes") } - expect(old_people_proposal.hot_score).to be 1 - end - - it "remains the same for not voted people_proposals" do - new = create(:legislation_people_proposal, created_at: now) - old = create(:legislation_people_proposal, created_at: 1.day.ago) - older = create(:legislation_people_proposal, created_at: 2.months.ago) - expect(new.hot_score).to be 0 - expect(old.hot_score).to be 0 - expect(older.hot_score).to be 0 - end - - it "increases for people_proposals with more positive votes" do - more_positive_votes = create(:legislation_people_proposal) - 2.times { more_positive_votes.vote_by(voter: create(:user), vote: "yes") } - - less_positive_votes = create(:legislation_people_proposal) - less_positive_votes.vote_by(voter: create(:user), vote: "yes") - - expect(more_positive_votes.hot_score).to be > less_positive_votes.hot_score - end - - it "increases for people_proposals with the same amount of positive votes within less days" do - newer_people_proposal = create(:legislation_people_proposal, created_at: now) - 5.times { newer_people_proposal.vote_by(voter: create(:user), vote: "yes") } - - older_people_proposal = create(:legislation_people_proposal, created_at: 1.day.ago) - 5.times { older_people_proposal.vote_by(voter: create(:user), vote: "yes") } - - expect(newer_people_proposal.hot_score).to be > older_people_proposal.hot_score - end - - it "increases for people_proposals voted within the period (last month by default)" do - newer_people_proposal = create(:legislation_people_proposal, created_at: 2.months.ago) - 20.times { create(:vote, votable: newer_people_proposal, created_at: 3.days.ago) } - - older_people_proposal = create(:legislation_people_proposal, created_at: 2.months.ago) - 20.times { create(:vote, votable: older_people_proposal, created_at: 40.days.ago) } - - expect(newer_people_proposal.hot_score).to be > older_people_proposal.hot_score - end - - describe "actions which affect it" do - let(:people_proposal) { create(:legislation_people_proposal) } - - before do - 5.times { people_proposal.vote_by(voter: create(:user), vote: "yes") } - 2.times { people_proposal.vote_by(voter: create(:user), vote: "no") } - end - - it "increases with positive votes" do - previous = people_proposal.hot_score - 3.times { people_proposal.vote_by(voter: create(:user), vote: "yes") } - expect(previous).to be < people_proposal.hot_score - end - - it "decreases with negative votes" do - previous = people_proposal.hot_score - 3.times { people_proposal.vote_by(voter: create(:user), vote: "no") } - expect(previous).to be > people_proposal.hot_score - end - end - end -end