class Rexport::RexportModel

Attributes

klass[RW]
path[RW]

Public Class Methods

new(klass, path: nil) click to toggle source
# 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

Public Instance Methods

add_association_methods(options = {}) click to toggle source

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
# 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 = {}) click to toggle source

Adds a data item to rexport_fields

# 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) click to toggle source
# 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) click to toggle source
# File lib/rexport/rexport_model.rb, line 23
def field_path(field_name)
  [path, field_name].compact * "."
end
filter_column(field) click to toggle source
# 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) click to toggle source

Returns an array of export methods corresponding with field_names

# 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) click to toggle source

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

# 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_fields() click to toggle source
# File lib/rexport/rexport_model.rb, line 15
def rexport_fields
  @rexport_fields ||= ActiveSupport::HashWithIndifferentAccess.new
end
rexport_fields_array() click to toggle source
# File lib/rexport/rexport_model.rb, line 19
def rexport_fields_array
  rexport_fields.values.sort
end

Protected Instance Methods

get_rexport_method(field_name) click to toggle source

Returns the export method for a given field_name

# File lib/rexport/rexport_model.rb, line 91
def get_rexport_method(field_name)
  rexport_fields[field_name]&.method || raise(NoMethodError)
end
get_rexport_model(associations) click to toggle source

Returns a rexport_model for the associated class by following the chain of associations

# File lib/rexport/rexport_model.rb, line 79
def get_rexport_model(associations)
  associations.empty? ? self : rexport_models[associations.dup]
end
rexport_models() click to toggle source

Memoize rexport_models to avoid initializing rexport_fields multiple times

# File lib/rexport/rexport_model.rb, line 84
def rexport_models
  @rexport_models ||= Hash.new do |hash, key|
    hash[key] = self.class.new(klass.get_klass_from_associations(key))
  end
end

Private Instance Methods

add_rexport_fields_for(associations:, methods:, type:) click to toggle source
# File lib/rexport/rexport_model.rb, line 97
def add_rexport_fields_for(associations:, methods:, type:)
  associations.each do |association|
    methods.each do |method|
      add_rexport_field("#{association}_#{method}", method: "#{association}.#{method}", type: type)
    end
  end
end
foreign_key_for(association_name) click to toggle source
# File lib/rexport/rexport_model.rb, line 105
def foreign_key_for(association_name)
  klass.reflect_on_association(association_name).foreign_key if association_name.present?
end
initialize_rexport_fields() click to toggle source
# File lib/rexport/rexport_model.rb, line 109
def initialize_rexport_fields
  klass.content_columns.each { |field| add_rexport_field(field.name, type: field.type) }
  klass.initialize_local_rexport_fields(self) if klass.respond_to?(:initialize_local_rexport_fields)
end