Merge branch 'master' into legislation-module-stable

This commit is contained in:
Amaia Castro
2016-12-15 11:26:22 +01:00
16 changed files with 355 additions and 2 deletions

View File

@@ -0,0 +1,49 @@
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
def create
@geozone = Geozone.new(geozone_params)
if @geozone.save
redirect_to admin_geozones_path
else
render :new
end
end
def update
if @geozone.update(geozone_params)
redirect_to admin_geozones_path
else
render :edit
end
end
def destroy
if @geozone.safe_to_destroy?
@geozone.destroy
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
end
private
def geozone_params
params.require(:geozone).permit(:name, :external_code, :census_code, :html_map_coordinates)
end
end

View File

@@ -44,6 +44,8 @@ module Abilities
can [:read, :update, :destroy, :summary], SpendingProposal can [:read, :update, :destroy, :summary], SpendingProposal
can [:search, :edit, :update, :create, :index, :destroy], Banner can [:search, :edit, :update, :create, :index, :destroy], Banner
can [:index, :create, :edit, :update, :destroy], Geozone
can [:manage], ::Legislation::Process can [:manage], ::Legislation::Process
can [:manage], ::Legislation::DraftVersion can [:manage], ::Legislation::DraftVersion
end end

View File

@@ -1,9 +1,17 @@
class Geozone < ActiveRecord::Base class Geozone < ActiveRecord::Base
has_many :proposals
has_many :spending_proposals has_many :spending_proposals
has_many :debates
has_many :users
validates :name, presence: true validates :name, presence: true
def self.names def self.names
Geozone.pluck(:name) Geozone.pluck(:name)
end end
def safe_to_destroy?
Geozone.reflect_on_all_associations(:has_many).all? do |association|
association.klass.where(geozone: self).empty?
end
end
end end

View File

@@ -91,6 +91,12 @@
<% end %> <% end %>
</li> </li>
<li <%= 'class=active' if controller_name == 'geozones' %>>
<%= link_to admin_geozones_path do %>
<span class="icon-settings"></span><%= t('admin.menu.geozones') %>
<% end %>
</li>
<li <%= 'class=active' if controller_name == 'activity' %>> <li <%= 'class=active' if controller_name == 'activity' %>>
<%= link_to admin_activity_path do %> <%= link_to admin_activity_path do %>
<span class="icon-eye"></span><%= t('admin.menu.activity') %> <span class="icon-eye"></span><%= t('admin.menu.activity') %>

View File

@@ -0,0 +1,15 @@
<% if @geozone.errors.any? %>
<div id="error_explanation" data-alert class="callout alert" data-closable>
<button class="close-button" aria-label="<%= t("application.close") %>" type="button" data-close>
<span aria-hidden="true">&times;</span>
</button>
<strong>
<%= @geozone.errors.count %>
<%= t("admin.geozones.errors.form.error", count: @geozone.errors.count) %>
</strong>
</div>
<% end %>

View File

@@ -0,0 +1,29 @@
<%= form_for [:admin, @geozone] do |f| %>
<%= render 'errors' %>
<div clas="row">
<div class="small-12 medium-6 large-4 column">
<%= f.label :name, t("admin.geozones.geozone.name") %>
<%= f.text_field :name, label: false %>
</div>
<div class="small-12 medium-6 large-4 column">
<%= f.label :html_map_coordinates, t("admin.geozones.geozone.coordinates") %>
<%= f.text_field :html_map_coordinates, label: false %>
</div>
<div class="small-12 medium-6 large-2 column">
<%= f.label :external_code, t("admin.geozones.geozone.external_code") %>
<%= f.text_field :external_code, label: false %>
</div>
<div class="small-12 medium-6 large-2 column">
<%= f.label :census_code, t("admin.geozones.geozone.census_code") %>
<%= f.text_field :census_code, label: false %>
</div>
</div>
<div class="row">
<div class="actions small-12 large-3 medium-3 column">
<%= f.submit(class: "button expanded", value: t("admin.geozones.edit.form.submit_button")) %>
</div>
</div>
<% end %>

View File

@@ -0,0 +1,13 @@
<div class="row">
<div class="small-12 column">
<%= link_to admin_geozones_path, class: "back" do %>
<span class="icon-angle-left"></span>
<%= t("admin.geozones.edit.back") %>
<% end %>
<h1><%= t("admin.geozones.edit.editing") %></h1>
<%= render "form" %>
</div>
</div>

View File

@@ -0,0 +1,33 @@
<%= link_to t("admin.geozones.index.create"),
new_admin_geozone_path, class: "button success float-right" %>
<h2 class="inline-block"><%= t("admin.geozones.index.title") %></h2>
<table>
<thead>
<tr>
<th><%= t("admin.geozones.geozone.name") %></th>
<th><%= t("admin.geozones.geozone.external_code") %></th>
<th><%= t("admin.geozones.geozone.census_code") %></th>
<th><%= t("admin.geozones.geozone.coordinates") %></th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @geozones.each do |geozone| %>
<tr id="<%= dom_id(geozone) %>">
<td><%= geozone.name %></td>
<td><%= geozone.external_code %></td>
<td><%= geozone.census_code %></td>
<td><%= geozone.html_map_coordinates %></td>
<td>
<%= link_to t("admin.geozones.index.edit"), edit_admin_geozone_path(geozone), class: 'edit-banner button hollow' %>
</td>
<td>
<%= link_to t("admin.geozones.index.delete"), admin_geozone_path(geozone), method: :delete, class: 'button hollow alert' %>
</td>
</tr>
<% end %>
</tbody>
</table>

