2

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!)

Dan Halliday
  • 725
  • 6
  • 22
  • http://stackoverflow.com/questions/1251352/ruby-inherit-code-that-works-with-class-variables – Dave Newton May 03 '12 at 20:27
  • are you planning to change those values ever or are they really constants? – jstim May 03 '12 at 20:30
  • This is the one I was looking for. But I agree with jstim--and the first referenced SO post hints that they're more like constants anyway. http://stackoverflow.com/questions/8369050/ruby-and-class-variables-in-inherit-class/8369249#8369249 – Dave Newton May 03 '12 at 20:37
  • Thanks for the link Dave — yes I should definitely be using a constant for the `asset_domain` and I now have some ideas about how to state the `asset_path`. Great! – Dan Halliday May 04 '12 at 08:37

2 Answers2

4

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.

Community
  • 1
  • 1
jstim
  • 2,432
  • 1
  • 21
  • 28
  • Thanks for the clear answer. You're right actually, the asset domain does have a broader scope than just my `Documents`. And it's an inherent app setting that I should be able to change easily, so putting a constant in `environment.rb` makes perfect sense. (Say if assets.example.com breaks and I need to use assets.example2.com, I just change it there and redeploy — easy!). – Dan Halliday May 04 '12 at 08:16
1

You can simply override the icon function from Document in Tutorial (as it inherits from it) and have it return the correct path.

This is a classic case of Polymorphism in object-oriented programming. An example:

class Document
  attr_accessor :title, :body, :icon

  ASSET_DOMAIN = "http://assets.example.com/"

  def icon
    return ASSET_DOMAIN + "documents/" + "document_icon.png"
  end
end

class Tutorial < Document
  def icon
    return ASSET_DOMAIN + "tutorials/" + "tutorial_icon.png"
  end
end

d = Document.new
puts d.icon

i = Tutorial.new
puts i.icon

Output:

http://assets.example.com/documents/document_icon.png
http://assets.example.com/tutorials/tutorial_icon.png

Note because Tutorial is a subclass of Document, it inherits both its fields and its methods. Therefore :title, :body and :icon do not need to be redefined within Tutorial and the icon method can be redefined to give the desired output. It's also wise to store a value that will rarely change in a constant, ASSET_DOMAIN in this case.

xiy
  • 808
  • 6
  • 13
  • Thanks for the answer on the `asset_path` part of the question. I'm going to do what you suggest and override the `icon` accessor method in each child class (as there aren't many), but it does seem a bit redundant to keep reimplementing the method? Say I change my folder structure on `assets.example.com`. I have to tell each class (rather than just the `Document`) how to form a path for the new structure. I'm actually leaning more towards a _convention over configuration_ approach of taking the class name as the path name... – Dan Halliday May 04 '12 at 08:26