Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 27edbeb

Browse files
committed
Merge pull request #562 from simonv3/master
Close #556. Catch null error when there are no common names
2 parents 88722d9 + 6b287f3 commit 27edbeb

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

app/assets/javascripts/crops/edit.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
openFarmApp.controller('editCropCtrl', ['$scope', '$http', 'cropService',
2-
function newGuideCtrl($scope, $http, cropService) {
2+
function editCropCtrl($scope, $http, cropService) {
33
$scope.alerts = [];
44
$scope.s3upload = '';
55
$scope.editCrop = {};
6+
var cropId = getIDFromURL('crops');
7+
68
var setCrop = function(success, crop){
79
$scope.editCrop = crop;
810
};
911

10-
cropService.getCrop(getIDFromURL('crops'), $scope.alerts, setCrop);
12+
cropService.getCrop(cropId, $scope.alerts, setCrop);
1113

1214
$scope.submitForm = function(){
1315
$scope.editCrop.sending = true;
@@ -16,8 +18,13 @@ openFarmApp.controller('editCropCtrl', ['$scope', '$http', 'cropService',
1618
if (typeof $scope.editCrop.common_names === 'string'){
1719
commonNames = $scope.editCrop.common_names.split(/,+|\n+/)
1820
.map(function(s){ return s.trim(); });
21+
if (commonNames !== null){
22+
commonNames = commonNames.filter(function(s){
23+
return s.length > 0;
24+
});
25+
}
26+
1927
}
20-
commonNames = commonNames.filter(function(s){ return s.length > 0; });
2128

2229
var params = {
2330
crop: {
@@ -41,6 +48,7 @@ openFarmApp.controller('editCropCtrl', ['$scope', '$http', 'cropService',
4148
var cropCallback = function(success, crop){
4249
$scope.editCrop.sending = false;
4350
$scope.editCrop = crop;
51+
window.location.href = '/crops/' + $scope.editCrop._id + '/';
4452
};
4553

4654
cropService.updateCrop($scope.editCrop._id,

app/controllers/api/crops_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ class CropsController < Api::Controller
44
def index
55
if params[:query].present? && (params[:query].length > 2)
66
q = params[:query]
7-
render json: { crops: Crop.search(q, fields: ['name^20', 'common_names^10', 'binomial_name^10', 'description'], limit: 5) }
7+
crops = Crop.search(q,
8+
limit: 25,
9+
partial: true,
10+
misspellings: { distance: 2 },
11+
fields: ['name^20',
12+
'common_names^10',
13+
'binomial_name^10',
14+
'description'],
15+
boost_by: [:guides_count]
16+
)
17+
render json: { crops: crops }
818
else
919
render json: { crops: [] }
1020
end

app/models/detail_option.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This is a catch-all class to store information
2+
# on guides.
3+
#
4+
# Category is something like "environment", "location", "practices",
5+
# "soil", "light"
6+
7+
class DetailOption
8+
include Mongoid::Document
9+
field :name, type: String
10+
field :description, type: String
11+
field :help, type: String
12+
field :category, type: String
13+
end

db/seeds.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@
4747
stratify = FactoryGirl.create(:stage_action_option, name: 'Stratify')
4848
train = FactoryGirl.create(:stage_action_option, name: 'Train')
4949

50+
# Seed the details for each guide.
51+
FactoryGirl.create(:detail_option,
52+
name: 'Greenhouse',
53+
category: 'environment')
54+
FactoryGirl.create(:detail_option, name: 'Potted', category: 'environment')
55+
FactoryGirl.create(:detail_option, name: 'Inside', category: 'environment')
56+
FactoryGirl.create(:detail_option, name: 'Inside', category: 'environment')
57+
outside = FactoryGirl.create(:detail_option,
58+
name: 'Outside',
59+
category: 'environment')
60+
61+
62+
FactoryGirl.create(:detail_option, name: 'Loam', category: 'soil')
63+
FactoryGirl.create(:detail_option, name: 'Clay', category: 'soil')
64+
65+
FactoryGirl.create(:detail_option, name: 'Full Sun', category: 'light')
66+
FactoryGirl.create(:detail_option, name: 'Partial Sun', category: 'light')
67+
FactoryGirl.create(:detail_option, name: 'Shaded', category: 'light')
68+
FactoryGirl.create(:detail_option, name: 'Indirect Light', category: 'light')
69+
70+
FactoryGirl.create(:detail_option, name: 'Organic', category: 'practices')
71+
FactoryGirl.create(:detail_option, name: 'Hydroponic', category: 'practices')
72+
FactoryGirl.create(:detail_option, name: 'Intensive', category: 'practices')
73+
FactoryGirl.create(:detail_option,
74+
name: 'Conventional',
75+
category: 'practices')
76+
FactoryGirl.create(:detail_option,
77+
name: 'Permaculture',
78+
category: 'practices')
79+
80+
5081
prep = FactoryGirl.create(:stage_option, name: 'Preparation', order: 0)
5182
prep.stage_action_options = [water, fertilize, amend, prepare]
5283

@@ -76,5 +107,7 @@
76107
dormant = FactoryGirl.create(:stage_option, name: 'Dormant', order: 8)
77108
dormant.stage_action_options = [prune, cover, tap]
78109

110+
111+
79112
Guide.all.each{ |gde| gde.update_attributes(user: admin) }
80113
end

test/factories/detail_options.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryGirl.define do
2+
factory :detail_option do
3+
name { "#{Faker::Name.last_name}" }
4+
description { Faker::Lorem.sentence }
5+
help { Faker::Lorem.sentence }
6+
category { "#{Faker::Lorem.word}" }
7+
end
8+
end

0 commit comments

Comments
 (0)