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

Commit

Permalink
Rename sp_type to share_type and sp_button_html to share_button_html
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuuleh authored and vincemtnz committed Jun 21, 2018
1 parent 120cb5e commit 19d8794
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/controllers/share/shares_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def update

def update_url
@page.share_buttons.each do |button|
url = params[button.sp_type.to_sym]
url = params[button.share_type.to_sym]

if url
ShareVariantBuilder.update_button_url(url, button)
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ def toggle_featured_link(page)

def share_url(button)
if button.share_progress?
"http://sumof.us/99/#{button.sp_id}/#{button.sp_type}"
"http://sumof.us/99/#{button.sp_id}/#{button.share_type}"
else
# Extracts the share URL out of the sp_button_html field, which contains the markup for the button.
URI.extract(button.sp_button_html).first
# Extracts the share URL out of the share_button_html field, which contains the markup for the button.
URI.extract(button.share_button_html).first
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/lib/share_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def raw_data
private

def button
@button ||= Share::Button.find_by(page_id: @page.id, sp_type: @type)
@button ||= Share::Button.find_by(page_id: @page.id, share_type: @type)
end
end
10 changes: 5 additions & 5 deletions app/liquid/shares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ class << self
def get_all(page)
buttons_with_variants(page).inject({}) do |shares, button|
view_params = {
css_class: class_from_html(button.sp_button_html)
css_class: class_from_html(button.share_button_html)
}
unless button.share_progress?
variant = select_share_variant(button.sp_type, page)
variant = select_share_variant(button.share_type, page)
view_params[:variant_id] = variant.id
# Prepend the desired query parameters (uri encoded) into the url we want to share
url = button.url << ERB::Util.url_encode("?src=whatsapp&variant_id=#{variant.id}")
view_params[:link_html] = button.sp_button_html.gsub('%7BLINK%7D', url)
view_params[:link_html] = button.share_button_html.gsub('%7BLINK%7D', url)
end
shares[button.sp_type] = view_params
shares[button.share_type] = view_params
shares
end
end

def get_all_html(page)
buttons_with_variants(page).inject({}) do |shares, button|
shares[button.sp_type] = button.sp_button_html.html_safe unless button.sp_button_html.blank?
shares[button.share_type] = button.share_button_html.html_safe unless button.share_button_html.blank?
shares
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/models/share/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# updated_at :datetime not null
# sp_id :string
# page_id :integer
# sp_type :string
# sp_button_html :string
# share_type :string
# share_button_html :string
# analytics :text
#

Expand All @@ -22,8 +22,8 @@ class Share::Button < ApplicationRecord

def share_progress?
# the share types currently managed by share progress
%w[facebook twitter email].include? sp_type
%w[facebook twitter email].include? share_type
end

scope :share_progress, -> { where sp_type: %w[facebook twitter email] }
scope :share_progress, -> { where share_type: %w[facebook twitter email] }
end
12 changes: 6 additions & 6 deletions app/services/share_variant_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def update_button_url(url, button)
if button.share_progress?
sp_button = ShareProgress::Button.new(id: button.sp_id,
page_url: url,
button_template: "sp_#{variant_initials(button.sp_type)}_large")
button_template: "sp_#{variant_initials(button.share_type)}_large")
return unless sp_button.save
end
button.update(url: url)
Expand All @@ -48,22 +48,22 @@ def update
@variant = variant_class.find(@id)
@variant.assign_attributes(@params)
return @variant if @variant.changed.empty? || @variant.invalid?
@button = Share::Button.find_by(sp_type: @variant_type, page_id: @page.id)
@button = Share::Button.find_by(share_type: @variant_type, page_id: @page.id)
update_and_return
end

def create
@variant = variant_class.new(@params)
@variant.page = @page
return @variant unless @variant.valid?
@button = Share::Button.find_or_initialize_by(sp_type: @variant_type, page_id: @page.id)
@button = Share::Button.find_or_initialize_by(share_type: @variant_type, page_id: @page.id)
@variant.button = @button
update_and_return
end

def destroy
@variant = variant_class.find(@id)
@button = Share::Button.find_by(sp_type: @variant_type, page_id: @page.id)
@button = Share::Button.find_by(share_type: @variant_type, page_id: @page.id)

