From d3ac7504dc85b84c17ef70bf690cc782e4928148 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Sat, 8 Feb 2014 16:39:56 +0100 Subject: [PATCH] search function --- app/controllers/home_controller.rb | 15 +++++++++ app/models/fetprofile.rb | 1 + app/models/neuigkeit.rb | 3 +- app/models/thema.rb | 1 + app/views/fetprofiles/_compact.html.erb | 18 +++++++++++ app/views/fetprofiles/_fetprofile.html.erb | 16 +++------- app/views/home/_search_results.html.erb | 37 ++++++++++++++++++++++ app/views/home/search.html.erb | 17 ++++++++++ app/views/home/search.js.erb | 1 + app/views/neuigkeiten/_compact.html.erb | 25 +++++++++++++++ app/views/neuigkeiten/_neuigkeit.html.erb | 26 +-------------- 11 files changed, 122 insertions(+), 38 deletions(-) create mode 100644 app/views/fetprofiles/_compact.html.erb create mode 100644 app/views/home/_search_results.html.erb create mode 100644 app/views/home/search.html.erb create mode 100644 app/views/home/search.js.erb create mode 100644 app/views/neuigkeiten/_compact.html.erb diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index a2b6ea5..846c82b 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -11,4 +11,19 @@ class HomeController < ApplicationController def linksnotimplemented render 'links_notimplemented' end + def search + unless params['query'].nil? || params['query'].empty? + @neuigkeiten=Neuigkeit.search(params['query']) + @fetprofiles = Fetprofile.search(params['query']) + @themen=Thema.search(params['query']) + else + @neuigkeiten=[] + @fetprofiles=[] + @themen=[] + end + respond_to do |format| + format.html + format.js + end + end end diff --git a/app/models/fetprofile.rb b/app/models/fetprofile.rb index 7c23182..92bfb28 100644 --- a/app/models/fetprofile.rb +++ b/app/models/fetprofile.rb @@ -24,6 +24,7 @@ validates :desc, :presence=>true validates :nachname, length:{minimum: 3},:presence=>true validates :vorname, length:{minimum: 3},:presence=>true has_many :users + scope :search, ->(query) {where("nachname like ? or vorname like ? or short like ?", "%#{query}%", "%#{query}%", "%#{query}%")} accepts_nested_attributes_for :memberships, :reject_if=>lambda{|a| a[:typ].blank?|| a[:start].blank? ||a[:gremium_id].blank?}, :allow_destroy=>true diff --git a/app/models/neuigkeit.rb b/app/models/neuigkeit.rb index 21fe98c..2a0f582 100755 --- a/app/models/neuigkeit.rb +++ b/app/models/neuigkeit.rb @@ -24,9 +24,10 @@ class Neuigkeit < ActiveRecord::Base has_many :calentries, as: :object mount_uploader :picture, PictureUploader scope :published, -> {where("datum <= ? AND datum IS NOT NULL", Time.now.to_date).order(:datum).reverse_order} - scope :recent, -> { published.where("updated_at >= ? ",Time.now - 7.days)} + scope :recent, -> { published.order(:datum).reverse_order.limit(15)} scope :unpublished, -> {where("datum >= ? OR datum IS NULL", Date.today)} scope :public, ->{includes(:rubrik).where("rubriken.public"=>:true)} + scope :search, ->(query) {where("text like ? or title like ?", "%#{query}%", "%#{query}%")} accepts_nested_attributes_for :calentries, :allow_destroy=>true , :reject_if=> lambda{|a| a[:start].blank?} before_validation :sanitize def datum_nilsave diff --git a/app/models/thema.rb b/app/models/thema.rb index 22128cd..58097ac 100644 --- a/app/models/thema.rb +++ b/app/models/thema.rb @@ -18,6 +18,7 @@ class Thema < ActiveRecord::Base has_one :gremium validates :themengruppe, :presence => true validates :title, :presence => true + scope :search, ->(query) {where("text like ? or title like ?", "%#{query}%", "%#{query}%")} translates :title,:text, :versioning =>true, :fallbacks_for_empty_translations => true end diff --git a/app/views/fetprofiles/_compact.html.erb b/app/views/fetprofiles/_compact.html.erb new file mode 100644 index 0000000..fe1f3df --- /dev/null +++ b/app/views/fetprofiles/_compact.html.erb @@ -0,0 +1,18 @@ +
+ + + <%= image_tag fetprofile.picture.portrait.url %> + + +
+

<%= fetprofile.name %>

+ +

<%= fetprofile.fetmail %>

+ <% if false %> +

<%= fetprofile.desc %>

