Sha256: 6ce72432c54420624869d033c6fb46826bd89b7fc45dc9d8be30789491030ac1

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module UploadColumn
  module MagicColumns

    def self.included(base)
      super
      base.send :alias_method_chain, :set_upload_column, :magic_columns
      base.send :alias_method_chain, :set_upload_column_temp, :magic_columns
      base.send :alias_method_chain, :save_uploaded_files, :magic_columns
    end

    def set_upload_column_with_magic_columns(name, file)
      set_upload_column_without_magic_columns(name, file)
      evaluate_magic_columns_for_upload_column(name)
    end

    def set_upload_column_temp_with_magic_columns(name, path)
      set_upload_column_temp_without_magic_columns(name, path)
      evaluate_magic_columns_for_upload_column(name)
    end

    def save_uploaded_files_with_magic_columns
      save_uploaded_files_without_magic_columns
      self.class.reflect_on_upload_columns.each do |name, column|
        evaluate_magic_columns_for_upload_column(name)
      end
    end

    private

    def evaluate_magic_columns_for_upload_column(name)

      self.class.column_names.each do |column_name|

        statement, predicate = column_name.split('_', 2)

        if statement and predicate and name.to_s == statement and not self.read_attribute(column_name.to_sym)
          uploaded_file = self.send(:get_upload_column, name.to_sym)

          self.send(:write_attribute, column_name.to_sym, handle_predicate(uploaded_file, predicate))
        end

      end
    end

    def handle_predicate(uploaded_file, predicate)
      return uploaded_file.send(predicate.to_sym) if uploaded_file.respond_to?(predicate.to_sym)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uploadcolumn-0.3.4 lib/upload_column/magic_columns.rb