Module: Rexport::DataFields::ClassMethods

Defined in:
lib/rexport/data_fields.rb

Instance Method Summary collapse

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
64
65
66
67
68
69
70
71
# File 'lib/rexport/data_fields.rb', line 54

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

  methods = options.reverse_merge('methods' => 'name')['methods']
  methods = [methods] if methods.kind_of?(String)

  associations = options['associations']
  associations = [associations] if associations.kind_of?(String)

  type = options['filter'] ? :association : nil

  associations.each do |association|
    methods.each do |method|
      add_rexport_field("#{association}_#{method}", :method => "#{association}.#{method}", :type => type)
    end
  end
end

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

Adds a data item to rexport_fields



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

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

#get_klass_from_associations(*associations) ⇒ Object

Returns the associated class by following the associations



93
94
95
96
97
# File 'lib/rexport/data_fields.rb', line 93

def get_klass_from_associations(*associations)
  associations.flatten!
  return self if associations.empty?
  reflect_on_association(associations.shift.to_sym).klass.get_klass_from_associations(associations)
end

#get_rexport_method(field_name) ⇒ Object

Returns the export method for a given field_name

Raises:

  • (NoMethodError)


100
101
102
103
104
105
106
107
# File 'lib/rexport/data_fields.rb', line 100

def get_rexport_method(field_name)
  raise NoMethodError unless rexport_fields[field_name] or dynamic_rexport_fields[field_name]
  if rexport_fields[field_name]
    rexport_fields[field_name].method
  else
    dynamic_rexport_fields[field_name].method
  end
end

#get_rexport_methods(*field_names) ⇒ Object

Returns an array of export methods corresponding with field_names



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

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

#remove_rexport_fields(*fields) ⇒ Object

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



75
76
77
# File 'lib/rexport/data_fields.rb', line 75

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

#reset_column_information_with_rexport_resetObject



109
110
111
112
# File 'lib/rexport/data_fields.rb', line 109

def reset_column_information_with_rexport_reset
  reset_column_information_without_rexport_reset
  @rexport_fields = nil
end

#rexport_fieldsObject

Returns hash of exportable data items



32
33
34
35
36
37
38
# File 'lib/rexport/data_fields.rb', line 32

def rexport_fields
  unless @rexport_fields
    @rexport_fields = HashWithIndifferentAccess.new
    initialize_rexport_fields
  end
  @rexport_fields
end

#rexport_fields_arrayObject

Returns sorted array of rexport DataFields



41
42
43
# File 'lib/rexport/data_fields.rb', line 41

def rexport_fields_array
  rexport_fields.merge(dynamic_rexport_fields).values.sort
end