Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #384 from SumOfUs/hallway-mods
Browse files Browse the repository at this point in the history
[Pre-deploy] Modifications from hallway tests
  • Loading branch information
osahyoun committed Feb 23, 2016
2 parents 3ada8f0 + 9b8c59e commit 4c5b569
Show file tree
Hide file tree
Showing 27 changed files with 207 additions and 31 deletions.
20 changes: 20 additions & 0 deletions app/assets/javascripts/layout_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const setupOnce = require('setup_once');

events: {
'click .radio-group__option': 'updateSelected',
'change .layout-type-checkbox': 'showRelevantLayouts'
},

updateSelected(e) {
Expand All @@ -28,6 +29,25 @@ const setupOnce = require('setup_once');
}
},

showRelevantLayouts(e) {
const $target = $(e.target);
const layoutClasses = this.getLayoutClasses($target.attr('id'));
const layoutRows = $target.closest('.form-group').find(layoutClasses.inverse_class).not(layoutClasses.own_class);
if ($target.is(':checked')) {
layoutRows.removeClass('hidden');
} else {
layoutRows.addClass('hidden');
}
},

getLayoutClasses(layout_select_id) {
if (layout_select_id==='primary') {
return {inverse_class: '.post-action-layout', own_class: '.primary-layout'}
} else if (layout_select_id==='follow-up') {
return {inverse_class: '.primary-layout', own_class: '.post-action-layout'}
}
}

});

