Class: Rexport::RexportModel

Inherits:
Object
  • Object
show all
Defined in:
lib/rexport/rexport_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, path: nil) ⇒ RexportModel

Returns a new instance of RexportModel.



9
10
11
12
13
# File 'lib/rexport/rexport_model.rb', line 9

def initialize(klass, path: nil)
  self.klass = klass
  self.path = path&.to_s
  initialize_rexport_fields
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



5
6
7
# File 'lib/rexport/rexport_model.rb', line 5

def klass
  @klass
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/rexport/rexport_model.rb', line 5

def path
  @path
end

Instance Method Details

#add_association_methods(options = {}) ⇒ Object

Adds associated methods to rexport_fields

:associations - an association or arrary of associations
:methods - a method or array of methods
:filter - if true will send type: :association to add_report_field


54
55
56
57
58
59
60
61
62
63
# File 'lib/rexport/rexport_model.rb', line 54

def add_association_methods(options = {})
  options.stringify_keys!
  options.assert_valid_keys(%w[associations methods filter])

  add_rexport_fields_for(
    associations: [options["associations"]].flatten,
    methods:      [options["methods"] || "name"].flatten,
    type:         (options["filter"] ? :association : nil)
  )
end

#add_rexport_field(name, options = {}) ⇒ Object

Adds a data item to rexport_fields



40
41
42
# File 'lib/rexport/rexport_model.rb', line 40

def add_rexport_field(name, options = {})
  rexport_fields[name.to_s] = DataField.new(name, options)
end

#collection_from_association(association) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rexport/rexport_model.rb', line 27

def collection_from_association(association)
  if klass.respond_to?("find_#{association}_for_rexport")
    klass.public_send("find_#{association}_for_rexport")
  else
    klass.reflect_on_association(association.to_sym).klass.all
  end
end

#field_path(field_name) ⇒ Object



23
24
25
# File 'lib/rexport/rexport_model.rb', line 23

def field_path(field_name)
  [path, field_name].compact * "."
end

#filter_column(field) ⇒ Object



35
36
37
# File 'lib/rexport/rexport_model.rb', line 35

def filter_column(field)
  foreign_key_for(field.association_name) || field.method
end

#get_rexport_methods(*field_names) ⇒ Object

Returns an array of export methods corresponding with field_names



66
67
68
69
70
71
72
73
74
# File 'lib/rexport/rexport_model.rb', line 66

def get_rexport_methods(*field_names)
  field_names.flatten.map do |f|
    components = f.to_s.split(".")
    field_name = components.pop
    components.push(get_rexport_model(components).get_rexport_method(field_name)) * "."
  rescue NoMethodError
    "undefined_rexport_field"
  end
end

#remove_rexport_fields(*fields) ⇒ Object

Removes files from rexport_fields useful to remove content columns you don’t want included in exports



46
47
48
# File 'lib/rexport/rexport_model.rb', line 46

def remove_rexport_fields(*fields)
  fields.flatten.each { |field| rexport_fields.delete(field.to_s) }
end

#rexport_fieldsObject



15
16
17
# File 'lib/rexport/rexport_model.rb', line 15

def rexport_fields
  @rexport_fields ||= ActiveSupport::HashWithIndifferentAccess.new
end

#rexport_fields_arrayObject



19
20
21
# File 'lib/rexport/rexport_model.rb', line 19

def rexport_fields_array
  rexport_fields.values.sort
end