Skip to content

Commit

Permalink
Rerun whole campaigns
Browse files Browse the repository at this point in the history
  • Loading branch information
pezholio committed Jun 22, 2015
1 parent b63e701 commit e636504
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
15 changes: 14 additions & 1 deletion app/controllers/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,23 @@ def show
end
end

def rerun
authorize! :manage, :all

@campaign.sidekiq_delay.rerun!
flash[:notice] = "Campaign queued for rerun"

begin
redirect_to :back
rescue ActionController::RedirectBackError
redirect_to root_path
end
end

private

def get_campaign
@campaign = CertificationCampaign.find(params[:id])
@campaign = CertificationCampaign.find(params[:id] || params[:campaign_id])
authorize! :read, @campaign
end

Expand Down
7 changes: 7 additions & 0 deletions app/models/certification_campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ def published_count
certificate_generators.joins(:certificate).merge(Certificate.published).count
end

def rerun!
certificate_generators.each do |c|
dataset = Dataset.find(c.dataset.id)
CertificateGenerator.update(dataset, nil, c.survey.access_code, user)
end
end

end
3 changes: 3 additions & 0 deletions app/views/campaigns/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<li><%= pluralize @campaign.published_count, 'certificate' %> published</li>
<li><%= pluralize @campaign.duplicate_count, 'dataset' %> already existed</li>
</ul>

<%= button_to "Rerun campaign", campaign_rerun_path(@campaign), method: 'post' %>

<table class='table table-striped'>
<tr>
<th>
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@
get 'status/events' => 'main#status_events'
get 'legacy_stats.csv' => 'main#legacy_stats', format: 'csv'
get 'embed_stats.csv' => 'embed_stats#index', format: 'csv', as: :embed_stats
resources :campaigns

resources :campaigns do
post 'rerun', to: 'campaigns#rerun', as: 'rerun'
end

# private stats
get 'status/published_certificates.csv' => 'main#published_certificates'
Expand Down
27 changes: 27 additions & 0 deletions features/rerun_whole_campaigns.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@api @data_kitten
Feature: Rerun campaigns

Background:
Given I am signed in as the API user

Scenario: Rerun campaign button queues correctly
Given I have a campaign "brian"
And that campaign has 5 certificates
And I visit the campaign page for "brian"
Then the campaign should be queued to be rerun
When I click "Rerun campaign"
And I should be redirected to the campaign page for "brian"
And I should see "Campaign queued for rerun"

Scenario: Rerunnning campaigns when new data is present
Given I want to create a certificate via the API
And I apply a campaign "brian"
And the field "publisherUrl" is missing from my metadata
And my URL autocompletes via DataKitten
When I request a certificate via the API
And the certificate is created
And I visit the campaign page for "brian"
When I add the field "publisherUrl" with the value "http://example.com" to my metadata
And my URL autocompletes via DataKitten
And I click "Rerun campaign"
Then I should see "1 certificate published"
4 changes: 2 additions & 2 deletions features/step_definitions/autogeneration_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
}
end

When(/^I click "(.*?)"$/) do |arg1|
click_on("regenerate")
When(/^I click "(.*?)"$/) do |text|
click_on(text)
end

Given(/^my metadata has the email "(.*?)" associated$/) do |email|
Expand Down
25 changes: 25 additions & 0 deletions features/step_definitions/campaign_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,28 @@
campaign = CertificationCampaign.find_by_name(name)
assert_not_equal nil, campaign
end

Given(/^I have a campaign "(.*?)"$/) do |name|
@campaign = name
end

Given(/^that campaign has (\d+) certificates?$/) do |num|
num.to_i.times do |i|
steps %Q{
Given I want to create a certificate via the API with the URL "http://data.example.com/datasets/#{i}"
And I apply a campaign "#{@campaign}"
And I request a certificate via the API
And the certificate is created
}
end
end

Then(/^the campaign should be queued to be rerun$/) do
campaign = CertificationCampaign.find_by_name(@campaign)
CertificationCampaign.any_instance.expects(:rerun!)
end

When(/^I should be redirected to the campaign page for "(.*?)"$/) do |campaign|
campaign = CertificationCampaign.find_by_name(campaign)
assert_equal page.current_path, campaign_path(campaign)
end

0 comments on commit e636504

Please sign in to comment.