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

Commit

Permalink
WIP commit for Omar
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuuleh committed Nov 21, 2018
1 parent 232a26c commit bde20ce
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 115 deletions.
1 change: 1 addition & 0 deletions app/controllers/plugins/thermometers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# This controller is only used for ActionsThermometers.
class Plugins::ThermometersController < Plugins::BaseController
private

Expand Down
1 change: 0 additions & 1 deletion app/controllers/plugins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def index
end

def show
pp 'PARAMS:', params
@plugin = Plugins.find_for(params[:type], params[:id])
end

Expand Down
9 changes: 7 additions & 2 deletions app/liquid/liquid_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def personalization_data
outstanding_fields: outstanding_fields,
donation_bands: donation_bands,
actions_thermometer: actions_thermometer,
donations_thermometer: donations_thermometer,
call_tool: call_tool_data,
email_tool: email_tool_data,
email_pension: email_pension_data,
Expand Down Expand Up @@ -118,8 +119,12 @@ def donation_bands
end

def actions_thermometer
data = plugin_data.deep_symbolize_keys[:plugins][:actions_thermometer].try(:values).try(:first)
pp 'Actions thermometer data:', data
plugin_data.deep_symbolize_keys[:plugins][:actions_thermometer].try(:values).try(:first)
end

def donations_thermometer
data = plugin_data.deep_symbolize_keys[:plugins][:donations_thermometer].try(:values).try(:first)
pp 'Donations thermometer data: ', data
data
end

Expand Down
22 changes: 1 addition & 21 deletions app/models/plugins/actions_thermometer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@
class Plugins::ActionsThermometer < Plugins::Thermometer
belongs_to :page, touch: true

DEFAULTS = { offset: 0 }.freeze
GOALS = [100, 200, 300, 400, 500,
1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000,
10_000, 15_000, 20_000, 25_000, 50_000, 75_000, 100_000,
150_000, 200_000, 250_000, 300_000, 500_000, 750_000,
1_000_000, 1_500_000, 2_000_000].freeze

validates :offset, presence: true,
numericality: { greater_than_or_equal_to: 0 }
after_initialize :set_defaults

def current_total
offset + action_count
end
Expand All @@ -50,33 +45,18 @@ def liquid_data(_supplemental_data = {})
)
end

def name
self.class.name.demodulize
end

private

def action_count
@action_count ||= if page.campaign_id.present?
Page.where(campaign_id: page.campaign_id).sum(:action_count)
page.campaign.action_count
else
page.action_count
end
end

def abbreviate_number(number)
return number.to_s if number < 1000
return "#{(goal / 1000).to_i}k" if number < 1_000_000
locale = page.try(:language).try(:code)
"%g #{I18n.t('thermometer.million', locale: locale)}" % (goal / 1_000_000.0).round(1)
end

def next_goal_as_multiple_of(step)
remainder = current_total % step
current_total + step - remainder
end

def set_defaults
self.offset ||= DEFAULTS[:offset]
end
end
61 changes: 17 additions & 44 deletions app/models/plugins/donations_thermometer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,38 @@
class Plugins::DonationsThermometer < Plugins::Thermometer
belongs_to :page, touch: true

DEFAULTS = { offset: 0 }.freeze
GOALS = [100, 200, 300, 400, 500,
1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000,
10_000, 15_000, 20_000, 25_000, 50_000, 75_000, 100_000,
150_000, 200_000, 250_000, 300_000, 500_000, 750_000,
1_000_000, 1_500_000, 2_000_000].freeze

validates :offset, presence: true,
numericality: { greater_than_or_equal_to: 0 }
after_initialize :set_defaults

def current_total
offset + action_count
Money.from_amount(offset, Settings.default_currency) + total_donations
end

def current_progress
current_total / goal.to_f * 100
end

def goal
GOALS.find { |goal| current_total < goal } || next_goal_as_multiple_of(1_000_000)
total_donations / fundraising_goal * 100
end

def liquid_data(_supplemental_data = {})
attributes.merge(
percentage: current_progress,
remaining: ActionController::Base.helpers.number_with_delimiter(goal - current_total),
signatures: ActionController::Base.helpers.number_with_delimiter(current_total),
goal_k: abbreviate_number(goal)
remaining: ActionController::Base.helpers.number_with_delimiter(fundraising_goal - current_total),
total_donations: ActionController::Base.helpers.number_with_delimiter(current_total),
goal_k: abbreviate_number(fundraising_goal)
)
end

def name
self.class.name.demodulize
end

private

def action_count
@action_count ||= if page.campaign_id.present?
Page.where(campaign_id: page.campaign_id).sum(:action_count)
else
page.action_count
end
end

def abbreviate_number(number)
return number.to_s if number < 1000
return "#{(goal / 1000).to_i}k" if number < 1_000_000
locale = page.try(:language).try(:code)
"%g #{I18n.t('thermometer.million', locale: locale)}" % (goal / 1_000_000.0).round(1)
end

def next_goal_as_multiple_of(step)
remainder = current_total % step
current_total + step - remainder
def fundraising_goal
if page.campaign.blank?
page.fundraising_goal
else
page.campaign.fundraising_goal
end
end

