Refactored shared article import. Added meta search gem.

This commit is contained in:
benni 2011-05-19 22:22:05 +02:00
parent 1e33411516
commit 013637a4fc
8 changed files with 42 additions and 54 deletions

View file

@ -12,6 +12,7 @@ gem 'jquery-rails'
gem 'simple_form' gem 'simple_form'
gem 'rails3_acts_as_paranoid' gem 'rails3_acts_as_paranoid'
gem 'meta_where' gem 'meta_where'
gem 'meta_search'
gem 'inherited_resources' gem 'inherited_resources'
group :development do group :development do

View file

@ -50,6 +50,11 @@ GEM
i18n (>= 0.4.0) i18n (>= 0.4.0)
mime-types (~> 1.16) mime-types (~> 1.16)
treetop (~> 1.4.8) 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) meta_where (1.0.4)
activerecord (~> 3.0.0) activerecord (~> 3.0.0)
activesupport (~> 3.0.0) activesupport (~> 3.0.0)
@ -107,6 +112,7 @@ DEPENDENCIES
hirb hirb
inherited_resources inherited_resources
jquery-rails jquery-rails
meta_search
meta_where meta_where
mysql mysql
prawn (<= 0.6.3) prawn (<= 0.6.3)

View file

@ -201,43 +201,18 @@ class ArticlesController < ApplicationController
# renders a view to import articles in local database # renders a view to import articles in local database
# #
def shared def shared
conditions = [] # build array of keywords, required for meta search _all suffix
conditions << "supplier_id = #{@supplier.shared_supplier.id}" params[:search][:name_contains_all] = params[:search][:name_contains_all].split(' ') if params[:search]
# check for keywords # Build search with meta search plugin
conditions << params[:import_query].split(' ').collect { |keyword| "name LIKE '%#{keyword}%'" }.join(' AND ') unless params[:import_query].blank? @search = @supplier.shared_supplier.shared_articles.search(params[:search])
# check for selected lists @articles = @search.paginate :page => params[:page], :per_page => 10
conditions << "(" + params[:lists].collect {|list| "list = '#{list[0]}'"}.join(" OR ") + ")" if params[:lists] render :layout => false
# 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
end end
# fills a form whith values of the selected shared_article # fills a form whith values of the selected shared_article
def import def import
shared_article = SharedArticle.find(params[:shared_article_id]) @article = SharedArticle.find(params[:shared_article_id]).build_new_article
@article = Article.new( render :action => 'new', :layout => false
: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
end end
# sync all articles with the external database # sync all articles with the external database

View file

@ -88,15 +88,6 @@ module ApplicationHelper
return weekdays[dayNumber] return weekdays[dayNumber]
end end
# highlights a phrase in given text
# based on the rails text-helper 'highlight'
def highlight_phrase(text, phrase, highlighter = '<strong class="highlight">\1</strong>')
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 # to set a title for both the h1-tag and the title in the header
def title(page_title, show_title = true) def title(page_title, show_title = true)
content_for(:title) { page_title.to_s } content_for(:title) { page_title.to_s }

View file

@ -30,4 +30,21 @@ class SharedArticle < ActiveRecord::Base
set_table_name :articles set_table_name :articles
belongs_to :shared_supplier, :foreign_key => :supplier_id 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 end

View file

@ -1,5 +1,4 @@
%p= pagination_links_remote @articles, :per_page => 10, | %p= pagination_links_remote @articles, :per_page => 10, :params => {:search => params[:search]}
:params => {:import_query => params[:import_query], :lists => params[:lists], :regional => params[:regional]} |
%table.list %table.list
%thead %thead
%tr %tr
@ -14,14 +13,13 @@
%tbody %tbody
- for article in @articles - for article in @articles
%tr{:class => cycle('even','odd', :name => 'import_search_results')} %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.origin
%td= article.manufacturer %td= article.manufacturer
%td= article.note %td= article.note
%td= number_to_currency(article.price) %td= number_to_currency(article.price)
%td= article.unit %td= article.unit
%td= article.unit_quantity %td= article.unit_quantity
%td= link_to_remote 'importieren', | %td= link_to 'importieren', import_supplier_articles_path(@supplier, :shared_article_id => article.id),
:url => import_supplier_articles_path(@supplier, :shared_article_id => article.id), | :remote => true
:method => :get |

View file

@ -26,18 +26,17 @@
.column_content .column_content
#search{:style => "padding-bottom:3em"} #search{:style => "padding-bottom:3em"}
= form_tag shared_supplier_articles_path(@supplier), :method => :get, :remote => true do = 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" = submit_tag "Suchen"
- if @supplier.shared_supplier.lists - if @supplier.shared_supplier.lists
Suche in folgenden Listen: Suche in folgenden Listen:
- @supplier.shared_supplier.lists.each do |token, name| - @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 = name
| |
Nur aus der Region: Nur aus der Region:
= check_box_tag "regional", "1", false = check_box_tag "search[origin_equals]", "REG", false
#search_results #search_results
// "import_search_results" will be rendered
= link_to "Schließen", "#import", 'data-toggle-this' => '#import' = link_to "Schließen", "#import", 'data-toggle-this' => '#import'
.single_column{:style => 'width:100%; clear:both'} .single_column{:style => 'width:100%; clear:both'}

View file

@ -0,0 +1 @@
$('#search_results').html('<%= escape_javascript(render("import_search_results")) %>');