From 56fc33c584d1900ebb1b818af08ca7b5fc421c0f Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 21 Mar 2015 07:03:46 +0700 Subject: [PATCH 1/6] Catch null error when there are no common names --- app/assets/javascripts/application.js | 1 + app/assets/javascripts/crops/edit.js | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 8e1bc9f59..72e7bd422 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -66,6 +66,7 @@ var getUrlVar = function(key) { var getIDFromURL = function(key) { var result = new RegExp(key + "/([0-9a-zA-Z\-]*)", "i").exec(window.location.pathname); + console.log(result) return result && unescape(result[1]) || ""; }; diff --git a/app/assets/javascripts/crops/edit.js b/app/assets/javascripts/crops/edit.js index 15b710d9a..4e01b5bff 100644 --- a/app/assets/javascripts/crops/edit.js +++ b/app/assets/javascripts/crops/edit.js @@ -1,13 +1,15 @@ openFarmApp.controller('editCropCtrl', ['$scope', '$http', 'cropService', - function newGuideCtrl($scope, $http, cropService) { + function editCropCtrl($scope, $http, cropService) { $scope.alerts = []; $scope.s3upload = ''; $scope.editCrop = {}; + var cropId = getIDFromURL('crops'); + var setCrop = function(success, crop){ $scope.editCrop = crop; }; - cropService.getCrop(getIDFromURL('crops'), $scope.alerts, setCrop); + cropService.getCrop(cropId, $scope.alerts, setCrop); $scope.submitForm = function(){ $scope.editCrop.sending = true; @@ -16,8 +18,13 @@ openFarmApp.controller('editCropCtrl', ['$scope', '$http', 'cropService', if (typeof $scope.editCrop.common_names === 'string'){ commonNames = $scope.editCrop.common_names.split(/,+|\n+/) .map(function(s){ return s.trim(); }); + if (commonNames !== null){ + commonNames = commonNames.filter(function(s){ + return s.length > 0; + }); + } + } - commonNames = commonNames.filter(function(s){ return s.length > 0; }); var params = { crop: { @@ -41,6 +48,7 @@ openFarmApp.controller('editCropCtrl', ['$scope', '$http', 'cropService', var cropCallback = function(success, crop){ $scope.editCrop.sending = false; $scope.editCrop = crop; + window.location.href = '/crops/' + $scope.editCrop._id + '/'; }; cropService.updateCrop($scope.editCrop._id, From 13417031def41338a26a36974a531679f9627784 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 21 Mar 2015 09:58:22 +0700 Subject: [PATCH 2/6] Close #548. Make search same as crop_searches --- app/controllers/api/crops_controller.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/crops_controller.rb b/app/controllers/api/crops_controller.rb index 3aafdd764..345a17a19 100644 --- a/app/controllers/api/crops_controller.rb +++ b/app/controllers/api/crops_controller.rb @@ -4,7 +4,17 @@ class CropsController < Api::Controller def index if params[:query].present? && (params[:query].length > 2) q = params[:query] - render json: { crops: Crop.search(q, fields: ['name^20', 'common_names^10', 'binomial_name^10', 'description'], limit: 5) } + crops = Crop.search(q, + limit: 25, + partial: true, + misspellings: {distance: 2}, + fields: ['name^20', + 'common_names^10', + 'binomial_name^10', + 'description'], + boost_by: [:guides_count] + ) + render json: { crops: crops } else render json: { crops: [] } end From f3dd7dd1265b2f63f6dd0b894566bed7affed66f Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 21 Mar 2015 09:58:45 +0700 Subject: [PATCH 3/6] Respond to hound --- app/assets/javascripts/application.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 72e7bd422..8e1bc9f59 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -66,7 +66,6 @@ var getUrlVar = function(key) { var getIDFromURL = function(key) { var result = new RegExp(key + "/([0-9a-zA-Z\-]*)", "i").exec(window.location.pathname); - console.log(result) return result && unescape(result[1]) || ""; }; From 53fdc62312ce6ef2ad407bc14b55370ef45bd4b8 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 21 Mar 2015 10:12:57 +0700 Subject: [PATCH 4/6] Add spaces to crop controller for hound, Add Detail Option model --- app/controllers/api/crops_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/crops_controller.rb b/app/controllers/api/crops_controller.rb index 345a17a19..3f101098b 100644 --- a/app/controllers/api/crops_controller.rb +++ b/app/controllers/api/crops_controller.rb @@ -7,7 +7,7 @@ def index crops = Crop.search(q, limit: 25, partial: true, - misspellings: {distance: 2}, + misspellings: { distance: 2 }, fields: ['name^20', 'common_names^10', 'binomial_name^10', From 097d0e565f96c93dfa1349d8e97becd02a187fd9 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 21 Mar 2015 10:14:25 +0700 Subject: [PATCH 5/6] Actually add detail option model --- app/models/detail_option.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 app/models/detail_option.rb diff --git a/app/models/detail_option.rb b/app/models/detail_option.rb new file mode 100644 index 000000000..1e792b040 --- /dev/null +++ b/app/models/detail_option.rb @@ -0,0 +1,13 @@ +# This is a catch-all class to store information +# on guides. +# +# Category is something like "environment", "location", "practices", +# "soil", "light" + +class DetailOption + include Mongoid::Document + field :name, type: String + field :description, type: String + field :help, type: String + field :category, type: String +end From 6b287f38232ca8b55c905fe239539165247d5510 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 21 Mar 2015 10:49:31 +0700 Subject: [PATCH 6/6] Add more details --- db/seeds.rb | 33 ++++++++++++++++++++++++++++++++ test/factories/detail_options.rb | 8 ++++++++ 2 files changed, 41 insertions(+) create mode 100644 test/factories/detail_options.rb diff --git a/db/seeds.rb b/db/seeds.rb index 007223849..4c23b53f3 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -47,6 +47,37 @@ stratify = FactoryGirl.create(:stage_action_option, name: 'Stratify') train = FactoryGirl.create(:stage_action_option, name: 'Train') + # Seed the details for each guide. + FactoryGirl.create(:detail_option, + name: 'Greenhouse', + category: 'environment') + FactoryGirl.create(:detail_option, name: 'Potted', category: 'environment') + FactoryGirl.create(:detail_option, name: 'Inside', category: 'environment') + FactoryGirl.create(:detail_option, name: 'Inside', category: 'environment') + outside = FactoryGirl.create(:detail_option, + name: 'Outside', + category: 'environment') + + + FactoryGirl.create(:detail_option, name: 'Loam', category: 'soil') + FactoryGirl.create(:detail_option, name: 'Clay', category: 'soil') + + FactoryGirl.create(:detail_option, name: 'Full Sun', category: 'light') + FactoryGirl.create(:detail_option, name: 'Partial Sun', category: 'light') + FactoryGirl.create(:detail_option, name: 'Shaded', category: 'light') + FactoryGirl.create(:detail_option, name: 'Indirect Light', category: 'light') + + FactoryGirl.create(:detail_option, name: 'Organic', category: 'practices') + FactoryGirl.create(:detail_option, name: 'Hydroponic', category: 'practices') + FactoryGirl.create(:detail_option, name: 'Intensive', category: 'practices') + FactoryGirl.create(:detail_option, + name: 'Conventional', + category: 'practices') + FactoryGirl.create(:detail_option, + name: 'Permaculture', + category: 'practices') + + prep = FactoryGirl.create(:stage_option, name: 'Preparation', order: 0) prep.stage_action_options = [water, fertilize, amend, prepare] @@ -76,5 +107,7 @@ dormant = FactoryGirl.create(:stage_option, name: 'Dormant', order: 8) dormant.stage_action_options = [prune, cover, tap] + + Guide.all.each{ |gde| gde.update_attributes(user: admin) } end diff --git a/test/factories/detail_options.rb b/test/factories/detail_options.rb new file mode 100644 index 000000000..d7aceae90 --- /dev/null +++ b/test/factories/detail_options.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :detail_option do + name { "#{Faker::Name.last_name}" } + description { Faker::Lorem.sentence } + help { Faker::Lorem.sentence } + category { "#{Faker::Lorem.word}" } + end +end