Skip to content

Commit

Permalink
Issue #12: notify url
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFelipe committed Feb 18, 2014
1 parent 9da199e commit 6f46f45
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 79 deletions.
17 changes: 6 additions & 11 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
class NotificationsController < ApplicationController
authorize_resource
skip_authorization_check :only => [:notify]
skip_before_filter :authenticate_user!, :only => :notify

active_scaffold :"notification" do |config|

config.action_links.add 'preview',
:label => I18n.t('active_scaffold.notification.asynchronous.preview'),
:page => true,
:inline => true,
:type => :collection

config.action_links.add 'set_query_date',
:label => "<i title='#{I18n.t('active_scaffold.notification.set_query_date')}' class='fa fa-clock-o'></i>".html_safe,
:page => true,
Expand Down Expand Up @@ -75,10 +71,9 @@ def set_query_date
render :action => 'set_query_date'
end

def preview
notifier = Notifier.instance
@job = notifier.job
@should_run = notifier.should_run?
@frequency = CustomVariable.notification_frequency
def notify
Notifier.instance.asynchronous_emails
render :inline => 'Ok'
end

end
1 change: 1 addition & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def initialize(user)
elsif role_id == Role::ROLE_SECRETARIA
can :manage, (Ability::ALL_MODELS - [User, Role, CustomVariable, Version, Notification])
end
can :notify, Notification

# Define abilities for the passed in user here. For example:
#
Expand Down
18 changes: 0 additions & 18 deletions app/models/custom_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ class CustomVariable < ActiveRecord::Base

validates :variable, :presence => true

after_save :reschedule_notifier

def reschedule_notifier
return unless (variable_changed? || value_changed?)
return unless ['notification_frequency', 'notification_start_at'].include? variable
Notifier.instance.start_job(false)
end

def self.single_advisor_points
config = CustomVariable.find_by_variable(:single_advisor_points)
config.nil? ? 1.0 : config.value.to_f
Expand All @@ -39,16 +31,6 @@ def self.identity_issuing_country
Country.find_by_name(config.nil? ? "Brasil": config.value)
end

def self.notification_frequency
config = CustomVariable.find_by_variable(:notification_frequency)
config.nil? ? "1d" : config.value.to_s
end

def self.notification_start_at
config = CustomVariable.find_by_variable(:notification_start_at)
config.nil? ? "12:00" : config.value.to_s
end

def self.class_schedule_text
config = CustomVariable.find_by_variable(:class_schedule_text)
config.nil? ? "" : config.value
Expand Down
20 changes: 0 additions & 20 deletions app/views/notifications/preview.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
end

collection do
get 'preview'
get 'notify'
end
end

Expand Down
31 changes: 2 additions & 29 deletions lib/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,14 @@ class Notifier
include Singleton

def initialize
@async_notifications = []
@async_notifications = []
@logger = Rails.logger
@job = nil

return unless should_run?

@scheduler = Rufus::Scheduler.new
start_job(Rails.env.development? || Rails.env.test?)
end

def start_job(development)
@job.unschedule unless @job.nil?

if development
first_at = Time.now + 1.second
else
first_at = Time.parse(CustomVariable.notification_start_at)
if first_at < Time.now
first_at += 1.day
end
end
@logger.info "[Notifications] #{Time.now} - Scheduling next execution to #{first_at}"
@job = @scheduler.schedule_every CustomVariable.notification_frequency, :first_at => first_at do
@logger.info "[Notifications] #{Time.now} - Running notifications"
asynchronous_emails
end
end

def should_run?
Rails.application.config.should_send_emails
end

def job
@job
end

def new_notification(&block)
@async_notifications << lambda { yield }
end
Expand All @@ -56,6 +28,7 @@ def run_notifications
end

def asynchronous_emails
@logger.info "Sending Registered Notifications"
send_emails(run_notifications)
end

Expand Down

0 comments on commit 6f46f45

Please sign in to comment.