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

Commit

Permalink
[vini/brian] Fixed email preview functionality on the petition page a…
Browse files Browse the repository at this point in the history
…nd updated the spec for it to run in Capybara.
  • Loading branch information
Vinicius Gomes committed Aug 30, 2012
1 parent 9d7a980 commit 18bc93c
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ group :test, :development do
gem 'jslint_on_rails'
gem 'capybara'
gem 'capybara-webkit'
gem 'debugger'
end

gem 'haml'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.3.3)
columnize (0.3.6)
compass (0.12.2)
chunky_png (~> 1.2)
fssm (>= 0.2.7)
Expand All @@ -114,6 +115,13 @@ GEM
css_parser (1.2.6)
addressable
rdoc
debugger (1.2.0)
columnize (>= 0.3.1)
debugger-linecache (~> 1.1.1)
debugger-ruby_core_source (~> 1.1.3)
debugger-linecache (1.1.1)
debugger-ruby_core_source (>= 1.1.1)
debugger-ruby_core_source (1.1.3)
diff-lcs (1.1.3)
dkim (0.2.0)
erubis (2.7.0)
Expand Down Expand Up @@ -329,6 +337,7 @@ DEPENDENCIES
capybara-webkit
coffee-rails (~> 3.2.1)
compass-rails
debugger
dkim
facebook_share_widget!
factory_girl_rails (~> 3.0)
Expand Down
43 changes: 28 additions & 15 deletions app/assets/javascripts/petition_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,40 @@ function initEditPetition(root) {
});
}

function send_email_preview(form, url) {
function authenticityToken() {
return $('meta[name="csrf-token"]').attr('content');
}

function sendEmailPreview(form, url) {
var valuesToSubmit = form.serialize();
$.ajax({
url: url,
data: valuesToSubmit,
type: 'POST',
dataType: "JSON",
statusCode: {
200: function() {
alert("Email sent!");
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus + "...Failed to send email because...\n" + jqXHR.responseText);
}
});
$.ajax({
url: url,
data: valuesToSubmit,
type: 'POST',
dataType: "JSON",
headers: { 'X-CSRF-Token': authenticityToken() },
statusCode: {
200: function() {
alert("Email sent!");
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus + "...Failed to send email because...\n" + jqXHR.responseText);
}
});
}

$(document).ready(function() {
$(".petition-form").each(function() {
initEditPetition(this);
$("#petition_facebook_description").counter({ goal: 300, count: 'down' });
});

$("#email_preview_link").click(function(evt) {
evt.preventDefault();
var form = $(this).closest('form'),
url = $(this).data("preview-url");

sendEmailPreview(form, url);
});
});
2 changes: 1 addition & 1 deletion app/views/petitions/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@
-# trick not to submit the form on enter
= f.button :submit, class: 'btn btn-primary span2', id: "petition_submit", onmousedown: "this.title=1", onclick: "if(this.title!=1){return false;}"
- if is_admin
= link_to "Email a preview to #{current_user.email}", "#", :id => "email_preview_link", :onclick => "send_email_preview($(this).closest('form'), '#{url_for(:action => 'send_email_preview', :controller => 'petitions')}');"
= link_to "Email a preview to #{current_user.email}", "#", :id => "email_preview_link", :data => { "preview-url" => url_for(:controller => "petitions", :action => "send_email_preview") }

1 change: 0 additions & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.action_mailer.delivery_method = :test

config.action_mailer.default_url_options = {
:host => 'test',
Expand Down
5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
resources :incoming_mails
resources :petitions do
resources :signatures
member { post 'again' }
member { post 'again'; put 'send_email_preview' }
collection { post 'send_email_preview' }
end
resources :social_tracking
resources :privacy
Expand All @@ -25,8 +26,6 @@
get 'unsubscribe', to: 'unsubscribes#new', as: 'subscribe'
get 'logout', to: 'sessions#destroy', as: 'logout'
get 'test_resque', to: 'signatures#test_resque', as:'test_resque'
post 'petitions/send_email_preview', to: 'petitions#send_email_preview', as: 'send_email_preview'
put 'petitions/:id/send_email_preview', to: 'petitions#send_email_preview', as: 'send_email_preview'

namespace(:admin) do
resources :petitions
Expand Down
9 changes: 7 additions & 2 deletions spec/requests/petitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@
let(:user) { create :admin_user }

it_behaves_like 'an author'
it 'can send a preview email to herself' do

it 'can send a preview email to herself', js: true, driver: :webkit do
login email, pass do
visit new_petition_path
page.should have_content "Email a preview to #{email}"
page.evaluate_script('window.alert = function() { return true; }')
click_link "Email a preview to #{email}"
end

ActionMailer::Base.should have(1).delivery
ActionMailer::Base.deliveries.first.to.should include(email)
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
config.before(:each) do
REDIS.flushdb
end

config.after(:each) do
ActionMailer::Base.deliveries.clear
end
end

Capybara.default_wait_time = 5
Expand Down

0 comments on commit 18bc93c

Please sign in to comment.