From 6175990533bb2864dcb2d06af41b2117cb98aae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Sun, 4 Dec 2016 20:41:51 +0100 Subject: [PATCH 01/15] Admin geozones: index, create Modified Geozone factory and seeds to be more complete --- app/controllers/admin/geozones_controller.rb | 28 ++++++++++++++ app/models/abilities/administrator.rb | 1 + app/views/admin/_menu.html.erb | 6 +++ app/views/admin/geozones/_errors.html.erb | 15 ++++++++ app/views/admin/geozones/_form.html.erb | 25 ++++++++++++ app/views/admin/geozones/index.html.erb | 24 ++++++++++++ app/views/admin/geozones/new.html.erb | 13 +++++++ config/locales/admin.en.yml | 20 ++++++++++ config/locales/admin.es.yml | 20 ++++++++++ config/routes.rb | 2 + spec/features/admin/geozones_spec.rb | 40 ++++++++++++++++++++ 11 files changed, 194 insertions(+) create mode 100644 app/controllers/admin/geozones_controller.rb create mode 100644 app/views/admin/geozones/_errors.html.erb create mode 100644 app/views/admin/geozones/_form.html.erb create mode 100644 app/views/admin/geozones/index.html.erb create mode 100644 app/views/admin/geozones/new.html.erb create mode 100644 spec/features/admin/geozones_spec.rb diff --git a/app/controllers/admin/geozones_controller.rb b/app/controllers/admin/geozones_controller.rb new file mode 100644 index 000000000..43f8f83f2 --- /dev/null +++ b/app/controllers/admin/geozones_controller.rb @@ -0,0 +1,28 @@ +class Admin::GeozonesController < ApplicationController + + respond_to :html + + load_and_authorize_resource + + def index + end + + def new + end + + def create + @geozone = Geozone.new(geozone_params) + + if @geozone.save + redirect_to admin_geozones_path + else + render :new + end + end + + private + + def geozone_params + params.require(:geozone).permit(:name, :external_code, :census_code) + end +end diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 0dfce6d3e..be5bed1ac 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -43,6 +43,7 @@ module Abilities can [:read, :update, :destroy, :summary], SpendingProposal can [:search, :edit, :update, :create, :index, :destroy], Banner + can [:index, :create], Geozone end end end diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 7ea48b2c6..3d3e807c7 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -83,6 +83,12 @@ <% end %> +
  • > + <%= link_to admin_geozones_path do %> + <%= t('admin.menu.geozones') %> + <% end %> +
  • +
  • > <%= link_to admin_activity_path do %> <%= t('admin.menu.activity') %> diff --git a/app/views/admin/geozones/_errors.html.erb b/app/views/admin/geozones/_errors.html.erb new file mode 100644 index 000000000..d0f3b849f --- /dev/null +++ b/app/views/admin/geozones/_errors.html.erb @@ -0,0 +1,15 @@ + +<% if @geozone.errors.any? %> + +
    + + + + <%= @geozone.errors.count %> + <%= t("admin.geozones.errors.form.error", count: @geozone.errors.count) %> + +
    + +<% end %> diff --git a/app/views/admin/geozones/_form.html.erb b/app/views/admin/geozones/_form.html.erb new file mode 100644 index 000000000..323a6fbb1 --- /dev/null +++ b/app/views/admin/geozones/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for [:admin, @geozone] do |f| %> + + <%= render 'errors' %> + +
    +
    + <%= f.label :name, t("admin.geozones.geozone.name") %> + <%= f.text_field :name, label: false %> +
    +
    + <%= f.label :external_code, t("admin.geozones.geozone.external_code") %> + <%= f.text_field :external_code, label: false %> +
    +
    + <%= f.label :census_code, t("admin.geozones.geozone.census_code") %> + <%= f.text_field :census_code, label: false %> +
    +
    + +
    +
    + <%= f.submit(class: "button expanded", value: t("admin.geozones.edit.form.submit_button")) %> +
    +
    +<% end %> diff --git a/app/views/admin/geozones/index.html.erb b/app/views/admin/geozones/index.html.erb new file mode 100644 index 000000000..78b4c4312 --- /dev/null +++ b/app/views/admin/geozones/index.html.erb @@ -0,0 +1,24 @@ +<%= link_to t("admin.geozones.index.create"), + new_admin_geozone_path, class: "button success float-right" %> + +

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

    + + + + + + + + + + + + <% @geozones.each do |geozone| %> + + + + + + <% end %> + +
    <%= t("admin.geozones.geozone.name") %><%= t("admin.geozones.geozone.external_code") %><%= t("admin.geozones.geozone.census_code") %>
    <%= geozone.name %><%= geozone.external_code %><%= geozone.census_code %>
    diff --git a/app/views/admin/geozones/new.html.erb b/app/views/admin/geozones/new.html.erb new file mode 100644 index 000000000..0d5080337 --- /dev/null +++ b/app/views/admin/geozones/new.html.erb @@ -0,0 +1,13 @@ +
    + +
    + <%= link_to admin_geozones_path, class: "back" do %> + + <%= t("admin.geozones.new.back") %> + <% end %> + +

    <%= t("admin.geozones.new.creating") %>

    + + <%= render "form" %> +
    +
    diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 2f73f64d8..6ddfddc47 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -97,6 +97,7 @@ en: admin: Admin menu banner: Manage banners debate_topics: Debate topics + geozones: Manage geozones hidden_comments: Hidden comments hidden_debates: Hidden debates hidden_proposals: Hidden proposals @@ -267,6 +268,25 @@ en: in_evaluation_count: In evaluation total_count: Total cost_for_geozone: Cost + geozones: + index: + title: Geozone + create: Create geozone + geozone: + name: Name + external_code: External code + census_code: Census code + errors: + form: + error: + one: "prevented this geozone from being saved" + other: 'prevented this geozone from being saved' + edit: + form: + submit_button: Save changes + new: + back: Volver + creating: Crear distrito stats: show: stats_title: Stats diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 5aada1ce1..1a28fa162 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -95,6 +95,7 @@ es: admin: Menú de administración banner: Gestionar banners debate_topics: Temas de debate + geozones: Gestionar distritos hidden_comments: Comentarios ocultos hidden_debates: Debates ocultos hidden_proposals: Propuestas ocultas @@ -265,6 +266,25 @@ es: in_evaluation_count: En evaluación total_count: Total cost_for_geozone: Coste total + geozones: + index: + title: Distritos + create: Crear un distrito + geozone: + name: Nombre + external_code: Código externo + census_code: Código del censo + errors: + form: + error: + one: "error impidió guardar el distrito" + other: "errores impidieron guardar el distrito." + edit: + form: + submit_button: Guardar cambios + new: + back: Volver + creating: Crear distrito stats: show: stats_title: Estadísticas diff --git a/config/routes.rb b/config/routes.rb index df1a7c6fb..0a621a0d5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -192,6 +192,8 @@ Rails.application.routes.draw do namespace :api do resource :stats, only: :show end + + resources :geozones, only: [:index, :new, :create] end namespace :moderation do diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb new file mode 100644 index 000000000..46bfc9a0c --- /dev/null +++ b/spec/features/admin/geozones_spec.rb @@ -0,0 +1,40 @@ +require 'rails_helper' + +feature 'Admin geozones' do + + background do + login_as(create(:administrator).user) + end + + scenario 'Show list of geozones' do + chamberi = create(:geozone, name: 'Chamberí') + retiro = create(:geozone, name: 'Retiro') + + visit admin_geozones_path + + expect(page).to have_content(chamberi.name) + expect(page).to have_content(retiro.name) + end + + scenario 'Create new geozone' do + visit admin_root_path + + within('#side_menu') do + click_link "Manage geozones" + end + + click_link "Create geozone" + + fill_in 'geozone_name', with: 'Fancy District' + fill_in 'geozone_external_code', with: 123 + fill_in 'geozone_census_code', with: 44 + + click_button 'Save changes' + + expect(page).to have_content 'Fancy District' + + visit admin_geozones_path + + expect(page).to have_content 'Fancy District' + end +end From c23ce8bdc64eb51ce2463e3bcf03a57c88aa3bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Sun, 4 Dec 2016 20:43:30 +0100 Subject: [PATCH 02/15] Modified Geozone factory and seeds to be more complete --- db/dev_seeds.rb | 5 ++++- spec/factories.rb | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 87dc94a2e..e65c68b48 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -34,7 +34,10 @@ Setting.create(key: 'per_page_code', value: "") Setting.create(key: 'comments_body_max_length', value: '1000') puts "Creating Geozones" -('A'..'Z').each{ |i| Geozone.create(name: "District #{i}") } +('A'..'Z').each do |i| + code = "#{i.each_byte.to_a.first}" + Geozone.create(name: "District #{i}", external_code: code, census_code: code) +end puts "Creating Users" diff --git a/spec/factories.rb b/spec/factories.rb index 7b446f3a0..c53f16be6 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -317,7 +317,8 @@ FactoryGirl.define do factory :geozone do sequence(:name) { |n| "District #{n}" } - census_code { '01' } + sequence(:external_code) { |n| "#{n}" } + sequence(:census_code) { |n| "#{n}" } end factory :banner do From 323c1b7593810e4c5b26e2d89f2b6da7e789b9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Wed, 7 Dec 2016 19:19:24 +0100 Subject: [PATCH 03/15] Fixed layout for Admin::GeozonesController --- app/controllers/admin/geozones_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/admin/geozones_controller.rb b/app/controllers/admin/geozones_controller.rb index 43f8f83f2..10747912a 100644 --- a/app/controllers/admin/geozones_controller.rb +++ b/app/controllers/admin/geozones_controller.rb @@ -1,4 +1,4 @@ -class Admin::GeozonesController < ApplicationController +class Admin::GeozonesController < Admin::BaseController respond_to :html From 4b31360d2ec2514ce522cc5707f0209aa6a77fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Wed, 7 Dec 2016 22:04:39 +0100 Subject: [PATCH 04/15] Updated geozone model associations --- app/models/geozone.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/geozone.rb b/app/models/geozone.rb index 787e2b7e2..0dc0e38d6 100644 --- a/app/models/geozone.rb +++ b/app/models/geozone.rb @@ -1,5 +1,8 @@ class Geozone < ActiveRecord::Base + has_many :proposals has_many :spending_proposals + has_many :debates + has_many :users validates :name, presence: true def self.names From fea2009396689ee4acc1cfdfc159536bd2f3ffd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Wed, 7 Dec 2016 22:08:22 +0100 Subject: [PATCH 05/15] Ability to edit geozones from admin dashboard --- app/controllers/admin/geozones_controller.rb | 9 +++++ app/models/abilities/administrator.rb | 2 +- app/views/admin/geozones/edit.html.erb | 13 +++++++ app/views/admin/geozones/index.html.erb | 4 +++ config/locales/admin.en.yml | 2 ++ config/locales/admin.es.yml | 2 ++ config/routes.rb | 2 +- spec/features/admin/geozones_spec.rb | 37 ++++++++++++++++++++ 8 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 app/views/admin/geozones/edit.html.erb diff --git a/app/controllers/admin/geozones_controller.rb b/app/controllers/admin/geozones_controller.rb index 10747912a..eb8119a91 100644 --- a/app/controllers/admin/geozones_controller.rb +++ b/app/controllers/admin/geozones_controller.rb @@ -9,6 +9,8 @@ class Admin::GeozonesController < Admin::BaseController def new end + def edit + end def create @geozone = Geozone.new(geozone_params) @@ -20,6 +22,13 @@ class Admin::GeozonesController < Admin::BaseController end end + def update + if @geozone.update(geozone_params) + redirect_to admin_geozones_path + else + render :edit + end + end private def geozone_params diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index be5bed1ac..975ba7a8d 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -43,7 +43,7 @@ module Abilities can [:read, :update, :destroy, :summary], SpendingProposal can [:search, :edit, :update, :create, :index, :destroy], Banner - can [:index, :create], Geozone + can [:index, :create, :edit, :update, :destroy], Geozone end end end diff --git a/app/views/admin/geozones/edit.html.erb b/app/views/admin/geozones/edit.html.erb new file mode 100644 index 000000000..b6b8c3fd9 --- /dev/null +++ b/app/views/admin/geozones/edit.html.erb @@ -0,0 +1,13 @@ +
    + +
    + <%= link_to admin_geozones_path, class: "back" do %> + + <%= t("admin.geozones.edit.back") %> + <% end %> + +

    <%= t("admin.geozones.edit.editing") %>

    + + <%= render "form" %> +
    +
    diff --git a/app/views/admin/geozones/index.html.erb b/app/views/admin/geozones/index.html.erb index 78b4c4312..9cedca9c6 100644 --- a/app/views/admin/geozones/index.html.erb +++ b/app/views/admin/geozones/index.html.erb @@ -9,6 +9,7 @@ <%= t("admin.geozones.geozone.name") %> <%= t("admin.geozones.geozone.external_code") %> <%= t("admin.geozones.geozone.census_code") %> + @@ -18,6 +19,9 @@ <%= geozone.name %> <%= geozone.external_code %> <%= geozone.census_code %> + + <%= link_to t("admin.geozones.index.edit"), edit_admin_geozone_path(geozone), class: 'edit-banner button hollow' %> + <% end %> diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 6ddfddc47..c46efd04d 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -284,6 +284,8 @@ en: edit: form: submit_button: Save changes + editing: Editing geozone + back: Go back new: back: Volver creating: Crear distrito diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 1a28fa162..3d14a717f 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -282,6 +282,8 @@ es: edit: form: submit_button: Guardar cambios + editing: Editando distrito + back: Volver new: back: Volver creating: Crear distrito diff --git a/config/routes.rb b/config/routes.rb index 0a621a0d5..885d155db 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -193,7 +193,7 @@ Rails.application.routes.draw do resource :stats, only: :show end - resources :geozones, only: [:index, :new, :create] + resources :geozones, only: [:index, :new, :create, :edit, :update, :destroy] end namespace :moderation do diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb index 46bfc9a0c..d571f6616 100644 --- a/spec/features/admin/geozones_spec.rb +++ b/spec/features/admin/geozones_spec.rb @@ -37,4 +37,41 @@ feature 'Admin geozones' do expect(page).to have_content 'Fancy District' end + + scenario 'Edit geozone with no associated elements' do + target_geozone = create(:geozone, name: 'Edit me!', census_code: '012') + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") do + click_link "Edit" + end + + fill_in 'geozone_name', with: 'New geozone name' + fill_in 'geozone_census_code', with: '333' + + click_button 'Save changes' + + within("#geozone_#{target_geozone.id}") do + expect(page).to have_content 'New geozone name' + expect(page).to have_content '333' + end + end + + scenario 'Edit geozone with associated elements' do + target_geozone = create(:geozone, name: 'Edit me!') + proposal = create(:proposal, title: 'Proposal with geozone', geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") do + click_link "Edit" + end + + fill_in 'geozone_name', with: 'New geozone name' + + click_button 'Save changes' + + expect(proposal.geozone.name).to eq('New geozone name') + end end From 4a58c786c8897257d203e64f21feb32df55f3cdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Wed, 7 Dec 2016 22:08:49 +0100 Subject: [PATCH 06/15] Fixed translations --- config/locales/admin.en.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index c46efd04d..ecba0e851 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -287,8 +287,8 @@ en: editing: Editing geozone back: Go back new: - back: Volver - creating: Crear distrito + back: Go back + creating: Create district stats: show: stats_title: Stats From 93e79228a7780967cb25f91a4083bca93c55aa62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Thu, 8 Dec 2016 13:16:42 +0100 Subject: [PATCH 07/15] Ability to delete geozones from admin dashboard --- app/controllers/admin/geozones_controller.rb | 24 ++++++++ app/views/admin/geozones/index.html.erb | 3 + config/locales/admin.en.yml | 5 ++ config/locales/admin.es.yml | 5 ++ spec/features/admin/geozones_spec.rb | 58 ++++++++++++++++++++ 5 files changed, 95 insertions(+) diff --git a/app/controllers/admin/geozones_controller.rb b/app/controllers/admin/geozones_controller.rb index eb8119a91..b87fec242 100644 --- a/app/controllers/admin/geozones_controller.rb +++ b/app/controllers/admin/geozones_controller.rb @@ -29,6 +29,30 @@ class Admin::GeozonesController < Admin::BaseController render :edit end end + + def destroy + # Check that in none of the other associated models talbes a record exists + # referencing this geozone + safe_to_destroy = true + + # safe_to_destroy &= Proposal.where(geozone: @geozone).empty? + # safe_to_destroy &= Debate.where(geozone: @geozone).empty? + # safe_to_destroy &= SpendingProposal.where(geozone: @geozone).empty? + # safe_to_destroy &= User.where(geozone: @geozone).empty? + + Geozone.reflect_on_all_associations.each do |association| + attached_model = association.klass + safe_to_destroy &= attached_model.where(geozone: @geozone).empty? + end + + if safe_to_destroy + @geozone.destroy + redirect_to admin_geozones_url, notice: t('admin.geozones.delete.success') + else + redirect_to admin_geozones_path, flash: { error: t('admin.geozones.delete.error') } + end + end + private def geozone_params diff --git a/app/views/admin/geozones/index.html.erb b/app/views/admin/geozones/index.html.erb index 9cedca9c6..69d9334d5 100644 --- a/app/views/admin/geozones/index.html.erb +++ b/app/views/admin/geozones/index.html.erb @@ -22,6 +22,9 @@ <%= link_to t("admin.geozones.index.edit"), edit_admin_geozone_path(geozone), class: 'edit-banner button hollow' %> + + <%= link_to t("admin.geozones.index.delete"), admin_geozone_path(geozone), method: :delete, class: 'button hollow alert' %> + <% end %> diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index ecba0e851..2187c458c 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -272,6 +272,8 @@ en: index: title: Geozone create: Create geozone + edit: Edit + delete: Delete geozone: name: Name external_code: External code @@ -289,6 +291,9 @@ en: new: back: Go back creating: Create district + delete: + success: Geozone successfully deleted + error: This geozone can't be deleted since there are elements attached to it stats: show: stats_title: Stats diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 3d14a717f..7ba79aad3 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -270,6 +270,8 @@ es: index: title: Distritos create: Crear un distrito + edit: Editar + delete: Borrar geozone: name: Nombre external_code: Código externo @@ -287,6 +289,9 @@ es: new: back: Volver creating: Crear distrito + delete: + success: Distrito borrado correctamente + error: No se puede borrar el distrito porque ya tiene elementos asociados stats: show: stats_title: Estadísticas diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb index d571f6616..c7cc605c8 100644 --- a/spec/features/admin/geozones_spec.rb +++ b/spec/features/admin/geozones_spec.rb @@ -73,5 +73,63 @@ feature 'Admin geozones' do click_button 'Save changes' expect(proposal.geozone.name).to eq('New geozone name') + + scenario 'Delete geozone with no associated elements' do + target_geozone = create(:geozone, name: 'Delete me!') + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).not_to have_content('Delete me!') + expect(Geozone.find_by_id(target_geozone.id)).to be_nil + end + + scenario 'Delete geozone with associated proposal' do + target_geozone = create(:geozone, name: 'Delete me!') + proposal = create(:proposal, geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).to have_content('Delete me!') + expect(proposal.reload.geozone).to eq(target_geozone) + end + + scenario 'Delete geozone with associated spending proposal' do + target_geozone = create(:geozone, name: 'Delete me!') + spending_proposal = create(:spending_proposal, geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).to have_content('Delete me!') + expect(spending_proposal.reload.geozone).to eq(target_geozone) + end + + scenario 'Delete geozone with associated debate' do + target_geozone = create(:geozone, name: 'Delete me!') + debate = create(:debate, geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).to have_content('Delete me!') + expect(debate.reload.geozone).to eq(target_geozone) + end + + scenario 'Delete geozone with associated user' do + target_geozone = create(:geozone, name: 'Delete me!') + user = create(:user, geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).to have_content('Delete me!') + expect(user.reload.geozone).to eq(target_geozone) end end From 0e1cea48c4ff238e93415a282f0cc056d2db4ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Thu, 8 Dec 2016 13:17:05 +0100 Subject: [PATCH 08/15] Fixed admin edit geozone spec --- spec/features/admin/geozones_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb index c7cc605c8..05aac67cc 100644 --- a/spec/features/admin/geozones_spec.rb +++ b/spec/features/admin/geozones_spec.rb @@ -72,7 +72,8 @@ feature 'Admin geozones' do click_button 'Save changes' - expect(proposal.geozone.name).to eq('New geozone name') + expect(proposal.reload.geozone.name).to eq('New geozone name') + end scenario 'Delete geozone with no associated elements' do target_geozone = create(:geozone, name: 'Delete me!') From 6b530201850733dd7fe0729636409ff7a1c42657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Thu, 8 Dec 2016 13:47:23 +0100 Subject: [PATCH 09/15] Show districts ordered by name --- app/controllers/admin/geozones_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/geozones_controller.rb b/app/controllers/admin/geozones_controller.rb index b87fec242..6b755e5f3 100644 --- a/app/controllers/admin/geozones_controller.rb +++ b/app/controllers/admin/geozones_controller.rb @@ -1,14 +1,16 @@ class Admin::GeozonesController < Admin::BaseController respond_to :html - + load_and_authorize_resource def index + @geozones = Geozone.all.order("LOWER(name)") end def new end + def edit end From 60da2a875bb07619489cab9e6ba69d6b43910c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Thu, 8 Dec 2016 16:50:36 +0100 Subject: [PATCH 10/15] CRUD html_map_coordinates geozone attribute --- app/controllers/admin/geozones_controller.rb | 9 ++------- app/views/admin/geozones/_form.html.erb | 10 +++++++--- app/views/admin/geozones/index.html.erb | 2 ++ config/locales/admin.en.yml | 1 + config/locales/admin.es.yml | 1 + 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/geozones_controller.rb b/app/controllers/admin/geozones_controller.rb index 6b755e5f3..a8648f287 100644 --- a/app/controllers/admin/geozones_controller.rb +++ b/app/controllers/admin/geozones_controller.rb @@ -33,15 +33,10 @@ class Admin::GeozonesController < Admin::BaseController end def destroy - # Check that in none of the other associated models talbes a record exists + # Check that in none of the other associated models tables a record exists # referencing this geozone safe_to_destroy = true - # safe_to_destroy &= Proposal.where(geozone: @geozone).empty? - # safe_to_destroy &= Debate.where(geozone: @geozone).empty? - # safe_to_destroy &= SpendingProposal.where(geozone: @geozone).empty? - # safe_to_destroy &= User.where(geozone: @geozone).empty? - Geozone.reflect_on_all_associations.each do |association| attached_model = association.klass safe_to_destroy &= attached_model.where(geozone: @geozone).empty? @@ -58,6 +53,6 @@ class Admin::GeozonesController < Admin::BaseController private def geozone_params - params.require(:geozone).permit(:name, :external_code, :census_code) + params.require(:geozone).permit(:name, :external_code, :census_code, :html_map_coordinates) end end diff --git a/app/views/admin/geozones/_form.html.erb b/app/views/admin/geozones/_form.html.erb index 323a6fbb1..e92b98c0b 100644 --- a/app/views/admin/geozones/_form.html.erb +++ b/app/views/admin/geozones/_form.html.erb @@ -3,15 +3,19 @@ <%= render 'errors' %>
    -
    +
    <%= f.label :name, t("admin.geozones.geozone.name") %> <%= f.text_field :name, label: false %>
    -
    +
    + <%= f.label :html_map_coordinates, t("admin.geozones.geozone.coordinates") %> + <%= f.text_field :html_map_coordinates, label: false %> +
    +
    <%= f.label :external_code, t("admin.geozones.geozone.external_code") %> <%= f.text_field :external_code, label: false %>
    -
    +
    <%= f.label :census_code, t("admin.geozones.geozone.census_code") %> <%= f.text_field :census_code, label: false %>
    diff --git a/app/views/admin/geozones/index.html.erb b/app/views/admin/geozones/index.html.erb index 69d9334d5..5a524e46e 100644 --- a/app/views/admin/geozones/index.html.erb +++ b/app/views/admin/geozones/index.html.erb @@ -9,6 +9,7 @@ <%= t("admin.geozones.geozone.name") %> <%= t("admin.geozones.geozone.external_code") %> <%= t("admin.geozones.geozone.census_code") %> + <%= t("admin.geozones.geozone.coordinates") %> @@ -19,6 +20,7 @@ <%= geozone.name %> <%= geozone.external_code %> <%= geozone.census_code %> + <%= geozone.html_map_coordinates %> <%= link_to t("admin.geozones.index.edit"), edit_admin_geozone_path(geozone), class: 'edit-banner button hollow' %> diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 2187c458c..370a06ccc 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -278,6 +278,7 @@ en: name: Name external_code: External code census_code: Census code + coordinates: Coordinates errors: form: error: diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 7ba79aad3..8819905d8 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -276,6 +276,7 @@ es: name: Nombre external_code: Código externo census_code: Código del censo + coordinates: Coordenadas errors: form: error: From c22a80b48ff458e1490992fd62189dff45d54785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Tue, 13 Dec 2016 12:30:47 +0100 Subject: [PATCH 11/15] Move geozone 'safe to destroy' check from controller to model --- app/controllers/admin/geozones_controller.rb | 13 ++----------- app/models/geozone.rb | 5 +++++ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/geozones_controller.rb b/app/controllers/admin/geozones_controller.rb index a8648f287..b73c13422 100644 --- a/app/controllers/admin/geozones_controller.rb +++ b/app/controllers/admin/geozones_controller.rb @@ -33,18 +33,9 @@ class Admin::GeozonesController < Admin::BaseController end def destroy - # Check that in none of the other associated models tables a record exists - # referencing this geozone - safe_to_destroy = true - - Geozone.reflect_on_all_associations.each do |association| - attached_model = association.klass - safe_to_destroy &= attached_model.where(geozone: @geozone).empty? - end - - if safe_to_destroy + if @geozone.safe_to_destroy? @geozone.destroy - redirect_to admin_geozones_url, notice: t('admin.geozones.delete.success') + redirect_to admin_geozones_path, notice: t('admin.geozones.delete.success') else redirect_to admin_geozones_path, flash: { error: t('admin.geozones.delete.error') } end diff --git a/app/models/geozone.rb b/app/models/geozone.rb index 0dc0e38d6..7e38ce97d 100644 --- a/app/models/geozone.rb +++ b/app/models/geozone.rb @@ -9,4 +9,9 @@ class Geozone < ActiveRecord::Base Geozone.pluck(:name) end + def safe_to_destroy? + Geozone.reflect_on_all_associations(:has_many).all? do |association| + association.klass.where(geozone: self).empty? + end + end end From 2a3dfdd595d003d4cf8b90d8b3080b17f5f2d89a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Tue, 13 Dec 2016 12:33:58 +0100 Subject: [PATCH 12/15] Refactor admin geozones specs --- spec/features/admin/geozones_spec.rb | 76 ++++++++++++++-------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb index 05aac67cc..6c4a1c468 100644 --- a/spec/features/admin/geozones_spec.rb +++ b/spec/features/admin/geozones_spec.rb @@ -19,11 +19,9 @@ feature 'Admin geozones' do scenario 'Create new geozone' do visit admin_root_path - within('#side_menu') do - click_link "Manage geozones" - end + within('#side_menu') { click_link 'Manage geozones' } - click_link "Create geozone" + click_link 'Create geozone' fill_in 'geozone_name', with: 'Fancy District' fill_in 'geozone_external_code', with: 123 @@ -39,98 +37,100 @@ feature 'Admin geozones' do end scenario 'Edit geozone with no associated elements' do - target_geozone = create(:geozone, name: 'Edit me!', census_code: '012') + geozone = create(:geozone, name: 'Edit me!', census_code: '012') visit admin_geozones_path - within("#geozone_#{target_geozone.id}") do - click_link "Edit" - end + within("#geozone_#{geozone.id}") { click_link 'Edit' } fill_in 'geozone_name', with: 'New geozone name' fill_in 'geozone_census_code', with: '333' click_button 'Save changes' - within("#geozone_#{target_geozone.id}") do + within("#geozone_#{geozone.id}") do expect(page).to have_content 'New geozone name' expect(page).to have_content '333' end end scenario 'Edit geozone with associated elements' do - target_geozone = create(:geozone, name: 'Edit me!') - proposal = create(:proposal, title: 'Proposal with geozone', geozone: target_geozone) + geozone = create(:geozone, name: 'Edit me!') + create(:proposal, title: 'Proposal with geozone', geozone: geozone) visit admin_geozones_path - within("#geozone_#{target_geozone.id}") do - click_link "Edit" - end + within("#geozone_#{geozone.id}") { click_link 'Edit' } fill_in 'geozone_name', with: 'New geozone name' click_button 'Save changes' - expect(proposal.reload.geozone.name).to eq('New geozone name') + within("#geozone_#{geozone.id}") do + expect(page).to have_content 'New geozone name' + end end scenario 'Delete geozone with no associated elements' do - target_geozone = create(:geozone, name: 'Delete me!') + geozone = create(:geozone, name: 'Delete me!') visit admin_geozones_path - within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + within("#geozone_#{geozone.id}") { click_link 'Delete' } expect(page).not_to have_content('Delete me!') - expect(Geozone.find_by_id(target_geozone.id)).to be_nil + expect(Geozone.where(id: geozone.id)).to be_empty end scenario 'Delete geozone with associated proposal' do - target_geozone = create(:geozone, name: 'Delete me!') - proposal = create(:proposal, geozone: target_geozone) + geozone = create(:geozone, name: 'Delete me!') + create(:proposal, geozone: geozone) visit admin_geozones_path - within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + within("#geozone_#{geozone.id}") { click_link 'Delete' } - expect(page).to have_content('Delete me!') - expect(proposal.reload.geozone).to eq(target_geozone) + within("#geozone_#{geozone.id}") do + expect(page).to have_content 'Delete me!' + end end scenario 'Delete geozone with associated spending proposal' do - target_geozone = create(:geozone, name: 'Delete me!') - spending_proposal = create(:spending_proposal, geozone: target_geozone) + geozone = create(:geozone, name: 'Delete me!') + create(:spending_proposal, geozone: geozone) visit admin_geozones_path - within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + within("#geozone_#{geozone.id}") { click_link 'Delete' } - expect(page).to have_content('Delete me!') - expect(spending_proposal.reload.geozone).to eq(target_geozone) + within("#geozone_#{geozone.id}") do + expect(page).to have_content 'Delete me!' + end end scenario 'Delete geozone with associated debate' do - target_geozone = create(:geozone, name: 'Delete me!') - debate = create(:debate, geozone: target_geozone) + geozone = create(:geozone, name: 'Delete me!') + create(:debate, geozone: geozone) visit admin_geozones_path - within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + within("#geozone_#{geozone.id}") { click_link 'Delete' } - expect(page).to have_content('Delete me!') - expect(debate.reload.geozone).to eq(target_geozone) + within("#geozone_#{geozone.id}") do + expect(page).to have_content 'Delete me!' + end end scenario 'Delete geozone with associated user' do - target_geozone = create(:geozone, name: 'Delete me!') - user = create(:user, geozone: target_geozone) + geozone = create(:geozone, name: 'Delete me!') + create(:user, geozone: geozone) visit admin_geozones_path - within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + within("#geozone_#{geozone.id}") { click_link 'Delete' } - expect(page).to have_content('Delete me!') - expect(user.reload.geozone).to eq(target_geozone) + within("#geozone_#{geozone.id}") do + expect(page).to have_content 'Delete me!' + end end end From c2006526c96ecea135d5e271f540e45b7e958f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Tue, 13 Dec 2016 12:40:11 +0100 Subject: [PATCH 13/15] Better way to set geozone codes when seeding --- db/dev_seeds.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index e65c68b48..02e5f9f6d 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -34,10 +34,7 @@ Setting.create(key: 'per_page_code', value: "") Setting.create(key: 'comments_body_max_length', value: '1000') puts "Creating Geozones" -('A'..'Z').each do |i| - code = "#{i.each_byte.to_a.first}" - Geozone.create(name: "District #{i}", external_code: code, census_code: code) -end +('A'..'Z').each { |i| Geozone.create(name: "District #{i}", external_code: i.ord, census_code: i.ord) } puts "Creating Users" From 419905fb591dbc3a959146c6da06acf4f166c360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Tue, 13 Dec 2016 13:25:38 +0100 Subject: [PATCH 14/15] Set expectations for flash inside admin geozones spec --- spec/features/admin/geozones_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb index 6c4a1c468..cb0392663 100644 --- a/spec/features/admin/geozones_spec.rb +++ b/spec/features/admin/geozones_spec.rb @@ -78,6 +78,7 @@ feature 'Admin geozones' do within("#geozone_#{geozone.id}") { click_link 'Delete' } + expect(page).to have_content 'Geozone successfully deleted' expect(page).not_to have_content('Delete me!') expect(Geozone.where(id: geozone.id)).to be_empty end @@ -90,6 +91,8 @@ feature 'Admin geozones' do within("#geozone_#{geozone.id}") { click_link 'Delete' } + expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" + within("#geozone_#{geozone.id}") do expect(page).to have_content 'Delete me!' end @@ -103,6 +106,8 @@ feature 'Admin geozones' do within("#geozone_#{geozone.id}") { click_link 'Delete' } + expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" + within("#geozone_#{geozone.id}") do expect(page).to have_content 'Delete me!' end @@ -116,6 +121,8 @@ feature 'Admin geozones' do within("#geozone_#{geozone.id}") { click_link 'Delete' } + expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" + within("#geozone_#{geozone.id}") do expect(page).to have_content 'Delete me!' end @@ -129,6 +136,8 @@ feature 'Admin geozones' do within("#geozone_#{geozone.id}") { click_link 'Delete' } + expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" + within("#geozone_#{geozone.id}") do expect(page).to have_content 'Delete me!' end From db029964fad186ff940107c64c092986a5583d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Tue, 13 Dec 2016 18:38:13 +0100 Subject: [PATCH 15/15] Check geozone 'safe_to_delete' behavior as unit test instead of integration test --- spec/features/admin/geozones_spec.rb | 47 +--------------------------- spec/models/geozone_spec.rb | 26 +++++++++++++++ 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb index cb0392663..44f9b0d03 100644 --- a/spec/features/admin/geozones_spec.rb +++ b/spec/features/admin/geozones_spec.rb @@ -83,7 +83,7 @@ feature 'Admin geozones' do expect(Geozone.where(id: geozone.id)).to be_empty end - scenario 'Delete geozone with associated proposal' do + scenario 'Delete geozone with associated element' do geozone = create(:geozone, name: 'Delete me!') create(:proposal, geozone: geozone) @@ -97,49 +97,4 @@ feature 'Admin geozones' do expect(page).to have_content 'Delete me!' end end - - scenario 'Delete geozone with associated spending proposal' do - geozone = create(:geozone, name: 'Delete me!') - create(:spending_proposal, geozone: geozone) - - visit admin_geozones_path - - within("#geozone_#{geozone.id}") { click_link 'Delete' } - - expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" - - within("#geozone_#{geozone.id}") do - expect(page).to have_content 'Delete me!' - end - end - - scenario 'Delete geozone with associated debate' do - geozone = create(:geozone, name: 'Delete me!') - create(:debate, geozone: geozone) - - visit admin_geozones_path - - within("#geozone_#{geozone.id}") { click_link 'Delete' } - - expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" - - within("#geozone_#{geozone.id}") do - expect(page).to have_content 'Delete me!' - end - end - - scenario 'Delete geozone with associated user' do - geozone = create(:geozone, name: 'Delete me!') - create(:user, geozone: geozone) - - visit admin_geozones_path - - within("#geozone_#{geozone.id}") { click_link 'Delete' } - - expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" - - within("#geozone_#{geozone.id}") do - expect(page).to have_content 'Delete me!' - end - end end diff --git a/spec/models/geozone_spec.rb b/spec/models/geozone_spec.rb index a29c4c918..1a7bf6b09 100644 --- a/spec/models/geozone_spec.rb +++ b/spec/models/geozone_spec.rb @@ -11,4 +11,30 @@ RSpec.describe Geozone, type: :model do geozone.name = nil expect(geozone).to_not be_valid end + + describe "#safe_to_destroy?" do + it "is true when not linked to other models" do + expect(geozone.safe_to_destroy?).to be_truthy + end + + it "is false when already linked to user" do + create(:user, geozone: geozone) + expect(geozone.safe_to_destroy?).to be_falsey + end + + it "is false when already linked to proposal" do + create(:proposal, geozone: geozone) + expect(geozone.safe_to_destroy?).to be_falsey + end + + it "is false when already linked to spending proposal" do + create(:spending_proposal, geozone: geozone) + expect(geozone.safe_to_destroy?).to be_falsey + end + + it "is false when already linked to debate" do + create(:debate, geozone: geozone) + expect(geozone.safe_to_destroy?).to be_falsey + end + end end