Refactored shared article import. Added meta search gem.
This commit is contained in:
parent
1e33411516
commit
013637a4fc
8 changed files with 42 additions and 54 deletions
1
Gemfile
1
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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 = '<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
|
||||
def title(page_title, show_title = true)
|
||||
content_for(:title) { page_title.to_s }
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
@ -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'}
|
||||
|
|
1
app/views/articles/shared.js.erb
Normal file
1
app/views/articles/shared.js.erb
Normal file
|
@ -0,0 +1 @@
|
|||
$('#search_results').html('<%= escape_javascript(render("import_search_results")) %>');
|
Loading…
Reference in a new issue