Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/controllers/crops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ def wrangle
respond_with @crops
end

def merge
authorize! :merge, Crop
if request.post?
master_crop = Crop.find_by(slug: params[:master_crop_slug])
duplicate_crop = Crop.find_by(slug: params[:duplicate_crop_slug])

if master_crop && duplicate_crop
if master_crop == duplicate_crop
flash[:alert] = "You cannot merge a crop with itself."
render :merge
else
master_crop.merge_with(duplicate_crop)
flash[:notice] = "Successfully merged #{duplicate_crop.name} into #{master_crop.name}."
redirect_to wrangle_crops_path
end
else
flash[:alert] = "Could not find one of the crops."
render :merge
end
else
# GET request, just render the form
end
end

def gbif
@crop = Crop.find(params[:crop_slug])
@crop.update_gbif_data!
Expand Down
1 change: 1 addition & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def member_abilities(member)
# only crop wranglers can create/edit/destroy crops
if member.role? :crop_wrangler
can :wrangle, Crop
can :merge, Crop
can :manage, Crop
can :manage, CropCompanion
can :manage, ScientificName
Expand Down
19 changes: 19 additions & 0 deletions app/models/crop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ def all_companions
(companions + parent.companions).uniq
end

def merge_with(other_crop)
Crop.transaction do
other_crop.alternate_names.update_all(crop_id: id)
other_crop.scientific_names.update_all(crop_id: id)
other_crop.plantings.update_all(crop_id: id)
other_crop.seeds.update_all(crop_id: id)
other_crop.harvests.update_all(crop_id: id)
other_crop.photo_associations.update_all(crop_id: id)
other_crop.varieties.update_all(parent_id: id)
other_crop.crop_posts.update_all(crop_id: id)

# Companions can be crop_a or crop_b
CropCompanion.where(crop_a_id: other_crop.id).update_all(crop_a_id: id)
CropCompanion.where(crop_b_id: other_crop.id).update_all(crop_b_id: id)

other_crop.destroy
end
end

private

def count_uses_of_property(col_name)
Expand Down
14 changes: 14 additions & 0 deletions app/views/crops/merge.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- content_for :title, "Merge Crops"

%h1 Merge Crops

= form_tag merge_crops_path, method: :post do
.form-group
= label_tag :master_crop_slug, "Master Crop (the one to keep)"
= text_field_tag :master_crop_slug, nil, class: 'form-control'

.form-group
= label_tag :duplicate_crop_slug, "Duplicate Crop (the one to delete)"
= text_field_tag :duplicate_crop_slug, nil, class: 'form-control'

= submit_tag "Merge", class: 'btn btn-primary'
1 change: 1 addition & 0 deletions app/views/crops/wrangle.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

%nav.nav
= link_to "Full crop hierarchy", hierarchy_crops_path, class: 'nav-link'
= link_to "Merge Duplicates", merge_crops_path, class: 'btn'
= link_to "Add Crop", new_crop_path, class: 'btn'

%section.crop_wranglers
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
collection do
get 'requested'
get 'wrangle'
get 'merge'
post 'merge'
get 'hierarchy'
get 'search'
end
Expand Down
Loading