Skip to content

Commit

Permalink
Merge remote branch 'rikesh/master': Adding support for Capybara cukes;
Browse files Browse the repository at this point in the history
advanced searching stuff.
  • Loading branch information
Tom Elkin committed Jul 16, 2011
2 parents d322fb7 + a09041c commit d9b25e3
Show file tree
Hide file tree
Showing 28 changed files with 811 additions and 81 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ group :cucumber do
gem 'cucumber', '0.9.4'
gem 'cucumber-rails', '0.3.2'
gem 'hpricot', '0.8.2'
gem 'launchy'
gem 'launchy', '0.4.0'

end

group :test do
gem 'rspec', '1.3.2'
gem 'rspec-rails', '1.3.4'
gem 'webrat', '0.7.1'
gem 'capybara', '1.0.0'
end

21 changes: 20 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ GEM
activesupport (= 2.3.11)
activesupport (2.3.11)
builder (2.1.2)
capybara (1.0.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 0.2.0)
xpath (~> 0.1.4)
childprocess (0.1.9)
ffi (~> 1.0.6)
configuration (1.2.0)
couchrest (0.34)
mime-types (>= 1.15)
Expand All @@ -29,6 +38,7 @@ GEM
diff-lcs (1.1.2)
escape (0.0.4)
fastercsv (1.5.3)
ffi (1.0.9)
gherkin (2.2.9)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
Expand Down Expand Up @@ -73,6 +83,12 @@ GEM
rspec-rails (1.3.4)
rack (>= 1.0.0)
rspec (~> 1.3.1)
rubyzip (0.9.4)
selenium-webdriver (0.2.2)
childprocess (>= 0.1.9)
ffi (>= 1.0.7)
json_pure
rubyzip
subexec (0.0.4)
sunspot (1.1.0)
escape (= 0.0.4)
Expand All @@ -85,11 +101,14 @@ GEM
nokogiri (>= 1.2.0)
rack (>= 1.0)
rack-test (>= 0.5.3)
xpath (0.1.4)
nokogiri (~> 1.3)

PLATFORMS
ruby

DEPENDENCIES
capybara (= 1.0.0)
couchrest (= 0.34)
cucumber (= 0.9.4)
cucumber-rails (= 0.3.2)
Expand All @@ -99,7 +118,7 @@ DEPENDENCIES
hpricot (= 0.8.2)
json (= 1.4.6)
json_pure (= 1.4.6)
launchy
launchy (= 0.4.0)
mime-types (= 1.16)
mini_magick (= 1.3.2)
pdf-reader (= 0.8.6)
Expand Down
37 changes: 29 additions & 8 deletions app/controllers/advanced_search_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
class AdvancedSearchController < ApplicationController

def index
def new
@forms = FormSection.by_order
@aside = 'shared/sidebar_links'

if params[:criteria_list]
@criteria_list = SearchCriteria.build_from_params params[:criteria_list]
@results = SearchService.search(@criteria_list)
else
@criteria_list = [ SearchCriteria.new ]

@criteria_list = [SearchCriteria.new]
@results = []
render :index
end

def index
@forms = FormSection.by_order
@aside = 'shared/sidebar_links'
new_search = !params[:criteria_list]

if new_search
@criteria_list = [SearchCriteria.new]
@results = []
else
@criteria_list = (child_fields_selected?(params[:criteria_list]) ? SearchCriteria.build_from_params(params[:criteria_list]): [])
append_advanced_user_criteria(params[:created_by_value], @criteria_list)
@results = SearchService.search(@criteria_list)
end
end

def child_fields_selected?(criteria_list)
!criteria_list.first[1]["field"].blank? if !criteria_list.first[1].nil?
end

def append_advanced_user_criteria(value, list)
if (value)
advanced_user_criteria = SearchCriteria.create_advanced_criteria({:field => "created_by", :value => value, :index => 12})
list.push(advanced_user_criteria)
end

end
end
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def discard_button(path)
def show_sidebar_links
sidebar_links = {"View All Children" => children_path,
"Search" => search_children_path,
"Advanced Search" => advanced_search_index_path}
"Advanced Search" => new_advanced_search_path}
sidebar_links.select do |_, link|
!current_page?(link)
end
Expand Down
8 changes: 5 additions & 3 deletions app/models/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ class Child < CouchRestRails::Document
validates_with_method :created_at, :method => :validate_created_at

def self.build_solar_schema
fields = ["unique_identifier"] + Field.all_text_names

fields = build_fields_for_solar
Sunspot.setup(Child) do
text *fields
end
end


def self.build_fields_for_solar
["unique_identifier", "created_by"] + Field.all_text_names
end

def validate_has_at_least_one_field_value
return true if FormSection.all_enabled_child_fields.any? { |field| is_filled_in? field }
Expand Down
7 changes: 4 additions & 3 deletions app/models/search_criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def initialize(params = {})
@field = params[:field] || ""
@value = (params[:value] || "").strip
end

def self.create_advanced_criteria(criteria)
SearchCriteria.new(:field => criteria[:field], :value => criteria[:value], :join => "AND", :index => criteria[:index].to_s)
end

def self.build_from_params(criteria_list)
text_fields = FormSection.all.map{ |form| form.all_text_fields }.flatten
Expand Down Expand Up @@ -51,7 +55,4 @@ def self.build_joins(list, lucene_query)
end
end




end
12 changes: 8 additions & 4 deletions app/views/advanced_search/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="error" style="display:none"></div>
</div>
<div class="help-text">Separate words by OR to search for more than one option eg. Tim OR Rahul</div>
<form>
<form action="index">
<div class='criteria_menu' style='display:none;' >
<div class='box-shadow content'>
<div style="float:right; margin-left: 6px;">
Expand All @@ -34,9 +34,6 @@
<% end %>
</div>
</div>




<script id="criteria_template" type="text/x-jquery-tmpl">
<p>
Expand All @@ -63,6 +60,13 @@
<p style='clear:both'>
<a class='add-criteria'>+ add more search options</a>
</p>

<hr>

<%= check_box_tag(:created_by) %>
<%= label_tag(:created_by_label, "Created by:") %>
<%= text_field_tag(:created_by_value, nil, :disabled => true) %>

</form>
</div>

Expand Down
37 changes: 37 additions & 0 deletions capybara_features/advanced_search.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Feature: So that I can find a child that has been entered in to RapidFTR
As a user of the website
I want to use the advanced search to find all relevant results

@javascript
Scenario: Searching for children by the user who created the child record
Given the following children exist in the system:
| name | created_by |
| Willis | aidWorker |
| Will | volunteer |

And I am logged in
And I am on child advanced search page

When I check "created_by"
And I fill in "aidWorker" for "created_by_value"
And I press "Search"

And I should see "Willis" in the search results

@javascript
Scenario: Searching for children by the child name field
Given the following children exist in the system:
| name | created_by |
| Willis | aidWorker |
| Will | volunteer |

And I am logged in
And I am on child advanced search page

When I click text "Select A Criteria"
And I click text "Name"
And I fill in "Wil" for "criteria_list[0][value]"
And I press "Search"

And I should see "Willis" in the search results
And I should see "Will" in the search results
9 changes: 9 additions & 0 deletions capybara_features/capybara.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@javascript
Feature: Test capybara is configured correctly

Scenario: Log into RapidFTR
Given a user "rapidftr" with a password "rapidftr"
Given I am on the login page
When I fill in "rapidftr" for "user_name"
And I fill in "rapidftr" for "password"
And I press "Log in"
18 changes: 18 additions & 0 deletions capybara_features/step_definitions/children_setup_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec/spec_helper'

Given /^the following children exist in the system:$/ do |children_table|
children_table.hashes.each do |child_hash|
child_hash.reverse_merge!(
'birthplace' => 'Cairo',
'photo_path' => 'features/resources/jorge.jpg',
'reporter' => 'zubair',
'age_is' => 'Approximate'
)
photo = uploadable_photo(child_hash.delete('photo_path')) if child_hash['photo_path'] != ''
unique_id = child_hash.delete('unique_id')
child = Child.new_with_user_name(child_hash['created_by'], child_hash)
child.photo = photo
child['unique_identifier'] = unique_id if unique_id
child.create!
end
end
52 changes: 52 additions & 0 deletions capybara_features/step_definitions/database_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Given /^an? (user|admin) "([^\"]*)" with(?: a)? password "([^\"]*)"$/ do |user_type, username, password|
user_type = user_type == 'user' ? 'User' : 'Administrator'
@user = User.new(
:user_name=>username,
:password=>password,
:password_confirmation=>password,
:user_type=> user_type,
:full_name=>username,
:email=>"#{username}@test.com")
@user.save!
end

Given /^an? (user|admin) "([^"]+)"$/ do |user_type, user_name|
Given %(a #{user_type} "#{user_name}" with password "123")
end

Given /^I am logged in as "(.+)"/ do |user_name|
@session = Session.for_user(User.find_by_user_name(user_name), nil)
@session.save!
@session.put_in_cookie cookies
end

Given /^I have an expired session/ do
@session.destroy
end

Given /^user "(.+)" is disabled$/ do |username|
user = User.find_by_user_name(username)
user.disabled = true
user.save!
end

Then /^user "(.+)" should be disabled$/ do |username|
User.find_by_user_name(username).should be_disabled
end

Then /^user "(.+)" should not be disabled$/ do |username|
User.find_by_user_name(username).should_not be_disabled
end

Given /^the following admin contact info:$/ do |table|
contact_info = table.hashes.inject({}) do |result, current|
result[current["key"]] = current["value"]
result
end
contact_info[:id] = "administrator"
ContactInformation.create contact_info
end




13 changes: 13 additions & 0 deletions capybara_features/step_definitions/login_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Given /^I am logged in$/ do
Given "there is a User"
Given "I am on the login page"
Given "I fill in \"#{User.first.user_name}\" for \"user_name\""
Given "I fill in \"123\" for \"password\""
Given "I press \"Log in\""
end

Given /there is a User/ do
unless @user
Given "a user \"mary\" with a password \"123\""
end
end
22 changes: 22 additions & 0 deletions capybara_features/step_definitions/more_web_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
# It is recommended to regenerate this file in the future when you upgrade to a
# newer version of cucumber-rails. Consider adding your own code to a new file
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.

require 'uri'
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))

module WithinHelpers
def with_scope(locator)
locator ? within(locator) { yield } : yield
end
end
World(WithinHelpers)

When /^I click text "([^"]*)"(?: within "([^\"]*)")?$/ do |text_value, selector|
with_scope(selector) do
page.find('//a', :text => text_value).click

end
end
7 changes: 7 additions & 0 deletions capybara_features/step_definitions/search_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec/spec_helper'

Then /^I should see "([^\"]*)" in the search results$/ do |value|

match = page.find('//a', :text => value)
raise Spec::Expectations::ExpectationNotMetError, "Could not find the value: #{value} in the search results" unless match
end
Loading

0 comments on commit d9b25e3

Please sign in to comment.