Permalink
Browse files

implement poster image picker

  • Loading branch information...
1 parent dbedea3 commit a7d37d5d4bf65edac7c3fbb319a9afd4a1642102 @mislav committed Nov 6, 2012
@@ -6,3 +6,24 @@ $(document).on 'click', '#facebox .netflix_picker article', (e) ->
data: netflix_id: $(this).data('netflix-id')
$.facebox.close()
false
+
+$(document).on 'click', '#facebox .poster_picker article', (e) ->
+ if e.which is 1
+ images = $(this).find('img')
+ params =
+ movie:
+ poster_medium_url: images.eq(0).attr('src')
+ poster_small_url: images.eq(1).attr('src')
+
+ $.ajax
+ type: 'PUT'
+ url: location.pathname
+ data: JSON.stringify(params)
+ contentType: 'application/json'
+
+ # prevents bug in Chrome where we remain scrolled completely off page
+ window.scrollTo 0
+ $.facebox.close()
+
+ $('.movie img.poster').attr('src', params.movie.poster_medium_url)
+ false
@@ -60,6 +60,13 @@ def edit
render :layout => !request.xhr?
end
+ def pick_poster
+ @posters = PosterFinder.call @movie
+
+ response.content_type = Mime::HTML
+ render :layout => !request.xhr?
+ end
+
def raw
@data = case params[:kind]
when 'tmdb'
@@ -73,7 +80,12 @@ def raw
def update
@movie.update_and_lock params[:movie]
@movie.save
- redirect_to movie_url(@movie)
+
+ if request.xhr?
+ head :ok
+ else
+ redirect_to movie_url(@movie)
+ end
end
def add_to_watch
@@ -0,0 +1,19 @@
+module PosterFinder
+ def self.medium_width() 185 end
+ def self.small_width() 92 end
+
+ def self.call movie
+ tmdb_config = Tmdb.configuration
+ tmdb_images = Tmdb.poster_images movie.tmdb_id
+
+ Rails.logger.debug "found %d poster images" % tmdb_images.size
+
+ tmdb_images.sort_by {|image| image.average_rating }.reverse.map do |image|
+ Poster.new \
+ tmdb_config.poster_url(medium_width * 2, image.file_path),
+ tmdb_config.poster_url(small_width * 2, image.file_path)
+ end
+ end
+
+ Poster = Struct.new(:medium_url, :small_url)
+end
@@ -0,0 +1,6 @@
+<% for poster in @posters -%>
+ <article>
+ <%= image_tag poster.medium_url, width: 185 %>
+ <%= image_tag poster.small_url, width: 92 %>
+ </article>
+<% end -%>
@@ -49,6 +49,7 @@
<% if @movie.customizable_plot? %>
<li><%= link_to "switch to #{@movie.next_plot_source} plot", change_plot_field_movie_path(@movie.id), :method => 'put' %></li>
<% end %>
+ <li><%= link_to "change poster image", pick_poster_movie_path(@movie.id), :rel => 'facebox.poster_picker' %></li>
<% if @movie.netflix_id.blank? %>
<li><%= link_to "link to Netflix", root_path(netflix: true, q: @movie.title), :rel => 'facebox.netflix_picker' %></li>
<% else %>
View
@@ -9,6 +9,7 @@
member do
get :wikipedia
get :raw
+ get :pick_poster
put :change_plot_field
put :link_to_netflix
put :add_to_watch
View
@@ -171,6 +171,14 @@ def url
end
+ class PosterImage < NibblerJSON
+ element :file_path
+ element :width
+ element :height
+ element 'vote_average' => :average_rating
+ element 'vote_count' => :votes
+ end
+
endpoint(:movie_search, 'search/movie?{-join|&|api_key,query}') do
elements :results, :with => Movie
alias_method :movies, :results
@@ -200,6 +208,14 @@ def self.configuration
end
endpoint(:movie_cast, 'movie/{tmdb_id}/casts?api_key={api_key}', Cast)
+
+ endpoint(:movie_images, 'movie/{tmdb_id}/images?language=en&api_key={api_key}') do
+ elements :posters, :with => PosterImage
+ end
+
+ def self.poster_images tmdb_id
+ get(:movie_images, :tmdb_id => tmdb_id).posters
+ end
end
require 'tmdb_ignores'

0 comments on commit a7d37d5

Please sign in to comment.