weixin_39782500 2020-11-22 01:58
浏览 0

Class Table Inheritance

As a cleaner alternative to Single Table Inheritance, we talked about implementing "Class Table Inheritance":http://martinfowler.com/eaaCatalog/classTableInheritance.html in DM.

As an added benefit, this would make it very easy to do Model Translation à la "Globalize":http://www.globalize-rails.org/globalize/. To really facilitate switching languages, we discussed adding a context block that would allow DM to automatically choose a sub-class of a given parent class based on the supplied context (e.g. locale).

 Ruby
database(:default, 'en-US') do
  products = Product.all
  products.first.name # => Meatballs
end

database(:default, 'es-ES') do
  products = Product.all
  products.first.name # => Albóndigas
end

Created by Josh H - 2007-11-10 20:37:30 UTC

Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/53

该提问来源于开源项目:datamapper/dm-core

  • 写回答

10条回答 默认 最新

  • weixin_39782500 2020-11-22 01:58
    关注

    I suggest that if/when class table inheritance is implemented, it’s done in such a way that Modules which are used as Mixins in the class hierarchy may have their own table too.

    I haven’t yet looked in detail at the DataMapper implementation, but this is how I’m currently doing it with an ActiveRecord plugin:

    A class method ’mixin_extra_columns_from_table’ defined on DataMapper::Base, usable like so:

    module Publishable def self.included(klass) klass.send(:mixin_extra_columns_from_table, :table_name => ’publishables’) end

    def publish unless first_published_date self.first_published_date = Date.today end end

    ... etc ... end

    class Content < DataMapper::Base include Publishable end

    this can then be used for the actual inheritance too, if you define DataMapper::Base#inherited to call it in the right on the subclass.

    by Matthew W

    评论

报告相同问题?