Skip to content

Commit

Permalink
Refactor Discover Search and Two Actor Search
Browse files Browse the repository at this point in the history
Ensure that @not_found is returned properly and proper params
are passed.
  • Loading branch information
mikevallano committed Jul 13, 2016
1 parent d554680 commit 2f32955
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
28 changes: 15 additions & 13 deletions app/controllers/tmdb_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,17 @@ def actor_search

def two_actor_search
if params[:actor] && params[:actor2]
@actor = I18n.transliterate(params[:actor])
@actor2 = I18n.transliterate(params[:actor2])
if params[:page]
@page = params[:page]
else
@page = 1
params[:actor] = I18n.transliterate(params[:actor])
params[:actor2] = I18n.transliterate(params[:actor2])
if !params[:page].present?
params[:page] = 1
end
if params[:sort_by].present?
@sort_by = params[:sort_by]
else
@sort_by = "popularity"
if !params[:sort_by].present?
params[:sort_by] = "popularity"
end
# binding.pry
# tmdb_handler_two_actor_search(@name_one, @name_two)
tmdb_handler_discover_search(nil, nil, nil, nil, @actor, @actor2, nil, nil, @sort_by, @page)
tmdb_handler_discover_search(params)
end
end

Expand Down Expand Up @@ -135,14 +132,19 @@ def director_search
end

def discover_search
#format date/year hash passed in params
params[:date].present? ? params[:year] = params[:date][:year] : params[:year] = nil
puts "params: #{params}"
#format date/year hash passed in params. otherwise year is passed directly on paginated pages
params[:year] = params[:date][:year] if params[:date].present?

@cleaned_params = params.slice(:sort_by, :year, :genre, :actor, :actor2,
:company, :mpaa_rating, :year_select, :page)

puts "cleaned params: #{@cleaned_params}"

@passed_params = @cleaned_params.select{ |k, v| v.present?}

puts "passed params: #{@passed_params}"

if @passed_params.any?
parse_params(@passed_params) #module to help parse

Expand Down
2 changes: 1 addition & 1 deletion app/views/tmdb/discover_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<h1>Advanced Search</h1>

<% if !@movies.present? %>
<% if !@movies.present? && !@not_found.present? %>

<h2>All fields are optional. Search by whatever you want.</h2>

Expand Down
47 changes: 22 additions & 25 deletions app/views/tmdb/two_actor_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1>Find Common Movies Between 2 Actors</h1>


<% if !@actor.present? && !@actor2.present? %>
<% if !@actor.present? && !@actor2.present? && !@not_found.present? %>

<p>Enter two actors and see the movies they've been in together.</p>

Expand All @@ -13,38 +13,35 @@
<%= submit_tag "Search", id: "search_button_two_actor_search", class: "form-control-submit search-form-submit" %>
<% end %>
<% else %>
<% if @not_found.present? %>
<h2>Hmm. <%= @not_found %></h2>
<p> Check your spelling and try again.</p>
<p class="button-main"><%= link_to "Search Again", two_actor_search_path %></p>
<% elsif @not_found.present? %>

<% else %> <!-- # @not_found.present? -->
<h2><%= "Those two actors have been in #{@movies.size} movies together:" %></h2>
<br>
<h2>Hmm. <%= @not_found %></h2>
<p> Check your spelling and try again.</p>
<p class="button-main"><%= link_to "Search Again", two_actor_search_path %></p>

<div id ="two_actor_search_loop">
<%= render partial: 'movies/movie_partial_loop', object: @movies %>
</div> <!-- movies_actor_search_loop -->
<% else %>
<h2><%= "Those two actors have been in #{@movies.size} movies together:" %></h2>
<br>

</br>
<p class="button-main"><%= link_to "Search Again", two_actor_search_path, id: "two_actor_search_again_link" %></p>
</br>
<div id ="two_actor_search_loop">
<%= render partial: 'movies/movie_partial_loop', object: @movies %>
</div> <!-- movies_actor_search_loop -->

<%= "Page #{@page} of #{@total_pages}" %></br>
</br>
<p class="button-main"><%= link_to "Search Again", two_actor_search_path, id: "two_actor_search_again_link" %></p>
</br>

<% if @previous_page.present? %>
<%= link_to "Previous page", actor_search_path(actor: I18n.transliterate("#{@actor}"), page: "#{@previous_page}") %></br>
<% end %>
<%= "Page #{@page} of #{@total_pages}" %></br>

<% if @next_page.present? %>
<%= link_to "Next page", actor_search_path(actor: I18n.transliterate("#{@actor}"), page: "#{@next_page}") %>
<% end %>
<% if @previous_page.present? %>
<%= link_to "Previous page", actor_search_path(actor: I18n.transliterate("#{@actor}"), page: "#{@previous_page}") %></br>
<% end %>
<% end %> <!-- # @not_found.present? -->
<% if @next_page.present? %>
<%= link_to "Next page", actor_search_path(actor: I18n.transliterate("#{@actor}"), page: "#{@next_page}") %>
<% end %>
<% end %> <!-- # if !@actor.present? && !@actor2.present? -->
<% end %> <!-- # @not_found.present? -->

</br>

Expand Down
12 changes: 10 additions & 2 deletions lib/tmdb_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,21 @@ def tmdb_handler_discover_search(params)
if @actor.present?
@actor1_url = "https://api.themoviedb.org/3/search/person?query=#{@actor}&api_key=#{ENV['tmdb_api_key']}"
@actor1_search_result = JSON.parse(open(@actor1_url).read, symbolize_names: true)[:results]
@actor1_search_result.present? ? @actor1_id = @actor1_search_result.first[:id] : @not_found = "No results for '#{actor}'."
if !@actor1_search_result.present?
return @not_found = "No results for '#{actor}'."
else
@actor1_id = @actor1_search_result.first[:id]
end
end

if @actor2.present?
@actor2_url = "https://api.themoviedb.org/3/search/person?query=#{@actor2}&api_key=#{ENV['tmdb_api_key']}"
@actor2_search_result = JSON.parse(open(@actor2_url).read, symbolize_names: true)[:results]
@actor2_search_result.present? ? @actor2_id = @actor2_search_result.first[:id] : @not_found = "No results for '#{actor2}'."
if !@actor2_search_result.present?
return @not_found = "No results for '#{actor2}'."
else
@actor2_id = @actor2_search_result.first[:id]
end
end

if @actor1_id.present? && @actor2_id.present?
Expand Down

0 comments on commit 2f32955

Please sign in to comment.