def set_defaults
self.offset ||= DEFAULTS[:offset]
def total_donations
if page.campaign.blank?
page.total_donations
else
page.campaign.total_donations
end
end
end
44 changes: 2 additions & 42 deletions app/models/plugins/thermometer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,18 @@ def self.store_full_sti_class
belongs_to :page, touch: true

DEFAULTS = { offset: 0 }.freeze
GOALS = [100, 200, 300, 400, 500,
1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000,
10_000, 15_000, 20_000, 25_000, 50_000, 75_000, 100_000,
150_000, 200_000, 250_000, 300_000, 500_000, 750_000,
1_000_000, 1_500_000, 2_000_000].freeze

validates :offset, presence: true,
numericality: { greater_than_or_equal_to: 0 }
after_initialize :set_defaults

def current_total
offset + action_count
end

def current_progress
current_total / goal.to_f * 100
end

def goal
GOALS.find { |goal| current_total < goal } || next_goal_as_multiple_of(1_000_000)
end

def liquid_data(_supplemental_data = {})
attributes.merge(
percentage: current_progress,
remaining: ActionController::Base.helpers.number_with_delimiter(goal - current_total),
signatures: ActionController::Base.helpers.number_with_delimiter(current_total),
goal_k: abbreviate_number(goal)
)
end

def name
self.class.name.demodulize
end

private

def action_count
@action_count ||= if page.campaign_id.present?
Page.where(campaign_id: page.campaign_id).sum(:action_count)
else
page.action_count
end
def set_defaults
self.offset ||= DEFAULTS[:offset]
end

def abbreviate_number(number)
Expand All @@ -74,13 +43,4 @@ def abbreviate_number(number)
locale = page.try(:language).try(:code)
"%g #{I18n.t('thermometer.million', locale: locale)}" % (goal / 1_000_000.0).round(1)
end

def next_goal_as_multiple_of(step)
remainder = current_total % step
current_total + step - remainder
end

def set_defaults
self.offset ||= DEFAULTS[:offset]
end
end
2 changes: 1 addition & 1 deletion app/views/plugins/thermometers/_form.slim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.col-md-7
= render partial: 'plugins/shared/toggle_form', locals: { plugin: plugin, path: plugins_thermometer_path(plugin) }

- name = "plugins_actions_thermometer_#{plugin.id}"
- name = "plugins_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)
Expand Down
7 changes: 3 additions & 4 deletions app/views/plugins/thermometers/_thermometer.liquid
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<!-- This is the template for ActionsThermometer. It needed to keep under _thermometer.liquid partial name
to ensure that all legacy templates (including the ones seeded into the DB and no longer in the filesystem)
continued to work. This template is not used by DonationsThermometer. -->
continued to work. This template is not used by DonationsThermometer. This partial will be cached, so the
javascript replaces the values for signature, remaining, and thermometer width -->
{% if plugins.actions_thermometer[ref].active %}
<!-- this partial will be cached, so the javascript replaces the
values for signature, remaining, and thermometer width -->
<div class='thermometer'>
<div class="thermometer__stats">
<div class='thermometer__signatures'>
Expand All @@ -16,7 +15,7 @@
</div>
</div>
<div class="thermometer__bg">
<div class="thermometer__mercury" style="width: {{ plugins.thermometer[ref].percentage }}%"></div>
<div class="thermometer__mercury" style="width: {{ plugins.actions_thermometer[ref].percentage }}%"></div>
</div>
</div>
{% endif %}
36 changes: 36 additions & 0 deletions spec/liquid/liquid_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,42 @@
end
end

describe 'donations_thermometer' do
it 'is nil if no donations thermometer plugin' do
create :plugins_fundraiser, :donations_thermometer, page: page
expect(page.plugins.size).to eq 1
expect(LiquidRenderer.new(page).personalization_data['donations_thermometer']).to eq nil
end

it "is serializes the actions thermometer plugin's data" do
t1 = create :plugins_thermometer, :donations_thermometer, page: page
t1.current_progress # allow goal to update
expected = t1.liquid_data.stringify_keys
actual = LiquidRenderer.new(page).personalization_data['donations_thermometer']
# disagreement over timestamps is not what this test is about
[expected, actual].each do |h|
h.delete('updated_at')
h.delete('created_at')
end
expect(actual).to eq expected
end

it 'is uses the first if multiple actions thermometer plugins' do
t1 = create :plugins_thermometer, :donations_thermometer, page: page, ref: 'secondary'
create :plugins_thermometer, :donations_thermometer, page: page
expect(page.plugins.size).to eq 2
t1.current_progress # allow goal to update
expected = t1.liquid_data.stringify_keys
actual = LiquidRenderer.new(page).personalization_data['donations_thermometer']
# disagreement over timestamps is not what this test is about
[expected, actual].each do |h|
h.delete('updated_at')
h.delete('created_at')
end
expect(actual).to eq expected
end
end

describe 'action_count' do
it 'serializes page.action_count' do
page.action_count = 1337
Expand Down

0 comments on commit bde20ce

Please sign in to comment.