$.subscribe("layout:edit pages:new", function(){
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/page-edit.scss
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,7 @@ body.page-edit-body {
.well .tooltip__trigger, .tooltip--white .tooltip__trigger {
background: white;
}

.checkbox input[type="checkbox"].layout-type-checkbox {
margin-left: 0px;
}
3 changes: 2 additions & 1 deletion app/controllers/liquid_layouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def set_liquid_layout

# Never trust parameters from the scary internet, only allow the white list through.
def liquid_layout_params
params.require(:liquid_layout).permit(:title, :content, :description, :experimental)
params.require(:liquid_layout).permit(:title, :content, :description, :experimental,
:primary_layout, :post_action_layout, :default_follow_up_layout_id)
end
end
2 changes: 0 additions & 2 deletions app/helpers/api/actions_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/campaigns_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/form_elements_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/forms_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/home_helper.rb

This file was deleted.

33 changes: 33 additions & 0 deletions app/helpers/layout_select_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module LayoutSelectHelper

def construct_layout_select_class(liquid_layout, page, field)
hidden = check_hidden(liquid_layout, field)
active = check_active(liquid_layout, page, field)
post_action_layout = liquid_layout.post_action_layout ? 'post-action-layout' : ''
primary_layout = liquid_layout.primary_layout ? 'primary-layout' : ''
"#{hidden} #{active} #{primary_layout} #{post_action_layout}"
end

def specify_layout_types(field)
(field == :liquid_layout_id) ? 'primary' : 'follow-up'
end

private

def check_hidden(liquid_layout, field)
if field == :liquid_layout_id
return 'hidden' unless liquid_layout.primary_layout
elsif field == :follow_up_liquid_layout_id
return 'hidden' unless liquid_layout.post_action_layout
end
''
end

def check_active(liquid_layout, page, field)
if field == :follow_up_liquid_layout_id && page.follow_up_plan.to_sym == :with_page
return '' # the redirect option will be the active one in this case
end
# page.send(field) calls either page.liquid_layout_id or page.follow_up_liquid_layout_id
liquid_layout.id == page.send(field) ? 'active' : ''
end
end
2 changes: 0 additions & 2 deletions app/helpers/liquid_layouts_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/liquid_partials_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/plugins/petitions_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/plugins/thermometers_helper.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/models/liquid_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class LiquidLayout < ActiveRecord::Base
has_paper_trail

has_many :pages
belongs_to :default_follow_up_layout, class_name: LiquidLayout

validates :title, presence: true, allow_blank: false
validates :content, presence: true, allow_blank: false
validates :experimental, inclusion: {in: [true, false]}
Expand Down
9 changes: 9 additions & 0 deletions app/services/page_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(params)
end

def save_and_enqueue
set_follow_up_plan
QueueManager.push(page, job_type: :create) if page.save
page
end
Expand All @@ -21,5 +22,13 @@ def save_and_enqueue
def page
@page ||= Page.new(@params)
end

def set_follow_up_plan
follow_up_layout = page.liquid_layout.try(:default_follow_up_layout)
if follow_up_layout.present?
page.follow_up_liquid_layout = follow_up_layout
end
end

end

11 changes: 11 additions & 0 deletions app/views/liquid_layouts/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@
.form-group
= f.label :content, "Markup"
= f.text_area :content, class: 'form-control syntax-highlighting'
.form-group
= f.label :default_follow_up_layout_id, t('liquid.default_follow_up_layout')
= f.select :default_follow_up_layout_id, options_from_collection_for_select(LiquidLayout.all, 'id', 'title', f.object.default_follow_up_layout_id), {include_blank: 'None'}, {class: 'selectize-container', multiple: false}
.checkbox
= f.label :experimental
= f.check_box :experimental
= "#{t('liquid.experimental')} #{t('liquid.experimental_explanation')}"
.checkbox
= f.label :primary_layout
= f.check_box :primary_layout
= "#{t('liquid.primary_layout')} #{t('liquid.experimental_explanation')}"
.checkbox
= f.label :post_action_layout
= f.check_box :post_action_layout
= "#{t('liquid.post_action_layout')} #{t('liquid.experimental_explanation')}"

.form-group
button.btn.btn-default Save
11 changes: 7 additions & 4 deletions app/views/pages/_layout_select.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
- offer_redirect ||= false
.radio-group.btn-group-vertical.layout-settings__button-group role="group"
- LiquidLayout.campaigner_friendly.sort_by(&:title).each do |ll|
- liquid_matters = (!offer_redirect || f.object.follow_up_plan.to_sym == :with_liquid)
- active_class = (ll.id == f.object.send(field) && liquid_matters) ? 'active' : ''
= f.label field, value: ll.id, class: "btn btn-default radio-group__option #{active_class}"
- layout_select_classes = construct_layout_select_class(ll, f.object, field)
= f.label field, value: ll.id, class: "btn btn-default radio-group__option #{layout_select_classes}"
= f.radio_button field, ll.id, class: 'hidden-irrelevant'
span.layout-settings__title= ll.title
span.layout-settings__description= ll.description
Expand All @@ -17,4 +16,8 @@
= f.radio_button :follow_up_plan, :with_liquid, class: 'hidden-irrelevant'
span.layout-settings__title Redirect to another page
span.layout-settings__description
= f.select :follow_up_page_id, options_from_collection_for_select(Page.all, 'id', 'title', f.object.follow_up_page_id), {}, html_options= {class: 'selectize-container', multiple: false}
= f.select :follow_up_page_id, options_from_collection_for_select(Page.all, 'id', 'title', f.object.follow_up_page_id), {}, {class: 'selectize-container', multiple: false}
.checkbox
= check_box_tag specify_layout_types(field), 'Show all', false, {class: 'layout-type-checkbox'}
= label_tag specify_layout_types(field) do
| Show all layouts
2 changes: 1 addition & 1 deletion app/views/share/_new_form.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

= f.hidden_field :name
= render partial: "share/#{share.name}s/share_fields", locals: { form: f, share: share, page: page }
= f.submit 'Add share variant', class: 'btn btn-primary', 'data-disable-with' => t('common.saving')
= f.submit 'Save share variant', class: 'btn btn-primary', 'data-disable-with' => t('common.saving')
5 changes: 5 additions & 0 deletions config/locales/champaign.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ en:
liquid:
experimental: Experimental
experimental_explanation: (experimental templates are not available to campaigners)
primary_layout: Make available as a primary layout
primary_layout_explanation: (as opposed to a follow-up layout)
post_action_layout: Make available as a follow-up layout
post_action_explanation: (follow-up layouts are rendered after a user takes an action)
default_follow_up_layout: Default template to render to follow-up after this one

campaigns:
new:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160218001400_add_default_follow_up_layout_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDefaultFollowUpLayoutId < ActiveRecord::Migration
def change
add_reference :liquid_layouts, :default_follow_up_layout
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddPrimaryAndPostActionColumns < ActiveRecord::Migration
def change
add_column :liquid_layouts, :primary_layout, :boolean
add_column :liquid_layouts, :post_action_layout, :boolean
end
end
9 changes: 6 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@
create_table "liquid_layouts", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description"
t.boolean "experimental", default: false, null: false
t.boolean "experimental", default: false, null: false
t.integer "default_follow_up_layout_id"
t.boolean "primary_layout"
t.boolean "post_action_layout"
end

create_table "liquid_partials", force: :cascade do |t|
Expand Down
40 changes: 40 additions & 0 deletions lib/tasks/follow_up_layouts.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace :champaign do

desc "Associate liquid layouts known to be petition or donation forms with their default post-action layouts"
task associate_layouts: :environment do
puts "Associating petition and donation layouts with default post action layouts"

post_petition_share = LiquidLayout.find_by(title: "Post Petition Share")
post_donation_share = LiquidLayout.find_by(title: "Post Donation Share")

petition_layouts = LiquidLayout.where(title: ["Petition With Large Image", "Petition With Small Image"])
donation_layouts = LiquidLayout.where(title: ["Fundraiser With Large Image", "Fundraiser With Small Image"])

#Associate petition_layouts and donation_layouts with their desired default follow up layouts:
petition_layouts.each do |layout|
layout.update_attributes({
primary_layout: true,
post_action_layout: false,
default_follow_up_layout: post_petition_share})
layout.save
end

donation_layouts.each do |layout|
layout.update_attributes({
primary_layout: true,
post_action_layout: false,
default_follow_up_layout: post_donation_share})
layout.save
end

#For post_petition_share and post_donation_share, update primary_layout and post_action_layout
[post_petition_share, post_donation_share].each do |share_layout|
share_layout.update_attributes({
primary_layout: false,
post_action_layout: true
})
end

puts "Finished default follow-up layout associations."
end
end
2 changes: 2 additions & 0 deletions spec/models/liquid_layout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
it { is_expected.to respond_to :experimental }
it { is_expected.to respond_to :description }
it { is_expected.to respond_to :pages }
it { is_expected.to respond_to :default_follow_up_layout }
it { is_expected.to respond_to :default_follow_up_layout_id }
it { is_expected.to respond_to :partial_names }
it { is_expected.to respond_to :partial_refs }

Expand Down
7 changes: 4 additions & 3 deletions spec/models/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

describe Page do

let(:english) { create :language }
let(:liquid_layout) { create :liquid_layout }
let(:page) { create :page }
let(:english) { create :language }
let!(:follow_up_layout) { create :liquid_layout }
let!(:liquid_layout) { create :liquid_layout, default_follow_up_layout: follow_up_layout }
let(:page) { create :page }

let(:page_params) { attributes_for :page, liquid_layout_id: liquid_layout.id }
let(:image_file) { File.new(Rails.root.join('spec','fixtures','test-image.gif')) }
Expand Down
27 changes: 27 additions & 0 deletions spec/requests/pages_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rails_helper'

describe "pages" do
let(:english) { create :language }
let(:page_params) { { title: 'Away we go!', language_id: english.id } }

describe 'POST create' do
it 'has the right follow-up url if liquid layout has a default follow-up url' do
follow_up_layout = create :liquid_layout, default_follow_up_layout: nil
liquid_layout = create :liquid_layout, default_follow_up_layout: follow_up_layout
expect {
post pages_path, page: page_params.merge(liquid_layout_id: liquid_layout.id)
}.to change{ Page.count }.by 1
page = Page.last
expect(PageFollower.new_from_page(page).follow_up_path).to eq "/a/#{page.slug}/follow-up"
end

it 'has a blank follow-up url if liquid layout has no default follow-up url' do
liquid_layout = create :liquid_layout, default_follow_up_layout: nil
expect {
post pages_path, page: page_params.merge(liquid_layout_id: liquid_layout.id)
}.to change{ Page.count }.by 1
page = Page.last
expect(PageFollower.new_from_page(page).follow_up_path).to be_nil
end
end
end
24 changes: 23 additions & 1 deletion spec/services/page_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
let(:language) { create(:language) }
let(:params) {{ title: "Foo Bar", liquid_layout_id: template.id, language_id: language.id }}
let(:content) { "{% include 'petition' %}<div class='foo'>{% include 'thermometer' %}</div>"}
let(:template) { create :liquid_layout, content: content }
let(:follow_up_template) {create :liquid_layout, default_follow_up_layout: nil}
let(:template) { create :liquid_layout, content: content, default_follow_up_layout: follow_up_template }

before :each do
create :liquid_partial, title: 'petition', content: '{{ plugins.petition[ref].lol }}'
Expand Down Expand Up @@ -37,4 +38,25 @@
expect { subject }.to change{ plugin.count }.by 1
end
end

it 'sets follow up layout for a page created with a layout that has a default post-action layout' do
subject
expect(Page.first.follow_up_liquid_layout_id).to eq(follow_up_template.id)
# follow-up plan is 'with liquid'
expect(Page.first.follow_up_plan).to eq "with_liquid"
end

it 'sets no follow up layout for a page created with a layout that has no default post-action layout' do
params[:liquid_layout_id] = follow_up_template.id
PageBuilder.create(params)
expect(Page.first.follow_up_liquid_layout_id).to be nil
# follow up page hasn't yet been set at this step and should be nil
expect(Page.first.follow_up_page).to be nil
end

it 'creates no page and throws no error for when there is an attempt to create a page without a liquid layout' do
params[:liquid_layout_id] = nil
expect{ subject }.not_to raise_error
expect{ subject }.not_to change{ Page.count }
end
end

0 comments on commit 4c5b569

Please sign in to comment.