From 013637a4fc7d50c73777e13e3c93f9dca75cfb44 Mon Sep 17 00:00:00 2001 From: benni Date: Thu, 19 May 2011 22:22:05 +0200 Subject: [PATCH] Refactored shared article import. Added meta search gem. --- Gemfile | 1 + Gemfile.lock | 6 +++ app/controllers/articles_controller.rb | 41 ++++--------------- app/helpers/application_helper.rb | 9 ---- app/models/shared_article.rb | 21 +++++++++- .../articles/_import_search_results.haml | 10 ++--- app/views/articles/index.haml | 7 ++-- app/views/articles/shared.js.erb | 1 + 8 files changed, 42 insertions(+), 54 deletions(-) create mode 100644 app/views/articles/shared.js.erb diff --git a/Gemfile b/Gemfile index 0906428c..46c4a51d 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,7 @@ gem 'jquery-rails' gem 'simple_form' gem 'rails3_acts_as_paranoid' gem 'meta_where' +gem 'meta_search' gem 'inherited_resources' group :development do diff --git a/Gemfile.lock b/Gemfile.lock index b70016c1..9e35c0a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -50,6 +50,11 @@ GEM i18n (>= 0.4.0) mime-types (~> 1.16) treetop (~> 1.4.8) + meta_search (1.0.5) + actionpack (~> 3.0.2) + activerecord (~> 3.0.2) + activesupport (~> 3.0.2) + arel (~> 2.0.2) meta_where (1.0.4) activerecord (~> 3.0.0) activesupport (~> 3.0.0) @@ -107,6 +112,7 @@ DEPENDENCIES hirb inherited_resources jquery-rails + meta_search meta_where mysql prawn (<= 0.6.3) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index e98f279f..fa9714dd 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -201,43 +201,18 @@ class ArticlesController < ApplicationController # renders a view to import articles in local database # def shared - conditions = [] - conditions << "supplier_id = #{@supplier.shared_supplier.id}" - # check for keywords - conditions << params[:import_query].split(' ').collect { |keyword| "name LIKE '%#{keyword}%'" }.join(' AND ') unless params[:import_query].blank? - # check for selected lists - conditions << "(" + params[:lists].collect {|list| "list = '#{list[0]}'"}.join(" OR ") + ")" if params[:lists] - # check for regional articles - conditions << "origin = 'REG'" if params[:regional] - - @articles = SharedArticle.paginate :page => params[:page], :per_page => 10, :conditions => conditions.join(" AND ") - render :update do |page| - page.replace_html 'search_results', :partial => "import_search_results" - end + # build array of keywords, required for meta search _all suffix + params[:search][:name_contains_all] = params[:search][:name_contains_all].split(' ') if params[:search] + # Build search with meta search plugin + @search = @supplier.shared_supplier.shared_articles.search(params[:search]) + @articles = @search.paginate :page => params[:page], :per_page => 10 + render :layout => false end # fills a form whith values of the selected shared_article def import - shared_article = SharedArticle.find(params[:shared_article_id]) - @article = Article.new( - :supplier_id => params[:supplier_id], - :name => shared_article.name, - :unit => shared_article.unit, - :note => shared_article.note, - :manufacturer => shared_article.manufacturer, - :origin => shared_article.origin, - :price => shared_article.price, - :tax => shared_article.tax, - :deposit => shared_article.deposit, - :unit_quantity => shared_article.unit_quantity, - :order_number => shared_article.number, - # convert to db-compatible-string - :shared_updated_on => shared_article.updated_on.to_formatted_s(:db)) - - render :update do |page| - page["edit_article"].replace_html :partial => 'new' - page["edit_article"].show - end + @article = SharedArticle.find(params[:shared_article_id]).build_new_article + render :action => 'new', :layout => false end # sync all articles with the external database diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6c6d2fbb..89014248 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -88,15 +88,6 @@ module ApplicationHelper return weekdays[dayNumber] end - # highlights a phrase in given text - # based on the rails text-helper 'highlight' - def highlight_phrase(text, phrase, highlighter = '\1') - unless phrase.blank? || text.nil? - phrase.split(' ').each {|keyword| text.gsub!(/(#{Regexp.escape(keyword)})/i, highlighter)} - end - return text - end - # to set a title for both the h1-tag and the title in the header def title(page_title, show_title = true) content_for(:title) { page_title.to_s } diff --git a/app/models/shared_article.rb b/app/models/shared_article.rb index 3606e77c..7a99f41a 100644 --- a/app/models/shared_article.rb +++ b/app/models/shared_article.rb @@ -23,11 +23,28 @@ # class SharedArticle < ActiveRecord::Base - + # connect to database from sharedLists-Application SharedArticle.establish_connection(Foodsoft.config[:shared_lists]) # set correct table_name in external DB set_table_name :articles - + belongs_to :shared_supplier, :foreign_key => :supplier_id + + def build_new_article + shared_supplier.supplier.articles.build( + :name => name, + :unit => unit, + :note => note, + :manufacturer => manufacturer, + :origin => origin, + :price => price, + :tax => tax, + :deposit => deposit, + :unit_quantity => unit_quantity, + :order_number => number, + # convert to db-compatible-string + :shared_updated_on => updated_on.to_formatted_s(:db) + ) + end end diff --git a/app/views/articles/_import_search_results.haml b/app/views/articles/_import_search_results.haml index 180425ae..89c3ce33 100644 --- a/app/views/articles/_import_search_results.haml +++ b/app/views/articles/_import_search_results.haml @@ -1,5 +1,4 @@ -%p= pagination_links_remote @articles, :per_page => 10, | - :params => {:import_query => params[:import_query], :lists => params[:lists], :regional => params[:regional]} | +%p= pagination_links_remote @articles, :per_page => 10, :params => {:search => params[:search]} %table.list %thead %tr @@ -14,14 +13,13 @@ %tbody - for article in @articles %tr{:class => cycle('even','odd', :name => 'import_search_results')} - %td= highlight_phrase article.name, params[:import_query] + %td= highlight article.name, params[:search][:name_contains_all] %td= article.origin %td= article.manufacturer %td= article.note %td= number_to_currency(article.price) %td= article.unit %td= article.unit_quantity - %td= link_to_remote 'importieren', | - :url => import_supplier_articles_path(@supplier, :shared_article_id => article.id), | - :method => :get | + %td= link_to 'importieren', import_supplier_articles_path(@supplier, :shared_article_id => article.id), + :remote => true \ No newline at end of file diff --git a/app/views/articles/index.haml b/app/views/articles/index.haml index d174bcbd..12e1b2ab 100644 --- a/app/views/articles/index.haml +++ b/app/views/articles/index.haml @@ -26,18 +26,17 @@ .column_content #search{:style => "padding-bottom:3em"} = form_tag shared_supplier_articles_path(@supplier), :method => :get, :remote => true do - = text_field_tag :import_query, params['import_query'], :size => 10 + = text_field_tag "search[name_contains_all]", "", :size => 10 = submit_tag "Suchen" - if @supplier.shared_supplier.lists Suche in folgenden Listen: - @supplier.shared_supplier.lists.each do |token, name| - = check_box_tag "lists[#{token}]", "1", true + = check_box_tag "search[list_equals_any][]", token, true = name | Nur aus der Region: - = check_box_tag "regional", "1", false + = check_box_tag "search[origin_equals]", "REG", false #search_results - // "import_search_results" will be rendered = link_to "Schließen", "#import", 'data-toggle-this' => '#import' .single_column{:style => 'width:100%; clear:both'} diff --git a/app/views/articles/shared.js.erb b/app/views/articles/shared.js.erb new file mode 100644 index 00000000..08ce3fa7 --- /dev/null +++ b/app/views/articles/shared.js.erb @@ -0,0 +1 @@ +$('#search_results').html('<%= escape_javascript(render("import_search_results")) %>'); \ No newline at end of file