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

Commit

Permalink
Refactor thermometersController to work with both types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuuleh committed Nov 22, 2018
1 parent 187b7d3 commit f759950
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/controllers/plugins/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
# # frozen_string_literal: true
class Plugins::BaseController < ApplicationController
before_action :authenticate_user!

Expand Down
27 changes: 19 additions & 8 deletions app/controllers/plugins/thermometers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
# frozen_string_literal: true

# This controller is only used for ActionsThermometers.
class Plugins::ThermometersController < Plugins::BaseController
def update
@plugin = Plugins::Thermometer.find(params[:id])

respond_to do |format|
if @plugin.update(permitted_params)
format.js { render json: {} }
else
format.js { render json: { errors: @plugin.errors, name: plugin_name.to_sym }, status: :unprocessable_entity }
end
end
end

private

def plugin_class
params[:plugins_actions_thermometer].blank? ? Plugins::DonationsThermometer : Plugins::ActionsThermometer
end

def permitted_params
params
.require(:plugins_thermometer)
.require(plugin_name)
.permit(:title, :offset, :goal, :active, :type)
end

def plugin_class
Plugins::ActionsThermometer
end

def plugin_symbol
:plugins_thermometer
def plugin_name
plugin_class.name.underscore.tr('/', '_')
end
end
4 changes: 2 additions & 2 deletions app/views/pages/edit.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ section.page-edit-step#layout data-icon='cog'
- next if low_priority_plugins.include? plugin.class
section.page-edit-step id=plugin_section_id(plugin) data-icon=plugin_icon(plugin)
h1.page-edit-step__title= plugin_title(plugin)
= render "#{plugin.class.base_class.name.underscore.pluralize}/form", plugin: plugin, page: @page
= render "#{plugin.class.name.underscore.pluralize}/form", plugin: plugin, page: @page

section.page-edit-step#pictures data-icon='camera-retro'
h1.page-edit-step__title = t('.pictures')
Expand All @@ -33,7 +33,7 @@ section.page-edit-step#sources data-icon='link'
- next unless low_priority_plugins.include? plugin.class
section.page-edit-step id=plugin_section_id(plugin) data-icon=plugin_icon(plugin)
h1.page-edit-step__title= plugin_title(plugin)
= render "#{plugin.class.base_class.name.underscore.pluralize}/form", plugin: plugin, page: @page
= render "#{plugin.class.name.underscore.pluralize}/form", plugin: plugin, page: @page

section.page-edit-step.page-edit-step--just-title#review data-icon='eye' data-link-to=member_facing_page_url(@page)
h1.page-edit-step__title
Expand Down
12 changes: 12 additions & 0 deletions app/views/plugins/actions_thermometers/_form.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.col-md-7
= render partial: 'plugins/shared/toggle_form', locals: { plugin: plugin, path: plugins_thermometer_path(plugin) }

- name = "plugins_actions_thermometer_#{plugin.id}"
= form_for plugin, url: plugins_thermometer_path(plugin), remote: true, as: name, html: {class: 'plugin-settings one-form', data: {type: name }} do |f|
= render "shared/error_messages", target: plugin
= render "shared/success_message", success: (defined?(success) || false)
= render 'plugins/shared/plugin_metadata', f: f

.form-group
= label_with_tooltip(f, :offset, t('plugins.thermometer.offset'), t('tooltips.thermometer.actions.offset'))
= f.text_field(:offset, class: 'form-control')
Empty file.
12 changes: 12 additions & 0 deletions app/views/plugins/donations_thermometers/_form.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.col-md-7
= render partial: 'plugins/shared/toggle_form', locals: { plugin: plugin, path: plugins_thermometer_path(plugin) }

- name = "plugins_donations_thermometer_#{plugin.id}"
= form_for plugin, url: plugins_thermometer_path(plugin), remote: true, as: name, html: {class: 'plugin-settings one-form', data: {type: name }} do |f|
= render "shared/error_messages", target: plugin
= render "shared/success_message", success: (defined?(success) || false)
= render 'plugins/shared/plugin_metadata', f: f

