Merge pull request #3149 from NahiaSolutions/add_cards_to_custom_pages

Add cards to custom pages
This commit is contained in:
Alberto
2019-01-29 16:05:47 +01:00
committed by GitHub
24 changed files with 277 additions and 21 deletions

View File

@@ -2728,7 +2728,8 @@ table {
// 24. Homepage
// ------------
.home-page {
.home-page,
.custom-page {
a {

View File

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

View File

@@ -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
@@ -22,8 +25,7 @@ class Admin::Widget::CardsController < Admin::BaseController
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
@@ -33,8 +35,7 @@ class Admin::Widget::CardsController < Admin::BaseController
@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
@@ -43,7 +44,7 @@ class Admin::Widget::CardsController < Admin::BaseController
image_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
params.require(:widget_card).permit(
:link_url, :button_text, :button_url, :alignment, :header,
:link_url, :button_text, :button_url, :alignment, :header, :site_customization_page_id,
translation_params(Widget::Card),
image_attributes: image_attributes
)
@@ -53,6 +54,20 @@ class Admin::Widget::CardsController < Admin::BaseController
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

View File

@@ -9,6 +9,7 @@ class PagesController < ApplicationController
@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]

View File

@@ -54,11 +54,15 @@ module AdminHelper
end
def menu_customization?
["pages", "banners", "information_texts"].include?(controller_name) || menu_homepage?
["pages", "banners", "information_texts"].include?(controller_name) || 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

View File

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

View File

@@ -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"
@@ -15,6 +16,12 @@ class Widget::Card < ActiveRecord::Base
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

View File

@@ -122,7 +122,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>

View File

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

View File

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

View File

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

View File

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

View File

@@ -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">

View File

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

View File

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

View File

@@ -1,3 +1,4 @@
<%= content_for :body_class, "custom-page" %>
<% provide :title do %><%= @custom_page.title %><% end %>
<div class="row margin-top">
@@ -16,4 +17,11 @@
<%= render '/shared/print' %>
</div>
<% end %>
<% if @cards.any? %>
<div class="small-12 column">
<%= render "cards" %>
</div>
<% end %>
</div>

View File

@@ -1448,6 +1448,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.

View File

@@ -1448,6 +1448,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: Título
description: Los módulos activos aparecerán en la homepage en el mismo orden que aquí.

View File

@@ -217,7 +217,9 @@ namespace :admin do
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'

View File

@@ -0,0 +1,5 @@
class AddSiteCustomizationPageToWidgetCards < ActiveRecord::Migration
def change
add_reference :widget_cards, :site_customization_page, index: true
end
end

View File

@@ -1520,11 +1520,14 @@ ActiveRecord::Schema.define(version: 20190103132925) do
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

View File

@@ -130,6 +130,58 @@ feature 'Cards' do
end
end
context "Page card" do
let!(:custom_page) { create(:site_customization_page) }
scenario "Create", :js do
visit admin_site_customization_pages_path
click_link "See Cards"
click_link "Create card"
fill_in "Title", with: "Card for a custom page"
click_button "Create card"
expect(page).to have_current_path admin_site_customization_page_cards_path(custom_page)
expect(page).to have_content "Card for a custom page"
end
scenario "Edit", :js do
create(:widget_card, page: custom_page, title: "Original title")
visit admin_site_customization_page_cards_path(custom_page)
expect(page).to have_content("Original title")
click_link "Edit"
within(".translatable-fields") do
fill_in "Title", with: "Updated title"
end
click_button "Save card"
expect(page).to have_current_path admin_site_customization_page_cards_path(custom_page)
expect(page).to have_content "Updated title"
expect(page).not_to have_content "Original title"
end
scenario "Destroy", :js do
create(:widget_card, page: custom_page, title: "Card title")
visit admin_site_customization_page_cards_path(custom_page)
expect(page).to have_content("Card title")
accept_confirm do
click_link "Delete"
end
expect(page).to have_current_path admin_site_customization_page_cards_path(custom_page)
expect(page).not_to have_content "Card title"
end
end
end
pending "add image expectactions"

View File

@@ -135,6 +135,15 @@ feature "Custom Pages" do
expect(page).to have_selector("h1", text: "Another custom page")
expect(page).to have_content("Subtitle for custom page")
end
scenario "Show widget cards for that page" do
custom_page = create(:site_customization_page, :published)
create(:widget_card, page: custom_page, title: "Card Highlights")
visit custom_page.url
expect(page).to have_content "Card Highlights"
end
end
end
end

View File

@@ -26,11 +26,27 @@ describe Widget::Card do
it "returns cards for the homepage body" do
header = create(:widget_card, header: true)
card1 = create(:widget_card, header: false, title: "Card 1")
card2 = create(:widget_card, header: false, title: "Card 2")
card3 = create(:widget_card, header: false, title: "Card 3")
card1 = create(:widget_card, header: false)
card2 = create(:widget_card, header: false)
page_card = create(:widget_card, header: false, page: create(:site_customization_page))
expect(Widget::Card.body).to eq([card1, card2, card3])
expect(Widget::Card.body).to include(card1)
expect(Widget::Card.body).to include(card2)
expect(Widget::Card.body).not_to include(header)
expect(Widget::Card.body).not_to include(page_card)
end
end
describe "#custom page" do
it "return cards for the custom pages" do
header = create(:widget_card, header: true)
card = create(:widget_card, header: false)
card1 = create(:widget_card, header: false, title: "Card 1", site_customization_page_id: 1)
card2 = create(:widget_card, header: false, title: "Card 2", site_customization_page_id: 1)
card3 = create(:widget_card, header: false, title: "Card 3", site_customization_page_id: 1)
expect(Widget::Card.page(1)).to eq([card1, card2, card3])
end
end