Forum: Ruby on Rails file_column configurability

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ramin B. (Guest)
on 2005-11-18 17:06
(Received via mailing list)
I wasn't sure where to email this request, so I am hoping the author of
file_column actually sees this. I was wondering if there were any plans
to
allow some configurability in the file_column plugin. By configurability
I
mean allowing the developer to define which table column is used as the
unique identifier when creating image folders. Right now it seems like
it
uses the ID of the table. It would be nice to allow a different field
(say a
username in a Users table.) Along the same lines, it would also be very
nice
if the developer could change the location where file_column stores the
images. Say instead of using the model name, I want to store everything
in
$RAILS_ROOT/public/assets/profiles/<username>

Thanks!
gerret.apelt (Guest)
on 2005-11-18 17:28
(Received via mailing list)
On 11/18/05, Ramin <removed_email_address@domain.invalid> wrote:
> if the developer could change the location where file_column stores the
> images. Say instead of using the model name, I want to store everything in
> $RAILS_ROOT/public/assets/profiles/<username>

You can set the storage base path with the :store_dir option:

file_column :image, :store_dir => "foobar"

As for the path prefix, you could dynamically redefine the method
FileColumn::PermanentUploadedFile#relative_path_prefix somewhere in
your application code. The default implementation is pretty
straight-forward

 def relative_path_prefix
      @instance.id.to_s
 end

so just replace 'id' with 'username'


Sebastian, would it be possible to provide a callback hook for setting
the storage path dynamically?

In my current project I have Mugshot AR instances that are part of
Collection AR instances. The Image AR has one file_column field and a
few vanilla fields. It would be great if I could easily layout my
storage so that my images would be saved in subdirectories of the
collections to which they belong, so that they end up stored somewhat
like this (disregarding any sanitychecks)

#{options[:store_dir]}/#{mugshot.collection.name}/#{mugshot.id}/

I would love to have that addition!

cheers
Gerret
Ramin B. (Guest)
on 2005-11-18 17:40
(Received via mailing list)
Sweet! thanks..
I've created a Wiki page for file_column. Feel free to add to it.

http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn

Gerret, I added your tips to the Wiki as well, I hope you don't mind.
Ramin B. (Guest)
on 2005-11-18 18:46
(Received via mailing list)
Any tips on "dynamically redefining" a method? I'm pretty new to
Ruby/Rails
and I'm not sure how to redefine the relatve_path_prefix method. Where
in my
code do I put this anyway? In the lib folder as a new file or I can
pretty
much put it anywhere?

Thank you
gerret.apelt (Guest)
on 2005-11-18 20:56
(Received via mailing list)
You can put this at the bottom of your config/environment.rb. Remember
to restart your server!

module FileColumn
  class PermanentUploadedFile
    def relative_path_prefix
      @instance.filename.to_s
    end
  end
end

Gerret
Ramin B. (Guest)
on 2005-11-18 21:14
(Received via mailing list)
What exactly is @intance?

The problem is, my image is actually not in my User model, but in my
Profile
model. Each User has_many Profiles and Profile belongs_to User. So if
@intance is the Profile object, then I can't do @intance.login.to_s,
since
login is a field in the User object.
Ramin B. (Guest)
on 2005-11-18 21:26
(Received via mailing list)
Ok, I kinda figured out what I need to do, here is my code:

module FileColumn
class PermanentUploadedFile
def relative_path_prefix
user = User.find(@instance.user_id)
user.login.to_s
end
end
end

So now it is using the User.login as the relative_path_prefix, but some
odd
reason the actual folder isn't being created. It's still creating the
folder
using the @instance.id <http://instance.id>. I'm guessing I need to
override
another method? Any ideas anyone? I'll keep looking through the code to
try
and figure out and i'll post what I find here and on the Wiki.
Ramin B. (Guest)
on 2005-11-18 21:32
(Received via mailing list)
I found the other bit of code that needs to be changed. It is the
initialize
method. I simply made an exact copy of it in my environment.rb right
above
my redefined relative_path_prefix method and simple changed the @dir
variable to my User.login. So now it looks like this:

def initialize(*args)
super *args

user = User.find(@instance.user_id) # new code
@dir = File.join(store_dir, user.login.to_s) # changed code
# ... rest of code
end

Not sure if this the best way to do it since I am not a Ruby expert, but
it
seems to work. If anyone can suggest "cleaner" or more efficient code,
I'd
be very geatful.

Thanks.
Ramin B. (Guest)
on 2005-11-18 21:44
(Received via mailing list)
skanthak (Guest)
on 2005-11-19 13:26
(Received via mailing list)
Hi Ramin,

On 11/18/05, Ramin <removed_email_address@domain.invalid> wrote:
> Wiki updated:
>  http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn

thanks for sharing your experience with using/modifying file_column.
Since the feature has been requested has been somebody else as well I
will add it to my todo list. So far, your changes look pretty sane to
me.

Sebastian
skanthak (Guest)
on 2005-11-19 13:32
(Received via mailing list)
Hi Gerret,

On 11/18/05, Gerret A. <removed_email_address@domain.invalid> wrote:
> #{options[:store_dir]}/#{mugshot.collection.name}/#{mugshot.id}/
>
> I would love to have that addition!

shouldn't be too hard. BaseUploadedFile has a store_dir method, that
just returns a value from the @options hash right now. The
straightforward change would be to check, if a callback is defined
somewhere in the options and call this instead. If you implement it
yourself and send me a patch including some tests I will be happy to
include it.

Sebastian
This topic is locked and can not be replied to.