Search code examples
ruby-on-railsrubysti

Declaring static properties of Rails model subclasses


I'm new to Ruby and Rails (and programming!), and trying to figure out the idiomatic way of passing properties from a model to its STI children.

I have a generic model 'Document', and some models that inherit from it — let's take 'Tutorial' as the example. I have a string field for an 'icon', in which I want to store the icon's filename but not full path (I think the path should be up to each model, as it's a detail of retrieving the record's data?):

class Document < ActiveRecord::Base
  attr_accessible :title, :body, :icon

  @@asset_domain = "http://assets.example.com/"
  @@asset_path = "documents/"

  def icon
    @@asset_domain.to_s + @@asset_path.to_s + read_attribute(:icon).to_s
  end
end

This is the kind of thing I'd like to do with the subclasses, so they look for their 'icons' (or any other asset) in an appropriate place.

class Tutorial < Document
  attr_accessible :title, :body, :icon

  @@asset_path = "tutorials/"

  # Other tutorial-only stuff
end

I've read about class variables and understand why what I've written above didn't work quite as I intended, but what's the best way of overriding 'asset_path' in the Tutorial class? I don't think I should use instance variables as the values don't need to change per instance of the model. Any ideas much appreciated (even if it means rethinking it!)


Solution

  • It looks like you are trying to create a constant value that you can reuse to build paths. Instead of using a class variable, I would use a constant.

    Now the question of placement:

    In-Class

    If it really only needs to be used in Document and the classes that inherit from it, define a constant at the top of the stack:

    # document.rb
    #
    class Document < ActiveRecord::Base
      attr_accessible :title, :body, :icon
    
      ASSET_DOMAIN = "http://assets.example.com/"
    
    end
    

    This would be accessible in Document Tutorial and other objects that inherit from those.

    Environment.rb

    If this is a value you are going to use everywhere, what about adding a constant to your environment.rb? That way you don't have to remember to redefine it in all the classes you placed it.

    # environment.rb
    #
    # other config info
    #
    ASSET_DOMAIN = "http://assets.example.com/"
    

    And then you could build links wherever you like and not be constrained by Class:

    # documents.rb
    #
    icon_path = ASSET_DOMAIN + path_and_file
    
    # tutorial.rb
    #
    icon_path = ASSET_DOMAIN + path_and_file
    
    # non_document_model.rb
    #
    icon_path = ASSET_DOMAIN + path_and_file
    

    This may be editorializing, but rubyists seem to cringe when they see @@. There's a time and place, but for the kind of thing you want to do I would use a constant and decide where you need to place it.