def file_column(attr, options={})
options = DEFAULT_OPTIONS.merge(options) if options
my_options = FileColumn::init_options(options,
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(my_options, 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|
instance_variable_set state_attr, send(state_method).assign(file)
if my_options[:after_assign]
my_options[:after_assign].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
define_method "#{attr}_options" do
send(state_method).options
end
private after_save_method, after_destroy_method
FileColumn::Magick::file_column(self, attr, my_options) if options[:magick]
end