Method: FileColumn::ClassMethods#file_column

Defined in:
lib/file_column.rb

#file_column(attr, options = {}) ⇒ Object

handle the attr attribute as a “file-upload” column, generating additional methods as explained above. You should pass the attribute’s name as a symbol, like this:

file_column :image

You can pass in an options hash that overrides the options in DEFAULT_OPTIONS.



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/file_column.rb', line 616

def file_column(attr, options={})
  options = DEFAULT_OPTIONS.merge(options) if options
  
  my_options = FileColumn::init_options(options, 
                                        ActiveSupport::Inflector.underscore(self.name).to_s,
                                        attr.to_s)
  
  state_attr = "@#{attr}_state".to_sym
  state_method = "#{attr}_state".to_sym
  
  define_method state_method do
    result = instance_variable_get state_attr
    if result.nil?
      result = FileColumn::create_state(self, attr.to_s)
      instance_variable_set state_attr, result
    end
    result
  end
  
  private state_method
  
  define_method attr do |*args|
    send(state_method).absolute_path *args
  end
  
  define_method "#{attr}_relative_path" do |*args|
    send(state_method).relative_path *args
  end

  define_method "#{attr}_dir" do
    send(state_method).absolute_dir
  end

  define_method "#{attr}_relative_dir" do
    send(state_method).relative_dir
  end

  define_method "#{attr}=" do |file|
    state = send(state_method).assign(file)
    instance_variable_set state_attr, state
    if state.options[:after_upload] and state.just_uploaded?
      state.options[:after_upload].each do |sym|
        self.send sym
      end
    end
  end
  
  define_method "#{attr}_temp" do
    send(state_method).temp_path
  end
  
  define_method "#{attr}_temp=" do |temp_path|
    instance_variable_set state_attr, send(state_method).assign_temp(temp_path)
  end
  
  after_save_method = "#{attr}_after_save".to_sym
  
  define_method after_save_method do
    instance_variable_set state_attr, send(state_method).after_save
  end
  
  after_save after_save_method
  
  after_destroy_method = "#{attr}_after_destroy".to_sym
  
  define_method after_destroy_method do
    send(state_method).after_destroy
  end
  after_destroy after_destroy_method
  
  define_method "#{attr}_just_uploaded?" do
    send(state_method).just_uploaded?
  end

  # this creates a closure keeping a reference to my_options
  # right now that's the only way we store the options. We
  # might use a class attribute as well
  define_method "#{attr}_options" do
    my_options
  end

  private after_save_method, after_destroy_method

  FileColumn::MagickExtension::file_column(self, attr, my_options) if options[:magick]
end