cirandas.net

ref: master

plugins/spaminator/controllers/spaminator_plugin_admin_controller.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class SpaminatorPluginAdminController < AdminController
  append_view_path File.join(File.dirname(__FILE__) + '/../views')

  def index
    @settings ||= Noosfero::Plugin::Settings.new(environment, SpaminatorPlugin, params[:settings])
    @reports_count = SpaminatorPlugin::Report.from_environment(environment).count
    @reports = SpaminatorPlugin::Report.from_environment(environment).order('created_at desc').limit(3)
    @next_run = settings.period.to_i + ((settings.last_run || Date.today).to_date - Date.today)
    if request.post?
      settings.period = nil if settings.period.blank?
      settings.save!
      redirect_to :action => 'index'
    end
  end

  def deploy
    if !settings.deployed
      SpaminatorPlugin.schedule_scan(environment)
      settings.deployed = true
      settings.save!
    end
    redirect_to :action => 'index'
  end

  def withhold
    remove_scheduled_scan
    settings.deployed = false
    settings.save!
    redirect_to :action => 'index'
  end

  def scan
    if !settings.scanning
      settings.scanning = true
      settings.save!
      remove_scheduled_scan
      Delayed::Job.enqueue(SpaminatorPlugin::ScanJob.new(environment.id))
    end
    redirect_to :action => 'index'
  end

  def reports
    @reports = SpaminatorPlugin::Report.from_environment(environment).order('created_at desc')
  end

  private

  def settings
    @settings ||= Noosfero::Plugin::Settings.new(environment, SpaminatorPlugin)
  end

  def remove_scheduled_scan
    if settings.scheduled_scan
      Delayed::Job.find(settings.scheduled_scan).destroy
      settings.scheduled_scan = nil
      settings.save!
    end
  end

end