Class: Gluttonberg::Content::ImportExportCSV::ExportUtils
- Inherits:
-
Object
- Object
- Gluttonberg::Content::ImportExportCSV::ExportUtils
- Defined in:
- lib/gluttonberg/content/import_export_csv.rb
Overview
ClassMethods
Instance Attribute Summary collapse
-
#all_records ⇒ Object
Returns the value of attribute all_records.
-
#export_column_names ⇒ Object
Returns the value of attribute export_column_names.
-
#export_wysiwyg_columns ⇒ Object
Returns the value of attribute export_wysiwyg_columns.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#local_options ⇒ Object
Returns the value of attribute local_options.
Class Method Summary collapse
Instance Method Summary collapse
- #_prepare_names(source, default) ⇒ Object
- #all_export_columns ⇒ Object
-
#initialize(all_records, local_options, klass) ⇒ ExportUtils
constructor
A new instance of ExportUtils.
- #prepare_export_column_names ⇒ Object
- #prepare_row(record) ⇒ Object
Constructor Details
#initialize(all_records, local_options, klass) ⇒ ExportUtils
Returns a new instance of ExportUtils.
81 82 83 84 85 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 81 def initialize(all_records, , klass) self.all_records = all_records self. = self.klass = klass end |
Instance Attribute Details
#all_records ⇒ Object
Returns the value of attribute all_records.
80 81 82 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 80 def all_records @all_records end |
#export_column_names ⇒ Object
Returns the value of attribute export_column_names.
80 81 82 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 80 def export_column_names @export_column_names end |
#export_wysiwyg_columns ⇒ Object
Returns the value of attribute export_wysiwyg_columns.
80 81 82 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 80 def export_wysiwyg_columns @export_wysiwyg_columns end |
#klass ⇒ Object
Returns the value of attribute klass.
80 81 82 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 80 def klass @klass end |
#local_options ⇒ Object
Returns the value of attribute local_options.
80 81 82 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 80 def @local_options end |
Class Method Details
.export(all_records, local_options, klass) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 87 def self.export(all_records, , klass) export_utils = ExportUtils.new(all_records, , klass) export_utils.prepare_export_column_names require 'csv' csv_string = CSV.generate do |csv| csv << export_utils.all_export_columns export_utils.all_records.each do |record| csv << export_utils.prepare_row(record) end end csv_string end |
Instance Method Details
#_prepare_names(source, default) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 113 def _prepare_names(source, default) if self. && self..has_key?(source) self.[source].dup else klass.send(default).dup end end |
#all_export_columns ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 121 def all_export_columns temp_columns = self.export_column_names.blank? ? [] : self.export_column_names.dup temp_columns << self.export_wysiwyg_columns.dup unless self.export_wysiwyg_columns.blank? temp_columns << :published_at temp_columns << :updated_at temp_columns = temp_columns.flatten temp_columns.uniq! temp_columns end |
#prepare_export_column_names ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 101 def prepare_export_column_names self.export_column_names = _prepare_names(:export_columns, :import_export_columns) self.export_wysiwyg_columns = _prepare_names(:wysiwyg_columns, :wysiwyg_columns) if self.export_column_names.blank? raise "Please define export_column_names property" end self.export_column_names end |
#prepare_row(record) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 131 def prepare_row(record) row = [] self.all_export_columns.each do |column| if self.export_wysiwyg_columns && self.export_wysiwyg_columns.include?(column) if record.send(column).blank? row << "" else val = record.send(column) if val.is_a? String row << val.html_safe elsif val.is_a? String row << val.collect{|v| v.is_a? String ? v.html_safe : v} else row << val end end else row << record.send(column) end end row.flatten end |