diff --git a/app/mailers/orbit_mailer.rb b/app/mailers/orbit_mailer.rb index f0fa7c7..ccaaab0 100644 --- a/app/mailers/orbit_mailer.rb +++ b/app/mailers/orbit_mailer.rb @@ -61,8 +61,9 @@ class OrbitMailer < ActionMailer::Base }) mail_log.save - - # email.destroy + + email.is_sent = true + email.save end diff --git a/app/models/email.rb b/app/models/email.rb index be96d2c..40bf1ea 100644 --- a/app/models/email.rb +++ b/app/models/email.rb @@ -11,6 +11,8 @@ class Email field :mail_sentdate , :type => DateTime, :default => Time.now field :mail_lang, :default => I18n.locale + field :is_sent, :type => Boolean, :default => false + field :template # Path to template file field :template_data # Data to render template @@ -19,14 +21,20 @@ class Email has_many :email_files, :autosave => true, :dependent => :destroy - scope :can_deliver, ->{ where(:mail_sentdate.lte => Time.now) } + scope :can_deliver, ->{ where(:mail_sentdate.lte => Time.now, :is_sent=>false) } def deliver OrbitMailer.set_mail(self).deliver end + def self.deliver_all + Email.can_deliver.each do |email| + OrbitMailer.set_mail(email).deliver + end + end + def module_app=(app) - self.module_app_key = app.key + self.module_app_key = app.key rescue nil end def module_app @@ -34,7 +42,7 @@ class Email end def create_user=(user) - self.create_user_id = user.id + self.create_user_id = user.id rescue nil end def create_user @@ -42,7 +50,7 @@ class Email end def update_user=(user) - self.update_user_id = user.id + self.update_user_id = user.id rescue nil end def update_user diff --git a/app/models/email_log.rb b/app/models/email_log.rb index 64593c8..3ef9bb1 100644 --- a/app/models/email_log.rb +++ b/app/models/email_log.rb @@ -9,7 +9,7 @@ class EmailLog field :mailer_count def module_app=(app) - self.module_app_key = app.key + self.module_app_key = app.key rescue nil end def module_app @@ -17,7 +17,7 @@ class EmailLog end def mail_user=(user) - self.mail_user_id = user.id + self.mail_user_id = user.id rescue nil end def mail_user diff --git a/app/views/admin/sites/_mail_cron.html.erb b/app/views/admin/sites/_mail_cron.html.erb index 6cb75bc..be03b58 100644 --- a/app/views/admin/sites/_mail_cron.html.erb +++ b/app/views/admin/sites/_mail_cron.html.erb @@ -6,15 +6,6 @@ <% end -%> <%= format_value(mail_cron.mail_sentdate) %> - <%= mail_cron.mail_subject %> -
- -
- + <%= mail_cron.mail_subject %> <%= mail_cron.module_app.key %> \ No newline at end of file diff --git a/app/views/admin/sites/_mail_cron_log.html.erb b/app/views/admin/sites/_mail_cron_log.html.erb index 5174194..1b57db3 100644 --- a/app/views/admin/sites/_mail_cron_log.html.erb +++ b/app/views/admin/sites/_mail_cron_log.html.erb @@ -6,14 +6,7 @@ <% end -%> <%= format_value(mail_cron_log.created_at) %> - <%= mail_cron_log.mail_subject %> -
- -
- + <%= mail_cron_log.mail_subject %> <%= mail_cron_log.mail_user.user_name %> <%= mail_cron_log.module_app.key %> \ No newline at end of file diff --git a/lib/tasks/email.rake b/lib/tasks/email.rake new file mode 100644 index 0000000..0d0ba79 --- /dev/null +++ b/lib/tasks/email.rake @@ -0,0 +1,5 @@ +namespace :email do + task :deliver_all => :environment do + Email.deliver_all + end +end \ No newline at end of file diff --git a/lib/tasks/orbit_cron.rake b/lib/tasks/orbit_cron.rake new file mode 100644 index 0000000..5224a20 --- /dev/null +++ b/lib/tasks/orbit_cron.rake @@ -0,0 +1,36 @@ +namespace :orbit_cron do + task :install => :environment do + + # Setup environment in crontab + cron_env ="#!/bin/bash\n"+ + "PATH=#{ENV['PATH']}\n"+ + "RUBYLIB=#{ENV['RUBYLIB']}\n"+ + "GEM_HOME=#{ENV['GEM_HOME']}\n"+ + "GEM_PATH=#{ENV['GEM_PATH']}\n"+ + "RUBYOPT=#{ENV['RUBYOPT']}\n\n" + + # Email cron job + email_cron = "* * * * * cd #{Rails.root.to_s} && bundle exec rake email:deliver_all > /dev/null\n" + + File.open('lib/tasks/orbit_cron', "w+") do |f| + f.write(cron_env + email_cron) + end + + # Check if current crontab is empty + if %x(crontab -l).eql? "" + %x(crontab #{Rails.root.to_s+'/lib/tasks/orbit_cron'}) + %x(rm #{Rails.root.to_s+'/lib/tasks/orbit_cron'}) + puts "\n" + puts "Orbit cron jobs installed\n" + else + puts "\n" + puts "============================ Warning! ============================\n" + puts "Crontab is not empty!\n" + puts "Please edit crontab with command 'crontab -e' and paste the following lines:\n" + puts "=================================================================\n" + puts cron_env + email_cron + puts "=================================================================\n" + puts "\n\n" + end + end +end \ No newline at end of file