app/models/unidom/common/concerns/model_extension.rb in unidom-common-1.10 vs app/models/unidom/common/concerns/model_extension.rb in unidom-common-1.11
- old
+ new
@@ -1,5 +1,8 @@
+##
+# Model Extension 是通用的模型扩展关注点,提供参数验证、JSON 注解字段、加密字段等。
+
module Unidom::Common::Concerns::ModelExtension
extend ActiveSupport::Concern
include Unidom::Common::Concerns::ArgumentValidation
@@ -113,10 +116,14 @@
#relation = base.all
#scopes.each do |s| relation = relation.send s.to_sym end
#relation
end
+ ##
+ # 对当前对象进行软删除。软删除之后, #closed_at 会被置为当前时间, #defunct 会被置为 false 。并且对象会被自动保存。如:
+ # model = YourModel.find your_id
+ # model.soft_destroy
def soft_destroy
self.closed_at = Time.now
self.defunct = true
self.save
end
@@ -141,9 +148,13 @@
# to_id(person.id) # person.id
def to_id(model)
model.respond_to?(:id) ? model.id : model
end
+ ##
+ # 将模型对象或者 code 转换成 code 。如:
+ # to_id(category) # category.id
+ # to_id(category.code) # category.id
def to_code(code)
code.respond_to?(:code) ? code.code : code
end
end