Class: Array
Instance Method Summary collapse
- #select_with_index(&block) ⇒ Object
-
#to_auto_complete_list(opts = {}) ⇒ Object
to_auto_complete_list(:label => [“? (?)”,:auditable_type,:auditable_id], :value => [“?-?”,:auditable_type,:auditable_id]).
-
#to_csv(columns) ⇒ Object
for backwards compatability this method gets overritten by to_csv in 1.9 core so will only be used by ruby 1.8 apps TODO: remove Array.to_csv method when ruby 1.8 no longer needed.
-
#to_csv_with_columns(columns) ⇒ Object
transforms the collection into a csv formatted string.
Instance Method Details
#select_with_index(&block) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/itrigga/core_ext/array.rb', line 43 def select_with_index(&block) selected = [] self.each_index do |i| selected << self[i] if block.call(self[i],i) == true end selected end |
#to_auto_complete_list(opts = {}) ⇒ Object
to_auto_complete_list(:label => [“? (?)”,:auditable_type,:auditable_id], :value => [“?-?”,:auditable_type,:auditable_id])
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/itrigga/core_ext/array.rb', line 55 def to_auto_complete_list(opts = {}) if opts.blank? || opts[:label].blank? || opts[:value].blank? || (opts[:label].kind_of?(Array) && opts[:label].size < 2) return [] end list = collect do |obj| [active_record_type_condition_rendering(opts[:label],obj), active_record_type_condition_rendering(opts[:value],obj)] end # a :include_blank key # if it == true then just include a blank array (ie all elements) # if it == Array then render it using the AR rendering eg ["?",property] if opts[:include_blank] == true list.unshift(["All",""]) elsif opts[:include_blank] list = collect {|obj| [active_record_type_condition_rendering(opts[:include_blank],obj), active_record_type_condition_rendering(opts[:include_blank],obj)] }.uniq + list end list.compact.uniq end |
#to_csv(columns) ⇒ Object
for backwards compatability this method gets overritten by to_csv in 1.9 core so will only be used by ruby 1.8 apps TODO: remove Array.to_csv method when ruby 1.8 no longer needed
38 39 40 |
# File 'lib/itrigga/core_ext/array.rb', line 38 def to_csv(columns) to_csv_with_columns(columns) end |
#to_csv_with_columns(columns) ⇒ Object
transforms the collection into a csv formatted string
each column is described by a label/property pair eg [[“Published”,“created_at”]] generates a column with label “Published” and the data value being the created_at field
Because ruby 1.9 includes FasterCSV in the core we should use that if we can
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/itrigga/core_ext/array.rb', line 12 def to_csv_with_columns(columns) if Gem::Version.new(''+RUBY_VERSION) >= Gem::Version.new("1.9.0") # 1.9 version CSV.generate do |csv| csv << columns.collect{|c| c.to_a.first } self.each do |result| csv << columns.collect{|c| c.to_a.last }.collect{|c| result.send(c) } end end else # 1.8 version FasterCSV.generate do |csv| csv << columns.collect{|c| c.to_a.first } self.each do |result| csv << columns.collect{|c| c.to_a.last }.collect{|c| result.send(c) } end end end end |