Tag: 葡萄 实体

从父类inheritance类定义

我正在我的Rails模型中构建Grape Entities,如下所述: https://github.com/ruby-grape/grape-entity#entity-organization 目前,我正在根据模型本身的列哈希自动创建默认值。 所以我有一个静态的get_entity方法,它暴露了所有模型的列: class ApplicationRecord < ActiveRecord::Base def self.get_entity(target) self.columns_hash.each do |name, column| target.expose name, documentation: { desc: "Col #{name} of #{self.to_s}" } end end end 然后我在这里有一个示例Book模型在声明的Entity子类中使用它(注释还显示了我如何覆盖模型列之一的文档): class Book < ActiveRecord::Base class Entity < Grape::Entity Book::get_entity(self) # expose :some_column, documentation: {desc: "this is an override"} end end 这种方法的缺点是我总是需要在我想要实体的每个模型中复制和粘贴类Entity声明。 任何人都可以帮我自动为ApplicationRecord的所有子项生成类Entity吗? 然后,如果我需要覆盖,我将需要在类中具有Entity声明,否则如果默认声明足够并且可以保持原样。 注意: 我不能直接在ApplicationRecord中添加类Entity定义,因为Entity类应该调用get_entity,get_entity依赖于Books的column_hash。 解: […]