Skip to content

Commit

Permalink
Merge pull request consuldemocracy#1846 from AyuntamientoMadrid/ods-c…
Browse files Browse the repository at this point in the history
…ards

[Upstream] Add cards to custom pages
  • Loading branch information
decabeza authored Feb 7, 2019
2 parents b78377c + 0aa2bb6 commit 3b416d3
Show file tree
Hide file tree
Showing 24 changed files with 277 additions and 21 deletions.
3 changes: 2 additions & 1 deletion app/assets/stylesheets/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2736,7 +2736,8 @@ table {
// 24. Homepage
// ------------

.home-page {
.home-page,
.custom-page {

a {

Expand Down
9 changes: 9 additions & 0 deletions app/controllers/admin/site_customization/cards_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Admin::SiteCustomization::CardsController < Admin::SiteCustomization::BaseController
skip_authorization_check

def index
@page = ::SiteCustomization::Page.find(params[:page_id])
@cards = @page.cards
end

end
31 changes: 23 additions & 8 deletions app/controllers/admin/widget/cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ class Admin::Widget::CardsController < Admin::BaseController
include Translatable

def new
@card = ::Widget::Card.new(header: header_card?)
if header_card?
@card = ::Widget::Card.new(header: header_card?)
else
@card = ::Widget::Card.new(site_customization_page_id: params[:page_id])
end
end

def create
@card = ::Widget::Card.new(card_params)
if @card.save
notice = "Success"
redirect_to admin_homepage_url, notice: notice
redirect_to_customization_page_cards_or_homepage
else
render :new
end
Expand All @@ -22,8 +25,7 @@ def edit
def update
@card = ::Widget::Card.find(params[:id])
if @card.update(card_params)
notice = "Updated"
redirect_to admin_homepage_url, notice: notice
redirect_to_customization_page_cards_or_homepage
else
render :edit
end
Expand All @@ -33,8 +35,7 @@ def destroy
@card = ::Widget::Card.find(params[:id])
@card.destroy

notice = "Removed"
redirect_to admin_homepage_url, notice: notice
redirect_to_customization_page_cards_or_homepage
end

private
Expand All @@ -44,7 +45,7 @@ def card_params

params.require(:widget_card).permit(
:label, :title, :description, :link_text, :link_url,
:button_text, :button_url, :alignment, :header,
:button_text, :button_url, :alignment, :header, :site_customization_page_id,
translation_params(Widget::Card),
image_attributes: image_attributes
)
Expand All @@ -54,6 +55,20 @@ def header_card?
params[:header_card].present?
end

def redirect_to_customization_page_cards_or_homepage
notice = t("admin.site_customization.pages.cards.#{params[:action]}.notice")

if @card.site_customization_page_id
redirect_to admin_site_customization_page_cards_path(page), notice: notice
else
redirect_to admin_homepage_url, notice: notice
end
end

def page
::SiteCustomization::Page.find(@card.site_customization_page_id)
end

def resource
Widget::Card.find(params[:id])
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def show
@banners = Banner.in_section('help_page').with_active

if @custom_page.present?
@cards = @custom_page.cards
render action: :custom_page
else
render action: params[:id]
Expand Down
8 changes: 6 additions & 2 deletions app/helpers/admin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ def menu_settings?

def menu_customization?
["pages", "banners", "information_texts", "documents"].include?(controller_name) ||
menu_homepage?
menu_homepage? || menu_pages?
end

def menu_homepage?
["homepage", "cards"].include?(controller_name)
["homepage", "cards"].include?(controller_name) && params[:page_id].nil?
end

def menu_pages?
["pages", "cards"].include?(controller_name) && params[:page_id].present?
end

def official_level_options
Expand Down
1 change: 1 addition & 0 deletions app/models/site_customization/page.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SiteCustomization::Page < ActiveRecord::Base
VALID_STATUSES = %w(draft published)
has_many :cards, class_name: 'Widget::Card', foreign_key: 'site_customization_page_id'

translates :title, touch: true
translates :subtitle, touch: true
Expand Down
9 changes: 8 additions & 1 deletion app/models/widget/card.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Widget::Card < ActiveRecord::Base
include Imageable
belongs_to :page, class_name: 'SiteCustomization::Page', foreign_key: 'site_customization_page_id'

# table_name must be set before calls to 'translates'
self.table_name = "widget_cards"
Expand All @@ -15,6 +16,12 @@ def self.header
end

def self.body
where(header: false).order(:created_at)
where(header: false, site_customization_page_id: nil).order(:created_at)
end

#add widget cards to custom pages
def self.page(page_id)
where(site_customization_page_id: page_id)
end

end
2 changes: 1 addition & 1 deletion app/views/admin/_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<%= link_to t("admin.menu.site_customization.homepage"), admin_homepage_path %>
</li>

<li <%= "class=is-active" if controller_name == "pages" %>>
<li <%= "class=is-active" if menu_pages? || controller_name == "pages" %>>
<%= link_to t("admin.menu.site_customization.pages"), admin_site_customization_pages_path %>
</li>

Expand Down
30 changes: 30 additions & 0 deletions app/views/admin/site_customization/cards/_card.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<tr id="<%= dom_id(card) %>" class="homepage-card">
<td>
<%= card.label %><br>
<%= card.title %>
</td>
<td><%= card.description %></td>
<td>
<%= card.link_text %><br>
<%= card.link_url %>
</td>

<!-- remove conditional once specs have image validations -->
<td>
<% if card.image.present? %>
<%= link_to t("admin.shared.show_image"), card.image_url(:large),
title: card.image.title, target: "_blank" %>
<% end %>
</td>
<td>
<%= link_to t("admin.actions.edit"),
edit_admin_widget_card_path(card, page_id: params[:page_id]),
class: "button hollow" %>

<%= link_to t("admin.actions.delete"),
admin_widget_card_path(card, page_id: params[:page_id]),
method: :delete,
data: { confirm: t('admin.actions.confirm') },
class: "button hollow alert" %>
</td>
</tr>
16 changes: 16 additions & 0 deletions app/views/admin/site_customization/cards/_cards.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<table>
<thead>
<tr>
<th><%= t("admin.site_customization.pages.cards.title") %></th>
<th class="small-4"><%= t("admin.site_customization.pages.cards.description") %></th>
<th><%= t("admin.site_customization.pages.cards.link_text") %> / <%= t("admin.site_customization.pages.cards.link_url") %></th>
<th><%= t("admin.shared.image") %></th>
<th class="small-2"><%= t("admin.shared.actions") %></th>
</tr>
</thead>
<tbody>
<% cards.each do |card| %>
<%= render "card", card: card %>
<% end %>
</tbody>
</table>
21 changes: 21 additions & 0 deletions app/views/admin/site_customization/cards/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<% provide :title do %>
<%= t("admin.header.title") %> - <%= t("admin.menu.site_customization.pages") %> - <%= @page.title %>
<% end %>
<%= back_link_to admin_site_customization_pages_path %>
<div id="cards">
<h2 class="inline-block">
<%= @page.title %> <%= t("admin.site_customization.pages.cards.cards_title") %></h2>

<div class="float-right">
<%= link_to t("admin.site_customization.pages.cards.create_card"),
new_admin_widget_card_path(page_id: params[:page_id]), class: "button" %>
</div>

<% if @cards.present? %>
<%= render "cards", cards: @cards %>
<% else %>
<div class="callout primary clear">
<%= t("admin.site_customization.pages.cards.no_cards") %>
</div>
<% end %>
</div>
5 changes: 5 additions & 0 deletions app/views/admin/site_customization/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<tr>
<th><%= t("admin.site_customization.pages.page.title") %></th>
<th><%= t("admin.site_customization.pages.page.slug") %></th>
<th><%= t("admin.site_customization.pages.page.cards_title") %></th>
<th><%= t("admin.site_customization.pages.page.created_at") %></th>
<th><%= t("admin.site_customization.pages.page.updated_at") %></th>
<th><%= t("admin.site_customization.pages.page.status") %></th>
Expand All @@ -26,6 +27,10 @@
<%= link_to page.title, edit_admin_site_customization_page_path(page) %>
</td>
<td><%= page.slug %></td>
<td>
<%= link_to t("admin.site_customization.pages.page.see_cards"), admin_site_customization_page_cards_path(page),
class: "button hollow expanded" %>
</td>
<td><%= I18n.l page.created_at, format: :short %></td>
<td><%= I18n.l page.created_at, format: :short %></td>
<td><%= t("admin.site_customization.pages.page.status_#{page.status}") %></td>
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/widget/cards/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</div>

<%= f.hidden_field :header, value: @card.header? %>
<%= f.hidden_field :site_customization_page_id, value: @card.site_customization_page_id %>

<div class="image-form">
<div class="image small-12 column">
Expand Down
16 changes: 16 additions & 0 deletions app/views/pages/_card.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id="<%= dom_id(card) %>" class="card small-12 medium-6 column margin-bottom end large-4" data-equalizer-watch>
<%= link_to card.link_url do %>
<figure class="figure-card">
<div class="gradient"></div>
<% if card.image.present? %>
<%= image_tag(card.image_url(:large), alt: card.image.title) %>
<% end %>
<figcaption>
<span><%= card.label %></span><br>
<h3><%= card.title %></h3>
</figcaption>
</figure>
<p class="description"><%= card.description %></p>
<p><%= card.link_text %></p>
<% end %>
</div>
7 changes: 7 additions & 0 deletions app/views/pages/_cards.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h3 class="title"><%= t("welcome.cards.title") %></h3>

<div class="row" data-equalizer data-equalizer-on="medium">
<% @cards.find_each do |card| %>
<%= render "card", card: card %>
<% end %>
</div>
8 changes: 8 additions & 0 deletions app/views/pages/custom_page.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= content_for :body_class, "custom-page" %>
<% provide :title do %><%= @custom_page.title %><% end %>

<div class="row margin-top">
Expand All @@ -16,4 +17,11 @@
<%= render '/shared/print' %>
</div>
<% end %>

<% if @cards.any? %>
<div class="small-12 column">
<%= render "cards" %>
</div>
<% end %>

</div>
17 changes: 17 additions & 0 deletions config/locales/en/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,23 @@ en:
status_published: Published
title: Title
slug: Slug
cards_title: Cards
see_cards: See Cards
cards:
cards_title: cards
create_card: Create card
no_cards: There are no cards.
title: Title
description: Description
link_text: Link text
link_url: Link URL
create:
notice: "Success"
update:
notice: "Updated"
destroy:
notice: "Removed"

homepage:
title: Homepage
description: The active modules appear in the homepage in the same order as here.
Expand Down
10 changes: 10 additions & 0 deletions config/locales/es/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,16 @@ es:
status_published: Publicada
title: Título
slug: Slug
cards_title: Tarjetas
see_cards: Ver tarjetas
cards:
cards_title: tarjetas
create_card: Crear tarjeta
no_cards: No hay tarjetas.
title: Título
description: Descripción
link_text: Texto del enlace
link_url: URL del enlace
homepage:
title: Homepage
description: Los módulos activos aparecerán en la homepage en el mismo orden que aquí.
Expand Down
4 changes: 3 additions & 1 deletion config/routes/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@
resources :geozones, only: [:index, :new, :create, :edit, :update, :destroy]

namespace :site_customization do
resources :pages, except: [:show]
resources :pages, except: [:show] do
resources :cards, only: [:index]
end
resources :images, only: [:index, :update, :destroy]
resources :content_blocks, except: [:show]
delete '/heading_content_blocks/:id', to: 'content_blocks#delete_heading_content_block', as: 'delete_heading_content_block'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSiteCustomizationPageToWidgetCards < ActiveRecord::Migration
def change
add_reference :widget_cards, :site_customization_page, index: true
end
end
9 changes: 6 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1754,11 +1754,14 @@
t.string "link_text"
t.string "link_url"
t.string "label"
t.boolean "header", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "header", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "site_customization_page_id"
end

add_index "widget_cards", ["site_customization_page_id"], name: "index_widget_cards_on_site_customization_page_id", using: :btree

create_table "widget_feeds", force: :cascade do |t|
t.string "kind"
t.integer "limit", default: 3
Expand Down
Loading

0 comments on commit 3b416d3

Please sign in to comment.