Skip to content

Commit

Permalink
Merge pull request #562 from simonv3/master
Browse files Browse the repository at this point in the history
Close #556. Catch null error when there are no common names
  • Loading branch information
simonv3 committed Mar 21, 2015
2 parents 88722d9 + 6b287f3 commit 27edbeb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
14 changes: 11 additions & 3 deletions app/assets/javascripts/crops/edit.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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: {
Expand All @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion app/controllers/api/crops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/models/detail_option.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions test/factories/detail_options.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 27edbeb

Please sign in to comment.