Skip to content

Instantly share code, notes, and snippets.

@ksss
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save ksss/ab8a36f8fde27ecaad55 to your computer and use it in GitHub Desktop.

Select an option

Save ksss/ab8a36f8fde27ecaad55 to your computer and use it in GitHub Desktop.
class Photo < ActiveRecord::Base
extend Refile::ActiveRecord::Attachment::IDName
end
module Refile
module ActiveRecord
module Attachment
module IDName
def attachment(name, raise_errors: false, **options)
super
attacher = "#{name}_attacher"
after_save do
store = __send__(attacher).store
case store
when Refile::Backend::FileSystem
::File.rename store.path(@image_id), store.path(id)
else
fail NotImplementedError
end
end
class_eval "alias #{name}_id id"
define_method :"#{name}_id=" do |value|
instance_variable_set :"@#{name}_id", value
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment