Skip to content

Commit

Permalink
Merge pull request #378 from unepwcmc/refresh-cta
Browse files Browse the repository at this point in the history
Make CTAs as CMS components
  • Loading branch information
stacytalbot authored Apr 8, 2020
2 parents ba73380 + e6d5e56 commit d6603e7
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 14 deletions.
61 changes: 61 additions & 0 deletions app/controllers/admin/call_to_actions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Admin::CallToActionsController < Comfy::Admin::Cms::BaseController
before_action :build_cta, :only => [:new, :create]
before_action :load_cta, :only => [:show, :edit, :update, :destroy]

def index
@ctas = CallToAction.page(params[:page])
end

def show
render
end

def new
render
end

def edit
render
end

def create
@cta.save!
flash[:success] = 'Call To Action created'
redirect_to action: :show, id: @cta
rescue ActiveRecord::RecordInvalid
flash.now[:danger] = 'Failed to create Call To Action'
render action: :new
end

def update
@cta.update_attributes!(cta_params)
flash[:success] = 'Call To Action updated'
redirect_to action: :show, id: @cta
rescue ActiveRecord::RecordInvalid
flash.now[:danger] = 'Failed to update Call To Action'
render action: :edit
end

def destroy
@cta.destroy
flash[:success] = 'Call To Action deleted'
redirect_to action: :index
end

protected

def build_cta
@cta = CallToAction.new(cta_params)
end

def load_cta
@cta = CallToAction.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:danger] = 'Call To Action not found'
redirect_to action: :index
end

def cta_params
params.fetch(:call_to_action, {}).permit(:css_class, :title, :summary, :url, :updated)
end
end
8 changes: 8 additions & 0 deletions app/helpers/cms_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ def get_filtered_pages pages

pages
end

def cta_api
@cta_api ||= CallToAction.find_by_css_class('api')
end

def cta_live_report
@cta_live_report ||= CallToAction.find_by_css_class('live-report')
end
end
16 changes: 16 additions & 0 deletions app/models/call_to_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CallToAction < ActiveRecord::Base
self.table_name = "comfy_cms_call_to_actions"

validates :title,
presence: true,
length: {maximum: 75},
allow_nil: false
validates :summary,
presence: true,
length: {maximum: 150},
allow_nil: false
validates :url,
presence: true,
length: {maximum: 255},
allow_nil: false
end
10 changes: 10 additions & 0 deletions app/views/admin/call_to_actions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p>The title may have up to 75 characters and the description may have up to 150.</p>

<%= form.text_field :css_class, maxlength: 75 %>
<%= form.text_field :title, maxlength: 75 %>
<%= form.text_field :summary, maxlength: 150 %>
<%= form.text_field :url, maxlength: 255 %>
<%= form.check_box :updated %>
<%= form.form_group class: 'form-actions' do %>
<%= form.submit class: 'btn btn-primary' %>
<% end %>
6 changes: 6 additions & 0 deletions app/views/admin/call_to_actions/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="page-header">
<h2>Edit Call To Action</h2>
</div>
<%= comfy_form_with model: @cta, url: {action: :update} do |form| %>
<%= render form %>
<% end %>
30 changes: 30 additions & 0 deletions app/views/admin/call_to_actions/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="page-header">
<%= link_to 'New Call To Action', new_admin_call_to_action_path, class: 'btn btn-secondary float-right', title: 'Create new call to action' %>
<h2>Call To Actions</h2>
</div>

<table class="table table-hover table-bordered wrap">
<tr>
<th class="wrap">Class (Max characters: 75)</th>
<th class="wrap">Title (Max characters: 75)</th>
<th class="wrap">Summary (Max characters: 150)</th>
<th class="wrap">Url</th>
<th>Updated</th>
<th></th>
</tr>
<% @ctas.each do |cta| %>
<tr>
<td class="wrap"><%= cta.css_class %></td>
<td class="wrap"><%= cta.title %></td>
<td class="wrap"><%= cta.summary %></td>
<td class="wrap"><%= cta.url %></td>
<td><%= cta.updated.to_s %></td>
<td>
<div class="btn-group btn-group-sm">
<%= link_to 'Edit', edit_admin_call_to_action_path(cta), :class => 'btn btn-outline-secondary' %>
<%= link_to 'Delete', admin_call_to_action_path(cta), :method => :delete, :data => { :confirm => 'Are you sure?' }, :class => 'btn btn-danger' %>
</div>
</td>
</tr>
<% end %>
</table>
6 changes: 6 additions & 0 deletions app/views/admin/call_to_actions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="page-header">
<h2>New Call To Action</h2>
</div>
<%= comfy_form_with model: @cta, url: {action: :create} do |form| %>
<%= render form %>
<% end %>
24 changes: 24 additions & 0 deletions app/views/admin/call_to_actions/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="page-header">
<h2>Call To Action</h2>
</div>
<p>
<strong>CSS Class:</strong>
<%= @cta.css_class %>
</p>
<p>
<strong>Title:</strong>
<%= @cta.title %>
</p>
<p>
<strong>Summary:</strong>
<%= @cta.summary %>
</p>
<p>
<strong>Url:</strong>
<%= @cta.url %>
</p>
<p>
<strong>Updated:</strong>
<%= @cta.updated.to_s %>
</p>
<%= link_to 'View all', admin_call_to_actions_path, class: 'btn btn-primary' %>
5 changes: 5 additions & 0 deletions app/views/comfy/admin/cms/partials/_navigation_inner.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- %li= active_link_to 'HomeCarouselSlides', admin_home_carousel_slides_path -->

