From 3c682b8a9b5bd0fc577d62268b25f151e7c90962 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 12:21:05 +0100 Subject: [PATCH 01/28] adds signature sheets --- .../admin/signature_sheets_controller.rb | 33 +++++++++ app/helpers/signature_sheets_helper.rb | 8 +++ app/models/proposal.rb | 4 +- app/models/signature.rb | 72 +++++++++++++++++++ app/models/signature_sheet.rb | 43 +++++++++++ app/models/spending_proposal.rb | 8 ++- app/views/admin/_menu.html.erb | 8 +++ .../admin/signature_sheets/index.html.erb | 26 +++++++ app/views/admin/signature_sheets/new.html.erb | 9 +++ .../admin/signature_sheets/show.html.erb | 28 ++++++++ app/views/debates/_form.html.erb | 1 - config/initializers/vote_extensions.rb | 3 + config/routes.rb | 2 + .../20161214212918_create_signature_sheets.rb | 11 +++ .../20161214233817_create_signatures.rb | 11 +++ db/schema.rb | 21 +++++- 16 files changed, 282 insertions(+), 6 deletions(-) create mode 100644 app/controllers/admin/signature_sheets_controller.rb create mode 100644 app/helpers/signature_sheets_helper.rb create mode 100644 app/models/signature.rb create mode 100644 app/models/signature_sheet.rb create mode 100644 app/views/admin/signature_sheets/index.html.erb create mode 100644 app/views/admin/signature_sheets/new.html.erb create mode 100644 app/views/admin/signature_sheets/show.html.erb create mode 100644 db/migrate/20161214212918_create_signature_sheets.rb create mode 100644 db/migrate/20161214233817_create_signatures.rb diff --git a/app/controllers/admin/signature_sheets_controller.rb b/app/controllers/admin/signature_sheets_controller.rb new file mode 100644 index 000000000..83d1e93ce --- /dev/null +++ b/app/controllers/admin/signature_sheets_controller.rb @@ -0,0 +1,33 @@ +class Admin::SignatureSheetsController < Admin::BaseController + + def index + @signature_sheets = SignatureSheet.all + end + + def new + @signature_sheet = SignatureSheet.new + end + + def create + @signature_sheet = SignatureSheet.new(signature_sheet_params) + @signature_sheet.author = current_user + if @signature_sheet.save + @signature_sheet.delay.verify_signatures + redirect_to [:admin, @signature_sheet] + else + render :new + end + end + + def show + @signature_sheet = SignatureSheet.find(params[:id]) + @invalid_signatures = @signature_sheet.invalid_signatures + end + + private + + def signature_sheet_params + params.require(:signature_sheet).permit(:signable_type, :signable_id, :document_numbers) + end + +end \ No newline at end of file diff --git a/app/helpers/signature_sheets_helper.rb b/app/helpers/signature_sheets_helper.rb new file mode 100644 index 000000000..14424639c --- /dev/null +++ b/app/helpers/signature_sheets_helper.rb @@ -0,0 +1,8 @@ +module SignatureSheetsHelper + + def signable_options + [[t("activerecord.models.proposal", count: 1), Proposal], + [t("activerecord.models.spending_proposal", count: 1), SpendingProposal]] + end + +end \ No newline at end of file diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 7bd4b7a92..fd828c31e 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -122,9 +122,9 @@ class Proposal < ActiveRecord::Base retired_at.present? end - def register_vote(user, vote_value) + def register_vote(user, vote_value, signature=nil) if votable_by?(user) && !archived? - vote_by(voter: user, vote: vote_value) + vote_by(voter: user, vote: vote_value, vote_scope: signature) end end diff --git a/app/models/signature.rb b/app/models/signature.rb new file mode 100644 index 000000000..3a1d197eb --- /dev/null +++ b/app/models/signature.rb @@ -0,0 +1,72 @@ +class Signature < ActiveRecord::Base + belongs_to :signature_sheet + belongs_to :user + + validate :in_census + validate :not_already_voted + + scope :valid, -> { where(status: 'verified') } + scope :invalid, -> { where.not(status: 'verified') } + + def in_census + return true if user_exists? + errors.add(:document_number, :not_in_census) unless in_census? + end + + def not_already_voted + errors.add(:document_number, :already_voted) if already_voted? + end + + def verify + if valid? + assign_vote + update_attribute(:status, 'verified') + else + error = errors.messages[:document_number].first + update_attribute(:status, error) + end + end + + private + + def assign_vote + if user_exists? + assign_vote_to_user + else + create_user + assign_vote_to_user + end + end + + def assign_vote_to_user + signable.register_vote(user, "yes", "signature") + #Vote.create(votable: signable, voter: user, signature: self) + end + + def user_exists? + user = User.where(document_number: document_number).exists? + end + + def create_user + user = User.where(document_number: document_number, erased_at: Time.now).create + end + + def in_census? + document_types.any? do |document_type| + CensusApi.new.call(document_type, document_number).valid? + end + end + + def already_voted? + signable.voters.where(document_number: document_number).exists? + end + + def signable + signature_sheet.signable + end + + def document_types + %w(1 2 3 4) + end + +end \ No newline at end of file diff --git a/app/models/signature_sheet.rb b/app/models/signature_sheet.rb new file mode 100644 index 000000000..7829833a3 --- /dev/null +++ b/app/models/signature_sheet.rb @@ -0,0 +1,43 @@ +class SignatureSheet < ActiveRecord::Base + belongs_to :signable, polymorphic: true + belongs_to :author, class_name: 'User', foreign_key: 'author_id' + + VALID_SIGNABLES = %w( Proposal SpendingProposal ) + + has_many :signatures + + validates :author, presence: true + validates :signable_type, inclusion: {in: VALID_SIGNABLES} + validates :document_numbers, presence: true + validates :signable, presence: true + validate :signable_found + + def name + "#{signable_name} + #{signable_id}" + end + + def signable_name + I18n.t("activerecord.models.#{signable_type.underscore}", count: 1) + end + + def verify_signatures + parsed_document_numbers.each do |document_number| + signature = signatures.new(document_number: document_number) + signature.save(validate: false) + signature.verify + end + update(processed: true) + end + + def invalid_signatures + signatures.invalid.group_by(&:status) + end + + def parsed_document_numbers + document_numbers.split(",") + end + + def signable_found + errors.add(:signable_id, :not_found) if errors.messages[:signable].present? + end +end \ No newline at end of file diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 223e9adfe..111f4c3dd 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -125,13 +125,17 @@ class SpendingProposal < ActiveRecord::Base return :organization if user.organization? end + def voters + User.active.where(id: votes_for.voters) + end + def votable_by?(user) reason_for_not_being_votable_by(user).blank? end - def register_vote(user, vote_value) + def register_vote(user, vote_value, signature=nil) if votable_by?(user) - vote_by(voter: user, vote: vote_value) + vote_by(voter: user, vote: vote_value, vote_scope: signature) end end diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 3d3e807c7..3cdb057fb 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -35,6 +35,14 @@ <% end %> + <% if feature?(:signature_sheets) %> +
  • > + <%= link_to admin_signature_sheets_path do %> + <%= t("admin.menu.signature_sheets") %> + <% end %> +
  • + <% end %> +
  • > <%= link_to admin_banners_path do %> <%= t("admin.menu.banner") %> diff --git a/app/views/admin/signature_sheets/index.html.erb b/app/views/admin/signature_sheets/index.html.erb new file mode 100644 index 000000000..d03530e3b --- /dev/null +++ b/app/views/admin/signature_sheets/index.html.erb @@ -0,0 +1,26 @@ +

    Signatures

    + +
    + <%= link_to 'Nueva hoja de firmas', new_admin_signature_sheet_path %> +
    + + + + + + + + <% @signature_sheets.each do |signature_sheet| %> + + + + + + <% end %> +
    <%= t("admin.signature_sheet.name") %><%= t("admin.signature_sheet.author") %><%= t("admin.signature_sheet.created_at") %>
    + <%= link_to signature_sheet.name, [:admin, signature_sheet] %> + + <%= signature_sheet.author.name %> + + <%= l(signature_sheet.created_at, format: :short) %> +
    \ No newline at end of file diff --git a/app/views/admin/signature_sheets/new.html.erb b/app/views/admin/signature_sheets/new.html.erb new file mode 100644 index 000000000..c3fe19b55 --- /dev/null +++ b/app/views/admin/signature_sheets/new.html.erb @@ -0,0 +1,9 @@ +<%= form_for [:admin, @signature_sheet] do |f| %> + <%= render 'shared/errors', + resource: @signature_sheet %> + + <%= f.select :signable_type, signable_options %> + <%= f.text_field :signable_id %> + <%= f.text_area :document_numbers %> + <%= f.submit %> +<% end %> \ No newline at end of file diff --git a/app/views/admin/signature_sheets/show.html.erb b/app/views/admin/signature_sheets/show.html.erb new file mode 100644 index 000000000..84f5cb672 --- /dev/null +++ b/app/views/admin/signature_sheets/show.html.erb @@ -0,0 +1,28 @@ +

    <%= @signature_sheet.name %>

    + +
    Documentos: <%= @signature_sheet.document_numbers %>
    +
    Creado: <%= l(@signature_sheet.created_at, format: :short) %>
    +
    Autor: <%= @signature_sheet.author.name %>
    +
    + +
    + Hay <%= @signature_sheet.signatures.valid.count %> firmas válidas +
    + +
    + Hay <%= @signature_sheet.signatures.invalid.count %> firmas inválidas +
    +
    + +<% if @signature_sheet.signatures.invalid.any? %> + <% @invalid_signatures.each do |error, signatures| %> +
    <%= error %>
    +
    + <%= signatures.map(&:document_number).join(",") %> +
    + <% end %> +<% end %> + +<% unless @signature_sheet.processed? %> + Aún hay firmas que se están verificando por el Padrón, por favor refresca la página en unos instantes. +<% end %> \ No newline at end of file diff --git a/app/views/debates/_form.html.erb b/app/views/debates/_form.html.erb index 30152b366..c88045f26 100644 --- a/app/views/debates/_form.html.erb +++ b/app/views/debates/_form.html.erb @@ -1,6 +1,5 @@ <%= form_for(@debate) do |f| %> - <%= render 'shared/errors', resource: @debate %>
    diff --git a/config/initializers/vote_extensions.rb b/config/initializers/vote_extensions.rb index 345cb8f01..fce7b49eb 100644 --- a/config/initializers/vote_extensions.rb +++ b/config/initializers/vote_extensions.rb @@ -1,4 +1,6 @@ ActsAsVotable::Vote.class_eval do + belongs_to :signature + def self.for_debates(debates) where(votable_type: 'Debate', votable_id: debates) end @@ -14,4 +16,5 @@ ActsAsVotable::Vote.class_eval do def value vote_flag end + end diff --git a/config/routes.rb b/config/routes.rb index 885d155db..5019cda40 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -149,6 +149,8 @@ Rails.application.routes.draw do get :summary, on: :collection end + resources :signature_sheets, only: [:index, :new, :create, :show] + resources :banners, only: [:index, :new, :create, :edit, :update, :destroy] do collection { get :search} end diff --git a/db/migrate/20161214212918_create_signature_sheets.rb b/db/migrate/20161214212918_create_signature_sheets.rb new file mode 100644 index 000000000..e3f4e6a58 --- /dev/null +++ b/db/migrate/20161214212918_create_signature_sheets.rb @@ -0,0 +1,11 @@ +class CreateSignatureSheets < ActiveRecord::Migration + def change + create_table :signature_sheets do |t| + t.references :signable, polymorphic: true + t.text :document_numbers + t.boolean :processed + t.references :author + t.timestamps + end + end +end \ No newline at end of file diff --git a/db/migrate/20161214233817_create_signatures.rb b/db/migrate/20161214233817_create_signatures.rb new file mode 100644 index 000000000..c8c178582 --- /dev/null +++ b/db/migrate/20161214233817_create_signatures.rb @@ -0,0 +1,11 @@ +class CreateSignatures < ActiveRecord::Migration + def change + create_table :signatures do |t| + t.references :signature_sheet + t.references :user + t.string :document_number + t.string :status, default: nil + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index a93942873..ab8dead98 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: 20161102133838) do +ActiveRecord::Schema.define(version: 20161214233817) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -326,6 +326,25 @@ ActiveRecord::Schema.define(version: 20161102133838) do add_index "settings", ["key"], name: "index_settings_on_key", using: :btree + create_table "signature_sheets", force: :cascade do |t| + t.integer "signable_id" + t.string "signable_type" + t.text "document_numbers" + t.boolean "processed" + t.integer "author_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "signatures", force: :cascade do |t| + t.integer "signature_sheet_id" + t.integer "user_id" + t.string "document_number" + t.string "status" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "spending_proposals", force: :cascade do |t| t.string "title" t.text "description" From 2eb5dfdd4b63c8354d7c66319f4350552030d3cd Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 12:21:17 +0100 Subject: [PATCH 02/28] adds translations --- config/i18n-tasks.yml | 2 ++ config/locales/activerecord.en.yml | 7 ++++++- config/locales/activerecord.es.yml | 7 ++++++- config/locales/admin.en.yml | 5 +++++ config/locales/admin.es.yml | 5 +++++ config/locales/en.yml | 1 + config/locales/es.yml | 1 + 7 files changed, 26 insertions(+), 2 deletions(-) diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index edaaa0958..eb2e11735 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -95,6 +95,8 @@ search: # - '{devise,simple_form}.*' ignore_missing: - 'unauthorized.*' + - 'activerecord.models.proposal' + - 'activerecord.models.spending_proposal' - 'activerecord.errors.models.proposal_notification.*' - 'activerecord.errors.models.direct_message.*' - 'errors.messages.blank' diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 7de5b340e..b2fc86ce2 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -90,4 +90,9 @@ en: proposal_notification: attributes: minimum_interval: - invalid: "You have to wait a minium of %{interval} days between notifications" \ No newline at end of file + invalid: "You have to wait a minium of %{interval} days between notifications" + signature: + attributes: + document_number: + not_in_census: 'Not verified by Census' + already_voted: 'Already voted this proposal' \ No newline at end of file diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 4dae4cb56..bc683970c 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -90,4 +90,9 @@ es: proposal_notification: attributes: minimum_interval: - invalid: "Debes esperar un mínimo de %{interval} días entre notificaciones" \ No newline at end of file + invalid: "Debes esperar un mínimo de %{interval} días entre notificaciones" + signature: + attributes: + document_number: + not_in_census: 'No verificado por Padrón' + already_voted: 'Ya ha votado esta propuesta' \ No newline at end of file diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 370a06ccc..373ec6085 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -111,6 +111,7 @@ en: settings: Configuration settings spending_proposals: Spending proposals stats: Statistics + signature_sheets: Signature Sheets moderators: index: title: Moderators @@ -295,6 +296,10 @@ en: delete: success: Geozone successfully deleted error: This geozone can't be deleted since there are elements attached to it + signature_sheet: + author: Author + created_at: Creation date + name: Name stats: show: stats_title: Stats diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 8819905d8..ec921b63a 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -109,6 +109,7 @@ es: settings: Configuración global spending_proposals: Propuestas de inversión stats: Estadísticas + signature_sheets: Hojas de firmas moderators: index: title: Moderadores @@ -293,6 +294,10 @@ es: delete: success: Distrito borrado correctamente error: No se puede borrar el distrito porque ya tiene elementos asociados + signature_sheet: + author: Autor + created_at: Fecha de creación + name: Nombre stats: show: stats_title: Estadísticas diff --git a/config/locales/en.yml b/config/locales/en.yml index 3ae90565f..5b55fb65d 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -154,6 +154,7 @@ en: spending_proposal: Spending proposal user: Account verification/sms: phone + signature_sheet: Signature sheet geozones: none: All city all: All scopes diff --git a/config/locales/es.yml b/config/locales/es.yml index 8d5dfb490..df9d59f58 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -154,6 +154,7 @@ es: spending_proposal: la propuesta de gasto user: la cuenta verification/sms: el teléfono + signature_sheet: la hoja de firmas geozones: none: Toda la ciudad all: Todos los ámbitos From 7d3cb112acf7cb7e76301e882ac4da925864f6ef Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 12:21:23 +0100 Subject: [PATCH 03/28] updates seeds --- db/seeds.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/seeds.rb b/db/seeds.rb index 1607a0aff..0b256d018 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -64,6 +64,7 @@ Setting['feature.twitter_login'] = true Setting['feature.facebook_login'] = true Setting['feature.google_login'] = true Setting['feature.public_stats'] = true +Setting['feature.signatures'] = true # Spending proposals feature flags Setting['feature.spending_proposal_features.voting_allowed'] = true From 398206c37576f80e5210f8e0d7d9317963ca3b76 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 12:21:35 +0100 Subject: [PATCH 04/28] updates census api with invalid stubbed response --- lib/census_api.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/census_api.rb b/lib/census_api.rb index 678a748c6..7f88557f2 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -74,7 +74,7 @@ class CensusApi if end_point_available? client.call(:get_habita_datos, message: request(document_type, document_number)).body else - stubbed_response_body + stubbed_response(document_type, document_number) end end @@ -97,8 +97,20 @@ class CensusApi Rails.env.staging? || Rails.env.preproduction? || Rails.env.production? end - def stubbed_response_body - {get_habita_datos_response: {get_habita_datos_return: {hay_errores: false, datos_habitante: { item: {fecha_nacimiento_string: "31-12-1980", identificador_documento: "12345678Z", descripcion_sexo: "Varón" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}} + def stubbed_response(document_type, document_number) + if document_number == "12345678Z" && document_type == "1" + stubbed_valid_response + else + stubbed_invalid_response + end + end + + def stubbed_valid_response + {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: { item: {fecha_nacimiento_string: "31-12-1980", identificador_documento: "12345678Z", descripcion_sexo: "Varón", nombre: "José", apellido1: "García" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}} + end + + def stubbed_invalid_response + {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {}, datos_vivienda: {}}}} end def is_dni?(document_type) From 76723070e223f3b63585d476c25e0a9ec9b4b10b Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 12:21:45 +0100 Subject: [PATCH 05/28] adds pending specs --- spec/factories.rb | 8 ++++ spec/features/admin/signature_sheets_spec.rb | 35 +++++++++++++++ spec/models/signature_sheet_spec.rb | 30 +++++++++++++ spec/models/signature_spec.rb | 46 ++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 spec/features/admin/signature_sheets_spec.rb create mode 100644 spec/models/signature_sheet_spec.rb create mode 100644 spec/models/signature_spec.rb diff --git a/spec/factories.rb b/spec/factories.rb index c53f16be6..19bfc952a 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -343,4 +343,12 @@ FactoryGirl.define do association :sender, factory: :user association :receiver, factory: :user end + + factory :signature_sheet do + + end + + factory :signature do + + end end diff --git a/spec/features/admin/signature_sheets_spec.rb b/spec/features/admin/signature_sheets_spec.rb new file mode 100644 index 000000000..3ba13d819 --- /dev/null +++ b/spec/features/admin/signature_sheets_spec.rb @@ -0,0 +1,35 @@ +feature 'Signature sheets' do + + background do + admin = create(:administrator) + login_as(admin.user) + end + + scenario "Index" + + scenario 'Create' do + visit admin_path + + click_link 'Signature Sheets' + click_link 'New' + + select "Proposal", from: "signable_type" + fill_in "signable_id", with: "1" + fill_in "document_numbers", with: "12345678Z, 99999999Z" + click_button "Save" + + expect(page).to have_content "Signature sheet saved successfully" + end + + scenario 'Errors on create' + + scenario 'Show' do + #display signable + #display created_at + #display author + #display valid signatures count + #display invalid signatures count + #display invalid signatures with their error + end + +end \ No newline at end of file diff --git a/spec/models/signature_sheet_spec.rb b/spec/models/signature_sheet_spec.rb new file mode 100644 index 000000000..2f956541c --- /dev/null +++ b/spec/models/signature_sheet_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +describe SignatureSheet do + + describe "validations" do + it "should be valid" + it "should not be valid without a valid signable" + it "should not be valid without document numbers" + it "should not be valid without an author" + end + + describe "name" do + it "returns name for proposal signature sheets" + it "returns name for spending proposal signature sheets" + end + + describe "verify_signatures" do + it "marks signature sheet as processed after verifing all document numbers" + end + + describe "invalid_signatures" do + it "returns invalid signatures" + it "does not return valid signatures" + end + + describe "parsed_document_numbers" do + it "returns an array after spliting document numbers by newlines" + end + +end \ No newline at end of file diff --git a/spec/models/signature_spec.rb b/spec/models/signature_spec.rb new file mode 100644 index 000000000..d1f37a3ec --- /dev/null +++ b/spec/models/signature_spec.rb @@ -0,0 +1,46 @@ +require 'rails_helper' + +describe Signature + + describe "validations" do + it "should be valid" + it "should be valid if user exists" + it "should not be valid if already voted" + it "should not be valid if not in census" + end + + describe "in census" do + it "checks for all document_types" + end + + describe "verify" do + + describie "valid" do + it "sets status to verified" + it "asigns vote to user" + end + + describe "invalid" + it "sets status to error" + it "does not asign any votes" + end + + end + + describe "assign vote" do + + describe "existing user" do + it "assigns vote to user" + it "does not assign vote to user if already voted" + it "marks the vote as coming from a signature" + end + + describe "inexistent user" do + it "creates a user with that document number" + it "assign the vote to newly created user" + it "marks the vote as coming from a signature" + end + + end + +end \ No newline at end of file From 7ec6a874b6a1770798b4b0044f3ee9c4abf41789 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 12:25:34 +0100 Subject: [PATCH 06/28] updates seeds with signature sheet feature --- db/seeds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 0b256d018..7b1af3cba 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -64,7 +64,7 @@ Setting['feature.twitter_login'] = true Setting['feature.facebook_login'] = true Setting['feature.google_login'] = true Setting['feature.public_stats'] = true -Setting['feature.signatures'] = true +Setting['feature.signature_sheets'] = true # Spending proposals feature flags Setting['feature.spending_proposal_features.voting_allowed'] = true From a7aed27db4312b18db8f916b2a8ac84d35dddebd Mon Sep 17 00:00:00 2001 From: Alberto Garcia Cabeza Date: Tue, 20 Dec 2016 14:57:24 +0100 Subject: [PATCH 07/28] adds styles and i18n to signature sheets admin views --- .../admin/signature_sheets/index.html.erb | 51 ++++++++++--------- app/views/admin/signature_sheets/new.html.erb | 23 +++++++-- .../admin/signature_sheets/show.html.erb | 50 ++++++++++++------ config/locales/activerecord.en.yml | 4 ++ config/locales/activerecord.es.yml | 4 ++ config/locales/admin.en.yml | 19 ++++++- config/locales/admin.es.yml | 19 ++++++- config/locales/settings.en.yml | 1 + config/locales/settings.es.yml | 1 + 9 files changed, 127 insertions(+), 45 deletions(-) diff --git a/app/views/admin/signature_sheets/index.html.erb b/app/views/admin/signature_sheets/index.html.erb index d03530e3b..3aea83918 100644 --- a/app/views/admin/signature_sheets/index.html.erb +++ b/app/views/admin/signature_sheets/index.html.erb @@ -1,26 +1,31 @@ -

    Signatures

    +

    <%= t("admin.signature_sheets.index.title") %>

    -
    - <%= link_to 'Nueva hoja de firmas', new_admin_signature_sheet_path %> -
    +<%= link_to t("admin.signature_sheets.index.new"), new_admin_signature_sheet_path, + class: "button success float-right" %> - - - - - - - <% @signature_sheets.each do |signature_sheet| %> - - - - +<% if @signature_sheets.any? %> +
    <%= t("admin.signature_sheet.name") %><%= t("admin.signature_sheet.author") %><%= t("admin.signature_sheet.created_at") %>
    - <%= link_to signature_sheet.name, [:admin, signature_sheet] %> - - <%= signature_sheet.author.name %> - - <%= l(signature_sheet.created_at, format: :short) %> -
    + + + + - <% end %> -
    <%= t("admin.signature_sheets.name") %><%= t("admin.signature_sheets.author") %><%= t("admin.signature_sheets.created_at") %>
    \ No newline at end of file + <% @signature_sheets.each do |signature_sheet| %> + + + <%= link_to signature_sheet.name, [:admin, signature_sheet] %> + + + <%= signature_sheet.author.name %> + + + <%= l(signature_sheet.created_at, format: :short) %> + + + <% end %> + +<% else %> +
    + <%= t("admin.signature_sheets.no_signature_sheets") %> +
    +<% end %> diff --git a/app/views/admin/signature_sheets/new.html.erb b/app/views/admin/signature_sheets/new.html.erb index c3fe19b55..013913346 100644 --- a/app/views/admin/signature_sheets/new.html.erb +++ b/app/views/admin/signature_sheets/new.html.erb @@ -1,9 +1,22 @@ +<%= render 'shared/back_link' %> + +

    <%= t("admin.signature_sheets.new.title") %>

    + <%= form_for [:admin, @signature_sheet] do |f| %> <%= render 'shared/errors', resource: @signature_sheet %> - <%= f.select :signable_type, signable_options %> - <%= f.text_field :signable_id %> - <%= f.text_area :document_numbers %> - <%= f.submit %> -<% end %> \ No newline at end of file +
    + <%= f.select :signable_type, signable_options %> +
    + +
    + <%= f.text_field :signable_id %> +
    + + <%= f.label :document_numbers %> +

    <%= t("admin.signature_sheets.new.document_numbers_note") %>

    + <%= f.text_area :document_numbers, rows: "6", label: false %> + + <%= f.submit(class: "button", value: t("admin.signature_sheets.new.submit")) %> +<% end %> diff --git a/app/views/admin/signature_sheets/show.html.erb b/app/views/admin/signature_sheets/show.html.erb index 84f5cb672..357f173f2 100644 --- a/app/views/admin/signature_sheets/show.html.erb +++ b/app/views/admin/signature_sheets/show.html.erb @@ -1,28 +1,48 @@ -

    <%= @signature_sheet.name %>

    +

    <%= @signature_sheet.name %>

    -
    Documentos: <%= @signature_sheet.document_numbers %>
    -
    Creado: <%= l(@signature_sheet.created_at, format: :short) %>
    -
    Autor: <%= @signature_sheet.author.name %>
    -
    - -
    - Hay <%= @signature_sheet.signatures.valid.count %> firmas válidas +
    + <%= t("admin.signature_sheets.show.created_at") %> + <%= l(@signature_sheet.created_at, format: :short) %> +  •  + <%= t("admin.signature_sheets.show.author") %> + <%= @signature_sheet.author.name %>
    -
    - Hay <%= @signature_sheet.signatures.invalid.count %> firmas inválidas +
    +

    <%= t("admin.signature_sheets.show.documents") %>

    + <%= @signature_sheet.document_numbers %>
    -
    <% if @signature_sheet.signatures.invalid.any? %> +
    + + <%= t("admin.signature_sheets.show.valid", + count: @signature_sheet.signatures.valid.count ) %> + +
    <% @invalid_signatures.each do |error, signatures| %> -
    <%= error %>
    -
    - <%= signatures.map(&:document_number).join(",") %> +
    +

    + + <%= t("admin.signature_sheets.show.invalid", + count: @signature_sheet.signatures.invalid.count ) %> + (<%= error %>) + +

    + <%= signatures.map(&:document_number).join(",") %>
    <% end %> +<% else %> +
    + + <%= t("admin.signature_sheets.show.all_valid", + count: @signature_sheet.signatures.valid.count ) %> + +
    <% end %> <% unless @signature_sheet.processed? %> - Aún hay firmas que se están verificando por el Padrón, por favor refresca la página en unos instantes. +
    + <%= t("admin.signature_sheets.show.loading") %> +
    <% end %> \ No newline at end of file diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index b2fc86ce2..3b4e1f1c8 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -69,6 +69,10 @@ en: external_url: "Link to additional documentation" geozone_id: "Scope of operation" title: "Title" + signature_sheet: + signable_type: "Signable type" + signable_id: "Signable ID" + document_numbers: "Documents numbers" errors: models: user: diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index bc683970c..ea5aa14d6 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -69,6 +69,10 @@ es: external_url: "Enlace a documentación adicional" geozone_id: "Ámbito de actuación" title: "Título" + signature_sheet: + signable_type: "Tipo de hoja de firmas" + signable_id: "ID Propuesta ciudadana/Propuesta inversión" + document_numbers: "Números de documentos" errors: models: user: diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 373ec6085..6ba072419 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -296,10 +296,27 @@ en: delete: success: Geozone successfully deleted error: This geozone can't be deleted since there are elements attached to it - signature_sheet: + signature_sheets: author: Author created_at: Creation date name: Name + no_signature_sheets: "There are not signature_sheets" + index: + title: Signature sheets + new: New signature sheets + new: + title: New signature sheets + signable_type: Type of signature sheet + document_numbers_note: "Write the numbers separated by commas (,)" + submit: Create signature sheets + show: + created_at: Created + author: Author + documents: Documents + all_valid: "All the signatures (%{count}) are valid" + valid: "There are %{count} valid signatures" + invalid: "There are %{count} invalid signatures" + loading: "There are still signatures that are being verified by the Census, please refresh the page in a few moments" stats: show: stats_title: Stats diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index ec921b63a..ed4ad1bc1 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -294,10 +294,27 @@ es: delete: success: Distrito borrado correctamente error: No se puede borrar el distrito porque ya tiene elementos asociados - signature_sheet: + signature_sheets: author: Autor created_at: Fecha de creación name: Nombre + no_signature_sheets: "No existen hojas de firmas" + index: + title: Hojas de firmas + new: Nueva hoja de firmas + new: + title: Nueva hoja de firmas + signable_type: Tipo de hoja de firmas + document_numbers_note: "Introduce los números separados por comas (,)" + submit: Crear hoja de firmas + show: + created_at: Creado + author: Autor + documents: Documentos + all_valid: "Todas las firmas introducidas (%{count}) son válidas" + valid: "Hay %{count} firmas válidas" + invalid: "Hay %{count} firmas inválidas" + loading: "Aún hay firmas que se están verificando por el Padrón, por favor refresca la página en unos instantes" stats: show: stats_title: Estadísticas diff --git a/config/locales/settings.en.yml b/config/locales/settings.en.yml index 0de4e5feb..6d58f0727 100755 --- a/config/locales/settings.en.yml +++ b/config/locales/settings.en.yml @@ -27,6 +27,7 @@ en: facebook_login: Facebook login google_login: Google login debates: Debates + signature_sheets: Signature sheets spending_proposals: Investment projects spending_proposal_features: voting_allowed: Voting on investment projects diff --git a/config/locales/settings.es.yml b/config/locales/settings.es.yml index 05ba6c076..aeb737f5b 100644 --- a/config/locales/settings.es.yml +++ b/config/locales/settings.es.yml @@ -27,6 +27,7 @@ es: facebook_login: Registro con Facebook google_login: Registro con Google debates: Debates + signature_sheets: Hojas de firmas spending_proposals: Propuestas de inversión spending_proposal_features: voting_allowed: Votaciones sobre propuestas de inversión. \ No newline at end of file From 7e9b9036226ab760c1b4157a651b8f218beb031e Mon Sep 17 00:00:00 2001 From: Alberto Garcia Cabeza Date: Tue, 20 Dec 2016 14:57:43 +0100 Subject: [PATCH 08/28] adds setting signature sheets on dev_seeds --- db/dev_seeds.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 02e5f9f6d..27371a3a5 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -32,6 +32,7 @@ Setting.create(key: 'feature.facebook_login', value: "true") Setting.create(key: 'feature.google_login', value: "true") Setting.create(key: 'per_page_code', value: "") Setting.create(key: 'comments_body_max_length', value: '1000') +Setting.create(key: 'feature.signature_sheets', value: "true") puts "Creating Geozones" ('A'..'Z').each { |i| Geozone.create(name: "District #{i}", external_code: i.ord, census_code: i.ord) } From 4eb700e3d491bd10245964f125419ae4d321f1fe Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 16:46:08 +0100 Subject: [PATCH 09/28] refactors signature sheets --- .../admin/signature_sheets_controller.rb | 1 - app/models/signature.rb | 84 ++++++++----------- app/models/signature_sheet.rb | 12 +-- .../admin/signature_sheets/show.html.erb | 14 ++-- .../20161214212918_create_signature_sheets.rb | 2 +- .../20161214233817_create_signatures.rb | 2 +- 6 files changed, 48 insertions(+), 67 deletions(-) diff --git a/app/controllers/admin/signature_sheets_controller.rb b/app/controllers/admin/signature_sheets_controller.rb index 83d1e93ce..325ece443 100644 --- a/app/controllers/admin/signature_sheets_controller.rb +++ b/app/controllers/admin/signature_sheets_controller.rb @@ -21,7 +21,6 @@ class Admin::SignatureSheetsController < Admin::BaseController def show @signature_sheet = SignatureSheet.find(params[:id]) - @invalid_signatures = @signature_sheet.invalid_signatures end private diff --git a/app/models/signature.rb b/app/models/signature.rb index 3a1d197eb..37340b227 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -2,71 +2,59 @@ class Signature < ActiveRecord::Base belongs_to :signature_sheet belongs_to :user - validate :in_census - validate :not_already_voted + validates :document_number, presence: true + validates :signature_sheet, presence: true - scope :valid, -> { where(status: 'verified') } - scope :invalid, -> { where.not(status: 'verified') } + scope :verified, -> { where(verified: true) } + scope :unverified, -> { where(verified: false) } - def in_census - return true if user_exists? - errors.add(:document_number, :not_in_census) unless in_census? - end - - def not_already_voted - errors.add(:document_number, :already_voted) if already_voted? - end + before_save :verify def verify - if valid? + if verified? assign_vote - update_attribute(:status, 'verified') - else - error = errors.messages[:document_number].first - update_attribute(:status, error) + verified = true end end - private + def verified? + user_exists? || in_census? + end - def assign_vote - if user_exists? - assign_vote_to_user - else - create_user - assign_vote_to_user - end - end - def assign_vote_to_user - signable.register_vote(user, "yes", "signature") - #Vote.create(votable: signable, voter: user, signature: self) + def assign_vote + if user_exists? + assign_vote_to_user + else + create_user + assign_vote_to_user end + end - def user_exists? - user = User.where(document_number: document_number).exists? - end + def assign_vote_to_user + signable.register_vote(user, "yes", "signature") + end - def create_user - user = User.where(document_number: document_number, erased_at: Time.now).create - end + def user_exists? + user = User.where(document_number: document_number).exists? + end - def in_census? - document_types.any? do |document_type| - CensusApi.new.call(document_type, document_number).valid? - end - end + def create_user + user = User.where(document_number: document_number, erased_at: Time.now).create + end - def already_voted? - signable.voters.where(document_number: document_number).exists? + def in_census? + document_types.any? do |document_type| + CensusApi.new.call(document_type, document_number).valid? end + end - def signable - signature_sheet.signable - end + def signable + signature_sheet.signable + end - def document_types - %w(1 2 3 4) - end + def document_types + %w(1 2 3 4) + end end \ No newline at end of file diff --git a/app/models/signature_sheet.rb b/app/models/signature_sheet.rb index 7829833a3..a77e12098 100644 --- a/app/models/signature_sheet.rb +++ b/app/models/signature_sheet.rb @@ -13,7 +13,7 @@ class SignatureSheet < ActiveRecord::Base validate :signable_found def name - "#{signable_name} + #{signable_id}" + "#{signable_name} #{signable_id}" end def signable_name @@ -22,19 +22,13 @@ class SignatureSheet < ActiveRecord::Base def verify_signatures parsed_document_numbers.each do |document_number| - signature = signatures.new(document_number: document_number) - signature.save(validate: false) - signature.verify + signature = signatures.create(document_number: document_number) end update(processed: true) end - def invalid_signatures - signatures.invalid.group_by(&:status) - end - def parsed_document_numbers - document_numbers.split(",") + document_numbers.split(/\W+/) end def signable_found diff --git a/app/views/admin/signature_sheets/show.html.erb b/app/views/admin/signature_sheets/show.html.erb index 84f5cb672..2b57b92c5 100644 --- a/app/views/admin/signature_sheets/show.html.erb +++ b/app/views/admin/signature_sheets/show.html.erb @@ -6,21 +6,21 @@
    - Hay <%= @signature_sheet.signatures.valid.count %> firmas válidas + Hay <%= @signature_sheet.signatures.verified.count %> firmas válidas
    - Hay <%= @signature_sheet.signatures.invalid.count %> firmas inválidas + Hay <%= @signature_sheet.signatures.unverified.count %> firmas inválidas

    -<% if @signature_sheet.signatures.invalid.any? %> - <% @invalid_signatures.each do |error, signatures| %> -
    <%= error %>
    +<% if @signature_sheet.signatures.verified.any? %>
    - <%= signatures.map(&:document_number).join(",") %> + EL PADRÓN NO PUDO VERIFICAR LA INFORMACIÓN +
    +
    + <%= @signature_sheet.signatures.unverified.map(&:document_number).join(",") %>
    - <% end %> <% end %> <% unless @signature_sheet.processed? %> diff --git a/db/migrate/20161214212918_create_signature_sheets.rb b/db/migrate/20161214212918_create_signature_sheets.rb index e3f4e6a58..d34728a34 100644 --- a/db/migrate/20161214212918_create_signature_sheets.rb +++ b/db/migrate/20161214212918_create_signature_sheets.rb @@ -3,7 +3,7 @@ class CreateSignatureSheets < ActiveRecord::Migration create_table :signature_sheets do |t| t.references :signable, polymorphic: true t.text :document_numbers - t.boolean :processed + t.boolean :processed, default: false t.references :author t.timestamps end diff --git a/db/migrate/20161214233817_create_signatures.rb b/db/migrate/20161214233817_create_signatures.rb index c8c178582..fd6b53d3a 100644 --- a/db/migrate/20161214233817_create_signatures.rb +++ b/db/migrate/20161214233817_create_signatures.rb @@ -4,7 +4,7 @@ class CreateSignatures < ActiveRecord::Migration t.references :signature_sheet t.references :user t.string :document_number - t.string :status, default: nil + t.boolean :verified, default: false t.timestamps end end From d09348ee49bc55aaa8a4d7d6f721cd0a1cb6cad7 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 16:46:20 +0100 Subject: [PATCH 10/28] adds specs --- spec/factories.rb | 7 ++- spec/models/signature_sheet_spec.rb | 68 ++++++++++++++++++++++------- spec/models/signature_spec.rb | 12 ++--- 3 files changed, 64 insertions(+), 23 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 19bfc952a..322f214d5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -345,10 +345,13 @@ FactoryGirl.define do end factory :signature_sheet do - + association :signable, factory: :proposal + association :author, factory: :user + document_numbers "123A, 456B, 789C" end factory :signature do - + signature_sheet + sequence(:document_number) { |n| "#{n}A" } end end diff --git a/spec/models/signature_sheet_spec.rb b/spec/models/signature_sheet_spec.rb index 2f956541c..3b0ced629 100644 --- a/spec/models/signature_sheet_spec.rb +++ b/spec/models/signature_sheet_spec.rb @@ -2,29 +2,67 @@ require 'rails_helper' describe SignatureSheet do + let(:signature_sheet) { build(:signature_sheet) } + describe "validations" do - it "should be valid" - it "should not be valid without a valid signable" - it "should not be valid without document numbers" - it "should not be valid without an author" + + it "should be valid" do + expect(signature_sheet).to be_valid + end + + it "should not be valid without a valid signable" do + signature_sheet.signable = nil + expect(signature_sheet).to_not be_valid + end + + it "should not be valid without document numbers" do + signature_sheet.document_numbers = nil + expect(signature_sheet).to_not be_valid + end + + it "should not be valid without an author" do + signature_sheet.author = nil + expect(signature_sheet).to_not be_valid + end end - describe "name" do - it "returns name for proposal signature sheets" - it "returns name for spending proposal signature sheets" + describe "#name" do + it "returns name for proposal signature sheets" do + proposal = create(:proposal) + signature_sheet.signable = proposal + + expect(signature_sheet.name).to eq("Citizen proposal #{proposal.id}") + end + it "returns name for spending proposal signature sheets" do + spending_proposal = create(:spending_proposal) + signature_sheet.signable = spending_proposal + + expect(signature_sheet.name).to eq("Spending proposal #{spending_proposal.id}") + end end - describe "verify_signatures" do - it "marks signature sheet as processed after verifing all document numbers" + describe "#verify_signatures" do + it "creates signatures for each document number" do + signature_sheet = create(:signature_sheet, document_numbers: "123A, 456B") + signature_sheet.verify_signatures + + expect(Signature.count).to eq(2) + end + + it "marks signature sheet as processed" do + signature_sheet = create(:signature_sheet) + signature_sheet.verify_signatures + + expect(signature_sheet.processed).to eq(true) + end end - describe "invalid_signatures" do - it "returns invalid signatures" - it "does not return valid signatures" - end + describe "#parsed_document_numbers" do + it "returns an array after spliting document numbers by newlines or commas" do + signature_sheet.document_numbers = "123A\r\n456B\n789C,123B" - describe "parsed_document_numbers" do - it "returns an array after spliting document numbers by newlines" + expect(signature_sheet.parsed_document_numbers).to eq(['123A', '456B', '789C', '123B']) + end end end \ No newline at end of file diff --git a/spec/models/signature_spec.rb b/spec/models/signature_spec.rb index d1f37a3ec..7fac0e65a 100644 --- a/spec/models/signature_spec.rb +++ b/spec/models/signature_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe Signature +describe Signature do describe "validations" do it "should be valid" @@ -9,25 +9,25 @@ describe Signature it "should not be valid if not in census" end - describe "in census" do + describe "#in_census" do it "checks for all document_types" end - describe "verify" do + describe "#verify" do - describie "valid" do + describe "valid" do it "sets status to verified" it "asigns vote to user" end - describe "invalid" + describe "invalid" do it "sets status to error" it "does not asign any votes" end end - describe "assign vote" do + describe "#assign_vote" do describe "existing user" do it "assigns vote to user" From 53ac18f68f1c31fbe734f7f97636d55964fcf018 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 17:06:03 +0100 Subject: [PATCH 11/28] updates views --- .../admin/signature_sheets/show.html.erb | 33 +++++++++---------- config/locales/admin.en.yml | 6 ++-- config/locales/admin.es.yml | 6 ++-- db/schema.rb | 4 +-- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/app/views/admin/signature_sheets/show.html.erb b/app/views/admin/signature_sheets/show.html.erb index 357f173f2..4c65937e3 100644 --- a/app/views/admin/signature_sheets/show.html.erb +++ b/app/views/admin/signature_sheets/show.html.erb @@ -13,30 +13,29 @@ <%= @signature_sheet.document_numbers %>
    -<% if @signature_sheet.signatures.invalid.any? %> +<% if @signature_sheet.signatures.unverified.any? %>
    - <%= t("admin.signature_sheets.show.valid", - count: @signature_sheet.signatures.valid.count ) %> + <%= t("admin.signature_sheets.show.verified", + count: @signature_sheet.signatures.verified.count ) %>
    - <% @invalid_signatures.each do |error, signatures| %> -
    -

    - - <%= t("admin.signature_sheets.show.invalid", - count: @signature_sheet.signatures.invalid.count ) %> - (<%= error %>) - -

    - <%= signatures.map(&:document_number).join(",") %> -
    - <% end %> + +
    +

    + + <%= t("admin.signature_sheets.show.unverified", + count: @signature_sheet.signatures.unverified.count ) %> + (No verificadas por el Padrón) + +

    + <%= @signature_sheet.signatures.unverified.map(&:document_number).join(",") %> +
    <% else %>
    - <%= t("admin.signature_sheets.show.all_valid", - count: @signature_sheet.signatures.valid.count ) %> + <%= t("admin.signature_sheets.show.all_verified", + count: @signature_sheet.signatures.verified.count ) %>
    <% end %> diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 6ba072419..c90f430ef 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -313,9 +313,9 @@ en: created_at: Created author: Author documents: Documents - all_valid: "All the signatures (%{count}) are valid" - valid: "There are %{count} valid signatures" - invalid: "There are %{count} invalid signatures" + all_verified: "All the signatures (%{count}) are valid" + verified: "There are %{count} valid signatures" + unverified: "There are %{count} invalid signatures" loading: "There are still signatures that are being verified by the Census, please refresh the page in a few moments" stats: show: diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index ed4ad1bc1..fd454c13b 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -311,9 +311,9 @@ es: created_at: Creado author: Autor documents: Documentos - all_valid: "Todas las firmas introducidas (%{count}) son válidas" - valid: "Hay %{count} firmas válidas" - invalid: "Hay %{count} firmas inválidas" + all_verified: "Todas las firmas introducidas (%{count}) son válidas" + verified: "Hay %{count} firmas válidas" + unverified: "Hay %{count} firmas inválidas" loading: "Aún hay firmas que se están verificando por el Padrón, por favor refresca la página en unos instantes" stats: show: diff --git a/db/schema.rb b/db/schema.rb index ab8dead98..95d40a493 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -330,7 +330,7 @@ ActiveRecord::Schema.define(version: 20161214233817) do t.integer "signable_id" t.string "signable_type" t.text "document_numbers" - t.boolean "processed" + t.boolean "processed", default: false t.integer "author_id" t.datetime "created_at" t.datetime "updated_at" @@ -340,7 +340,7 @@ ActiveRecord::Schema.define(version: 20161214233817) do t.integer "signature_sheet_id" t.integer "user_id" t.string "document_number" - t.string "status" + t.boolean "verified", default: false t.datetime "created_at" t.datetime "updated_at" end From b10dbd7cb59816a56327a563cb501ceb14ea7ca4 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Dec 2016 18:00:36 +0100 Subject: [PATCH 12/28] adds specs --- app/models/signature.rb | 11 +++---- spec/models/signature_sheet_spec.rb | 15 ++++++++- spec/models/signature_spec.rb | 50 ++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/app/models/signature.rb b/app/models/signature.rb index 37340b227..b89bf2587 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -8,12 +8,14 @@ class Signature < ActiveRecord::Base scope :verified, -> { where(verified: true) } scope :unverified, -> { where(verified: false) } + delegate :signable, to: :signature_sheet + before_save :verify def verify if verified? assign_vote - verified = true + self.verified = true end end @@ -36,7 +38,8 @@ class Signature < ActiveRecord::Base end def user_exists? - user = User.where(document_number: document_number).exists? + self.user = User.where(document_number: document_number).first + self.user.present? end def create_user @@ -49,10 +52,6 @@ class Signature < ActiveRecord::Base end end - def signable - signature_sheet.signable - end - def document_types %w(1 2 3 4) end diff --git a/spec/models/signature_sheet_spec.rb b/spec/models/signature_sheet_spec.rb index 3b0ced629..d8f5c3513 100644 --- a/spec/models/signature_sheet_spec.rb +++ b/spec/models/signature_sheet_spec.rb @@ -10,11 +10,24 @@ describe SignatureSheet do expect(signature_sheet).to be_valid end - it "should not be valid without a valid signable" do + it "should be valid with a valid signable" do + signature_sheet.signable = create(:proposal) + expect(signature_sheet).to be_valid + + signature_sheet.signable = create(:spending_proposal) + expect(signature_sheet).to be_valid + end + + it "should not be valid without signable" do signature_sheet.signable = nil expect(signature_sheet).to_not be_valid end + it "should not be valid without a valid signable" do + signature_sheet.signable = create(:comment) + expect(signature_sheet).to_not be_valid + end + it "should not be valid without document numbers" do signature_sheet.document_numbers = nil expect(signature_sheet).to_not be_valid diff --git a/spec/models/signature_spec.rb b/spec/models/signature_spec.rb index 7fac0e65a..fe3ee47e0 100644 --- a/spec/models/signature_spec.rb +++ b/spec/models/signature_spec.rb @@ -2,22 +2,47 @@ require 'rails_helper' describe Signature do + let(:signature) { build(:signature) } + describe "validations" do - it "should be valid" - it "should be valid if user exists" - it "should not be valid if already voted" - it "should not be valid if not in census" + it "should be valid" do + expect(signature).to be_valid + end + + it "should not be valid without a document number" do + signature.document_number = nil + expect(signature).to_not be_valid + end + + it "should not be valid without an associated signature sheet" do + signature.signature_sheet = nil + expect(signature).to_not be_valid + end end describe "#in_census" do - it "checks for all document_types" + it "checks for all document_types" do + #### + end end describe "#verify" do - describe "valid" do - it "sets status to verified" - it "asigns vote to user" + describe "valid", :focus do + it "asigns vote to user" do + proposal = create(:proposal) + user = create(:user, document_number: "123A") + signature_sheet = create(:signature_sheet, signable: proposal) + signature = create(:signature, signature_sheet: signature_sheet, document_number: "123A") + + signature.verify + expect(user.voted_for?(proposal)).to be + end + + it "sets status to verified" do + + end + end describe "invalid" do @@ -27,6 +52,15 @@ describe Signature do end + describe "#verified?" do + it "returns true if user exists" do + + end + + it "returns true if document number in census" + it "returns false if user does not exist and not in census" + end + describe "#assign_vote" do describe "existing user" do From 675081141c1d3fe5d7c3c0163e62c4ab89792eaf Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 21 Dec 2016 18:46:31 +0100 Subject: [PATCH 13/28] refactors signatures --- .../admin/signature_sheets_controller.rb | 2 +- app/models/proposal.rb | 4 +- app/models/signature.rb | 48 +++++++--- app/models/signature_sheet.rb | 1 + app/models/spending_proposal.rb | 4 +- .../admin/signature_sheets/index.html.erb | 2 +- .../admin/signature_sheets/show.html.erb | 4 +- config/locales/admin.en.yml | 2 +- config/locales/responders.en.yml | 2 +- config/locales/responders.es.yml | 1 + ...20161221131403_add_signture_id_to_votes.rb | 5 + ...239_add_created_from_signature_to_users.rb | 5 + db/schema.rb | 92 ++++++++++++++++++- 13 files changed, 148 insertions(+), 24 deletions(-) create mode 100644 db/migrate/20161221131403_add_signture_id_to_votes.rb create mode 100644 db/migrate/20161221151239_add_created_from_signature_to_users.rb diff --git a/app/controllers/admin/signature_sheets_controller.rb b/app/controllers/admin/signature_sheets_controller.rb index 325ece443..60299c5a6 100644 --- a/app/controllers/admin/signature_sheets_controller.rb +++ b/app/controllers/admin/signature_sheets_controller.rb @@ -13,7 +13,7 @@ class Admin::SignatureSheetsController < Admin::BaseController @signature_sheet.author = current_user if @signature_sheet.save @signature_sheet.delay.verify_signatures - redirect_to [:admin, @signature_sheet] + redirect_to [:admin, @signature_sheet], notice: I18n.t('flash.actions.create.signature_sheet') else render :new end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index fd828c31e..7bd4b7a92 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -122,9 +122,9 @@ class Proposal < ActiveRecord::Base retired_at.present? end - def register_vote(user, vote_value, signature=nil) + def register_vote(user, vote_value) if votable_by?(user) && !archived? - vote_by(voter: user, vote: vote_value, vote_scope: signature) + vote_by(voter: user, vote: vote_value) end end diff --git a/app/models/signature.rb b/app/models/signature.rb index b89bf2587..b30d4a429 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -10,19 +10,16 @@ class Signature < ActiveRecord::Base delegate :signable, to: :signature_sheet - before_save :verify - - def verify - if verified? - assign_vote - self.verified = true - end - end - def verified? user_exists? || in_census? end + def verify + if verified? + assign_vote + mark_as_verified + end + end def assign_vote if user_exists? @@ -34,16 +31,32 @@ class Signature < ActiveRecord::Base end def assign_vote_to_user - signable.register_vote(user, "yes", "signature") + set_user + signable.register_vote(user, "yes") + assign_signature_to_vote + end + + def assign_signature_to_vote + vote = Vote.where(votable: signable, voter: user).first + vote.update(signature: self) end def user_exists? - self.user = User.where(document_number: document_number).first - self.user.present? + User.where(document_number: document_number).any? end def create_user - user = User.where(document_number: document_number, erased_at: Time.now).create + user_params = { + document_number: document_number, + created_from_signature: true, + verified_at: Time.now, + erased_at: Time.now, + email: "#{document_number}@signatures.com", + password: "12345678", + username: document_number, + terms_of_service: '1' + } + User.create(user_params) end def in_census? @@ -52,6 +65,15 @@ class Signature < ActiveRecord::Base end end + def set_user + user = User.where(document_number: document_number).first + update(user: user) + end + + def mark_as_verified + update(verified: true) + end + def document_types %w(1 2 3 4) end diff --git a/app/models/signature_sheet.rb b/app/models/signature_sheet.rb index a77e12098..6852820bb 100644 --- a/app/models/signature_sheet.rb +++ b/app/models/signature_sheet.rb @@ -23,6 +23,7 @@ class SignatureSheet < ActiveRecord::Base def verify_signatures parsed_document_numbers.each do |document_number| signature = signatures.create(document_number: document_number) + signature.verify end update(processed: true) end diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 111f4c3dd..d0563b342 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -133,9 +133,9 @@ class SpendingProposal < ActiveRecord::Base reason_for_not_being_votable_by(user).blank? end - def register_vote(user, vote_value, signature=nil) + def register_vote(user, vote_value) if votable_by?(user) - vote_by(voter: user, vote: vote_value, vote_scope: signature) + vote_by(voter: user, vote: vote_value) end end diff --git a/app/views/admin/signature_sheets/index.html.erb b/app/views/admin/signature_sheets/index.html.erb index 3aea83918..74241b5b0 100644 --- a/app/views/admin/signature_sheets/index.html.erb +++ b/app/views/admin/signature_sheets/index.html.erb @@ -11,7 +11,7 @@ <%= t("admin.signature_sheets.created_at") %> <% @signature_sheets.each do |signature_sheet| %> - + <%= link_to signature_sheet.name, [:admin, signature_sheet] %> diff --git a/app/views/admin/signature_sheets/show.html.erb b/app/views/admin/signature_sheets/show.html.erb index 4c65937e3..dfae240a2 100644 --- a/app/views/admin/signature_sheets/show.html.erb +++ b/app/views/admin/signature_sheets/show.html.erb @@ -14,14 +14,14 @@
    <% if @signature_sheet.signatures.unverified.any? %> -
    +
    <%= t("admin.signature_sheets.show.verified", count: @signature_sheet.signatures.verified.count ) %>
    -
    +

    <%= t("admin.signature_sheets.show.unverified", diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index c90f430ef..587b27f1d 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -308,7 +308,7 @@ en: title: New signature sheets signable_type: Type of signature sheet document_numbers_note: "Write the numbers separated by commas (,)" - submit: Create signature sheets + submit: Create signature sheet show: created_at: Created author: Author diff --git a/config/locales/responders.en.yml b/config/locales/responders.en.yml index ab1641799..f69d95c5f 100755 --- a/config/locales/responders.en.yml +++ b/config/locales/responders.en.yml @@ -9,7 +9,7 @@ en: proposal: "Proposal created successfully." proposal_notification: "Your message has been sent correctly." spending_proposal: "Spending proposal created successfully. You can access it from %{activity}" - + signature_sheet: "Signature sheet created successfully" save_changes: notice: Changes saved update: diff --git a/config/locales/responders.es.yml b/config/locales/responders.es.yml index 387085d69..4aeb969d9 100644 --- a/config/locales/responders.es.yml +++ b/config/locales/responders.es.yml @@ -9,6 +9,7 @@ es: proposal: "Propuesta creada correctamente." proposal_notification: "Tu message ha sido enviado correctamente." spending_proposal: "Propuesta de inversión creada correctamente. Puedes acceder a ella desde %{activity}" + signature_sheet: "Hoja de firmas creada correctamente" save_changes: notice: Cambios guardados update: diff --git a/db/migrate/20161221131403_add_signture_id_to_votes.rb b/db/migrate/20161221131403_add_signture_id_to_votes.rb new file mode 100644 index 000000000..28851fdac --- /dev/null +++ b/db/migrate/20161221131403_add_signture_id_to_votes.rb @@ -0,0 +1,5 @@ +class AddSigntureIdToVotes < ActiveRecord::Migration + def change + add_reference :votes, :signature, index: true + end +end diff --git a/db/migrate/20161221151239_add_created_from_signature_to_users.rb b/db/migrate/20161221151239_add_created_from_signature_to_users.rb new file mode 100644 index 000000000..214bd43cb --- /dev/null +++ b/db/migrate/20161221151239_add_created_from_signature_to_users.rb @@ -0,0 +1,5 @@ +class AddCreatedFromSignatureToUsers < ActiveRecord::Migration + def change + add_column :users, :created_from_signature, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 95d40a493..c5996b03a 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: 20161214233817) do +ActiveRecord::Schema.define(version: 20161221151239) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -211,6 +211,14 @@ ActiveRecord::Schema.define(version: 20161214233817) do t.string "census_code" end + create_table "geozones_poll_questions", force: :cascade do |t| + t.integer "geozone_id" + t.integer "question_id" + end + + add_index "geozones_poll_questions", ["geozone_id"], name: "index_geozones_poll_questions_on_geozone_id", using: :btree + add_index "geozones_poll_questions", ["question_id"], name: "index_geozones_poll_questions_on_question_id", using: :btree + create_table "identities", force: :cascade do |t| t.integer "user_id" t.string "provider" @@ -270,6 +278,78 @@ ActiveRecord::Schema.define(version: 20161214233817) do add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree + create_table "poll_booth_assignments", force: :cascade do |t| + t.integer "booth_id" + t.integer "poll_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "poll_booths", force: :cascade do |t| + t.string "name" + t.string "location" + end + + create_table "poll_officer_assignments", force: :cascade do |t| + t.integer "booth_assignment_id" + t.integer "officer_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "poll_officers", force: :cascade do |t| + t.integer "user_id" + end + + create_table "poll_partial_results", force: :cascade do |t| + t.integer "question_id" + t.integer "author_id" + t.string "answer" + t.integer "amount" + t.string "origin" + end + + add_index "poll_partial_results", ["answer"], name: "index_poll_partial_results_on_answer", using: :btree + add_index "poll_partial_results", ["author_id"], name: "index_poll_partial_results_on_author_id", using: :btree + add_index "poll_partial_results", ["origin"], name: "index_poll_partial_results_on_origin", using: :btree + add_index "poll_partial_results", ["question_id"], name: "index_poll_partial_results_on_question_id", using: :btree + + create_table "poll_questions", force: :cascade do |t| + t.integer "proposal_id" + t.integer "poll_id" + t.integer "author_id" + t.string "author_visible_name" + t.string "title" + t.string "summary" + t.string "valid_answers" + t.text "description" + t.integer "comments_count" + t.datetime "hidden_at" + t.datetime "created_at" + t.datetime "updated_at" + t.boolean "all_geozones", default: false + t.tsvector "tsv" + end + + add_index "poll_questions", ["author_id"], name: "index_poll_questions_on_author_id", using: :btree + add_index "poll_questions", ["poll_id"], name: "index_poll_questions_on_poll_id", using: :btree + add_index "poll_questions", ["proposal_id"], name: "index_poll_questions_on_proposal_id", using: :btree + add_index "poll_questions", ["tsv"], name: "index_poll_questions_on_tsv", using: :gin + + create_table "poll_voters", force: :cascade do |t| + t.string "document_number" + t.string "document_type" + t.integer "booth_assignment_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "polls", force: :cascade do |t| + t.string "name" + t.datetime "starts_at" + t.datetime "ends_at" + end + create_table "proposal_notifications", force: :cascade do |t| t.string "title" t.text "body" @@ -484,6 +564,7 @@ ActiveRecord::Schema.define(version: 20161214233817) do t.boolean "email_on_direct_message", default: true t.boolean "official_position_badge", default: false t.datetime "password_changed_at", default: '2016-11-02 13:51:14', null: false + t.boolean "created_from_signature", default: false end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree @@ -563,8 +644,10 @@ ActiveRecord::Schema.define(version: 20161214233817) do t.integer "vote_weight" t.datetime "created_at" t.datetime "updated_at" + t.integer "signature_id" end + add_index "votes", ["signature_id"], name: "index_votes_on_signature_id", using: :btree add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope", using: :btree add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope", using: :btree @@ -573,12 +656,19 @@ ActiveRecord::Schema.define(version: 20161214233817) do add_foreign_key "annotations", "users" add_foreign_key "failed_census_calls", "users" add_foreign_key "flags", "users" + add_foreign_key "geozones_poll_questions", "geozones" + add_foreign_key "geozones_poll_questions", "poll_questions", column: "question_id" add_foreign_key "identities", "users" add_foreign_key "locks", "users" add_foreign_key "managers", "users" add_foreign_key "moderators", "users" add_foreign_key "notifications", "users" add_foreign_key "organizations", "users" + add_foreign_key "poll_partial_results", "poll_questions", column: "question_id" + add_foreign_key "poll_partial_results", "users", column: "author_id" + add_foreign_key "poll_questions", "polls" + add_foreign_key "poll_questions", "proposals" + add_foreign_key "poll_questions", "users", column: "author_id" add_foreign_key "users", "geozones" add_foreign_key "valuators", "users" end From 0deba76db136a457c626a1ffa8e7d31cec7dc391 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 21 Dec 2016 18:46:38 +0100 Subject: [PATCH 14/28] adds specs --- spec/features/admin/signature_sheets_spec.rb | 68 ++++++-- spec/models/signature_spec.rb | 171 ++++++++++++++----- 2 files changed, 181 insertions(+), 58 deletions(-) diff --git a/spec/features/admin/signature_sheets_spec.rb b/spec/features/admin/signature_sheets_spec.rb index 3ba13d819..4c959d1ee 100644 --- a/spec/features/admin/signature_sheets_spec.rb +++ b/spec/features/admin/signature_sheets_spec.rb @@ -1,3 +1,5 @@ +require 'rails_helper' + feature 'Signature sheets' do background do @@ -5,31 +7,61 @@ feature 'Signature sheets' do login_as(admin.user) end - scenario "Index" + scenario "Index" do + 3.times { create(:signature_sheet) } - scenario 'Create' do - visit admin_path + visit admin_signature_sheets_path - click_link 'Signature Sheets' - click_link 'New' + expect(page).to have_css(".signature_sheet", count: 3) - select "Proposal", from: "signable_type" - fill_in "signable_id", with: "1" - fill_in "document_numbers", with: "12345678Z, 99999999Z" - click_button "Save" - - expect(page).to have_content "Signature sheet saved successfully" + SignatureSheet.all.each do |signature_sheet| + expect(page).to have_content signature_sheet.name + end end - scenario 'Errors on create' + scenario 'Create' do + proposal = create(:proposal) + visit new_admin_signature_sheet_path + + select "Citizen proposal", from: "signature_sheet_signable_type" + fill_in "signature_sheet_signable_id", with: proposal.id + fill_in "signature_sheet_document_numbers", with: "12345678Z, 99999999Z" + click_button "Create signature sheet" + + expect(page).to have_content "Signature sheet created successfully" + end + + scenario 'Errors on create' do + visit new_admin_signature_sheet_path + + click_button "Create signature sheet" + + expect(page).to have_content error_message + end scenario 'Show' do - #display signable - #display created_at - #display author - #display valid signatures count - #display invalid signatures count - #display invalid signatures with their error + proposal = create(:proposal) + user = Administrator.first.user + signature_sheet = create(:signature_sheet, + signable: proposal, + document_numbers: "12345678Z, 123A, 123B", + author: user) + signature_sheet.verify_signatures + + visit admin_signature_sheet_path(signature_sheet) + + expect(page).to have_content "Citizen proposal #{proposal.id}" + expect(page).to have_content "12345678Z, 123A, 123B" + expect(page).to have_content signature_sheet.created_at.strftime("%d %b %H:%M") + expect(page).to have_content user.name + + within("#verified_signatures") do + expect(page).to have_content 1 + end + + within("#unverified_signatures") do + expect(page).to have_content 2 + end end end \ No newline at end of file diff --git a/spec/models/signature_spec.rb b/spec/models/signature_spec.rb index fe3ee47e0..6f7237484 100644 --- a/spec/models/signature_spec.rb +++ b/spec/models/signature_spec.rb @@ -5,6 +5,7 @@ describe Signature do let(:signature) { build(:signature) } describe "validations" do + it "should be valid" do expect(signature).to be_valid end @@ -18,61 +19,151 @@ describe Signature do signature.signature_sheet = nil expect(signature).to_not be_valid end - end - - describe "#in_census" do - it "checks for all document_types" do - #### - end - end - - describe "#verify" do - - describe "valid", :focus do - it "asigns vote to user" do - proposal = create(:proposal) - user = create(:user, document_number: "123A") - signature_sheet = create(:signature_sheet, signable: proposal) - signature = create(:signature, signature_sheet: signature_sheet, document_number: "123A") - - signature.verify - expect(user.voted_for?(proposal)).to be - end - - it "sets status to verified" do - - end - - end - - describe "invalid" do - it "sets status to error" - it "does not asign any votes" - end end describe "#verified?" do - it "returns true if user exists" do + it "returns true if user exists" do + user = create(:user, :level_two, document_number: "123A") + signature = create(:signature, document_number: user.document_number) + + expect(signature.verified?).to eq(true) + end + + it "returns true if document number in census" do + signature = create(:signature, document_number: "12345678Z") + + expect(signature.verified?).to eq(true) + end + + it "returns false if user does not exist and not in census" do + signature = create(:signature, document_number: "123A") + + expect(signature.verified?).to eq(false) end - it "returns true if document number in census" - it "returns false if user does not exist and not in census" end describe "#assign_vote" do describe "existing user" do - it "assigns vote to user" - it "does not assign vote to user if already voted" - it "marks the vote as coming from a signature" + + it "assigns vote to user" do + user = create(:user, :level_two, document_number: "123A") + signature = create(:signature, document_number: user.document_number) + proposal = signature.signable + + signature.assign_vote + + expect(user.voted_for?(proposal)).to be + end + + it "does not assign vote to user multiple times" do + user = create(:user, :level_two, document_number: "123A") + signature = create(:signature, document_number: user.document_number) + + signature.assign_vote + signature.assign_vote + + expect(Vote.count).to eq(1) + end + + it "does not assign vote to user if already voted" do + proposal = create(:proposal) + user = create(:user, :level_two, document_number: "123A") + vote = create(:vote, votable: proposal, voter: user) + signature_sheet = create(:signature_sheet, signable: proposal) + signature = create(:signature, signature_sheet: signature_sheet, document_number: user.document_number) + + signature.assign_vote + + expect(Vote.count).to eq(1) + end + + it "marks the vote as coming from a signature" do + signature = create(:signature, document_number: "12345678Z") + + signature.assign_vote + + expect(Vote.last.signature).to eq(signature) + end + end describe "inexistent user" do - it "creates a user with that document number" - it "assign the vote to newly created user" - it "marks the vote as coming from a signature" + + it "creates a user with that document number" do + signature = create(:signature, document_number: "12345678Z") + proposal = signature.signable + + signature.assign_vote + + user = User.last + expect(user.document_number).to eq("12345678Z") + expect(user.created_from_signature).to eq(true) + expect(user.verified_at).to be + expect(user.erased_at).to be + end + + it "assign the vote to newly created user" do + signature = create(:signature, document_number: "12345678Z") + proposal = signature.signable + + signature.assign_vote + + user = signature.user + expect(user.voted_for?(proposal)).to be + end + + it "assigns signature to vote" do + signature = create(:signature, document_number: "12345678Z") + + signature.assign_vote + + expect(Vote.last.signature).to eq(signature) + end + end + + end + + describe "#verify" do + + describe "document in census" do + + it "calls assign_vote" do + signature = create(:signature, document_number: "12345678Z") + + expect(signature).to receive(:assign_vote) + signature.verify + end + + it "sets signature as verified" do + user = create(:user, :level_two, document_number: "123A") + signature = create(:signature, document_number: user.document_number) + + signature.verify + + expect(signature).to be_verified + end + + end + + describe "document not in census" do + + it "does not call assign_vote" do + signature = create(:signature, document_number: "123A") + + expect(signature).to_not receive(:assign_vote) + signature.verify + end + + it "maintains signature as not verified" do + signature = create(:signature, document_number: "123A") + + signature.verify + expect(signature).to_not be_verified + end end end From 25729ac259a7cc9775c23436fa44130c07350eaf Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 21 Dec 2016 18:55:28 +0100 Subject: [PATCH 15/28] cleans up --- app/helpers/signature_sheets_helper.rb | 2 +- app/models/spending_proposal.rb | 4 -- db/schema.rb | 93 +------------------------- 3 files changed, 3 insertions(+), 96 deletions(-) diff --git a/app/helpers/signature_sheets_helper.rb b/app/helpers/signature_sheets_helper.rb index 14424639c..acd75a5ab 100644 --- a/app/helpers/signature_sheets_helper.rb +++ b/app/helpers/signature_sheets_helper.rb @@ -1,7 +1,7 @@ module SignatureSheetsHelper def signable_options - [[t("activerecord.models.proposal", count: 1), Proposal], + [[t("activerecord.models.proposal", count: 1), Proposal], [t("activerecord.models.spending_proposal", count: 1), SpendingProposal]] end diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index d0563b342..223e9adfe 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -125,10 +125,6 @@ class SpendingProposal < ActiveRecord::Base return :organization if user.organization? end - def voters - User.active.where(id: votes_for.voters) - end - def votable_by?(user) reason_for_not_being_votable_by(user).blank? end diff --git a/db/schema.rb b/db/schema.rb index c5996b03a..acfc87dbb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -123,10 +123,10 @@ ActiveRecord::Schema.define(version: 20161221151239) do t.string "visit_id" t.datetime "hidden_at" t.integer "flags_count", default: 0 + t.datetime "ignored_flag_at" t.integer "cached_votes_total", default: 0 t.integer "cached_votes_up", default: 0 t.integer "cached_votes_down", default: 0 - t.datetime "ignored_flag_at" t.integer "comments_count", default: 0 t.datetime "confirmed_hide_at" t.integer "cached_anonymous_votes_total", default: 0 @@ -145,7 +145,6 @@ ActiveRecord::Schema.define(version: 20161221151239) do add_index "debates", ["cached_votes_total"], name: "index_debates_on_cached_votes_total", using: :btree add_index "debates", ["cached_votes_up"], name: "index_debates_on_cached_votes_up", using: :btree add_index "debates", ["confidence_score"], name: "index_debates_on_confidence_score", using: :btree - add_index "debates", ["description"], name: "index_debates_on_description", using: :btree add_index "debates", ["geozone_id"], name: "index_debates_on_geozone_id", using: :btree add_index "debates", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree add_index "debates", ["hot_score"], name: "index_debates_on_hot_score", using: :btree @@ -211,14 +210,6 @@ ActiveRecord::Schema.define(version: 20161221151239) do t.string "census_code" end - create_table "geozones_poll_questions", force: :cascade do |t| - t.integer "geozone_id" - t.integer "question_id" - end - - add_index "geozones_poll_questions", ["geozone_id"], name: "index_geozones_poll_questions_on_geozone_id", using: :btree - add_index "geozones_poll_questions", ["question_id"], name: "index_geozones_poll_questions_on_question_id", using: :btree - create_table "identities", force: :cascade do |t| t.integer "user_id" t.string "provider" @@ -278,78 +269,6 @@ ActiveRecord::Schema.define(version: 20161221151239) do add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree - create_table "poll_booth_assignments", force: :cascade do |t| - t.integer "booth_id" - t.integer "poll_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "poll_booths", force: :cascade do |t| - t.string "name" - t.string "location" - end - - create_table "poll_officer_assignments", force: :cascade do |t| - t.integer "booth_assignment_id" - t.integer "officer_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "poll_officers", force: :cascade do |t| - t.integer "user_id" - end - - create_table "poll_partial_results", force: :cascade do |t| - t.integer "question_id" - t.integer "author_id" - t.string "answer" - t.integer "amount" - t.string "origin" - end - - add_index "poll_partial_results", ["answer"], name: "index_poll_partial_results_on_answer", using: :btree - add_index "poll_partial_results", ["author_id"], name: "index_poll_partial_results_on_author_id", using: :btree - add_index "poll_partial_results", ["origin"], name: "index_poll_partial_results_on_origin", using: :btree - add_index "poll_partial_results", ["question_id"], name: "index_poll_partial_results_on_question_id", using: :btree - - create_table "poll_questions", force: :cascade do |t| - t.integer "proposal_id" - t.integer "poll_id" - t.integer "author_id" - t.string "author_visible_name" - t.string "title" - t.string "summary" - t.string "valid_answers" - t.text "description" - t.integer "comments_count" - t.datetime "hidden_at" - t.datetime "created_at" - t.datetime "updated_at" - t.boolean "all_geozones", default: false - t.tsvector "tsv" - end - - add_index "poll_questions", ["author_id"], name: "index_poll_questions_on_author_id", using: :btree - add_index "poll_questions", ["poll_id"], name: "index_poll_questions_on_poll_id", using: :btree - add_index "poll_questions", ["proposal_id"], name: "index_poll_questions_on_proposal_id", using: :btree - add_index "poll_questions", ["tsv"], name: "index_poll_questions_on_tsv", using: :gin - - create_table "poll_voters", force: :cascade do |t| - t.string "document_number" - t.string "document_type" - t.integer "booth_assignment_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "polls", force: :cascade do |t| - t.string "name" - t.datetime "starts_at" - t.datetime "ends_at" - end - create_table "proposal_notifications", force: :cascade do |t| t.string "title" t.text "body" @@ -390,7 +309,6 @@ ActiveRecord::Schema.define(version: 20161221151239) do add_index "proposals", ["author_id"], name: "index_proposals_on_author_id", using: :btree add_index "proposals", ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree add_index "proposals", ["confidence_score"], name: "index_proposals_on_confidence_score", using: :btree - add_index "proposals", ["description"], name: "index_proposals_on_description", using: :btree add_index "proposals", ["geozone_id"], name: "index_proposals_on_geozone_id", using: :btree add_index "proposals", ["hidden_at"], name: "index_proposals_on_hidden_at", using: :btree add_index "proposals", ["hot_score"], name: "index_proposals_on_hot_score", using: :btree @@ -563,7 +481,7 @@ ActiveRecord::Schema.define(version: 20161221151239) do t.boolean "email_digest", default: true t.boolean "email_on_direct_message", default: true t.boolean "official_position_badge", default: false - t.datetime "password_changed_at", default: '2016-11-02 13:51:14', null: false + t.datetime "password_changed_at", default: '2016-12-21 17:55:08', null: false t.boolean "created_from_signature", default: false end @@ -656,19 +574,12 @@ ActiveRecord::Schema.define(version: 20161221151239) do add_foreign_key "annotations", "users" add_foreign_key "failed_census_calls", "users" add_foreign_key "flags", "users" - add_foreign_key "geozones_poll_questions", "geozones" - add_foreign_key "geozones_poll_questions", "poll_questions", column: "question_id" add_foreign_key "identities", "users" add_foreign_key "locks", "users" add_foreign_key "managers", "users" add_foreign_key "moderators", "users" add_foreign_key "notifications", "users" add_foreign_key "organizations", "users" - add_foreign_key "poll_partial_results", "poll_questions", column: "question_id" - add_foreign_key "poll_partial_results", "users", column: "author_id" - add_foreign_key "poll_questions", "polls" - add_foreign_key "poll_questions", "proposals" - add_foreign_key "poll_questions", "users", column: "author_id" add_foreign_key "users", "geozones" add_foreign_key "valuators", "users" end From a07fbc5b41f50c6a046551c63d062775389ab0ef Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Fri, 23 Dec 2016 11:28:53 +0100 Subject: [PATCH 16/28] Extract mailer from name and address to a setting --- app/mailers/application_mailer.rb | 2 +- config/initializers/devise.rb | 2 +- db/dev_seeds.rb | 2 ++ db/seeds.rb | 5 +++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 1dbc5baa2..ea1d4fa40 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,5 +1,5 @@ class ApplicationMailer < ActionMailer::Base helper :settings - default from: "Consul " + default from: "#{Setting['mailer_from_name']} <#{Setting['mailer_from_address']}>" layout 'mailer' end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index edabf24d4..a6deae2a7 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -12,7 +12,7 @@ Devise.setup do |config| # Configure the e-mail address which will be shown in Devise::Mailer, # note that it will be overwritten if you use your own mailer class # with default "from" parameter. - config.mailer_sender = 'noreply@consul.es' + config.mailer_sender = "#{Setting['mailer_from_name']} <#{Setting['mailer_from_address']}>" # Configure the class responsible to send e-mails. config.mailer = 'DeviseMailer' diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 02e5f9f6d..631ae0f04 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -32,6 +32,8 @@ Setting.create(key: 'feature.facebook_login', value: "true") Setting.create(key: 'feature.google_login', value: "true") Setting.create(key: 'per_page_code', value: "") Setting.create(key: 'comments_body_max_length', value: '1000') +Setting.create(key: 'mailer_from_name', value: 'Consul') +Setting.create(key: 'mailer_from_address', value: 'noreply@consul.es') puts "Creating Geozones" ('A'..'Z').each { |i| Geozone.create(name: "District #{i}", external_code: i.ord, census_code: i.ord) } diff --git a/db/seeds.rb b/db/seeds.rb index 1607a0aff..73d8153ca 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -81,3 +81,8 @@ Setting['banner-img.banner-img-three'] = "Banner image 3" # Proposal notifications Setting['proposal_notification_minimum_interval_in_days'] = 3 Setting['direct_message_max_per_day'] = 3 + +# Email settings +Setting['mailer_from_name'] = 'Consul' +Setting['mailer_from_address'] = 'noreply@consul.es' + From 85ce7d72270f0345f3110551441804111cc69ee3 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Fri, 23 Dec 2016 15:45:20 +0100 Subject: [PATCH 17/28] Devise won't read mail from Settings in test environment --- config/initializers/devise.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index a6deae2a7..32f387ba9 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -12,7 +12,11 @@ Devise.setup do |config| # Configure the e-mail address which will be shown in Devise::Mailer, # note that it will be overwritten if you use your own mailer class # with default "from" parameter. - config.mailer_sender = "#{Setting['mailer_from_name']} <#{Setting['mailer_from_address']}>" + if Rails.env.test? + config.mailer_sender = "noreply@example.org" + else + config.mailer_sender = "#{Setting['mailer_from_name']} <#{Setting['mailer_from_address']}>" + end # Configure the class responsible to send e-mails. config.mailer = 'DeviseMailer' From 05a00df8ed32bccbfade1c7c3de29cf4d601ce14 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Fri, 23 Dec 2016 15:46:39 +0100 Subject: [PATCH 18/28] Set the default from address as consul.dev --- db/dev_seeds.rb | 2 +- db/seeds.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 631ae0f04..f0d710f2d 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -33,7 +33,7 @@ Setting.create(key: 'feature.google_login', value: "true") Setting.create(key: 'per_page_code', value: "") Setting.create(key: 'comments_body_max_length', value: '1000') Setting.create(key: 'mailer_from_name', value: 'Consul') -Setting.create(key: 'mailer_from_address', value: 'noreply@consul.es') +Setting.create(key: 'mailer_from_address', value: 'noreply@consul.dev') puts "Creating Geozones" ('A'..'Z').each { |i| Geozone.create(name: "District #{i}", external_code: i.ord, census_code: i.ord) } diff --git a/db/seeds.rb b/db/seeds.rb index 73d8153ca..972a0a495 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -84,5 +84,4 @@ Setting['direct_message_max_per_day'] = 3 # Email settings Setting['mailer_from_name'] = 'Consul' -Setting['mailer_from_address'] = 'noreply@consul.es' - +Setting['mailer_from_address'] = 'noreply@consul.dev' From 8c8b273de0d469c1eb069dc2b9ba050cd5979780 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Fri, 23 Dec 2016 15:51:20 +0100 Subject: [PATCH 19/28] Settings translation --- config/locales/settings.en.yml | 2 ++ config/locales/settings.es.yml | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/config/locales/settings.en.yml b/config/locales/settings.en.yml index 0de4e5feb..b21f32cd3 100755 --- a/config/locales/settings.en.yml +++ b/config/locales/settings.en.yml @@ -30,3 +30,5 @@ en: spending_proposals: Investment projects spending_proposal_features: voting_allowed: Voting on investment projects + mailer_from_name: Origin email name + mailer_from_address: Origin email address diff --git a/config/locales/settings.es.yml b/config/locales/settings.es.yml index 05ba6c076..a3171dfe7 100644 --- a/config/locales/settings.es.yml +++ b/config/locales/settings.es.yml @@ -29,4 +29,6 @@ es: debates: Debates spending_proposals: Propuestas de inversión spending_proposal_features: - voting_allowed: Votaciones sobre propuestas de inversión. \ No newline at end of file + voting_allowed: Votaciones sobre propuestas de inversión. + mailer_from_name: Nombre email remitente + mailer_from_address: Dirección email remitente From 37462b79cdbac64340141ec0b5071131093283d2 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 23 Dec 2016 18:37:39 +0100 Subject: [PATCH 20/28] removes unused translations --- config/locales/admin.en.yml | 1 - config/locales/admin.es.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 587b27f1d..4c13fcbe2 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -306,7 +306,6 @@ en: new: New signature sheets new: title: New signature sheets - signable_type: Type of signature sheet document_numbers_note: "Write the numbers separated by commas (,)" submit: Create signature sheet show: diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index fd454c13b..81736624c 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -304,7 +304,6 @@ es: new: Nueva hoja de firmas new: title: Nueva hoja de firmas - signable_type: Tipo de hoja de firmas document_numbers_note: "Introduce los números separados por comas (,)" submit: Crear hoja de firmas show: From ddeca6f06d75855d3e6ef8ce43b83242ea54c6cb Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 23 Dec 2016 19:05:25 +0100 Subject: [PATCH 21/28] fixes specs --- app/models/signature.rb | 2 +- app/views/admin/signature_sheets/new.html.erb | 2 +- spec/features/admin/signature_sheets_spec.rb | 4 ++++ spec/features/management/document_verifications_spec.rb | 4 ++-- spec/features/management/email_verifications_spec.rb | 4 ++-- spec/features/management/managed_users_spec.rb | 6 +++--- spec/features/management/users_spec.rb | 8 ++++---- 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/models/signature.rb b/app/models/signature.rb index b30d4a429..6116dc425 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -51,7 +51,7 @@ class Signature < ActiveRecord::Base created_from_signature: true, verified_at: Time.now, erased_at: Time.now, - email: "#{document_number}@signatures.com", + email: "#{document_number}@inexistent.dev", password: "12345678", username: document_number, terms_of_service: '1' diff --git a/app/views/admin/signature_sheets/new.html.erb b/app/views/admin/signature_sheets/new.html.erb index 013913346..f471e1ea5 100644 --- a/app/views/admin/signature_sheets/new.html.erb +++ b/app/views/admin/signature_sheets/new.html.erb @@ -19,4 +19,4 @@ <%= f.text_area :document_numbers, rows: "6", label: false %> <%= f.submit(class: "button", value: t("admin.signature_sheets.new.submit")) %> -<% end %> +<% end %> \ No newline at end of file diff --git a/spec/features/admin/signature_sheets_spec.rb b/spec/features/admin/signature_sheets_spec.rb index 4c959d1ee..a2474834a 100644 --- a/spec/features/admin/signature_sheets_spec.rb +++ b/spec/features/admin/signature_sheets_spec.rb @@ -29,6 +29,10 @@ feature 'Signature sheets' do click_button "Create signature sheet" expect(page).to have_content "Signature sheet created successfully" + + visit proposal_path(proposal) + + expect(page).to have_content "1 support" end scenario 'Errors on create' do diff --git a/spec/features/management/document_verifications_spec.rb b/spec/features/management/document_verifications_spec.rb index 674349815..130630762 100644 --- a/spec/features/management/document_verifications_spec.rb +++ b/spec/features/management/document_verifications_spec.rb @@ -47,7 +47,7 @@ feature 'DocumentVerifications' do scenario 'Verifying a user which does exists in the census but not in the db redirects allows sending an email' do visit management_document_verifications_path - fill_in 'document_verification_document_number', with: '1234' + fill_in 'document_verification_document_number', with: '12345678Z' click_button 'Check' expect(page).to have_content "Please introduce the email used on the account" @@ -66,7 +66,7 @@ feature 'DocumentVerifications' do expect_any_instance_of(Verification::Management::Document).to receive(:under_sixteen?).and_return(true) visit management_document_verifications_path - fill_in 'document_verification_document_number', with: '1234' + fill_in 'document_verification_document_number', with: '12345678Z' click_button 'Check' expect(page).to have_content "You must be over 16 to verify your account." diff --git a/spec/features/management/email_verifications_spec.rb b/spec/features/management/email_verifications_spec.rb index 22987f317..d68d9bd5f 100644 --- a/spec/features/management/email_verifications_spec.rb +++ b/spec/features/management/email_verifications_spec.rb @@ -8,7 +8,7 @@ feature 'EmailVerifications' do user = create(:user) visit management_document_verifications_path - fill_in 'document_verification_document_number', with: '1234' + fill_in 'document_verification_document_number', with: '12345678Z' click_button 'Check' expect(page).to have_content "Please introduce the email used on the account" @@ -30,7 +30,7 @@ feature 'EmailVerifications' do expect(page).to_not have_link "Verify my account" expect(page).to have_content "Account verified" - expect(user.reload.document_number).to eq('1234') + expect(user.reload.document_number).to eq('12345678Z') expect(user).to be_level_three_verified end diff --git a/spec/features/management/managed_users_spec.rb b/spec/features/management/managed_users_spec.rb index 78010bc39..311b8f5cd 100644 --- a/spec/features/management/managed_users_spec.rb +++ b/spec/features/management/managed_users_spec.rb @@ -57,7 +57,7 @@ feature 'Managed User' do user = create(:user) visit management_document_verifications_path - fill_in 'document_verification_document_number', with: '1234' + fill_in 'document_verification_document_number', with: '12345678Z' click_button 'Check' within(".account-info") do @@ -66,7 +66,7 @@ feature 'Managed User' do expect(page).not_to have_content "Email" expect(page).to have_content "Document type" expect(page).to have_content "Document number" - expect(page).to have_content "1234" + expect(page).to have_content "12345678Z" end expect(page).to have_content "Please introduce the email used on the account" @@ -88,7 +88,7 @@ feature 'Managed User' do login_as_manager visit management_document_verifications_path - fill_in 'document_verification_document_number', with: '1234' + fill_in 'document_verification_document_number', with: '12345678Z' click_button 'Check' expect(page).to have_content "Please introduce the email used on the account" diff --git a/spec/features/management/users_spec.rb b/spec/features/management/users_spec.rb index 1a0618e60..694720cc0 100644 --- a/spec/features/management/users_spec.rb +++ b/spec/features/management/users_spec.rb @@ -9,7 +9,7 @@ feature 'Users' do scenario 'Create a level 3 user from scratch' do visit management_document_verifications_path - fill_in 'document_verification_document_number', with: '1234' + fill_in 'document_verification_document_number', with: '12345678Z' click_button 'Check' expect(page).to have_content "Please introduce the email used on the account" @@ -45,10 +45,10 @@ feature 'Users' do end scenario 'Delete a level 2 user account from document verification page', :js do - level_2_user = create(:user, :level_two, document_number: 13579) + level_2_user = create(:user, :level_two, document_number: "12345678Z") visit management_document_verifications_path - fill_in 'document_verification_document_number', with: '13579' + fill_in 'document_verification_document_number', with: '12345678Z' click_button 'Check' expect(page).to_not have_content "This user account is already verified." @@ -62,7 +62,7 @@ feature 'Users' do expect(level_2_user.reload.erase_reason).to eq "Deleted by manager: manager_user_#{Manager.last.user_id}" visit management_document_verifications_path - fill_in 'document_verification_document_number', with: '13579' + fill_in 'document_verification_document_number', with: '12345678Z' click_button 'Check' expect(page).to have_content "no user account associated to it" From 42b0ff269611a3080dd9e76ea25d4af7128b029f Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 26 Dec 2016 11:51:28 +0100 Subject: [PATCH 22/28] optimize census call to return after finding a valid element --- app/models/signature.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/signature.rb b/app/models/signature.rb index 6116dc425..cc747516d 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -60,9 +60,10 @@ class Signature < ActiveRecord::Base end def in_census? - document_types.any? do |document_type| + response = document_types.detect do |document_type| CensusApi.new.call(document_type, document_number).valid? end + response.present? end def set_user From 1bb307580fe13bd602e79c54943f42b97a57a76d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 26 Dec 2016 12:03:39 +0100 Subject: [PATCH 23/28] makes translations aware of pluralisation --- config/locales/admin.en.yml | 10 +++++++--- config/locales/admin.es.yml | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 4c13fcbe2..b442d68d5 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -312,9 +312,13 @@ en: created_at: Created author: Author documents: Documents - all_verified: "All the signatures (%{count}) are valid" - verified: "There are %{count} valid signatures" - unverified: "There are %{count} invalid signatures" + verified: + one: "There is %{count} valid signature" + other: "There are %{count} valid signatures" + unverified: + one: "There is %{count} invalid signature" + other: "There are %{count} invalid signatures" + unverified_error: (Not verified by Census) loading: "There are still signatures that are being verified by the Census, please refresh the page in a few moments" stats: show: diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 81736624c..15c6697fb 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -310,9 +310,13 @@ es: created_at: Creado author: Autor documents: Documentos - all_verified: "Todas las firmas introducidas (%{count}) son válidas" - verified: "Hay %{count} firmas válidas" - unverified: "Hay %{count} firmas inválidas" + verified: + one: "Hay %{count} firma válida" + other: "Hay %{count} firmas válidas" + unverified: + one: "Hay %{count} firma inválida" + other: "Hay %{count} firmas inválidas" + unverified_error: (No verificadas por el Padrón) loading: "Aún hay firmas que se están verificando por el Padrón, por favor refresca la página en unos instantes" stats: show: From ba88f4d7cd207014e497bec1234bdc572e45273c Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 26 Dec 2016 12:04:14 +0100 Subject: [PATCH 24/28] takes into account asincronos processing --- .../admin/signature_sheets/show.html.erb | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/app/views/admin/signature_sheets/show.html.erb b/app/views/admin/signature_sheets/show.html.erb index dfae240a2..d84284179 100644 --- a/app/views/admin/signature_sheets/show.html.erb +++ b/app/views/admin/signature_sheets/show.html.erb @@ -13,32 +13,24 @@ <%= @signature_sheet.document_numbers %>

    -<% if @signature_sheet.signatures.unverified.any? %> -
    - - <%= t("admin.signature_sheets.show.verified", - count: @signature_sheet.signatures.verified.count ) %> - -
    -
    -

    - - <%= t("admin.signature_sheets.show.unverified", - count: @signature_sheet.signatures.unverified.count ) %> - (No verificadas por el Padrón) - -

    - <%= @signature_sheet.signatures.unverified.map(&:document_number).join(",") %> -
    -<% else %> -
    +
    + + <%= t("admin.signature_sheets.show.verified", + count: @signature_sheet.signatures.verified.count ) %> + +
    + +
    +

    - <%= t("admin.signature_sheets.show.all_verified", - count: @signature_sheet.signatures.verified.count ) %> + <%= t("admin.signature_sheets.show.unverified", + count: @signature_sheet.signatures.unverified.count ) %> + <%= t("admin.signature_sheets.show.unverified_error") %> -

    -<% end %> +

    + <%= @signature_sheet.signatures.unverified.map(&:document_number).join(",") %> +
    <% unless @signature_sheet.processed? %>
    From b8e175d3069cb860c3083e6ec31549a9a7ade79f Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 26 Dec 2016 12:18:36 +0100 Subject: [PATCH 25/28] adds formatting to document numbers --- app/views/admin/signature_sheets/show.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/admin/signature_sheets/show.html.erb b/app/views/admin/signature_sheets/show.html.erb index d84284179..a3787b67d 100644 --- a/app/views/admin/signature_sheets/show.html.erb +++ b/app/views/admin/signature_sheets/show.html.erb @@ -10,7 +10,7 @@

    <%= t("admin.signature_sheets.show.documents") %>

    - <%= @signature_sheet.document_numbers %> + <%= simple_format @signature_sheet.document_numbers %>
    @@ -29,7 +29,7 @@ <%= t("admin.signature_sheets.show.unverified_error") %>

    - <%= @signature_sheet.signatures.unverified.map(&:document_number).join(",") %> + <%= @signature_sheet.signatures.unverified.map(&:document_number).join(", ") %>
    <% unless @signature_sheet.processed? %> From c1b06b6501e69c38258bc36f942bb85f7fafe596 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 26 Dec 2016 12:45:48 +0100 Subject: [PATCH 26/28] creates user without email nor username --- app/models/signature.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/signature.rb b/app/models/signature.rb index cc747516d..5af28c56a 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -51,12 +51,14 @@ class Signature < ActiveRecord::Base created_from_signature: true, verified_at: Time.now, erased_at: Time.now, - email: "#{document_number}@inexistent.dev", - password: "12345678", - username: document_number, + password: random_password terms_of_service: '1' } - User.create(user_params) + User.create!(user_params) + end + + def random_password + (0...20).map { ('a'..'z').to_a[rand(26)] }.join end def in_census? From 5281bb8181a7e92d85a60509e0fa390053419412 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 26 Dec 2016 12:59:23 +0100 Subject: [PATCH 27/28] fixes typo --- app/models/signature.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/signature.rb b/app/models/signature.rb index 5af28c56a..91bb1d816 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -51,7 +51,7 @@ class Signature < ActiveRecord::Base created_from_signature: true, verified_at: Time.now, erased_at: Time.now, - password: random_password + password: random_password, terms_of_service: '1' } User.create!(user_params) From 316f9a037070eb5b4c947b40085caa02ca5831c2 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 26 Dec 2016 14:28:45 +0100 Subject: [PATCH 28/28] creates users with email nil to avoid index constraint --- app/models/signature.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/signature.rb b/app/models/signature.rb index 91bb1d816..47408858a 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -52,7 +52,8 @@ class Signature < ActiveRecord::Base verified_at: Time.now, erased_at: Time.now, password: random_password, - terms_of_service: '1' + terms_of_service: '1', + email: nil } User.create!(user_params) end