diff --git a/app/models/dunlop/source_file_model.rb b/app/models/dunlop/source_file_model.rb index e180213..ae2f25e 100644 --- a/app/models/dunlop/source_file_model.rb +++ b/app/models/dunlop/source_file_model.rb @@ -16,6 +16,7 @@ module Dunlop::SourceFileModel has_many :downloads, class_name: 'Dunlop::FileDownload', as: :file attr_accessor :working_file belongs_to :creator, polymorphic: true, optional: true + Dunlop.source_file_models << self mount_uploader :original_file, Dunlop::SourceFileUploader unless defined?(Paperclip) # CarrierWave still default. diff --git a/app/models/dunlop/target_file_model.rb b/app/models/dunlop/target_file_model.rb index 010ca57..8861d7b 100644 --- a/app/models/dunlop/target_file_model.rb +++ b/app/models/dunlop/target_file_model.rb @@ -14,6 +14,7 @@ module Dunlop::TargetFileModel has_many :downloads, class_name: 'Dunlop::FileDownload', as: :file attr_accessor :working_file, :original_file attr_accessor :resource + Dunlop.target_file_models << self mount_uploader :original_file, Dunlop::TargetFileUploader unless defined?(Paperclip) # CarrierWave still default. # used by dunlop-file_transfer diff --git a/lib/dunlop.rb b/lib/dunlop.rb index 83578df..5601c5d 100644 --- a/lib/dunlop.rb +++ b/lib/dunlop.rb @@ -20,4 +20,12 @@ module Dunlop def self.has_target_files? 'TargetFile'.safe_constantize end + + def self.source_file_models + @source_file_models ||= [] + end + + def self.target_file_models + @target_file_models ||= [] + end end diff --git a/lib/dunlop/engine.rb b/lib/dunlop/engine.rb index 07faff2..40cf27f 100644 --- a/lib/dunlop/engine.rb +++ b/lib/dunlop/engine.rb @@ -37,6 +37,8 @@ end module Dunlop class Engine < ::Rails::Engine isolate_namespace Dunlop + + #TODO this generates noise and pollutes API based implementations (Like Panda) initializer 'dunlop.action_controller' do |app| ActiveSupport.on_load :action_controller do helper Dunlop::ApplicationHelper if respond_to?(:helper) @@ -50,12 +52,28 @@ module Dunlop end # add migrations to containing application - initializer :append_migrations do |app| + initializer 'dunlop.append_migrations' do |app| unless app.root.to_s.match root.to_s config.paths["db/migrate"].expanded.each do |expanded_path| app.config.paths["db/migrate"] << expanded_path end end end + + config.after_initialize do + # Auto generate target-file's ::Job classes having purpose triggering .generate on the class + Dunlop.target_file_models.each do |target_file_base_model| + ## Generte a job for all target files + base_job = "#{target_file_base_model.name.deconstantize}::ApplicationJob".constantize + target_file_base_model.classes.each do |cls| + next if cls.const_defined?(:Job) + cls::Job = Class.new base_job do + def perform + self.class.name.sub(/::Job$/, '').constantize.generate! + end + end + end + end + end end end