View File

@@ -0,0 +1,13 @@
<div class="geozone-new row">
<div class="small-12 column">
<%= link_to admin_geozones_path, class: "back" do %>
<span class="icon-angle-left"></span>
<%= t("admin.geozones.new.back") %>
<% end %>
<h1><%= t("admin.geozones.new.creating") %></h1>
<%= render "form" %>
</div>
</div>

View File

@@ -148,6 +148,7 @@ en:
admin: Admin menu admin: Admin menu
banner: Manage banners banner: Manage banners
debate_topics: Debate topics debate_topics: Debate topics
geozones: Manage geozones
hidden_comments: Hidden comments hidden_comments: Hidden comments
hidden_debates: Hidden debates hidden_debates: Hidden debates
hidden_proposals: Hidden proposals hidden_proposals: Hidden proposals
@@ -319,6 +320,33 @@ en:
in_evaluation_count: In evaluation in_evaluation_count: In evaluation
total_count: Total total_count: Total
cost_for_geozone: Cost cost_for_geozone: Cost
geozones:
index:
title: Geozone
create: Create geozone
edit: Edit
delete: Delete
geozone:
name: Name
external_code: External code
census_code: Census code
coordinates: Coordinates
errors:
form:
error:
one: "prevented this geozone from being saved"
other: 'prevented this geozone from being saved'
edit:
form:
submit_button: Save changes
editing: Editing geozone
back: Go back
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: stats:
show: show:
stats_title: Stats stats_title: Stats

View File

@@ -146,6 +146,7 @@ es:
admin: Menú de administración admin: Menú de administración
banner: Gestionar banners banner: Gestionar banners
debate_topics: Temas de debate debate_topics: Temas de debate
geozones: Gestionar distritos
hidden_comments: Comentarios ocultos hidden_comments: Comentarios ocultos
hidden_debates: Debates ocultos hidden_debates: Debates ocultos
hidden_proposals: Propuestas ocultas hidden_proposals: Propuestas ocultas
@@ -317,6 +318,33 @@ es:
in_evaluation_count: En evaluación in_evaluation_count: En evaluación
total_count: Total total_count: Total
cost_for_geozone: Coste total cost_for_geozone: Coste total
geozones:
index:
title: Distritos
create: Crear un distrito
edit: Editar
delete: Borrar
geozone:
name: Nombre
external_code: Código externo
census_code: Código del censo
coordinates: Coordenadas
errors:
form:
error:
one: "error impidió guardar el distrito"
other: "errores impidieron guardar el distrito."
edit:
form:
submit_button: Guardar cambios
editing: Editando distrito
back: Volver
new:
back: Volver
creating: Crear distrito
delete:
success: Distrito borrado correctamente
error: No se puede borrar el distrito porque ya tiene elementos asociados
stats: stats:
show: show:
stats_title: Estadísticas stats_title: Estadísticas

View File

@@ -198,6 +198,8 @@ Rails.application.routes.draw do
namespace :api do namespace :api do
resource :stats, only: :show resource :stats, only: :show
end end
resources :geozones, only: [:index, :new, :create, :edit, :update, :destroy]
end end
namespace :moderation do namespace :moderation do

View File

@@ -35,7 +35,7 @@ Setting.create(key: 'per_page_code', value: "")
Setting.create(key: 'comments_body_max_length', value: '1000') Setting.create(key: 'comments_body_max_length', value: '1000')
puts "Creating Geozones" puts "Creating Geozones"
('A'..'Z').each{ |i| Geozone.create(name: "District #{i}") } ('A'..'Z').each { |i| Geozone.create(name: "District #{i}", external_code: i.ord, census_code: i.ord) }
puts "Creating Users" puts "Creating Users"

View File

@@ -317,7 +317,8 @@ FactoryGirl.define do
factory :geozone do factory :geozone do
sequence(:name) { |n| "District #{n}" } sequence(:name) { |n| "District #{n}" }
census_code { '01' } sequence(:external_code) { |n| "#{n}" }
sequence(:census_code) { |n| "#{n}" }
end end
factory :banner do factory :banner do

View File

@@ -0,0 +1,100 @@
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') { click_link 'Manage geozones' }
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
scenario 'Edit geozone with no associated elements' do
geozone = create(:geozone, name: 'Edit me!', census_code: '012')
visit admin_geozones_path
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_#{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
geozone = create(:geozone, name: 'Edit me!')
create(:proposal, title: 'Proposal with geozone', geozone: geozone)
visit admin_geozones_path
within("#geozone_#{geozone.id}") { click_link 'Edit' }
fill_in 'geozone_name', with: 'New geozone name'
click_button 'Save changes'
within("#geozone_#{geozone.id}") do
expect(page).to have_content 'New geozone name'
end
end
scenario 'Delete geozone with no associated elements' do
geozone = create(:geozone, name: 'Delete me!')
visit admin_geozones_path
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
scenario 'Delete geozone with associated element' do
geozone = create(:geozone, name: 'Delete me!')
create(: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
end

View File

@@ -11,4 +11,30 @@ RSpec.describe Geozone, type: :model do
geozone.name = nil geozone.name = nil
expect(geozone).to_not be_valid expect(geozone).to_not be_valid
end 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 end