Declaring static properties of Rails model subclasses

≡放荡痞女 提交于 2019-12-11 14:01:51

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10438647/declaring-static-properties-of-rails-model-subclasses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!