Sha256: 7c11bffcb3b373e78afe3c390572241a79a9e3eca99a9dcc0ed480458f023bd3

Contents?: true

Size: 1.78 KB

Versions: 51

Compression:

Stored size: 1.78 KB

Contents

module Workarea
  module DataFile
    module Operation
      extend ActiveSupport::Concern

      included do
        extend Dragonfly::Model

        field :model_type, type: String
        field :file_name, type: String
        field :file_uid, type: String
        field :file_type, type: String
        field :started_at, type: Time
        field :completed_at, type: Time
        field :created_by_id, type: String

        index(
          { created_at: 1 },
          { expire_after_seconds: Workarea.config.data_file_operation_ttl.seconds.to_i }
        )

        dragonfly_accessor :file, app: :workarea
        define_model_callbacks :process
        validates :model_type, presence: true
      end

      def name
        model_class.model_name.param_key.titleize
      end

      def model_class
        @model_class ||= model_type.constantize
      end

      def file_type
        super.presence_in(Workarea.config.data_file_formats) ||
          Workarea.config.data_file_formats.first
      end

      def mime_type
        "#{MIME::Types.type_for(file_type).first.to_s}; charset=utf-8"
      end

      def process!
        raise NotImplementedError
      end

      def complete?
        completed_at.present?
      end

      def sample_file_content
        format.serialize(samples)
      end

      def samples
        @samples ||= model_class.limit(Workarea.config.data_file_sample_size).to_a
      end

      def tempfile
        @tempfile ||= File.open(Pathname.new(Dir.tmpdir).join(generate_file_name), 'w')
      end

      def format
        @format ||= "Workarea::DataFile::#{file_type.camelize}".constantize.new(self)
      end

      private

      def generate_file_name
        "#{model_class.model_name.route_key}_#{Time.current.to_s(:export)}.#{file_type}"
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/data_file/operation.rb
workarea-core-3.5.26 app/models/workarea/data_file/operation.rb
workarea-core-3.4.45 app/models/workarea/data_file/operation.rb
workarea-core-3.5.25 app/models/workarea/data_file/operation.rb
workarea-core-3.5.23 app/models/workarea/data_file/operation.rb
workarea-core-3.4.44 app/models/workarea/data_file/operation.rb
workarea-core-3.5.22 app/models/workarea/data_file/operation.rb
workarea-core-3.4.43 app/models/workarea/data_file/operation.rb
workarea-core-3.5.21 app/models/workarea/data_file/operation.rb
workarea-core-3.4.42 app/models/workarea/data_file/operation.rb
workarea-core-3.5.20 app/models/workarea/data_file/operation.rb
workarea-core-3.4.41 app/models/workarea/data_file/operation.rb
workarea-core-3.5.19 app/models/workarea/data_file/operation.rb
workarea-core-3.4.40 app/models/workarea/data_file/operation.rb
workarea-core-3.5.18 app/models/workarea/data_file/operation.rb
workarea-core-3.4.39 app/models/workarea/data_file/operation.rb
workarea-core-3.5.17 app/models/workarea/data_file/operation.rb
workarea-core-3.4.38 app/models/workarea/data_file/operation.rb
workarea-core-3.5.16 app/models/workarea/data_file/operation.rb
workarea-core-3.4.37 app/models/workarea/data_file/operation.rb