Skip to content

Commit

Permalink
Merge pull request #374 from slacker87/master
Browse files Browse the repository at this point in the history
Fix issue #345 and also add a missing 'setup' instruction in readme
  • Loading branch information
simonv3 committed Dec 19, 2014
2 parents 93c69fa + 826a06f commit 05b73b5
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gem 'mongoid_slug'
gem 'aws-sdk', '~> 1.55.0'
gem 'mutations'
gem 'rack-attack'
gem 'impressionist'
gem 'impressionist', '~> 1.5.0'
gem 'mongoid-history'
gem 'rack-cors', require: 'rack/cors'
gem 'delayed_job_mongoid'
Expand All @@ -44,6 +44,7 @@ group :test do
gem 'doc_yo_self'
gem 'capybara'
gem 'poltergeist'
gem 'phantomjs', '>= 1.8.1', :require => 'phantomjs/poltergeist'
gem 'simplecov'
gem 'database_cleaner'
gem 'vcr'
Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ GEM
hashie (3.3.2)
high_voltage (2.2.1)
hike (1.2.3)
httpclient (2.5.1)
httpclient (2.5.3.3)
i18n (0.6.11)
impressionist (1.5.1)
httpclient (~> 2.2)
Expand Down Expand Up @@ -202,6 +202,7 @@ GEM
ast (>= 1.1, < 3.0)
slop (~> 3.4, >= 3.4.5)
patron (0.4.18)
phantomjs (1.9.7.1)
poltergeist (1.5.1)
capybara (~> 2.1)
cliver (~> 0.3.1)
Expand Down Expand Up @@ -368,7 +369,7 @@ DEPENDENCIES
font-awesome-sass
foundation-rails (~> 5.4.5)
high_voltage
impressionist
impressionist (~> 1.5.0)
jquery-rails
launchy
mongoid!
Expand All @@ -378,6 +379,7 @@ DEPENDENCIES
mutations
ng-rails-csrf
patron
phantomjs (>= 1.8.1)
poltergeist
pry
pry-nav
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $ git clone https://github.com/openfarmcc/OpenFarm.git
$ cd OpenFarm
$ bundle install
$ rake db:setup
$ echo "ENV['SECRET_KEY_BASE'] = '$(rake secret)'" >> config/app_environment_variables.rb
$ rails s
```

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/crop_searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ def search
query = params[:q].to_s
@crops = Crop.search(query,
limit: 25,
partial: true,
misspellings: {distance: 2},
fields: ['name^20',
'common_names^10',
'binomial_name^10',
'description'])
if @crops.empty?
if query.empty?
@crops = Crop.search('*', limit: 25)
end

Expand Down
1 change: 1 addition & 0 deletions app/models/crop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def search_data
end
slug :name


# See https://github.com/aq1018/mongoid-history
track_history on: [:description, :image],
modifier_field: :modifier,
Expand Down
3 changes: 3 additions & 0 deletions app/views/crop_searches/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<div class="crop-search-results row wide-row">
<div class="crop-results-container small-6 columns">
<h2 class="text-center">Choose a crop</h2>
<% if @crops.empty? %>
<p class="no-results">There are no crops matching "<%= params[:q].to_s %>", would you like to <%= link_to "add this crop", new_crop_path %>?</p>
<% end %>
<%= render partial: 'crop_results', object: @crops %>
</div>
<div class="guide-results-container small-6 columns">
Expand Down
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ class Application < Rails::Application
end
end
config.middleware.use Rack::Attack
config.after_initialize do
Crop.reindex unless Rails.env.test?
end
end
end

2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
config.assets.debug = true
config.quiet_assets = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end
end
38 changes: 36 additions & 2 deletions spec/features/crop_searches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
describe 'Crop search', type: :controller do
let!(:crop) { FactoryGirl.create(:crop, :radish) }

it 'finds documents' do
it 'finds individual crops' do
visit root_path
FactoryGirl.create_list(:crop, 10)
FactoryGirl.create(:crop, name: 'radish')
Crop.searchkick_index.refresh
fill_in 'q', with: 'radish'
click_button 'Search'
expect(page).to have_content(crop.name)
expect(page).to have_content('radish')
page.should_not have_content('no crops matching')
end

it 'handles empty searches' do
Expand All @@ -21,13 +23,45 @@
expect(page).to have_content(Crop.last.name)
end

it 'handles empty search results' do
visit root_path
fill_in 'q', with: 'pear'
FactoryGirl.create_list(:crop, 10)
Crop.searchkick_index.refresh
click_button 'Search'
expect(page).to have_content("no crops matching")
end

it 'handles plurals' do
visit root_path
fill_in 'q', with: crop.name
FactoryGirl.create_list(:crop, 10)
Crop.searchkick_index.refresh
click_button 'Search'
expect(page).to have_content(crop.name)
page.should_not have_content('no crops matching')
end

it 'handles misspellings' do
visit root_path
FactoryGirl.create_list(:crop, 10)
FactoryGirl.create(:crop, name: 'radish')
Crop.searchkick_index.refresh
fill_in 'q', with: 'radis'
click_button 'Search'
expect(page).to have_content('radish')
page.should_not have_content('no crops matching')
end

it 'handles multiple words' do
visit root_path
FactoryGirl.create_list(:crop, 10)
FactoryGirl.create(:crop, name: 'radish')
Crop.searchkick_index.refresh
fill_in 'q', with: 'pear radish'
click_button 'Search'
expect(page).to have_content('radish')
page.should_not have_content('no crops matching')
end

it 'has a top nav bar' do
Expand Down
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
Capybara.javascript_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, js_errors: true)
Capybara::Poltergeist::Driver.new(app, :phantomjs => Phantomjs.path)
end
# =====
Delayed::Worker.delay_jobs = false

# ===== VCR stuff (records HTTP requests for playback)
VCR.configure do |c|
c.cassette_library_dir = 'vcr'
Expand All @@ -53,6 +53,7 @@
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.fail_fast = true
config.order = "random"
if ENV['DOCS'] == 'true'
DocYoSelf.config do |c|
Expand Down Expand Up @@ -80,4 +81,4 @@

class ActionController::TestCase
include Devise::TestHelpers
end
end

0 comments on commit 05b73b5

Please sign in to comment.