From 12d151b68c18801513d8071c403a6fa7f6837dc3 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Mon, 13 Jul 2015 14:00:08 +0800 Subject: [PATCH] Added multiple forms for scientific and alternate names on crop creation --- app/assets/javascripts/seeds.js.coffee | 39 ++++++++++++++++++-------- app/controllers/crops_controller.rb | 32 +++++++++++++++++---- app/models/crop.rb | 2 +- app/views/crops/_form.html.haml | 38 +++++++++++++++++-------- 4 files changed, 81 insertions(+), 30 deletions(-) diff --git a/app/assets/javascripts/seeds.js.coffee b/app/assets/javascripts/seeds.js.coffee index 8db6c0a3f..339d7422b 100644 --- a/app/assets/javascripts/seeds.js.coffee +++ b/app/assets/javascripts/seeds.js.coffee @@ -6,20 +6,35 @@ jQuery -> $('.add-datepicker').datepicker('format' : 'yyyy-mm-dd') $ -> - template = "
Alternate name of crop.
" + sci_template = "
Scientific name of crop.
" - index = 2 + sci_index = $('#scientific_names .template').length + 1 - $('#add-crop-row').click -> - compiled_input = $(template.split("INDEX").join(index)) - $('#someform').append(compiled_input) - console.log("Added %d", index) - index = index + 1 + $('#add-sci_name-row').click -> + compiled_input = $(sci_template.split("INDEX").join(sci_index)) + $('#scientific_names').append(compiled_input) + sci_index = sci_index + 1 - $('#remove-crop-row').click -> - if (index > 2) - index = index - 1 - tmp = 'template[' + index + ']' + $('#remove-sci_name-row').click -> + if (sci_index > 2) + sci_index = sci_index - 1 + tmp = 'sci_template[' + sci_index + ']' element = document.getElementById(tmp) element.remove() - console.log("Removed %s", tmp) + + alt_template = "
Alternate name of crop.
" + + alt_index = $('#alternate_names .template').length + 1 + + $('#add-alt_name-row').click -> + compiled_input = $(alt_template.split("INDEX").join(alt_index)) + $('#alternate_names').append(compiled_input) + alt_index = alt_index + 1 + + $('#remove-alt_name-row').click -> + if (alt_index > 2) + alt_index = alt_index - 1 + tmp = 'alt_template[' + alt_index + ']' + element = document.getElementById(tmp) + console.log("%s",tmp) + element.remove() diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index ffd0ed3af..98d2ca6cb 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -94,9 +94,9 @@ class CropsController < ApplicationController # GET /crops/new.json def new @crop = Crop.new - 3.times do - @crop.scientific_names.build - end + @crop.alternate_names.build + @crop.scientific_names.build + respond_to do |format| format.html # new.html.haml format.json { render json: @crop } @@ -106,15 +106,15 @@ class CropsController < ApplicationController # GET /crops/1/edit def edit @crop = Crop.find(params[:id]) + @crop.alternate_names.build if @crop.alternate_names.blank? + @crop.scientific_names.build if @crop.scientific_names.blank? - (3 - @crop.scientific_names.size).times do - @crop.scientific_names.build - end end # POST /crops # POST /crops.json def create + @crop = Crop.new(crop_params) if current_member.has_role? :crop_wrangler @@ -128,6 +128,12 @@ class CropsController < ApplicationController respond_to do |format| if @crop.save + params[:alt_name].each do |index, value| + alt_name = @crop.alternate_names.create(name: value, creator_id: current_member.id) + end + params[:sci_name].each do |index, value| + sci_name = @crop.scientific_names.create(scientific_name: value, creator_id: current_member.id) + end unless current_member.has_role? :crop_wrangler Role.crop_wranglers.each do |w| Notifier.new_crop_request(w, @crop).deliver! @@ -154,6 +160,20 @@ class CropsController < ApplicationController respond_to do |format| if @crop.update(crop_params) + @crop.alternate_names.each do |alt_name| + alt_name.destroy + end + params[:alt_name].each do |index, value| + alt_name = @crop.alternate_names.create(name: value, creator_id: current_member.id) + end + + @crop.scientific_names.each do |sci_name| + sci_name.destroy + end + params[:sci_name].each do |index, value| + sci_name = @crop.scientific_names.create(scientific_name: value, creator_id: current_member.id) + end + if previous_status == "pending" requester = @crop.requester new_status = @crop.approval_status diff --git a/app/models/crop.rb b/app/models/crop.rb index 2773bbe38..2000ebfbb 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -7,7 +7,7 @@ class Crop < ActiveRecord::Base :allow_destroy => true, :reject_if => :all_blank - has_many :alternate_names, after_add: :update_index, after_remove: :update_index + has_many :alternate_names, after_add: :update_index, after_remove: :update_index, dependent: :destroy has_many :plantings has_many :photos, :through => :plantings has_many :seeds diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index 09dafa756..ea3a0c653 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -46,17 +46,33 @@ -# Everyone (wranglers and requesters) gets to add scientific names %h2 Scientific names - = button_tag "+", :id => "add-crop-row", :type => "button" - = button_tag "-", :id => "remove-crop-row", :type => "button" - - .form-group#someform - .template.col-md-12 - .col-md-2 - = label_tag :alternate_names, "Alternate name 1:", :class => 'control-label' - .col-md-8 - = text_field_tag :name, nil, :id => 'alt_name[1]', :class => 'form-control' - %span.help-block Alternate name of crop. - .col-md-2 + = button_tag "+", :id => "add-sci_name-row", :type => "button" + = button_tag "-", :id => "remove-sci_name-row", :type => "button" + + .form-group#scientific_names + - @crop.scientific_names.each.with_index do |sci, index| + .template.col-md-12{ :id => "sci_template[#{index+1}]" } + .col-md-2 + = label_tag :scientific_names, "Scientific name #{index+1}:", :class => 'control-label' + .col-md-8 + = text_field_tag "sci_name[#{index+1}]", sci.scientific_name, :id => "sci_name[#{index+1}]", :class => 'form-control' + %span.help-block Scientific name of crop. + .col-md-2 + + %h2 + Alternate names + = button_tag "+", :id => "add-alt_name-row", :type => "button" + = button_tag "-", :id => "remove-alt_name-row", :type => "button" + + .form-group#alternate_names + - @crop.alternate_names.each.with_index do |alt, index| + .template.col-md-12{ :id => "alt_template[#{index+1}]" } + .col-md-2 + = label_tag :alternate_names, "Alternate name #{index+1}:", :class => 'control-label' + .col-md-8 + = text_field_tag "alt_name[#{index+1}]", alt.name, :id => "alt_name[#{index+1}]", :class => 'form-control' + %span.help-block Alternate name of crop. + .col-md-2 -# This is used for comments from crop requesters. We need to show it -# to everyone, but we don't include it on new crops from wranglers.