Class: Excelizer::Base
- Inherits:
-
Object
- Object
- Excelizer::Base
- Defined in:
- lib/excelizer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build_xls(collection = model_class.all) ⇒ Object
- #get_collection_data(collection = model_class.all) ⇒ Object
- #model_class ⇒ Object
- #model_clean_attributes(model_class_ref) ⇒ Object
Class Method Details
.attr_downloadable(*attrs) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/excelizer.rb', line 6 def self.attr_downloadable(*attrs) attrs.each do |attr| define_method(attr) do eval "@#{attr}" end end end |
Instance Method Details
#build_xls(collection = model_class.all) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/excelizer.rb', line 14 def build_xls(collection=model_class.all) headers = *methods.select { |m| self.method(m).owner == self.class }.map { |m| m.to_s.titleize } records = get_collection_data(collection) Excelizer::Writer.write headers, records end |
#get_collection_data(collection = model_class.all) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/excelizer.rb', line 21 def get_collection_data(collection=model_class.all) model_methods = model_clean_attributes model_class # Gets the methods in the class definition and attr_downloadable own_methods = methods.select { |m| self.method(m).owner == self.class } # If the model class has the same method as the one defined in # attr_downloadble, that method will be called. collection.map do |child_instance| @model = child_instance own_methods.map do |attr| # Checks if the model has a method with the same name and # if the class definition overrides the model method reciever = self if model_methods.include?(attr.to_s) && reciever.send(attr).nil? reciever = child_instance end reciever.send(attr) end.compact end end |
#model_class ⇒ Object
43 44 45 |
# File 'lib/excelizer.rb', line 43 def model_class Object.const_get self.class.name.gsub "Downloader", "" end |
#model_clean_attributes(model_class_ref) ⇒ Object
47 48 49 |
# File 'lib/excelizer.rb', line 47 def model_clean_attributes(model_class_ref) model_class_ref.new.attributes.keys - model_class_ref.protected_attributes.to_a end |