diff --git a/app/controllers/admin/crop_companions_controller.rb b/app/controllers/admin/crop_companions_controller.rb new file mode 100644 index 000000000..1ca9122a8 --- /dev/null +++ b/app/controllers/admin/crop_companions_controller.rb @@ -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 diff --git a/app/models/ability.rb b/app/models/ability.rb index 9d80af4cf..4bce19085 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -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 diff --git a/app/models/crop.rb b/app/models/crop.rb index eb7285891..a647c766f 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -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) diff --git a/app/views/admin/crop_companions/index.html.haml b/app/views/admin/crop_companions/index.html.haml new file mode 100644 index 000000000..79d3a8706 --- /dev/null +++ b/app/views/admin/crop_companions/index.html.haml @@ -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?' } diff --git a/app/views/admin/crop_companions/new.html.haml b/app/views/admin/crop_companions/new.html.haml new file mode 100644 index 000000000..d060afd04 --- /dev/null +++ b/app/views/admin/crop_companions/new.html.haml @@ -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' diff --git a/app/views/crops/_wrangle.html.haml b/app/views/crops/_wrangle.html.haml index 10ce04bdc..92dd9c3b3 100644 --- a/app/views/crops/_wrangle.html.haml +++ b/app/views/crops/_wrangle.html.haml @@ -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') diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 25edfc4c8..5ab1525b8 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 3b68bc8f1..0cb18fb3f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20231026120000_add_source_url_to_crop_companions.rb b/db/migrate/20231026120000_add_source_url_to_crop_companions.rb new file mode 100644 index 000000000..b552c495b --- /dev/null +++ b/db/migrate/20231026120000_add_source_url_to_crop_companions.rb @@ -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