From 537e6066d8e73d43c4e7727796a0c4c5c6ef4fc0 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Sat, 18 Dec 2021 17:28:03 +0100 Subject: [PATCH 1/5] add Zones dashboard for superadmin --- app/controllers/manager/zones_controller.rb | 46 ++++++++++++++++++ app/dashboards/zone_dashboard.rb | 53 +++++++++++++++++++++ config/routes.rb | 2 + 3 files changed, 101 insertions(+) create mode 100644 app/controllers/manager/zones_controller.rb create mode 100644 app/dashboards/zone_dashboard.rb diff --git a/app/controllers/manager/zones_controller.rb b/app/controllers/manager/zones_controller.rb new file mode 100644 index 000000000..764c222c4 --- /dev/null +++ b/app/controllers/manager/zones_controller.rb @@ -0,0 +1,46 @@ +module Manager + class ZonesController < Manager::ApplicationController + # Overwrite any of the RESTful controller actions to implement custom behavior + # For example, you may want to send an email after a foo is updated. + # + # def update + # super + # send_foo_updated_email(requested_resource) + # end + + # Override this method to specify custom lookup behavior. + # This will be used to set the resource for the `show`, `edit`, and `update` + # actions. + # + # def find_resource(param) + # Foo.find_by!(slug: param) + # end + + # The result of this lookup will be available as `requested_resource` + + # Override this if you have certain roles that require a subset + # this will be used to set the records shown on the `index` action. + # + # def scoped_resource + # if current_user.super_admin? + # resource_class + # else + # resource_class.with_less_stuff + # end + # end + + # Override `resource_params` if you want to transform the submitted + # data before it's persisted. For example, the following would turn all + # empty values into nil values. It uses other APIs such as `resource_class` + # and `dashboard`: + # + # def resource_params + # params.require(resource_class.model_name.param_key). + # permit(dashboard.permitted_attributes). + # transform_values { |value| value == "" ? nil : value } + # end + + # See https://administrate-prototype.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/dashboards/zone_dashboard.rb b/app/dashboards/zone_dashboard.rb new file mode 100644 index 000000000..0c6114aa0 --- /dev/null +++ b/app/dashboards/zone_dashboard.rb @@ -0,0 +1,53 @@ +require "administrate/base_dashboard" + +class ZoneDashboard < Administrate::BaseDashboard + # ATTRIBUTE_TYPES + # a hash that describes the type of each of the model's fields. + # + # Each different type represents an Administrate::Field object, + # which determines how the attribute is displayed + # on pages throughout the dashboard. + ATTRIBUTE_TYPES = { + procedures: Field::HasMany, + id: Field::Number, + acronym: Field::String, + label: Field::String, + created_at: Field::DateTime, + updated_at: Field::DateTime + }.freeze + + # COLLECTION_ATTRIBUTES + # an array of attributes that will be displayed on the model's index page. + # + # By default, it's limited to four items to reduce clutter on index pages. + # Feel free to add, remove, or rearrange items. + COLLECTION_ATTRIBUTES = [:procedures, :id, :acronym, :label].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = [:procedures, :id, :acronym, :label, :created_at, :updated_at].freeze + + # FORM_ATTRIBUTES + # an array of attributes that will be displayed + # on the model's form (`new` and `edit`) pages. + FORM_ATTRIBUTES = [:procedures, :acronym, :label].freeze + + # COLLECTION_FILTERS + # a hash that defines filters that can be used while searching via the search + # field of the dashboard. + # + # For example to add an option to search for open resources by typing "open:" + # in the search field: + # + # COLLECTION_FILTERS = { + # open: ->(resources) { resources.where(open: true) } + # }.freeze + COLLECTION_FILTERS = {}.freeze + + # Overwrite this method to customize how zones are displayed + # across all pages of the admin dashboard. + # + def display_resource(zone) + "Zone #{zone.label}" + end +end diff --git a/config/routes.rb b/config/routes.rb index 517233f4c..ef117806a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,8 @@ Rails.application.routes.draw do resources :super_admins, only: [:index, :show, :destroy] + resources :zones + post 'demandes/create_administrateur' post 'demandes/refuse_administrateur' From c7b5e57d2af99ef9300b50bc27b9030ecb440752 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Sat, 18 Dec 2021 17:28:49 +0100 Subject: [PATCH 2/5] superadmin can get procedures from zone --- app/models/zone.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/zone.rb b/app/models/zone.rb index 31383ffc5..9a699c428 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -10,4 +10,5 @@ # class Zone < ApplicationRecord validates :acronym, presence: true, uniqueness: true + has_many :procedures, -> { order(published_at: :desc) }, inverse_of: :zone end From 90c971567d0cf478b9219a5a6fdf3cf816560549 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 21 Dec 2021 18:40:06 +0100 Subject: [PATCH 3/5] superadmin can show zone for a procedure --- app/dashboards/procedure_dashboard.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/dashboards/procedure_dashboard.rb b/app/dashboards/procedure_dashboard.rb index 992ea66b4..bba85da5b 100644 --- a/app/dashboards/procedure_dashboard.rb +++ b/app/dashboards/procedure_dashboard.rb @@ -16,6 +16,7 @@ class ProcedureDashboard < Administrate::BaseDashboard id: Field::Number.with_options(searchable: true), libelle: Field::String, description: Field::String, + zone: Field::BelongsTo, lien_site_web: Field::String, # TODO: use Field::Url when administrate-v0.12 will be released organisation: Field::String, direction: Field::String, @@ -47,6 +48,7 @@ class ProcedureDashboard < Administrate::BaseDashboard :id, :created_at, :libelle, + :zone, :service, :dossiers, :published_at, @@ -64,6 +66,7 @@ class ProcedureDashboard < Administrate::BaseDashboard :lien_site_web, :organisation, :direction, + :zone, :service, :created_at, :updated_at, From 9a38d5e04926659abdc3d0a0e02367d0143973b0 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 23 Dec 2021 11:33:22 +0100 Subject: [PATCH 4/5] superadmin can only index and show zones --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index ef117806a..4f13636ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,7 +54,7 @@ Rails.application.routes.draw do resources :super_admins, only: [:index, :show, :destroy] - resources :zones + resources :zones, only: [:index, :show] post 'demandes/create_administrateur' post 'demandes/refuse_administrateur' From 6e36fee7b302bb08b57e0b011d4b5cc619d189eb Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 23 Dec 2021 11:34:22 +0100 Subject: [PATCH 5/5] superadmin can display list of procedures without zone --- app/controllers/manager/zones_controller.rb | 10 ++- app/models/null_zone.rb | 31 +++++++++ app/views/manager/zones/index.html.erb | 70 +++++++++++++++++++++ 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 app/models/null_zone.rb create mode 100644 app/views/manager/zones/index.html.erb diff --git a/app/controllers/manager/zones_controller.rb b/app/controllers/manager/zones_controller.rb index 764c222c4..ff3a4eaef 100644 --- a/app/controllers/manager/zones_controller.rb +++ b/app/controllers/manager/zones_controller.rb @@ -12,9 +12,13 @@ module Manager # This will be used to set the resource for the `show`, `edit`, and `update` # actions. # - # def find_resource(param) - # Foo.find_by!(slug: param) - # end + def find_resource(param) + if param == "nil" + NullZone.new + else + Zone.find(param) + end + end # The result of this lookup will be available as `requested_resource` diff --git a/app/models/null_zone.rb b/app/models/null_zone.rb new file mode 100644 index 000000000..632400797 --- /dev/null +++ b/app/models/null_zone.rb @@ -0,0 +1,31 @@ +class NullZone + include ActiveModel::Model + + def procedures + Procedure.where(zone: nil).where.not(published_at: nil).order(published_at: :desc) + end + + def self.reflect_on_association(association) + OpenStruct.new(class_name: "Procedure") if association == :procedures + end + + def label + "non renseignée" + end + + def id + -1 + end + + def acronym + "NA" + end + + def created_at + "NA" + end + + def updated_at + "NA" + end +end diff --git a/app/views/manager/zones/index.html.erb b/app/views/manager/zones/index.html.erb new file mode 100644 index 000000000..fbf35930a --- /dev/null +++ b/app/views/manager/zones/index.html.erb @@ -0,0 +1,70 @@ +<%# +# Index + +This view is the template for the index page. +It is responsible for rendering the search bar, header and pagination. +It renders the `_table` partial to display details about the resources. + +## Local variables: + +- `page`: + An instance of [Administrate::Page::Collection][1]. + Contains helper methods to help display a table, + and knows which attributes should be displayed in the resource's table. +- `resources`: + An instance of `ActiveRecord::Relation` containing the resources + that match the user's search criteria. + By default, these resources are passed to the table partial to be displayed. +- `search_term`: + A string containing the term the user has searched for, if any. +- `show_search_bar`: + A boolean that determines if the search bar should be shown. + +[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection +%> + +<% content_for(:title) do %> + <%= display_resource_name(page.resource_name) %> +<% end %> + + + +
+ <%= render( + "collection", + collection_presenter: page, + collection_field_name: resource_name, + page: page, + resources: resources, + table_title: "page-title" + ) %> + + <%= paginate resources, param_name: '_page' %> +