Sha256: 2ebd1c2b6d5de2a2fbe216526abce735fcc20bb4532a315ad90c28dff1ce923a

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module DirModel
  module Export
    module Files
      extend ActiveSupport::Concern

      included do
        define_skip_method(self.file_name)
        define_file_method(self.file_name)
        define_extension_method(self.file_name)
      end

      module ClassMethods

        # Safe to override
        #
        # Define default skip method for a file
        # @param file_name [Symbol] the file: name
        def define_skip_method(file_name)
          define_method(:skip?) do
            !self.public_send(file_name).try(:present?)
          end
        end

        # Safe to override
        #
        # Define default file method for defined file
        # @param file_name [Symbol] the file: name
        def define_file_method(file_name)
          define_method(file_name) do
            file = file_or_uploader = source_model.public_send(file_name)
            if file_or_uploader.respond_to?(:file) # Carrierwave Uploader
              file = file_or_uploader.file
            end
            file
          end
        end

        # Safe to override
        #
        # Define default extension method for defined file
        # @param file_name [Symbol] the file: name
        def define_extension_method(file_name)
          define_method("#{file_name}_extension") do
            return unless self.public_send(file_name).respond_to?(:extension)
            self.public_send(file_name).extension # Carrierwave Uploader
          end
        end

        protected

        def file(file_name, options={})
          super
          define_skip_method(file_name)
          define_file_method(file_name)
          define_extension_method(file_name)
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dir_model-0.5.1 lib/dir_model/export/files.rb
dir_model-0.5.0 lib/dir_model/export/files.rb