if @variant.share_progress?
sp_button = ShareProgress::Button.new(share_progress_button_params(@variant, @button))
Expand Down Expand Up @@ -96,7 +96,7 @@ def update_button
case @variant_type
when :whatsapp
@button.update(
sp_button_html: '<a class="whatsapp_large" href="https://api.whatsapp.com'\
share_button_html: '<a class="whatsapp_large" href="https://api.whatsapp.com'\
"/send?text=#{ERB::Util.url_encode(@variant.text)}\"></a>",
url: @url
)
Expand All @@ -106,7 +106,7 @@ def update_button
def update_sp_resources
sp_button = ShareProgress::Button.new(share_progress_button_params(@variant, @button))
if sp_button.save
@button.update(sp_id: sp_button.id, sp_button_html: sp_button.share_button_html, url: sp_button.page_url)
@button.update(sp_id: sp_button.id, share_button_html: sp_button.share_button_html, url: sp_button.page_url)
@variant.update(sp_id: sp_button.variants[@variant_type].last[:id])
else
add_sp_errors_to_variant(sp_button, @variant)
Expand Down
2 changes: 1 addition & 1 deletion app/views/pages/_links_to_share_progress.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
td colspan='7'
- links = buttons.map do |btn|
- url = "https://run.shareprogress.org/analytics/#{btn.sp_id}"
- "<a href='#{url}' target='_blank'>#{btn.sp_type}</a>"
- "<a href='#{url}' target='_blank'>#{btn.share_type}</a>"
.shares-editor__stats-heading class="#{only_stats ? '' : 'hidden-closed'}"
= I18n.t('pages.edit.more_on_sp', {link: links.to_sentence}).html_safe
4 changes: 2 additions & 2 deletions app/views/share/_update_urls_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
= form_tag page_share_endpoint_path(@page), method: :put, remote: true
- @page.share_buttons.each do |button|
.form-group
= label_tag button.sp_type, "URL to share on #{button.sp_type.capitalize}"
= text_field_tag button.sp_type, button.url, class: 'form-control'
= label_tag button.share_type, "URL to share on #{button.share_type.capitalize}"
= text_field_tag button.share_type, button.url, class: 'form-control'
= submit_tag t('common.save'), class: 'btn btn-primary xhr-feedback', 'data-disable-with' => t('common.saving')

6 changes: 6 additions & 0 deletions db/migrate/20180614131526_rename_share_progress_columns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RenameShareProgressColumns < ActiveRecord::Migration[5.1]
def change
rename_column :share_buttons, :sp_type, :share_type
rename_column :share_buttons, :sp_button_html, :share_button_html
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180611102745) do
ActiveRecord::Schema.define(version: 20180614131526) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -563,8 +563,8 @@
t.datetime "updated_at", null: false
t.string "sp_id"
t.integer "page_id"
t.string "sp_type"
t.string "sp_button_html"
t.string "share_type"
t.string "share_button_html"
t.text "analytics"
t.index ["page_id"], name: "index_share_buttons_on_page_id"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/button_ids.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace :champaign do
task associate_buttons: :environment do
Share::Variant.all.each do |variant|
# ALERT - this needs good testing before running it on production
variant.button_id = Share::Button.find_by(page_id: variant.page_id, sp_type: variant.class.to_s.demodulize.downcase).id
variant.button_id = Share::Button.find_by(page_id: variant.page_id, share_type: variant.class.to_s.demodulize.downcase).id
variant.save
end
end
Expand Down
17 changes: 9 additions & 8 deletions spec/factories/share_buttons.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: share_buttons
Expand All @@ -10,8 +11,8 @@
# updated_at :datetime not null
# sp_id :string
# page_id :integer
# sp_type :string
# sp_button_html :string
# share_type :string
# share_button_html :string
# analytics :text
#

Expand All @@ -23,18 +24,18 @@
sp_id 2

trait :facebook do
sp_type 'facebook'
sp_button_html "<div class='sp_162041 sp_fb_large' ></div>"
share_type 'facebook'
share_button_html "<div class='sp_162041 sp_fb_large' ></div>"
end

trait :twitter do
sp_type 'twitter'
sp_button_html "<div class='sp_162043 sp_tw_large' ></div>"
share_type 'twitter'
share_button_html "<div class='sp_162043 sp_tw_large' ></div>"
end

