Module: MakeExportable::ClassMethods
- Defined in:
- lib/make_exportable/core.rb
Instance Method Summary collapse
-
#create_report(format, data_set, options = {}) ⇒ Object
create_report
creates a report from a set of data. -
#exportable?(format = nil) ⇒ Boolean
<tt>exportable?<?tt> returns true if the class has called “make_exportable”.
-
#get_export_data(options = {}) ⇒ Object
get_export_data
finds records for export using a combination of the default export options and the argument options, and returns an array of arrays representing the rows and columns of the export data. -
#to_export(format, options = {}) ⇒ Object
to_export
exports records from a class.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments) ⇒ Object (private)
method_missing
allows the class to accept dynamically named methods such as: SomeClass.to_xls_export(), SomeClass.create_csv_report()
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/make_exportable/core.rb', line 194 def method_missing(method_id, *arguments) possible_formats = MakeExportable.exportable_formats.keys.map(&:to_s).join('|') if match = /^create_(#{possible_formats})_report$/.match(method_id.to_s) format = match.captures.first self.create_report(format, *arguments) elsif match = /^to_(#{possible_formats})_export$/.match(method_id.to_s) format = match.captures.first self.to_export(format, *arguments) else super end end |
Instance Method Details
#create_report(format, data_set, options = {}) ⇒ Object
create_report
creates a report from a set of data. It takes three arguments: a format, the data set to use for the report, and an optional hash of options. The only meaningful option is :headers which sets the strings to be used as column headers for the data set. The value of :headers can be:
-
true - headers are the first row in the data set
-
false - headers are not in the data set and should not be added
-
array of strings to use for the column headers
The length of the headers must match the length of each row in the data set.
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/make_exportable/core.rb', line 177 def create_report(format, data_set, ={}) if [:headers] === true [:headers] = data_set.shift end validate_export_format(format) validate_export_data_lengths(data_set, [:headers]) format_class = MakeExportable.exportable_formats[format.to_sym] formater = format_class.new(data_set, [:headers]) return formater.generate, formater.mime_type end |
#exportable?(format = nil) ⇒ Boolean
<tt>exportable?<?tt> returns true if the class has called “make_exportable”. This is overriding the default :exportable? in ActiveRecord::Base which always returns false. If a format is passed as an argument, returns true only if the format is allowed for this class.
113 114 115 116 |
# File 'lib/make_exportable/core.rb', line 113 def exportable?(format=nil) return [:formats].include?(format.to_sym) if format return true end |
#get_export_data(options = {}) ⇒ Object
get_export_data
finds records for export using a combination of the default export options and the argument options, and returns an array of arrays representing the rows and columns of the export data. The first item (“row”) in the array will be an array of strings to be used as column headers. Valid options include :only, :except, :scopes and the standard find options. See to_export
for more details on the options.
Example:
User.get_export_data(:only => [:first_name, :last_name, :username])
# => [['first_name', 'last_name', 'username'], ['John', 'Doe', 'johndoe'], ['Joe', 'Smith', 'jsmith']] }
161 162 163 164 165 166 |
# File 'lib/make_exportable/core.rb', line 161 def get_export_data(={}) = .slice(:only, :except) records = find_export_data() export_data = map_export_data(records, ) return export_data end |
#to_export(format, options = {}) ⇒ Object
to_export
exports records from a class. It can be called directly on an ActiveRecord class, but it can also be called on an ActiveRelation scope. It takes two arguments: a format (required) and a hash of options (optional).
The options include:
-
:only and :except - specify columns or methods to export
-
:scopes - specify scopes to be called on the class before exporting
-
find options - for Rails 2.3 and earlier compatibility, standard find options
are supported (:conditions, :order, :limit, :offset, etc.). These will be deprecated and removed in future versions.
-
:headers - supply an array of custom headers for the columns of exported attributes,
the sizes of the header array and the exported columns must be equal.
Examples:
User.to_export(:xml, :only => [:first_name, :last_name, :username],
:order => 'users.last_name ASC')
User.visible.sorted_by_username.to_export('csv',
:only => [:first_name, :last_name, :username])
139 140 141 142 143 144 145 146 147 |
# File 'lib/make_exportable/core.rb', line 139 def to_export(format, ={}) export_data = get_export_data() # remove the auto-headers from the export_data (i.e. the first row) auto_headers = export_data.shift # Use auto-headers unless given alternates or false (use no headers) [:headers] = auto_headers unless ![:headers].blank? || [:headers] === false export_string = create_report(format, export_data, :headers => [:headers]) return export_string end |