Sha256: 83191332626060314ee6c19542809674e24adb95892b9e29acdcdf54a58ea798

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module DirModel
  module Export
    module Files
      extend ActiveSupport::Concern

      included do
        self.file_names.each do |*args|
          define_skip_method(*args)
          define_file_method(*args)
          define_extension_method(*args)
        end
      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("#{file_name}_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

1 entries across 1 versions & 1 rubygems

Version Path
dir_model-0.4.0 lib/dir_model/export/files.rb