trait :email do
sp_type 'email'
sp_button_html "<div class='sp_162130 sp_em_large' ></div>"
share_type 'email'
share_button_html "<div class='sp_162130 sp_em_large' ></div>"
end
end
end
2 changes: 1 addition & 1 deletion spec/helpers/pages_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
end

describe '#share_url' do
let(:button) { double(sp_id: '2', sp_type: 'facebook', share_progress?: true) }
let(:button) { double(sp_id: '2', share_type: 'facebook', share_progress?: true) }

it 'returns share url' do
expect(helper.share_url(button)).to eq('http://sumof.us/99/2/facebook')
Expand Down
16 changes: 8 additions & 8 deletions spec/models/share/button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# updated_at :datetime not null
# sp_id :string
# page_id :integer
# sp_type :string
# sp_button_html :string
# share_type :string
# share_button_html :string
# analytics :text
#

Expand Down Expand Up @@ -48,8 +48,8 @@
end

describe '.share_progress?' do
let(:facebook) { create :share_button, sp_type: 'facebook' }
let(:whatsapp) { create :share_button, sp_type: 'whatsapp' }
let(:facebook) { create :share_button, share_type: 'facebook' }
let(:whatsapp) { create :share_button, share_type: 'whatsapp' }
it 'returns true if the share type is managed by ShareProgress' do
expect(facebook.share_progress?).to eq true
expect(whatsapp.share_progress?).to eq false
Expand All @@ -58,10 +58,10 @@

describe 'scopes' do
describe 'share_progress' do
let(:facebook) { create :share_button, sp_type: 'facebook' }
let(:whatsapp) { create :share_button, sp_type: 'whatsapp' }
let(:twitter) { create :share_button, sp_type: 'twitter' }
let(:email) { create :share_button, sp_type: 'email' }
let(:facebook) { create :share_button, share_type: 'facebook' }
let(:whatsapp) { create :share_button, share_type: 'whatsapp' }
let(:twitter) { create :share_button, share_type: 'twitter' }
let(:email) { create :share_button, share_type: 'email' }
it 'returns buttons types that are managed by ShareProgress' do
expect(Share::Button.share_progress).to include(facebook, twitter, email)
expect(Share::Button.share_progress).to_not include(whatsapp)
Expand Down
8 changes: 4 additions & 4 deletions spec/services/share_variant_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

button = Share::Button.first
expect(button.sp_id).to eq('1')
expect(button.sp_button_html).to eq('<div />')
expect(button.share_button_html).to eq('<div />')
expect(button.url).to eq 'http://example.com/foo'
end

Expand Down Expand Up @@ -176,7 +176,7 @@

describe '.update' do
let!(:share) { create(:share_facebook, title: 'Foo') }
let!(:button) { create(:share_button, sp_type: 'facebook', page: page, sp_id: 23) }
let!(:button) { create(:share_button, share_type: 'facebook', page: page, sp_id: 23) }
let(:params) { { title: 'Bar' } }

subject(:update_variant) do
Expand Down Expand Up @@ -242,7 +242,7 @@
end

describe 'success' do
let!(:button) { create(:share_button, sp_type: 'facebook', page: page, sp_id: 24) }
let!(:button) { create(:share_button, share_type: 'facebook', page: page, sp_id: 24) }
let!(:share) { create(:share_facebook, title: 'herpaderp', sp_id: 24) }
let(:params) { { title: 'Bar' } }

Expand All @@ -269,7 +269,7 @@
end

describe 'failure' do
let!(:button) { create(:share_button, sp_type: 'facebook', page: page, sp_id: nil) }
let!(:button) { create(:share_button, share_type: 'facebook', page: page, sp_id: nil) }
let!(:share) { create(:share_facebook, title: 'herpaderp', sp_id: nil) }
let(:params) { { title: 'Bar' } }

Expand Down
2 changes: 1 addition & 1 deletion spec/views/smoke_tests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
assign :sp_variations, [build(:share_facebook,
id: 1,
button:
build(:share_button, sp_id: 2, sp_type: 'facebook'))]
build(:share_button, sp_id: 2, share_type: 'facebook'))]
assign :local_variations, []
expect { render template: 'pages/edit' }.not_to raise_error
end
Expand Down

0 comments on commit 19d8794

Please sign in to comment.