+ <% end %> + +
+ +
+ diff --git a/app/views/fetprofiles/_fetprofile.html.erb b/app/views/fetprofiles/_fetprofile.html.erb index 65a4039..b934e62 100644 --- a/app/views/fetprofiles/_fetprofile.html.erb +++ b/app/views/fetprofiles/_fetprofile.html.erb @@ -1,12 +1,4 @@ - -
-
- <%= image_tag fetprofile.picture.portrait.url %> -
-
- -

<%= link_to fetprofile.name, fetprofile %>

-

<%= fetprofile.fetmail %>

-

<%= fetprofile.desc.split(" ")[1..20].join(" ") %>

-

<%= fetprofile.active %>

-
+ +<%= link_to fetprofile, {class: :linkbox} do %> + <%= render :partial=>"fetprofiles/compact", :locals=>{:fetprofile=>fetprofile} %> +<% end %> diff --git a/app/views/home/_search_results.html.erb b/app/views/home/_search_results.html.erb new file mode 100644 index 0000000..9989e84 --- /dev/null +++ b/app/views/home/_search_results.html.erb @@ -0,0 +1,37 @@ + +<% unless @neuigkeiten.empty? %> +

Neuigkeiten

+<% end %> +<% @neuigkeiten.each do |neuigkeit| %> + +<% end %> + +<% unless @fetprofiles.empty? %> +

Mitarbeiter

+<% end %> + +<% @fetprofiles.each do |fetprofile| %> + +<% end %> + +<% unless @fetprofiles.empty? %> +

Themen

+<% end %> + +<% @themen.each do |thema| %> + +<% end %> diff --git a/app/views/home/search.html.erb b/app/views/home/search.html.erb new file mode 100644 index 0000000..a22bf16 --- /dev/null +++ b/app/views/home/search.html.erb @@ -0,0 +1,17 @@ +
+<%= semantic_form_for :search,:remote=>true, :url=>search_home_index_path, :html=>{:id=>"search_form", :method=>'get'} do |f| %> +<%= f.input :query, :input_html => { :name => 'query' } %> +<% end %> + +
+<%= render :partial=>"home/search_results" %> +
+ +
diff --git a/app/views/home/search.js.erb b/app/views/home/search.js.erb new file mode 100644 index 0000000..a126d53 --- /dev/null +++ b/app/views/home/search.js.erb @@ -0,0 +1 @@ +$("#searchresults").html("<%=escape_javascript( render :partial=>'home/search_results' )%>") \ No newline at end of file diff --git a/app/views/neuigkeiten/_compact.html.erb b/app/views/neuigkeiten/_compact.html.erb new file mode 100644 index 0000000..a6e8a3c --- /dev/null +++ b/app/views/neuigkeiten/_compact.html.erb @@ -0,0 +1,25 @@ + +
+
+


<%= image_tag neuigkeit.picture.thumb.url unless neuigkeit.picture.url.nil? %> +

+
+
+
+ <%= neuigkeit.rubrik.name %> + <%= "am "+ I18n.l(neuigkeit.try(:datum).try(:to_date)) unless neuigkeit.try(:datum).try(:to_date).nil? %> +
+

+ <%= neuigkeit.title%> +

+ <%= raw(neuigkeit.text_first_words) unless neuigkeit.text.nil?%> +
+<% if neuigkeit.has_calentries? %> +
+<%= image_tag("/iconnavy/time.png") %> + + <%= neuigkeit.calentries.first.text %> +
+ <% end %> + +
diff --git a/app/views/neuigkeiten/_neuigkeit.html.erb b/app/views/neuigkeiten/_neuigkeit.html.erb index e3cc1b9..dd98a3a 100755 --- a/app/views/neuigkeiten/_neuigkeit.html.erb +++ b/app/views/neuigkeiten/_neuigkeit.html.erb @@ -1,28 +1,4 @@ <%= link_to [neuigkeit.rubrik,neuigkeit], {class: :linkbox} do %> -
-
-


<%= image_tag neuigkeit.picture.thumb.url unless neuigkeit.picture.url.nil? %> -

-
-
-
- <%= neuigkeit.rubrik.name %> - <%= "am "+ I18n.l(neuigkeit.try(:datum).try(:to_date)) unless neuigkeit.try(:datum).try(:to_date).nil? %> -
-

- <%= neuigkeit.title%> -

- <%= raw(neuigkeit.text_first_words) unless neuigkeit.text.nil?%> -
-<% if neuigkeit.has_calentries? %> -
-<%= image_tag("/iconnavy/time.png") %> - - <%= neuigkeit.calentries.first.text %> -
- <% end %> - -
- +<%= render :partial=>"neuigkeiten/compact",:locals=> {:neuigkeit=> neuigkeit} %> <% end %>