Module: Rexport::ExportMethods
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/rexport/export_methods.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #copy ⇒ Object
- #export_filter_attributes=(attributes) ⇒ Object
-
#export_model ⇒ Object
Returns the export model class.
- #filter_value(filter_field) ⇒ Object
- #full_name ⇒ Object
-
#get_klass_from_path(path, klass = export_model) ⇒ Object
Returns a class based on a path array.
-
#has_rexport_field?(rexport_field) ⇒ Boolean
rubocop:disable Naming/PredicateName.
-
#header ⇒ Object
Returns an array with the header names from the associated export_items.
-
#records ⇒ Object
Returns the records for the export.
- #rexport_field?(rexport_field) ⇒ Boolean
-
#rexport_fields=(fields) ⇒ Object
Stores rexport_field names to update the export_items association after save Expects fields to be a hash with field names as the keys or an array of field names: { “field_one” => “1”, “field_two” => “1” } [“field_one”, “field_two”].
-
#rexport_model ⇒ Object
Returns a RexportModel for the current export_model.
-
#rexport_models ⇒ Object
Returns an array of RexportModels including export_model and associated rexport capable models.
-
#sample_records ⇒ Object
Returns a limited number of records for the export.
-
#to_csv(objects = nil) ⇒ Object
Returns a csv string with the export data.
-
#to_s ⇒ Object
Returns a string with the export data.
Instance Method Details
#copy ⇒ Object
117 118 119 120 121 122 |
# File 'lib/rexport/export_methods.rb', line 117 def copy self.class.create(attributes_for_copy) do |new_export| export_items.ordered.each { |item| new_export.export_items.build(item.attributes_for_copy) } export_filters.each { |filter| new_export.export_filters.build(filter.attributes_for_copy) } end end |
#export_filter_attributes=(attributes) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rexport/export_methods.rb', line 101 def export_filter_attributes=(attributes) attributes.each do |field, value| if value.blank? export_filters.find_by(filter_field: field)&.destroy elsif new_record? export_filters.build(filter_field: field, value: value) else export_filters.find_or_create_by(filter_field: field).update_attribute(:value, value) end end end |
#export_model ⇒ Object
Returns the export model class
53 54 55 |
# File 'lib/rexport/export_methods.rb', line 53 def export_model model_class_name.constantize end |
#filter_value(filter_field) ⇒ Object
113 114 115 |
# File 'lib/rexport/export_methods.rb', line 113 def filter_value(filter_field) export_filters.detect { |f| f.filter_field == filter_field }&.value end |
#full_name ⇒ Object
27 28 29 |
# File 'lib/rexport/export_methods.rb', line 27 def full_name "#{model_class_name.pluralize} - #{name}" end |
#get_klass_from_path(path, klass = export_model) ⇒ Object
Returns a class based on a path array
78 79 80 81 82 |
# File 'lib/rexport/export_methods.rb', line 78 def get_klass_from_path(path, klass = export_model) return klass unless (association_name = path.shift) get_klass_from_path(path, klass.reflect_on_association(association_name.to_sym).klass) end |
#has_rexport_field?(rexport_field) ⇒ Boolean
rubocop:disable Naming/PredicateName
84 85 86 87 |
# File 'lib/rexport/export_methods.rb', line 84 def has_rexport_field?(rexport_field) # rubocop:disable Naming/PredicateName ActiveSupport::Deprecation.warn("Calling #has_rexport_field? is deprecated. Use #rexport_field? instead") rexport_field?(rexport_field) end |
#header ⇒ Object
Returns an array with the header names from the associated export_items
48 49 50 |
# File 'lib/rexport/export_methods.rb', line 48 def header export_items.ordered.map(&:name) end |
#records ⇒ Object
Returns the records for the export
68 69 70 |
# File 'lib/rexport/export_methods.rb', line 68 def records @records ||= get_records end |
#rexport_field?(rexport_field) ⇒ Boolean
89 90 91 |
# File 'lib/rexport/export_methods.rb', line 89 def rexport_field?(rexport_field) rexport_fields.include?(rexport_field) end |
#rexport_fields=(fields) ⇒ Object
Stores rexport_field names to update the export_items association after save Expects fields to be a hash with field names as the keys or an array of field names: { “field_one” => “1”, “field_two” => “1” }
- “field_one”, “field_two”
97 98 99 |
# File 'lib/rexport/export_methods.rb', line 97 def rexport_fields=(fields) @rexport_fields = extract_rexport_fields(fields).map(&:to_s) end |
#rexport_model ⇒ Object
Returns a RexportModel for the current export_model
58 59 60 |
# File 'lib/rexport/export_methods.rb', line 58 def rexport_model @rexport_model ||= RexportModel.new(export_model) end |
#rexport_models ⇒ Object
Returns an array of RexportModels including export_model and associated rexport capable models
63 64 65 |
# File 'lib/rexport/export_methods.rb', line 63 def rexport_models @rexport_models ||= get_rexport_models(export_model) end |
#sample_records ⇒ Object
Returns a limited number of records for the export
73 74 75 |
# File 'lib/rexport/export_methods.rb', line 73 def sample_records get_records(Rexport::SAMPLE_SIZE) end |
#to_csv(objects = nil) ⇒ Object
Returns a csv string with the export data
37 38 39 40 41 42 43 44 45 |
# File 'lib/rexport/export_methods.rb', line 37 def to_csv(objects = nil) seed_records(objects) unless objects.nil? CSV.generate do |csv| csv << header records.each do |record| csv << record end end end |
#to_s ⇒ Object
Returns a string with the export data
32 33 34 |
# File 'lib/rexport/export_methods.rb', line 32 def to_s records.unshift(header).map { |line| line.join("|") }.join("\n") end |