.form-group
= label_with_tooltip(f, :offset, t('plugins.thermometer.offset'), t('tooltips.thermometer.donations.offset'))
= f.text_field(:offset, class: 'form-control')
6 changes: 5 additions & 1 deletion config/locales/champaign.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ en:
click_count: "How many times this share button has been clicked"
conversion_count: "How many times a page has been viewed through a share made out of this share variant."
local_conversion_rate: "Percentage of the clicks / shares of this variant that got at least one person to view the page."
thermometer_offset: "Number of fake signatures to add to true signature count. No commas or spaces."
thermometer:
actions:
offset: "Number of fake signatures to add to true signature count. No commas or spaces."
donations:
offset: "Unformatted amount in USD to add to the amount of donations collected. No commas or spaces."
fundraiser:
donation_band: "This determines the amounts displayed on the page to an unknown user without any information in their URL. Generally, you will want to stick with an option designed for non-donors."
recurring_default: 'This sets whether the recurring donation checkbox is checked and/or hidden by default. If the url param recurring_default=recurring, recurring_default=only_recurring or recurring_default=one_off is passed, that will override the behavior specified here.'
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/plugins/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

describe 'successful' do
before do
allow(plugin_class).to receive(:find).with('1') { plugin }
allow(plugin_class.base_class).to receive(:find).with('1') { plugin }
allow(plugin).to receive(:update) { true }
put :update, params: { id: '1', plugin_name => { title: 'bar' }, format: :js }
end

it 'finds the plugin' do
expect(plugin_class).to have_received(:find).with('1')
expect(plugin_class.base_class).to have_received(:find).with('1')
end

it 'updates the plugin' do
Expand All @@ -40,7 +40,7 @@

describe 'failure' do
before do
allow(plugin_class).to receive(:find).with('1') { plugin }
allow(plugin_class.base_class).to receive(:find).with('1') { plugin }
allow(plugin).to receive(:update) { false }
allow(plugin).to receive(:errors) { {} }
put :update, params: { id: '1', plugin_name => { title: 'bar' }, format: :js }
Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/plugins/thermometers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
require_relative 'shared_examples'

describe Plugins::ThermometersController do
include_examples 'plugins controller', Plugins::ActionsThermometer, :plugins_thermometer
include_examples 'plugins controller', Plugins::ActionsThermometer, :plugins_actions_thermometer
include_examples 'plugins controller', Plugins::DonationsThermometer, :plugins_donations_thermometer
end
4 changes: 2 additions & 2 deletions spec/liquid/liquid_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@
end

it 'is nil if no actions thermometer plugin' do
create :plugins_fundraiser, page: page
create :call_tool, page: page
expect(page.plugins.size).to eq 1
expect(LiquidRenderer.new(page).personalization_data['actions_thermometer']).to eq nil
end
Expand Down Expand Up @@ -397,7 +397,7 @@

describe 'donations_thermometer' do
it 'is nil if no donations thermometer plugin' do
create :plugins_fundraiser, page: page
create :call_tool, page: page
expect(page.plugins.size).to eq 1
expect(LiquidRenderer.new(page).personalization_data['donations_thermometer']).to eq nil
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@
describe 'plugins' do
it 'correctly lists the names of plugins' do
page = create :page
[create(:plugins_petition, page: page), create(:plugins_fundraiser, page: page), create(:plugins_actions_thermometer, page: page)]
plugin_names = %w[petition fundraiser actions_thermometer]
[create(:plugins_petition, page: page), create(:call_tool, page: page), create(:plugins_actions_thermometer, page: page)]
plugin_names = %w[petition call_tool actions_thermometer]
expect(page.plugin_names).to match_array(plugin_names)
end
end
Expand Down

0 comments on commit f759950

Please sign in to comment.