<li class="nav-item">
<%= link_to 'Call To Actions', admin_call_to_actions_path, class: 'nav-link', title: 'View Call to Actions' %>
</li>

This file was deleted.

10 changes: 5 additions & 5 deletions app/views/partials/ctas/_api.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<section class="cta--api container">
<div class="cta__container bg--grey-black">
<div class="cta__content">
<h2 class="h2-big-white"><%= t('ctas.api.title') %></h2>
<p class="text-intro"><%= t('ctas.api.intro') %></p>
<%= link_to t('ctas.api.button'), t('ctas.api.url'), target: '_blank', class: 'button--outline-white' %>
<h2 class="h2-big-white"><%= cta_api.title %></h2>
<p class="text-intro"><%= cta_api.summary %></p>

<%= link_to t('ctas.api.button'), cta_api.url, target: '_blank', class: 'button--outline-white' %>
</div>
</div>
</section>
</section>
14 changes: 8 additions & 6 deletions app/views/partials/ctas/_live-report.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<section class="cta--live-report bg--grey-xdark">
<section class="cta--<%= cta_live_report.css_class %> bg--grey-xdark">
<div class="container cta__container">
<div class="cta__content">
<span class="cta__ribbon"><%= t('global.status.updated') %></span>
<% if cta_live_report.updated %>
<span class="cta__ribbon"><%= t('global.status.updated') %></span>
<% end %>

<div>
<h2 class="cta__title h2-big"><%= t('ctas.live_report.title') %></h2>
<p class="text-intro"><%= t('ctas.live_report.intro') %></p>
<h2 class="cta__title h2-big"><%= cta_live_report.title %></h2>
<p class="text-intro"><%= cta_live_report.summary %></p>
</div>

<%= link_to t('ctas.live_report.button'), t('ctas.live_report.url'), target: '_blank', class: 'button--accent' %>
<%= link_to t('ctas.live_report.button'), cta_live_report.url, target: '_blank', class: 'button--accent' %>
</div>
</div>
</section>
</section>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Rails.application.routes.draw do
namespace :admin do
resources :home_carousel_slides
resources :call_to_actions
end

require 'sidekiq/web'
Expand Down
2 changes: 1 addition & 1 deletion db
107 changes: 107 additions & 0 deletions test/controllers/admin/call_to_actions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require_relative '../../test_helper'

class Admin::CallToActionsControllerTest < ActionController::TestCase

def setup
@cta = FactoryGirl.create(:call_to_action)
@site = ::Comfy::Cms::Site.create(label: 'test', identifier: 'test', hostname: 'localhost')
@request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("#{ComfortableMexicanSofa::AccessControl::AdminAuthentication.username}:#{ComfortableMexicanSofa::AccessControl::AdminAuthentication.password}")
end

def test_get_index
get :index
assert_response :success
assert assigns(:ctas)
assert_template :index
end

def test_get_show
get :show, params: {id: @cta}
assert_response :success
assert assigns(:cta)
assert_template :show
end

def test_get_show_failure
get :show, params: {id: 'invalid'}
assert_response :redirect
assert_redirected_to action: :index
assert_equal 'Call To Action not found', flash[:danger]
end

def test_get_new
get :new
assert_response :success
assert assigns(:cta)
assert_template :new
end

def test_get_edit
get :edit, params: {id: @cta}
assert_response :success
assert assigns(:cta)
assert_template :edit
end

def test_creation
assert_difference 'CallToAction.count' do
post :create, params: {
call_to_action: {
title: 'test title',
summary: 'test summary',
url: 'test url'
}
}
cta = CallToAction.last
assert_response :redirect
assert_redirected_to admin_call_to_action_path(cta)
assert_equal 'Call To Action created', flash[:success]
end
end

def test_creation_failure
assert_no_difference 'CallToAction.count' do
post :create, params: {call_to_action: {}}
assert_response :success
assert_template :new
assert_equal 'Failed to create Call To Action', flash[:danger]
end
end

def test_update
put :update, params: {
id: @cta,
call_to_action: {
title: 'Updated'
}
}
assert_response :redirect
assert_redirected_to action: :show, id: @cta
assert_equal 'Call To Action updated', flash[:success]
@cta.reload
assert_equal 'Updated', @cta.title
end

def test_update_failure
put :update, params: {
id: @cta,
call_to_action: {
title: ''
}
}
assert_response :success
assert_template :edit
assert_equal 'Failed to update Call To Action', flash[:danger]
@cta.reload
refute_equal '', @cta.title
end

def test_destroy
assert_difference 'CallToAction.count', -1 do
delete :destroy, params: {id: @cta}
assert_response :redirect
assert_redirected_to action: :index
assert_equal 'Call To Action deleted', flash[:success]
end
end
end
9 changes: 9 additions & 0 deletions test/factories/call_to_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryGirl.define do
factory :call_to_action do
css_class 'css_class'
title 'Title'
summary 'Summary'
url 'URL'
updated false
end
end
28 changes: 28 additions & 0 deletions test/models/call_to_action_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative '../test_helper'

class CallToActionTest < ActiveSupport::TestCase

def test_fixtures_validity
CallToAction.all.each do |cta|
assert cta.valid?, cta.errors.inspect
end
end

def test_validation
cta = CallToAction.new
assert cta.invalid?
assert_equal [:title, :summary, :url], cta.errors.keys
end

def test_creation
assert_difference 'CallToAction.count' do
CallToAction.create(
css_class: 'css_class',
title: 'test title',
summary: 'test summary',
url: 'test url'
)
end
end

end

0 comments on commit d6603e7

Please sign in to comment.