Module: ActivemodelObjectInfo::TableDefinition
- Defined in:
- lib/activemodel_object_info/table_definition.rb
Overview
重新扩展一些方法,用来方便在创建迁移文件的时候进行一些通用类的设定。 从 @version 0.3.0 开始支持了自动创建的时间戳字段增加索引
Instance Method Summary collapse
-
#generate_operations(*operations) ⇒ Object
生成对应操作的字段,包括操作人和操作时间戳,均使用默认设置。.
-
#operation_columns(*fields, **options) ⇒ Object
生成操作相关的字段。包含操作人、操作时间等信息。.
Instance Method Details
#generate_operations(*operations) ⇒ Object
生成对应操作的字段,包括操作人和操作时间戳,均使用默认设置。
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/activemodel_object_info/table_definition.rb', line 70 def generate_operations(*operations) # puts "generate_operations(#{operations})" action_fields = operations # 默认只有创建、更新和删除相关的字段 action_fields = %w[created updated deleted] if action_fields.empty? action_fields.each do |operation| column(operation.to_s, :integer, default: 0, comment: '删除标记') if operation.to_sym == :deleted operation_columns(operation) end end |
#operation_columns(*fields, **options) ⇒ Object
生成操作相关的字段。包含操作人、操作时间等信息。
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/activemodel_object_info/table_definition.rb', line 39 def operation_columns(*fields, **) # puts "operation_columns(#{fields}, #{options})" # 初始化设置选项 with_operator = [:with_operator].nil? ? true : [:with_operator] = [:with_timestamp].nil? ? true : [:with_timestamp] operator_prefix = [:operator_prefix] operator_suffix = [:operator_suffix] || '_by' = [:timestamp_prefix] = [:timestamp_suffix] || '_at' # 依照每个字段进行设置 fields.each do |field| # 如果要生成操作人信息 if with_operator operator_column_name = operator_prefix.to_s + field.to_s + operator_suffix.to_s column(operator_column_name, :bigint, index: true, comment: '操作人') if operator_column_name.present? end # 如果要生成操作时间戳信息 if = .to_s + field.to_s + .to_s column(, :datetime, index: true, comment: '操作时间戳') if .present? end end end |