diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index b03ac1368..32577c576 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -31,4 +31,5 @@ @import "admin/*"; @import "sdg/**/*"; @import "sdg_management/*"; +@import "sdg_management/**/*"; @import "widgets/**/*"; diff --git a/app/assets/stylesheets/mixins.scss b/app/assets/stylesheets/mixins.scss index 363653ed8..906c0bc3f 100644 --- a/app/assets/stylesheets/mixins.scss +++ b/app/assets/stylesheets/mixins.scss @@ -208,6 +208,11 @@ } } +@mixin regular-button($color: $brand) { + @include button($background: $color); + @extend %button; +} + @mixin hollow-button($color: $link) { @include button($style: hollow, $background: $color); @extend %button; diff --git a/app/assets/stylesheets/sdg_management/homepage/show.scss b/app/assets/stylesheets/sdg_management/homepage/show.scss new file mode 100644 index 000000000..098e8e311 --- /dev/null +++ b/app/assets/stylesheets/sdg_management/homepage/show.scss @@ -0,0 +1,11 @@ +.phase-cards { + > header { + align-items: flex-start; + display: flex; + + a { + @include regular-button; + margin-left: auto; + } + } +} diff --git a/app/components/sdg_management/homepage/show_component.html.erb b/app/components/sdg_management/homepage/show_component.html.erb index 3a06ebee1..53046b11b 100644 --- a/app/components/sdg_management/homepage/show_component.html.erb +++ b/app/components/sdg_management/homepage/show_component.html.erb @@ -1 +1,15 @@ <%= header %> + +<% phases.each do |phase| %> +
+
+

<%= phase.title %>

+ <%= link_to create_card_text(phase), new_sdg_management_sdg_phase_widget_card_path(phase) %> +
+ + <%= render Admin::Widget::Cards::TableComponent.new( + phase.cards, + no_cards_message: no_cards_message + ) %> +
+<% end %> diff --git a/app/components/sdg_management/homepage/show_component.rb b/app/components/sdg_management/homepage/show_component.rb index dd2ee5b56..e5dbaf6cf 100644 --- a/app/components/sdg_management/homepage/show_component.rb +++ b/app/components/sdg_management/homepage/show_component.rb @@ -12,4 +12,12 @@ class SDGManagement::Homepage::ShowComponent < ApplicationComponent def title t("sdg_management.homepage.title") end + + def create_card_text(phase) + t("sdg_management.homepage.create_card", phase: phase.title.downcase) + end + + def no_cards_message + t("sdg_management.homepage.no_cards") + end end diff --git a/app/controllers/sdg_management/cards_controller.rb b/app/controllers/sdg_management/cards_controller.rb new file mode 100644 index 000000000..f3af9ed19 --- /dev/null +++ b/app/controllers/sdg_management/cards_controller.rb @@ -0,0 +1,13 @@ +class SDGManagement::CardsController < SDGManagement::BaseController + include Admin::Widget::CardsActions + helper_method :index_path + + load_and_authorize_resource :phase, class: "SDG::Phase", id_param: "sdg_phase_id" + load_and_authorize_resource :card, through: :phase, class: "Widget::Card" + + private + + def index_path + sdg_management_homepage_path + end +end diff --git a/app/models/abilities/sdg/manager.rb b/app/models/abilities/sdg/manager.rb index 7dcbf6769..a3264c9f3 100644 --- a/app/models/abilities/sdg/manager.rb +++ b/app/models/abilities/sdg/manager.rb @@ -6,5 +6,7 @@ class Abilities::SDG::Manager can :read, ::SDG::Target can :manage, ::SDG::LocalTarget + can [:read, :update, :destroy], Widget::Card, cardable_type: "SDG::Phase" + can(:create, Widget::Card) { |card| card.cardable_type == "SDG::Phase" } end end diff --git a/app/models/sdg/phase.rb b/app/models/sdg/phase.rb index 6b5f5aa24..3796dcf21 100644 --- a/app/models/sdg/phase.rb +++ b/app/models/sdg/phase.rb @@ -1,8 +1,13 @@ class SDG::Phase < ApplicationRecord + include Cardable enum kind: %w[sensitization planning monitoring] validates :kind, presence: true, uniqueness: true def self.[](kind) find_by!(kind: kind) end + + def title + self.class.human_attribute_name("kind.#{kind}") + end end diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 84264fbce..eae5e99a0 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -336,6 +336,10 @@ en: sdg/local_target/translation: title: "Title" description: "Description" + sdg/phase/kind: + sensitization: "Sensitization" + planning: "Planning" + monitoring: "Monitoring" sdg/target: code: "Code" title: "Title" diff --git a/config/locales/en/sdg_management.yml b/config/locales/en/sdg_management.yml index e084621e8..f4736fe8e 100644 --- a/config/locales/en/sdg_management.yml +++ b/config/locales/en/sdg_management.yml @@ -6,6 +6,8 @@ en: title: "SDG content" homepage: title: "Homepage configuration" + create_card: "Create %{phase} card" + no_cards: "There are no cards for this phase" menu: budget_investments: "Participatory budgets" debates: "Debates" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index d59c643c4..788576928 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -331,6 +331,10 @@ es: sdg/local_target/translation: title: "Título" description: "Descripción" + sdg/phase/kind: + sensitization: "Sensibilización" + planning: "Planificación" + monitoring: "Seguimiento" sdg/target: code: "Código" title: "Título" diff --git a/config/locales/es/sdg_management.yml b/config/locales/es/sdg_management.yml index a749df9ee..488599bc5 100644 --- a/config/locales/es/sdg_management.yml +++ b/config/locales/es/sdg_management.yml @@ -6,6 +6,8 @@ es: title: "Contenido ODS" homepage: title: "Configuración de la página de inicio" + create_card: "Crear tarjeta de %{phase}" + no_cards: "No hay tarjetas para esta fase" menu: budget_investments: "Presupuestos participativos" debates: "Debates" diff --git a/config/routes/sdg_management.rb b/config/routes/sdg_management.rb index 445b0cf8f..c8816ad56 100644 --- a/config/routes/sdg_management.rb +++ b/config/routes/sdg_management.rb @@ -6,6 +6,10 @@ namespace :sdg_management do resources :local_targets, except: [:show] resource :homepage, controller: :homepage, only: [:show] + resources :phases, only: [], as: :sdg_phases do + resources :cards, except: [:index, :show], as: :widget_cards + end + types = SDG::Related::RELATABLE_TYPES.map(&:tableize) types_constraint = /#{types.join("|")}/ diff --git a/db/dev_seeds/sdg.rb b/db/dev_seeds/sdg.rb index 055f64e41..f7fe40a3a 100644 --- a/db/dev_seeds/sdg.rb +++ b/db/dev_seeds/sdg.rb @@ -30,3 +30,9 @@ section "Creating Sustainable Development Goals" do end end end + +section "Creating SDG homepage cards" do + SDG::Phase.all.each do |phase| + Widget::Card.create!(cardable: phase, title: "#{phase.title} card") + end +end diff --git a/spec/models/abilities/sdg/manager_spec.rb b/spec/models/abilities/sdg/manager_spec.rb index 11800ca04..fa4febb16 100644 --- a/spec/models/abilities/sdg/manager_spec.rb +++ b/spec/models/abilities/sdg/manager_spec.rb @@ -13,4 +13,9 @@ describe "Abilities::SDG::Manager" do it { should_not be_able_to(:read, SDG::Manager) } it { should_not be_able_to(:create, SDG::Manager) } it { should_not be_able_to(:delete, SDG::Manager) } + + it { should_not be_able_to(:update, create(:widget_card)) } + it { should be_able_to(:update, create(:widget_card, cardable: SDG::Phase.sample)) } + it { should_not be_able_to(:create, build(:widget_card)) } + it { should be_able_to(:create, build(:widget_card, cardable: SDG::Phase.sample)) } end diff --git a/spec/routing/polymorphic_routes_spec.rb b/spec/routing/polymorphic_routes_spec.rb index b0a601b77..814e67b01 100644 --- a/spec/routing/polymorphic_routes_spec.rb +++ b/spec/routing/polymorphic_routes_spec.rb @@ -195,6 +195,15 @@ describe "Polymorphic routes" do expect(sdg_management_polymorphic_path(target)).to eq sdg_management_local_target_path(target) end + + it "routes SDG phases widget cards" do + phase = SDG::Phase.sample + card = create(:widget_card, cardable: phase) + + expect(sdg_management_polymorphic_path(card)).to eq( + sdg_management_sdg_phase_widget_card_path(phase, card) + ) + end end end diff --git a/spec/system/sdg_management/homepage_spec.rb b/spec/system/sdg_management/homepage_spec.rb index 5abb9caed..598066adc 100644 --- a/spec/system/sdg_management/homepage_spec.rb +++ b/spec/system/sdg_management/homepage_spec.rb @@ -16,5 +16,37 @@ describe "SDG homepage configuration", :js do expect(page).to have_title "SDG content - Homepage configuration" end + + scenario "Create card" do + visit sdg_management_homepage_path + click_link "Create planning card" + + within(".translatable-fields") { fill_in "Title", with: "My planning card" } + click_button "Create card" + + within(".planning-cards") do + expect(page).to have_content "My planning card" + end + + within(".sensitization-cards") do + expect(page).to have_content "There are no cards for this phase" + end + end + + scenario "Update card" do + create(:widget_card, cardable: SDG::Phase["monitoring"], title: "My monitoring card") + + visit sdg_management_homepage_path + within(".monitoring-cards") { click_link "Edit" } + + within(".translatable-fields") { fill_in "Title", with: "Updated monitoring card" } + click_button "Save card" + + within(".monitoring-cards") do + expect(page).to have_css "tbody tr", count: 1 + expect(page).to have_content "Updated monitoring card" + expect(page).not_to have_content "My monitoring card" + end + end end end