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] 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