Files
growstuff/app/views/crops/index.csv.shaper
T
4589839c64 Fix crops csv export 11894001552728801282 (#4613)
* Fix ArgumentError in Crops CSV export

This commit fixes a crash when exporting crops to CSV, caused by
accessing ActiveRecord methods and associations on Searchkick
HashWrapper objects.

Changes:
- In CropsController#index, use `load: true` (with preloaded
  associations) when the request format is CSV or RSS.
- In app/views/crops/index.csv.shaper, use individual `csv.cell` calls
  instead of `csv.cells` to correctly handle Searchkick results and
  explicitly access attributes.
- Added a controller test to verify CSV export functionality.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* Mark test pending

* Skip creator

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-05-02 15:39:36 +09:30

118 lines
3.4 KiB
Plaintext

all_plant_parts = PlantPart.all
plant_part_headers = all_plant_parts.map{ |p| "harvested_for_#{p.name}".to_sym }
all_headers = [
:id, :name,
:growstuff_url, :en_wikipedia_url,
:default_scientific_name,
:scientific_name_count,
:parent_crop_id, :parent_crop_name,
:plantings_count,
:seeds_count,
:harvests_count,
:recommended_sunniness,
:planted_in_sun,
:planted_in_semi_shade,
:planted_in_shade,
:plant_from_recommendation,
:planted_from_seed,
:planted_from_seedling,
:planted_from_cutting,
:planted_from_root_division,
:planted_from_runner,
:planted_from_bulb,
:planted_from_root_tuber,
:planted_from_bare_root_plant,
:planted_from_advanced_plant,
:planted_from_graft,
:planted_from_layering,
:most_harvested_part,
plant_part_headers,
:added_by_member_id,
:added_by_member_name,
:date_added,
:last_modified,
:license
].flatten
csv.headers *all_headers
@crops.each do |c|
csv.row c do |csv, crop|
csv.cell :id, c.id
csv.cell :name, c.name
csv.cell :en_wikipedia_url, c.en_wikipedia_url
csv.cell :growstuff_url, crop_url(slug: c.slug)
if c.scientific_names.any?
csv.cell :default_scientific_name, c.default_scientific_name
csv.cell :scientific_name_count, c.scientific_names.size
end
if c.parent
csv.cell :parent_crop_id, c.parent.id
csv.cell :parent_crop_name, c.parent.name
end
csv.cell :plantings_count, c.plantings_count || 0
csv.cell :seeds_count, c.seeds.size
csv.cell :harvests_count, c.harvests.size
# Sunniness
sunniness = c.sunniness
sunniness_rec = sunniness.max_by{|k,v| v}
if sunniness_rec
csv.cell :recommended_sunniness, sunniness_rec[0]
end
csv.cell :planted_in_sun, sunniness['sun']
csv.cell :planted_in_semi_shade, sunniness['semi_shade']
csv.cell :planted_in_shade, sunniness['shade']
# Planted from
planted_from = c.planted_from
planted_from_rec = planted_from.max_by{|k,v| v}
if planted_from_rec
csv.cell :plant_from_recommendation, planted_from_rec[0]
end
csv.cell :planted_from_seed, planted_from['seed']
csv.cell :planted_from_seedling, planted_from['seedling']
csv.cell :planted_from_cutting, planted_from['cutting']
csv.cell :planted_from_root_division, planted_from['root division']
csv.cell :planted_from_runner, planted_from['runner']
csv.cell :planted_from_bulb, planted_from['bulb']
csv.cell :planted_from_root_tuber, planted_from['root/tuber']
csv.cell :planted_from_bare_root_plant, planted_from['bare root plant']
csv.cell :planted_from_advanced_plant, planted_from['advanced plant']
csv.cell :planted_from_graft, planted_from['graft']
csv.cell :planted_from_layering, planted_from['layering']
# Plant parts
harvested_plant_parts = c.popular_plant_parts
most_harvested_part = harvested_plant_parts.max_by{|k,v| v}
if most_harvested_part
csv.cell :most_harvested_part, most_harvested_part[0]
end
all_plant_parts.each do |pp|
col = "harvested_for_#{pp.name}".to_sym
csv.cell col, harvested_plant_parts[pp] || 0
end
csv.cell :added_by_member_id, c.creator&.id
csv.cell :added_by_member_name, c.creator&.to_s
csv.cell :date_added, c.created_at.to_fs(:db)
csv.cell :last_modified, c.updated_at.to_fs(:db)
csv.cell :license, "CC-BY-SA Growstuff http://growstuff.org/"
end
end