Skip to content

Commit

Permalink
Changed the search form, from dropdown to text field.
Browse files Browse the repository at this point in the history
Updated the test to work accoringly.
  • Loading branch information
iagirre committed Dec 12, 2017
1 parent 482ecc4 commit 28050c8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 16 deletions.
6 changes: 5 additions & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def search(params, selected_order)
fulltext params[:keyword] if params[:keyword].present?
with(:interest_ids, params[:interests]) if params[:interests].present?
with(:category_id, params[:category]) if params[:category].present?
with(:agent_id, params[:agent]) if params[:agent].present?
any do
fulltext(params[:agent_name], :fields => [:agent_name]) if params[:agent_name].present?
fulltext(params[:agent_name], :fields => [:agent_first_surname]) if params[:agent_name].present?
fulltext(params[:agent_name], :fields => [:agent_second_surname]) if params[:agent_name].present?
end
order_by :created_at, :desc
order_by :inscription_date, selected_order
paginate page: params[:format].present? ? 1 : params[:page] || 1, per_page: params[:format].present? ? 1000 : 10
Expand Down
5 changes: 5 additions & 0 deletions app/models/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ def fullname
scope :by_organization, ->(organization_id) { where(organization_id: organization_id) }
scope :from_active_organizations, -> { joins(:organization).where('canceled_at is NULL AND invalidate is FALSE') }

searchable do
text :name, :first_surname, :second_surname
integer :organization_id
end

end
7 changes: 4 additions & 3 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ class Organization < ActiveRecord::Base
interests.map(&:id)
end
integer :category_id
integer :agent_id, multiple: true do
agents.map(&:id)
end
integer :id
join(:name, :prefix => "agent", :target => Agent, :type => :text, :join => { :from => :organization_id, :to => :id })
join(:first_surname, :prefix => "agent", :target => Agent, :type => :text, :join => { :from => :organization_id, :to => :id })
join(:second_surname, :prefix => "agent", :target => Agent, :type => :text, :join => { :from => :organization_id, :to => :id })
end

scope :invalidated, -> { where('invalidate = ?', true) }
Expand Down
8 changes: 1 addition & 7 deletions app/views/organizations/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@
</div>

<div class="mb20">
<%= select_tag :agent,
options_for_select(Agent.from_active_organizations.all.map {
|agent| [agent.fullname, agent.id]
}, params[:agent]),
id: "agentFilter",
prompt: t("main.form.filter_by"),
class: "dropdown" %>
<%= text_field_tag :agent_name, params[:agent_name], placeholder: t("main.form.agent") %>
</div>

<div class="mb20">
Expand Down
1 change: 1 addition & 0 deletions config/locales/public.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ en:
sort_by: "Sort by"
filter_by: "Filter by"
descending: "Descending order"
agent: "Agent's name or surname"
1 change: 1 addition & 0 deletions config/locales/public.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ es:
sort_by: "Ordernar por"
filter_by: "Filtrar por"
descending: "Orden descendente"
agent: "Nombre o apellido del agente"
show:
participants: "Asisten"
location: "Lugar"
Expand Down
12 changes: 7 additions & 5 deletions spec/features/organizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,23 +406,25 @@

context 'Agents' do
background do
@org1.agents.push(create(:agent))
@org2.agents.push(create(:agent))
@agent1 = create(:agent, name: "Maria")
@agent2 = create(:agent, name: "Pedro")
@org1.agents.push(@agent1)
@org2.agents.push(@agent2)
Organization.reindex
end
scenario 'shows organizations based on the selected agent', :search do
scenario 'shows organizations based on the agent name', :search do
visit organizations_path

expect(page).to have_content(@org1.name)
expect(page).to have_content(@org2.name)

find('#agentFilter').find(:xpath, 'option[2]').select_option
fill_in :agent_name, with: "Maria"
click_button(I18n.t('main.form.search'))

expect(page).to have_content(@org1.name)
expect(page).to have_no_content(@org2.name)

find('#agentFilter').find(:xpath, 'option[3]').select_option
fill_in :agent_name, with: "Pedro"
click_button(I18n.t('main.form.search'))

expect(page).to have_content(@org2.name)
Expand Down

0 comments on commit 28050c8

Please sign in to comment.