Class: Droom::SuggestionsController

Inherits:
EngineController show all
Defined in:
app/controllers/droom/suggestions_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/droom/suggestions_controller.rb', line 7

def index
  fragment = params[:term]
  max = params[:limit] || 10
  show_when_empty = params[:empty] == "all"
  if !show_when_empty && fragment.blank?
    @suggestions = []
  else
    if @types.include?('event') && fragment.length > 6 && span = Chronic.parse(fragment, :guess => false)
      @suggestions = Droom::Event.falling_within(span).visible_to(current_user.person)
      @title = span.width > 86400 ? "Events in #{fragment}" : "Events on #{fragment}"
    else
      @suggestions = @klasses.collect {|klass|
        klass.constantize.visible_to(current_user.person).matching(fragment).limit(max.to_i)
      }.flatten.sort_by(&:name).slice(0, max.to_i)
    end
  end
  respond_with @suggestions do |format|
    format.json {
      render :json => @suggestions.map(&:as_suggestion).to_json
    }
    format.js {
      render :partial => "droom/shared/suggestions"
    }
  end
end