Created
July 24, 2013 05:54
-
-
Save teeerevor/6068345 to your computer and use it in GitHub Desktop.
branding_updater.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
#require 'branding_updater' | |
#BrandingElement.where(branding_type: 'SponsorBanner').select{|be| be.html_content =~ /<div/ } | |
class BrandingUpdater | |
def self.update_branding(branding_elements=[]) | |
html_sanatizer = HTML::FullSanitizer.new | |
branding_elements.each do |element| | |
if element.branding_type == 'SponsorBanner' | |
if element.html_content =~ /<div/ | |
#get the custom message text | |
message_text = html_sanatizer.sanitize(element.html_content) | |
#remove the div but keep the image | |
element.html_content = element.html_content.split(/<div/)[0] | |
custom_message_be = BrandingElement.where(tutoring_service_agreement_id: element.tutoring_service_agreement_id, | |
branding_type: 'ClientCustomMessage').first | |
if custom_message_be | |
custom_message_be.update_attribute('html_content', message_text) | |
else | |
BrandingElement.create(tutoring_service_agreement_id: element.tutoring_service_agreement_id, | |
branding_type: 'ClientCustomMessage', | |
html_content: message_text) | |
end | |
end | |
end | |
end | |
end | |
end | |
describe BrandingUpdater do | |
let(:branding_element){ FactoryGirl.build(:banner_branding_element, branding_type: 'something_else')} | |
let(:sponsor_banner){ FactoryGirl.build(:banner_branding_element)} | |
describe '#update_branding(branding_elements)' do | |
it 'will only edit SponsorBanners elements' do | |
BrandingElement.should_not_receive(:create) | |
BrandingUpdater.update_branding([branding_element]) | |
end | |
it 'will only edit SponsorBanners with divs' do | |
BrandingElement.should_not_receive(:create) | |
BrandingUpdater.update_branding([sponsor_banner]) | |
end | |
context 'using a valid sponsor branding element with custom message' do | |
let(:text){'Some custom message to students'} | |
subject{FactoryGirl.build(:banner_branding_element, html_content: "<img src='/images/client_banner_example.jpg'/><div>#{text}</div>")} | |
it 'will remove the divs from sponsorbranding' do | |
BrandingUpdater.update_branding([subject]) | |
expect(subject.html_content).not_to include '<div' | |
end | |
it 'will create a new branding element with sanitized text' do | |
BrandingElement.should_receive(:create).with(tutoring_service_agreement_id: subject.tutoring_service_agreement_id, | |
branding_type: 'ClientCustomMessage', | |
html_content: text) | |
BrandingUpdater.update_branding([subject]) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment