search.rb
1.53 KB
1
2
3
4
5
6
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
32
33
34
35
36
37
38
39
40
41
42
43
module Noosfero
module API
module V1
class Search < Grape::API
resource :search do
resource :article do
get do
# Security checks
sanitize_params_hash(params)
# APIHelpers
asset = :articles
context = environment
profile = environment.profiles.find(params[:profile_id]) if params[:profile_id]
scope = profile.nil? ? environment.articles.is_public : profile.articles.is_public
scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article')
scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present?
scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present?
query = params[:query] || ""
order = "more_recent"
options = {:filter => order, :template_id => params[:template_id]}
paginate_options = params.select{|k,v| [:page, :per_page].include?(k.to_sym)}.symbolize_keys
paginate_options.each_pair{|k,v| v=v.to_i}
paginate_options[:page]=1 if !paginate_options.keys.include?(:page)
search_result = find_by_contents(asset, context, scope, query, paginate_options, options)
articles = search_result[:results]
result = present_articles(articles)
result
end
end
end
end
end
end
end