Overriding a file_column attribute in model

I have a user model subclassed into Employees and Managers using STI. I
am using FileColumn to manage images, in this case logos. If an Employee
doesn’t have a logo of its own, I want my app to use the Manager’s logo
in views.

I did the following in models/users.rb:

class User < ActiveRecord::Base
file_column :logo
end

class Employee < User
belongs_to :Manager
def logo
self[:logo] || self.manager[:logo]
end
end

class Manager < User
has_many :employees
end


When I call <%= @user.logo %> and @user is an Employee, it returns the
correct filename, using the Employee’s when one is set, and otherwise
falling back on the Manager’s.

However, none of the file_column methods are available. All I’m getting
in @user.logo is a string. @user.logo.relative_path, for instance,
returns nil, as do calls to url_for_file_column. Obviously all my
accessor is returning is a string attribute, noit a file_coilumn. Is
there a sensible way in my model to get the behavior I want, i.e. to
override the default read accessor and return a file_column with the
methods and attributes automagically brought along?

I suppose I could replace instances of url_for_file_column with a custom
helper that checks the user object type and generates a
url_for_file_column for either the Employee or Manager as needed, but I
also use this in an ActionMailer template, and bringing view helpers
into there gets ugly.

Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs