Skip to content

Commit

Permalink
import bids as background task
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Jun 1, 2013
1 parent 7af8db0 commit c5b6950
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/controllers/bids_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def index

respond_to do |format|
format.html do
current_user.read_notifications(@project, :added_your_team_to_project) if current_user
current_user.read_notifications(@project, :added_your_team_to_project, :import_finished) if current_user
@bootstrap_data = serialized(search_results[:results], BidWithReviewSerializer, meta: search_results[:meta])
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def import_csv

def post_import_csv
file_contents = params.delete(:file).read
importer = CsvBidImporter.new(@project, file_contents, params)
flash[:success] = t('g.count_imported', count: importer.count)
importer = CsvBidImporter.delay.new(@project, file_contents, params, current_user)
flash[:success] = "Hang tight, we're importing your bids. Give us a minute, and we'll notify you when we're done."
redirect_to project_bids_path(@project)
end

Expand Down
5 changes: 4 additions & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def self.event_types
:your_bid_awarded,
:your_bid_dismissed,
:your_bid_unawarded,
:your_bid_undismissed
:your_bid_undismissed,
:import_finished
)
end

Expand All @@ -63,6 +64,8 @@ def path
project_questions_path(targetable)
when :your_bid_awarded, :your_bid_dismissed, :your_bid_unawarded, :your_bid_undismissed
vendor_bid_path(targetable.vendor, targetable)
when :import_finished
project_bids_path(targetable)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def add_owners_team!
end

def generate_abstract_if_blank!
return unless self.abstract.blank?
return unless self.abstract.blank? && !self.body.blank?
self.abstract = truncate(strip_tags(self.body).gsub(/\n/, ' '), length: 350, omission: "...")
end

Expand Down
18 changes: 14 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,23 @@ def has_permission_to_receive_event?(event)
end

def send_email_notifications_for?(event)
notification_preferences_for(event.event_type) == User.notification_preference_values[:on_with_email] &&
has_permission_to_receive_event?(event)
case Event.event_types[event.event_type]
when :import_finished
false
else
notification_preferences_for(event.event_type) == User.notification_preference_values[:on_with_email] &&
has_permission_to_receive_event?(event)
end
end

def receives_event?(event)
(User.notification_preference_values[:on] <= notification_preferences_for(event.event_type)) &&
has_permission_to_receive_event?(event)
case Event.event_types[event.event_type]
when :import_finished
true
else
(User.notification_preference_values[:on] <= notification_preferences_for(event.event_type)) &&
has_permission_to_receive_event?(event)
end
end

def notification_preferences_for(event_key)
Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@

config.action_mailer.delivery_method = :letter_opener

Delayed::Worker.delay_jobs = false
# Delayed::Worker.delay_jobs = false
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ en:
bid_submitted: "%{bid_vendor_name} submitted a bid for %{project_title}."
bid_unawarded: "%{user_display_name} unawarded %{bid_bidder_name}'s bid on %{project_title}."
bid_undismissed: "%{user_display_name} undismissed %{bid_bidder_name}'s bid on %{project_title}."
import_finished: "We finished importing your bids for %{project_title}."
project_amended: "The project %{project_title} has been amended."
project_comment: "%{comment_user_display_name} commented on %{comment_commentable_title}."
question_answered: "%{user_display_name} answered your question about %{project_title}."
Expand Down
9 changes: 8 additions & 1 deletion lib/csv_bid_importer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class CsvBidImporter
attr_reader :count

def initialize(project, file_contents, params = {})
def initialize(project, file_contents, params = {}, notify_user = nil)
require 'csv'
@count = 0
@project = project
@params = params
@notify_user = notify_user
@options = {}
@csv = CSV.parse file_contents.force_encoding('UTF-8'), headers: true

Expand All @@ -16,6 +17,8 @@ def initialize(project, file_contents, params = {})
setup_response_fields if params[:override_response_fields] && project.bids.count == 0

import

send_user_notification if @notify_user
end

def setup_response_fields
Expand Down Expand Up @@ -81,4 +84,8 @@ def transform_row(row)

transformed
end

def send_user_notification
@project.create_events(:import_finished, @notify_user, @project)
end
end

0 comments on commit c5b6950

Please sign in to comment.