Merge pull request #4259 from Growstuff/feature/companion-plantings

Add ability to create companion plantings
This commit is contained in:
Daniel O'Connor
2025-09-21 10:37:52 +09:30
committed by GitHub
9 changed files with 85 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
# frozen_string_literal: true
module Admin
class CropCompanionsController < AdminController
before_action :set_crop
def index
@crop_companions = @crop.crop_companions
end
def new
@crop_companion = @crop.crop_companions.new
end
def create
@crop_companion = @crop.crop_companions.new(crop_companion_params)
if @crop_companion.save
redirect_to admin_crop_crop_companions_path(@crop), notice: 'Companion was successfully created.'
else
render :new
end
end
def destroy
@crop_companion = @crop.crop_companions.find(params[:id])
@crop_companion.destroy
redirect_to admin_crop_crop_companions_path(@crop), notice: 'Companion was successfully destroyed.'
end
private
def set_crop
@crop = Crop.find_by!(slug: params[:crop_slug])
end
def crop_companion_params
params.require(:crop_companion).permit(:crop_b_id, :source_url)
end
end
end

View File

@@ -76,6 +76,7 @@ class Ability
if member.role? :crop_wrangler
can :wrangle, Crop
can :manage, Crop
can :manage, CropCompanion
can :manage, ScientificName
can :manage, AlternateName
can :openfarm, Crop

View File

@@ -153,6 +153,12 @@ class Crop < ApplicationRecord
where(["lower(crops.name) = :value", { value: name.downcase }])
end
def all_companions
return companions unless parent
(companions + parent.companions).uniq
end
private
def count_uses_of_property(col_name)

View File

@@ -0,0 +1,16 @@
%h1= "Companions for #{@crop.name}"
= link_to 'New Companion', new_admin_crop_crop_companion_path(@crop), class: 'btn btn-primary'
%table.table
%thead
%tr
%th Name
%th Source URL
%th Actions
%tbody
- @crop_companions.each do |companion|
%tr
%td= companion.crop_b.name
%td= companion.source_url
%td= link_to 'Delete', admin_crop_crop_companion_path(@crop, companion), method: :delete, data: { confirm: 'Are you sure?' }

View File

@@ -0,0 +1,6 @@
%h1= "New Companion for #{@crop.name}"
= bootstrap_form_for [:admin, @crop, @crop_companion] do |f|
= f.collection_select :crop_b_id, Crop.order(:name), :id, :name, { label: 'Companion' }
= f.text_field :source_url, label: 'Source URL'
= f.submit 'Create'

View File

@@ -14,6 +14,10 @@
= icon 'far', 'update'
Fetch data from GBIF
= link_to admin_crop_crop_companions_path(crop), class: 'dropdown-item' do
= icon 'fas', 'leaf'
Manage Companions
- if can? :destroy, crop
.dropdown-divider
= delete_button(crop, classes: 'dropdown-item text-danger')

View File

@@ -24,10 +24,10 @@
%section.prediction
= cute_icon
= render 'predictions', crop: @crop
- if @crop.companions.any?
- if @crop.all_companions.any?
%section.companions
%h2 Companions
- @crop.companions.each do |companion|
- @crop.all_companions.each do |companion|
= render 'crops/tiny', crop: companion
%section.photos

View File

@@ -138,6 +138,9 @@ Rails.application.routes.draw do
namespace :admin do
resources :members, param: :slug
resources :roles
resources :crops, param: :slug do
resources :crop_companions
end
end
namespace :api do

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddSourceUrlToCropCompanions < ActiveRecord::Migration[6.1]
def change
add_column :crop_companions, :source_url, :string
end
end