Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/fat_free_crm/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#to_csvObject

CSV export. Based on to_csv Rails plugin by Ary Djmal github.com/arydjmal/to_csv




81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fat_free_crm/core_ext/array.rb', line 81

def to_csv
  return '' if empty?

  klass = first.class
  columns = klass.columns.map(&:name).reject { |column| column =~ /password|token/ }

  CSV.generate do |csv|
    csv << columns.map { |column| klass.human_attribute_name(column) }
    each do |item|
      csv << columns.map { |column| item.send(column) }
    end
  end
end

#to_xlsObject

XLS export. Based on to_xls Rails plugin by Ary Djmal github.com/arydjmal/to_xls




49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fat_free_crm/core_ext/array.rb', line 49

def to_xls
  output =  '<?xml version="1.0" encoding="UTF-8"?><Workbook xmlns:x="urn:schemas-microsoft-com:office:excel"'
  output << ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"'
  output << ' xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office">'
  output << '<Worksheet ss:Name="Sheet1"><Table>'

  if any?
    klass = first.class
    columns = klass.columns.map(&:name).reject { |column| column =~ /deleted_at|password|token/ }

    output << columns.map do |column|
      klass.human_attribute_name(column).wrap('<Cell><Data ss:Type="String">', '</Data></Cell>')
    end.join.wrap('<Row>', '</Row>')

    each do |item|
      output << columns.map do |column|
        value = if column =~ /^(user_id|assigned_to|completed_by)$/ && item.respond_to?(:"#{$1}_full_name")
          item.send(:"#{$1}_full_name")
        else
          item.send(column)
        end
        value.to_s.wrap(%Q|<Cell><Data ss:Type="#{value.respond_to?(:abs) ? 'Number' : 'String'}">|, '</Data></Cell>')
      end.join.wrap('<Row>', '</Row>')
    end
  end

  output << '</Table></Worksheet></Workbook>'
end

#visible_to(user) ⇒ Object

NOTE: in ActiveRecord 2.x #visible_to was mixed into class ActiveRecord::NamedScope::Scope but with AREL scope it must be Array.

The following is used to filter out user activities based on activity subject’s permissions. For example:

Note that we can’t use named scope for the Activity since the join table name is based on subject type, which is polymorphic.




32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fat_free_crm/core_ext/array.rb', line 32

def visible_to(user)
  delete_if do |item|
    is_private = false
    if item.is_a?(Activity)
      subject = item.subject || Version.with_item_keys(item.subject_type, item.subject_id).last.reify
      if subject.respond_to?(:access) # NOTE: Tasks don't have :access as of yet.
        is_private = subject.user_id != user.id && subject.assigned_to != user.id &&
          (subject.access == "Private" || (subject.access == "Shared" && !subject.permissions.map(&:user_id).include?(user.id)))
      end
    end
    is_private
  end
end