Commit d92af47e by liyijie

Add common concern with model_headers default

parent 83431f12
...@@ -4,6 +4,7 @@ require 'axlsx' ...@@ -4,6 +4,7 @@ require 'axlsx'
module TalltyImportExport module TalltyImportExport
class Error < StandardError; end class Error < StandardError; end
autoload :Common, 'tallty_import_export/common'
autoload :Exportable, 'tallty_import_export/exportable' autoload :Exportable, 'tallty_import_export/exportable'
autoload :Importable, 'tallty_import_export/importable' autoload :Importable, 'tallty_import_export/importable'
end end
module TalltyImportExport
module Common
extend ActiveSupport::Concern
included do
end
module ClassMethods
def model_headers
columns.map do |column|
{
key: column.name,
name: column.comment || column.name,
attr_type: column.type,
format: column.type == :string ? :string : nil,
}
end
end
end
end
end
...@@ -2,6 +2,8 @@ module TalltyImportExport ...@@ -2,6 +2,8 @@ module TalltyImportExport
module Exportable module Exportable
extend ActiveSupport::Concern extend ActiveSupport::Concern
include Common
# export_headers / headers # export_headers / headers
# key: 属性的英文名 # key: 属性的英文名
# name: 属性的中文名 # name: 属性的中文名
...@@ -56,7 +58,7 @@ module TalltyImportExport ...@@ -56,7 +58,7 @@ module TalltyImportExport
@row_height ||= options.delete(:row_height) || 35 @row_height ||= options.delete(:row_height) || 35
@width ||= options.delete(:width) || 30 @width ||= options.delete(:width) || 30
@filename ||= options.delete(:filename) @filename ||= options.delete(:filename)
@headers = self.respond_to?(:export_headers) ? export_headers : headers @headers = self.try(:export_headers) || self.try(:headers) || self.try(:model_headers)
@headers.map! {|header| header.with_indifferent_access} @headers.map! {|header| header.with_indifferent_access}
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment