Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search age range #25

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def update
end

def search
@results = Profile.where('id != ?', current_customer.profile.id)
@results = Profile.where('profiles.id != ?', current_customer.profile.id)

if params[:gender]
@results = @results.where(gender: params[:gender])
Expand All @@ -29,6 +29,12 @@ def search
if params[:distance]
@results = @results.near(current_customer.profile.postcode, params[:distance].to_f)
end

if params[:age]
date_from = Date.today - (params[:age][:max]).to_i.years
date_to = Date.today - (params[:age][:min]).to_i.years
@results = @results.joins(:customer).where('customers.date_of_birth > ? AND customers.date_of_birth < ?', date_from, date_to)
end
end

private
Expand Down
16 changes: 11 additions & 5 deletions app/views/profiles/_search.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="column-4 offset-1">
<h1>Find your match!</h1>

<%= form_tag(search_profiles_path, method: "get") do %>
<%= form_tag(search_profiles_path, method: "get") do %>
<h2>Search by gender:</h2>

<%= radio_button_tag(:gender, "male") %>
Expand All @@ -20,8 +20,14 @@
<%= radio_button_tag(:distance, "150") %>
<%= label_tag(:distance_150, "Within 150km", class: 'm-r-2') %>

<div>
<%= submit_tag("Search", {id: 'search', class: 'wrapper btn brr-5 m-1'}) %>
</div>
<% end %>
<h2>Filter by age range</h2>

<%= number_field(:age, :min, in: 18..120, step: 1) %>
<%= label_tag(:age_min, "Minimum Age") %>
<%= number_field(:age, :max, in: 18..120, step: 1) %>
<%= label_tag(:age_max, "Maximum Age") %>

<%= submit_tag("Search", {class: 'btn brr-5 m-1'}) %>

<% end %>
</div>
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160721230116) do
ActiveRecord::Schema.define(version: 20160722095940) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -41,6 +41,17 @@
add_index "customers", ["reset_password_token"], name: "index_customers_on_reset_password_token", unique: true, using: :btree
add_index "customers", ["username"], name: "index_customers_on_username", unique: true, using: :btree

create_table "messages", force: :cascade do |t|
t.text "content"
t.integer "sender_id"
t.integer "receiver_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "replied_to_id"
t.boolean "read"
t.boolean "visible"
end

create_table "profiles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Expand Down
5 changes: 4 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Customer.destroy_all
Profile.destroy_all

Customer.destroy_all
Profile.destroy_all

genders = %w(male female other)
postcodes = %w(EN63AQ EN93TP EN110HD EN64NW EN55SR EN80NQ EN111HP EN36PX EN37SG EN28LH EN27AP EN11XN EN51SL EN93BS EN36QA EN20BY EN37EA EN107PA EN81LT EN53LH EN49QB EN92PT EN87JR EN111JB EN28DQ EN89XF EN87DJ EN88RJ EN65HH EN76EY EN55AJ EN14DU EN20HN EN26PR EN55SF EN19TH EN75NS EN87TH EN106RE EN14LW EN107DY EN28HR EN54JT EN62QE EN48DL EN93DE EN76LU EN75DH EN62DQ EN91SZ)
users = []
Expand Down Expand Up @@ -40,4 +43,4 @@
:password => 'password', :password_confirmation => 'password')
customer.profile.update(user_data)
customer.confirm
end
end
11 changes: 11 additions & 0 deletions features/search_profiles_by_age.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Search profiles by age-range

Background:
Given some users have registered
And Bob has signed in
And Bob is looking at his profile page

Scenario: A customer selects an age range to search by
When they select an age range
And they press search button
Then a list of age-filtered profiles is diplayed
11 changes: 11 additions & 0 deletions features/step_definitions/search_age_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
When(/^they select an age range$/) do
fill_in "age_min", with: 19
fill_in "age_max", with: 27
end

Then(/^a list of age\-filtered profiles is diplayed$/) do
expect(page).to_not have_content("Bob")
expect(page).to have_content("Penny")
expect(page).to have_content("Ric")
expect(page).to_not have_content("Jess")
end
3 changes: 2 additions & 1 deletion features/step_definitions/search_by_distance_steps.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Given(/^some users have registered$/) do
users = YAML.load_file 'features/support/users.yml'
ages=[18,22,25,33]

users.each_with_index do |user_data, i|
customer = instance_variable_set :"@customer_#{i + 1}", Customer.create!(
:email => Faker::Internet.email,
:username => Faker::Lorem.sentence,
:date_of_birth => (18..55).to_a.sample.years.ago,
:date_of_birth => ages[i].years.ago,
:password => 'password', :password_confirmation => 'password')
customer.profile.update(user_data)
customer.confirm
Expand Down