Class: SearchController

Inherits:
ApplicationController show all
Defined in:
app/controllers/search_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_navable, #current_navable=, #current_user, #point_navigation_to, #redirect_www_subdomain, #set_locale

Instance Method Details

#indexObject



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/search_controller.rb', line 6

def index
  query_string = params[ :query ]
  if query_string.present?
    
    # log search query for metrics analysis
    #
    metric_logger.log_event({query: query_string}, type: :search)
    current_user.try(:update_last_seen_activity, "sucht gerade etwas", nil)

    # browse users, pages, groups and events
    #
    q = "%" + query_string.gsub( ' ', '%' ) + "%"
    @users = User.where("CONCAT(first_name, ' ', last_name) LIKE ?", q)
      .order('last_name', 'first_name')
    @pages = Page.where("title like ? OR content like ?", q, q)
      .order('title')
    @groups = Group.where( "name like ?", q )
    @events = Event.where("name like ?", q).order('start_at DESC')

    # browse profile fields
    #
    profile_fields = ProfileField.where("value like ? or label like ?", q, q).collect do |profile_field|
      profile_field.parent || profile_field
    end.uniq
    profile_fields.each do |profile_field|
      if profile_field.profileable.kind_of? User
        @users << profile_field.profileable
      elsif profile_field.profileable.kind_of? Group
        @groups << profile_field.profileable
      end
    end
    
    # browse attachments
    #
    attachments = Attachment.where("title like ? or description like ?", q, q).where(parent_type: 'Page')
    @pages += attachments.collect { |attachment| attachment.parent }
    
    # eleminiate duplicate results
    #
    @users = @users.uniq
    @pages = @pages.uniq
    @groups = @groups.uniq
    @events = @events.uniq

    # AUTHORIZATION
    #
    @users = filter_by_authorization(@users)
    @pages = filter_by_authorization(@pages)
    @groups = filter_by_authorization(@groups)
    @events = filter_by_authorization(@events)

    @results = @users + @pages + @groups + @events
    if @results.count == 1
      redirect_to @results.first
    end
    
    if @results.count < 100
      @large_map_address_fields = @results.collect do |result|
        result.profile_fields.where(type: "ProfileFieldTypes::Address") if result.respond_to? :profile_fields
      end.flatten - [nil]
    end

    @pages = nil if @pages.count == 0
    @users = nil if @users.count == 0
    @groups = nil if @groups.count == 0
    @events = nil if @events.count == 0
    @results = nil if @results.count == 0

  end
  @navable = Page.find_intranet_root
  @title = "Suche: #{query_string}"

end

#lucky_guessObject

This action results in a redirection to the search result considered to be a lucky guess.

/search/guess?query=FooBar
would redirect to the Page with the title "FooBar".


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/search_controller.rb', line 86

def lucky_guess
  query_string = params[:query]
  if query_string.present?
    @result = Event.where(name: query_string).limit(1).first
    @result ||= Page.where(title: query_string).limit(1).first
    @result ||= Group.where(name: query_string).limit(1).first
    @result ||= User.find_by_name(query_string)
    @result ||= User.find_by_title(query_string)
    if @result
      redirect_to @result if can? :read, @result
    else
      redirect_to :action => :index, query: query_string
    end
  else
    redirect_to :action => :index
  end
end

#previewObject

This returns title and body of a preview field (quick search).



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/search_controller.rb', line 106

def preview
  @object = find_preview_object(params[:query])
  respond_to do |format|
    format.json do
      if @object
        preview_template = "search/preview_#{@object.class.name.underscore}" # e.g. preview_user
        render json: {
          :title => 'Suchergebnis',
          :body => render_to_string(partial: preview_template, locals: {query: params['query'], obj: @object}, formats: ['html'])
        }
      else
        head :no_content
      end
    end
  end
end