Module: Rexport::ExportMethods::InstanceMethods

Defined in:
lib/rexport/export_methods.rb

Instance Method Summary collapse

Instance Method Details

#copyObject



155
156
157
158
159
160
# File 'lib/rexport/export_methods.rb', line 155

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

#enabled?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/rexport/export_methods.rb', line 167

def enabled?
  export_model.count > 0
end

#export_filter_attributes=(attributes) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rexport/export_methods.rb', line 136

def export_filter_attributes=(attributes)
  attributes.each do |field, value|
    if value.blank?
      filter = export_filters.find_by(filter_field: field)
      filter.destroy if filter
    elsif new_record?
      export_filters.build(:filter_field => field, :value => value)
    else
      filter = export_filters.find_or_create_by(filter_field: field)
      filter.update_attribute(:value, value)
    end
  end
end

#export_modelObject

Returns the export model class



97
98
99
# File 'lib/rexport/export_methods.rb', line 97

def export_model
  model_name.constantize
end

#filter_value(filter_field) ⇒ Object



150
151
152
153
# File 'lib/rexport/export_methods.rb', line 150

def filter_value(filter_field)
  filter = export_filters.detect {|f| f.filter_field == filter_field}
  filter ? filter.value : nil
end

#full_nameObject



66
67
68
# File 'lib/rexport/export_methods.rb', line 66

def full_name
  "#{model_name.pluralize} - #{name}"
end

#get_klass_from_path(path, klass = export_model) ⇒ Object

Returns a class based on a path array



117
118
119
120
# File 'lib/rexport/export_methods.rb', line 117

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

Returns:

  • (Boolean)


122
123
124
# File 'lib/rexport/export_methods.rb', line 122

def has_rexport_field?(rexport_field)
  rexport_fields.include?(rexport_field)
end

#headerObject

Returns an array with the header names from the associated export_items



92
93
94
# File 'lib/rexport/export_methods.rb', line 92

def header
  export_items.ordered.map {|i| i.name}
end

#modifiable?Boolean

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/rexport/export_methods.rb', line 162

def modifiable?
  # override to disable edit and destroy links for specific exports
  true
end

#recordsObject

Returns the records for the export



107
108
109
# File 'lib/rexport/export_methods.rb', line 107

def records
  @records ||= get_records
end

#rexport_fields=(rexport_fields) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/rexport/export_methods.rb', line 126

def rexport_fields=(rexport_fields)
  @rexport_fields = if rexport_fields.respond_to?(:keys)
    @set_position = false
    rexport_fields.keys
  else
    @set_position = true
    rexport_fields
  end
end

#rexport_modelsObject

Returns an array of RexportModels including export_model and associated rexport capable models



102
103
104
# File 'lib/rexport/export_methods.rb', line 102

def rexport_models
  @rexport_models ||= get_rexport_models(export_model)
end

#sample_recordsObject

Returns a limited number of records for the export



112
113
114
# File 'lib/rexport/export_methods.rb', line 112

def sample_records
  get_records(Rexport::SAMPLE_SIZE)
end

#to_csv(objects = nil) ⇒ Object

Returns a csv string with the export data



81
82
83
84
85
86
87
88
89
# File 'lib/rexport/export_methods.rb', line 81

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_sObject

Returns a string with the export data



71
72
73
74
75
76
77
78
# File 'lib/rexport/export_methods.rb', line 71

def to_s
  String.new.tap do |result|
    result << header * '|' << "\n"
    records.each do |record|
      result << record * '|' << "\n"
    end
  end
end