Created
October 7, 2013 15:46
-
-
Save mxaly/6870108 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MediaController < ApplicationController | |
before_filter :authenticate_user!, :except => [:show, :index] | |
respond_to :html, :json | |
expose_decorated(:featured, decorator: MediumDecorator) { sorted_media 'featured' } | |
expose_decorated(:breaking, decorator: MediumDecorator) { sorted_media 'breaking' } | |
expose_decorated(:media) { Kaminari.paginate_array(search_results).page(params[:page]).per(10) } | |
expose_decorated(:medium, decorator: MediumDecorator, model: Medium) | |
def index | |
respond_with(media) | |
end | |
private | |
def sorted_media mask = 'all' | |
(Medium.public_send(mask) + Collection.public_send(mask)).sort_by{ |medium| medium.start_occurred_at }.reverse | |
end | |
def media_types | |
params[:search][:media_types].delete_if{ |type| type.empty? } | |
end | |
def media_statuses | |
statuses = params[:search][:media_status] | |
statuses.present? ? statuses.map { |status| status.to_i } : [Medium::STATUSES[:approved], Medium::STATUSES[:in_review]] | |
end | |
def search_collections | |
search = params[:search] | |
Collection.solr_search do | |
fulltext search[:title] do | |
fields(:name) | |
end | |
with(:start_occurred_at).less_than search[:time_range_end] | |
with(:start_occurred_at).greater_than search[:time_range_start] | |
with(:media_statuses).any_of(media_statuses) | |
with(:content_types).any_of(media_types) | |
if search[:longitude].present? | |
with(:location).in_radius(search[:latitude], search[:longitude], search[:radius]) | |
end | |
end | |
end | |
def search_media | |
search = params[:search] | |
Medium.solr_search do | |
fulltext search[:title] do | |
fields(:title) | |
end | |
with(:start_occurred_at).less_than search[:time_range_end] | |
with(:start_occurred_at).greater_than search[:time_range_start] | |
with(:status, media_statuses) | |
with(:content_type, media_types) | |
if search[:longitude].present? | |
with(:location).in_radius(search[:latitude], search[:longitude], search[:radius]) | |
end | |
end | |
end | |
def search_results | |
if params[:search] | |
(search_collections.results + search_media.results).sort_by{ |medium| medium.created_at }.reverse | |
else | |
sorted_media | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment