Merge pull request #30 from bennibu/rails3

Update
This commit is contained in:
JuliusR 2013-02-27 00:06:29 -08:00
commit 5032fbb63d
20 changed files with 88 additions and 303 deletions

View file

@ -1,8 +1,8 @@
class StockitController < ApplicationController class StockitController < ApplicationController
def index def index
@stock_articles = StockArticle.elements_for_index @stock_articles = StockArticle.includes(:supplier, :article_category).
@stock_article_selection = StockArticleSelection.new order('suppliers.name, article_categories.name, articles.name')
end end
def new def new
@ -32,11 +32,12 @@ class StockitController < ApplicationController
end end
def destroy def destroy
StockArticle.find(params[:id]).destroy @article = StockArticle.find(params[:id])
redirect_to stock_articles_path @article.destroy
render :layout => false
rescue => error rescue => error
flash[:error] = "Ein Fehler ist aufgetreten: " + error.message render :partial => "destroy_fail", :layout => false,
redirect_to stock_articles_path :locals => { :fail_msg => "Ein Fehler ist aufgetreten: " + error.message }
end end
#TODO: Fix this!! #TODO: Fix this!!

View file

@ -1,61 +0,0 @@
# encoding: utf-8
class StockitSelectionsController < ApplicationController
def index
@stock_article_selections = StockArticleSelection.find(:all, :order => 'created_at DESC')
end
def show
@stock_article_selection = StockArticleSelection.find(params[:id])
end
def create
@stock_article_selection = StockArticleSelection.new(params[:stock_article_selection])
@stock_article_selection.created_by = current_user
if @stock_article_selection.save
redirect_to(@stock_article_selection, :notice => 'Löschvorschlag für gewählte Artikel erstellt.')
else
@stock_articles = StockArticle.elements_for_index
render 'stockit/index'
end
end
def destroy # destroy selection without deleting articles
stock_article_selection = StockArticleSelection.find(params[:id])
stock_article_selection.destroy
redirect_to stock_article_selections_path, :notice => 'Löschvorschlag verworfen.'
end
def articles # destroy articles
stock_article_selection = StockArticleSelection.find(params[:id])
destroyed_articles_count = 0
failed_articles_count = 0
stock_article_selection.stock_articles.each do |article|
begin
article.destroy
destroyed_articles_count += 1
rescue => error # recover if article.destroy fails and continue with next article
failed_articles_count += 1
end
end
if destroyed_articles_count > 0
flash[:notice] = "#{destroyed_articles_count} gewählte Artikel gelöscht."
flash[:error] = "#{failed_articles_count} Artikel konnten nicht gelöscht werden." unless 0==failed_articles_count
else
flash[:error] = 'Löschvorgang fehlgeschlagen. Keine Artikel gelöscht.'
end
redirect_to stock_articles_path
end
def finished # delete all finished selections
finished_selections = StockArticleSelection.all.select { |sel| sel.deletable_count + sel.nondeletable_count <= 0 }
finished_selections.each { |sel| sel.destroy }
redirect_to stock_article_selections_path, :notice => 'Alle erledigten Löschvorschläge entfernt.'
end
end

View file

@ -1,18 +0,0 @@
# encoding: utf-8
module StockArticleSelectionsHelper
def article_deletion_classes(article)
className = "label label-success" # usual deletable case, maybe modified below
className = "label label-important" if article.quantity_available > 0
className = "label" if article.deleted?
className
end
def article_deletion_title(article)
myTitle = "Löschbar" # usual deletable case, maybe modified below
myTitle = "Nicht löschbar, da im Lager vorhanden" if article.quantity_available > 0
myTitle = "Bereits gelöscht" if article.deleted?
myTitle
end
end

View file

@ -1,18 +1,7 @@
# encoding: utf-8
module StockitHelper module StockitHelper
def stock_article_classes(article) def stock_article_classes(article)
class_names = [] class_names = []
class_names << "unavailable" if article.quantity_available <= 0 class_names << "unavailable" if article.quantity_available <= 0
class_names.join(" ") class_names.join(" ")
end end
def stock_article_delete_checkbox(article)
if article.quantity_available <= 0
check_box_tag "stock_article_selection[stock_article_ids][]", article.id, false,
{ :id => "checkbox_#{article.id}", :title => 'Zum löschen markieren' }
else
check_box_tag 'checkall', '1', false,
{ :disabled => true, :title => 'Verfügbare Artikel können nicht gelöscht werden.', :class => 'unavailable' }
end
end
end end

View file

@ -4,7 +4,7 @@ class Group < ActiveRecord::Base
has_many :memberships, :dependent => :destroy has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships has_many :users, :through => :memberships
validates :name, :presence => true, :length => {:in => 1..25}, :uniqueness => true validates :name, :presence => true, :length => {:in => 1..25}
attr_reader :user_tokens attr_reader :user_tokens

View file

@ -16,7 +16,7 @@ class Ordergroup < Group
has_many :orders, :through => :group_orders has_many :orders, :through => :group_orders
validates_numericality_of :account_balance, :message => 'ist keine gültige Zahl' validates_numericality_of :account_balance, :message => 'ist keine gültige Zahl'
validate :uniqueness_of_members validate :uniqueness_of_name, :uniqueness_of_members
after_create :update_stats! after_create :update_stats!
@ -107,5 +107,15 @@ class Ordergroup < Group
end end
end end
# Make sure, the name is uniq, add usefull message if uniq group is already deleted
def uniqueness_of_name
id = new_record? ? '' : self.id
group = Ordergroup.with_deleted.where('groups.id != ? AND groups.name = ?', id, name).first
if group.present?
message = group.deleted? ? :taken_with_deleted : :taken
errors.add :name, message
end
end
end end

View file

@ -23,11 +23,6 @@ class StockArticle < Article
available.collect { |a| a.quantity * a.gross_price }.sum available.collect { |a| a.quantity * a.gross_price }.sum
end end
def self.elements_for_index
StockArticle.includes(:supplier, :article_category).
order('suppliers.name, article_categories.name, articles.name')
end
protected protected
def check_quantity def check_quantity

View file

@ -1,36 +0,0 @@
# encoding: utf-8
class StockArticleSelection < ActiveRecord::Base
# Associations
has_and_belongs_to_many :stock_articles
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
belongs_to :finished_by, :class_name => 'User', :foreign_key => 'finished_by_user_id'
# Validations
validate :include_stock_articles
def all_articles
all_articles = stock_article_ids
end
def deletable_count
stock_articles.select { |a| a.quantity_available<=0 }.length
end
def nondeletable_count
stock_articles.select { |a| a.quantity_available>0 }.length
end
def deleted_count
stock_articles.only_deleted.count
end
protected
def include_stock_articles
errors.add(:stock_articles, "Es muss mindestens ein Lagerartikel ausgewählt sein.") if stock_articles.empty?
end
private
end

View file

@ -12,15 +12,13 @@ class Supplier < ActiveRecord::Base
attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number, attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number,
:delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity :delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity
validates :name, :presence => true, :length => { :in => 4..30 }, :uniqueness => true validates :name, :presence => true, :length => { :in => 4..30 }
validates :phone, :presence => true, :length => { :in => 8..20 } validates :phone, :presence => true, :length => { :in => 8..20 }
validates :address, :presence => true, :length => { :in => 8..50 } validates :address, :presence => true, :length => { :in => 8..50 }
validates_length_of :order_howto, :note, maximum: 250 validates_length_of :order_howto, :note, maximum: 250
# validates_length_of :name, :in => 4..30
# validates_uniqueness_of :name
validates_length_of :phone, :in => 8..20 validates_length_of :phone, :in => 8..20
validates_length_of :address, :in => 8..50 validates_length_of :address, :in => 8..50
validate :uniqueness_of_name
# sync all articles with the external database # sync all articles with the external database
# returns an array with articles(and prices), which should be updated (to use in a form) # returns an array with articles(and prices), which should be updated (to use in a form)
@ -66,5 +64,17 @@ class Supplier < ActiveRecord::Base
end end
return [updated_articles, outlisted_articles] return [updated_articles, outlisted_articles]
end end
protected
# Make sure, the name is uniq, add usefull message if uniq group is already deleted
def uniqueness_of_name
id = new_record? ? '' : self.id
supplier = Supplier.with_deleted.where('suppliers.id != ? AND suppliers.name = ?', id, name).first
if supplier.present?
message = supplier.deleted? ? :taken_with_deleted : :taken
errors.add :name, message
end
end
end end

View file

@ -5,6 +5,7 @@ class Workgroup < Group
# returns all non-finished tasks # returns all non-finished tasks
has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], :order => 'due_date ASC' has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], :order => 'due_date ASC'
validates_uniqueness_of :name
validates_presence_of :task_name, :weekday, :task_required_users, :next_weekly_tasks_number, validates_presence_of :task_name, :weekday, :task_required_users, :next_weekly_tasks_number,
:if => :weekly_task :if => :weekly_task
validates_numericality_of :next_weekly_tasks_number, :greater_than => 0, :less_than => 21, :only_integer => true, validates_numericality_of :next_weekly_tasks_number, :greater_than => 0, :less_than => 21, :only_integer => true,
@ -61,7 +62,7 @@ class Workgroup < Group
# add validation check on update # add validation check on update
# Return an error if this is the last group with admin role and role_admin should set to false # Return an error if this is the last group with admin role and role_admin should set to false
def last_admin_on_earth def last_admin_on_earth
if !role_admin && Workgroup.where(:role_admin => true, :id.ne => id).empty? if !role_admin && !Workgroup.where('role_admin = ? AND id != ?', true, id).exists?
errors.add(:role_admin, "Der letzten Gruppe mit Admin-Rechten darf die Admin-Rolle nicht entzogen werden") errors.add(:role_admin, "Der letzten Gruppe mit Admin-Rechten darf die Admin-Rolle nicht entzogen werden")
end end
end end

View file

@ -0,0 +1,5 @@
-# please polish the following line if you know how, same in view destroy
var errorDiv = $('<div class="alert fade in alert-error"><a class="close" data-dismiss="alert" href="#">x</a></div>');
errorDiv.append(document.createTextNode('#{j(fail_msg)}'));
$('div.container-fluid').prepend(errorDiv);

View file

@ -0,0 +1,14 @@
-# please polish the following line if you know how, same in partial _destroy_fail
var successDiv = $('<div class="alert fade in alert-success"><a class="close" data-dismiss="alert" href="#">x</a></div>');
successDiv.append(document.createTextNode('Artikel #{j(@article.name)} gelöscht.'));
$('div.container-fluid').prepend(successDiv);
-# WARNING: If you try to use the escape j(...) here, an error occurs:
-# Ein Fehler ist aufgetreten: undefined method `gsub' for 50:Fixnum
-# However, it should work without without escaping.
-# Note that article names which are purely numeric, e.g. 12345, are escaped correctly (see above).
$('#stockArticle-#{@article.id}').remove();
-# WARNING: Do not use a simple .fadeOut() above, because it conflicts with the show/hide function of unavailable articles.

View file

@ -2,7 +2,7 @@
- content_for :javascript do - content_for :javascript do
:javascript :javascript
$(function() { $(function() {
$('tr.unavailable,input.unavailable,div.unavailable').hide(); $('tr.unavailable').hide();
}) })
.well.well-small .well.well-small
@ -12,7 +12,7 @@
Ansichtsoptionen Ansichtsoptionen
%span.caret %span.caret
%ul.dropdown-menu %ul.dropdown-menu
%li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable,input.unavailable,div.unavailable', tabindex: -1 %li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable', tabindex: -1
.btn-group .btn-group
= link_to_if @current_user.role_orders?, "Lagerbestellung online stellen", new_order_path(supplier_id: 0), = link_to_if @current_user.role_orders?, "Lagerbestellung online stellen", new_order_path(supplier_id: 0),
@ -20,7 +20,6 @@
= link_to "Neuen Lagerartikel anlegen", new_stock_article_path, class: 'btn' = link_to "Neuen Lagerartikel anlegen", new_stock_article_path, class: 'btn'
= link_to "Inventur anlegen", new_stock_taking_path, class: 'btn' = link_to "Inventur anlegen", new_stock_taking_path, class: 'btn'
= link_to "Inventurübersicht", stock_takings_path, class: 'btn' = link_to "Inventurübersicht", stock_takings_path, class: 'btn'
= link_to 'Löschvorschläge', stock_article_selections_path, class: 'btn'
.btn-group .btn-group
= link_to '#', data: {toggle: 'dropdown'}, class: 'btn dropdown-toggle' do = link_to '#', data: {toggle: 'dropdown'}, class: 'btn dropdown-toggle' do
@ -30,15 +29,9 @@
- Supplier.all.each do |supplier| - Supplier.all.each do |supplier|
%li= link_to supplier.name, new_supplier_delivery_path(supplier), tabindex: -1 %li= link_to supplier.name, new_supplier_delivery_path(supplier), tabindex: -1
= form_for @stock_article_selection do |form|
- if @stock_article_selection.errors.has_key?(:stock_articles)
.alert.alert-error
= @stock_article_selection.errors.get(:stock_articles).join(" ")
%table.table.table-hover#articles %table.table.table-hover#articles
%thead %thead
%tr %tr
%th= check_box_tag 'checkall', '1', false,
{ 'data-check-all' => 'table#articles tr.unavailable', :title => 'Alle löschbaren Artikel', :class => 'unavailable' }
%th Artikel %th Artikel
%th im Lager %th im Lager
%th davon bestellt %th davon bestellt
@ -51,8 +44,7 @@
%th %th
%tbody %tbody
- for article in @stock_articles - for article in @stock_articles
%tr{:class => stock_article_classes(article)} %tr{:class => stock_article_classes(article), :id => "stockArticle-#{article.id}"}
%td= stock_article_delete_checkbox(article)
%td=h article.name %td=h article.name
%td= article.quantity %td= article.quantity
%td= article.quantity - article.quantity_available %td= article.quantity - article.quantity_available
@ -65,9 +57,8 @@
%td %td
= link_to "Bearbeiten", edit_stock_article_path(article), class: 'btn btn-mini' = link_to "Bearbeiten", edit_stock_article_path(article), class: 'btn btn-mini'
= link_to "Löschen", article, :method => :delete, :confirm => "Bist Du sicher?", = link_to "Löschen", article, :method => :delete, :confirm => "Bist Du sicher?",
class: 'btn btn-mini btn-danger' class: 'btn btn-mini btn-danger', :remote => true
.form-actions.unavailable
= submit_tag "Artikel zum Löschen vormerken", { :class => 'unavailable btn' }
%p %p
Aktueller Lagerwert: Aktueller Lagerwert:
= number_to_currency StockArticle.stock_value = number_to_currency StockArticle.stock_value

View file

@ -1,26 +0,0 @@
%table.table.table-hover
%tr
%th Artikel
%th Zusammenfassung
%th Erstellt am
%th Erstellt von
%th Optionen
- stock_article_selections.each do |stock_article_selection|
%tr
%td
- for article in stock_article_selection.stock_articles.with_deleted
%span{:class => article_deletion_classes(article), :title => article_deletion_title(article)}= article.name
%td
%span{:class => 'label label-success'}= "#{stock_article_selection.deletable_count} Löschbar"
%span{:class => 'label'}= "#{stock_article_selection.deleted_count} Gelöscht"
%span{:class => 'label label-important'}= "#{stock_article_selection.nondeletable_count} Nicht löschbar"
%td= format_date(stock_article_selection.created_at)
%td= link_to_user_message_if_valid stock_article_selection.created_by
%td
= link_to 'Anzeigen', stock_article_selection, class: 'btn btn-small'
= link_to "Artikel löschen", articles_stock_article_selection_path(stock_article_selection), :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-small btn-danger'
= link_to "Verwerfen", stock_article_selection, :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn btn-small'

View file

@ -1,25 +0,0 @@
- title "Löschvorschläge für Lagerartikel"
.well.well-small
.btn-toolbar
.btn-group
= link_to "Lager anzeigen", stock_articles_path, class: 'btn'
= link_to "Aufräumen", finished_stock_article_selections_path, :method => 'delete',
:confirm => 'Wirklich alle erledigten Löschvorschläge entfernen?', class: 'btn'
.well
%h2 Ausstehende Löschvorschläge
- open_selections = @stock_article_selections.select { |sel| sel.deletable_count + sel.nondeletable_count > 0 }
- if open_selections.length == 0
%p Es gibt keine ausstehenden Löschvorschläge.
%ul
%li= link_to "Löschvorschlag erstellen", stock_articles_path
- else
= render :partial => 'overview', :locals => {:stock_article_selections => open_selections}
%h2 Erledigte Löschvorschläge
- finished_selections = @stock_article_selections.select { |sel| sel.deletable_count + sel.nondeletable_count <= 0 }
- if finished_selections.length == 0
%p Es gibt keine erledigten Löschvorschläge.
- else
= render :partial => 'overview', :locals => {:stock_article_selections => finished_selections}

View file

@ -1,25 +0,0 @@
- title "Löschvorschlag für #{@stock_article_selection.stock_articles.count} Lagerartikel"
.well.well-small
.btn-toolbar
.btn-group
= link_to "Lager anzeigen", stock_articles_path, class: 'btn'
= link_to 'Alle Löschvorschläge anzeigen', stock_article_selections_path, class: 'btn'
%dl
%dt Löschvorschlag vom:
%dd= format_time(@stock_article_selection.created_at)
%dt Erstellt durch:
%dd= link_to_user_message_if_valid(@stock_article_selection.created_by)
%dt Zu löschende Artikel
- for article in @stock_article_selection.stock_articles.with_deleted
%dd
%span{:class => article_deletion_classes(article), :title => article_deletion_title(article)}= article.name
%p
= link_to 'Artikel löschen', articles_stock_article_selection_path(@stock_article_selection), :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-danger'
= link_to 'Verwerfen', @stock_article_selection, :method => :delete,
:confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn'
= link_to 'Alle Löschvorschläge anzeigen', stock_article_selections_path, class: 'btn'

View file

@ -143,6 +143,7 @@ de:
odd: muss ungerade sein odd: muss ungerade sein
record_invalid: ! 'Gültigkeitsprüfung ist fehlgeschlagen: %{errors}' record_invalid: ! 'Gültigkeitsprüfung ist fehlgeschlagen: %{errors}'
taken: ist bereits vergeben taken: ist bereits vergeben
taken_with_deleted: ist bereits vergeben (eine gelöschte Gruppe)
too_long: ist zu lang (nicht mehr als %{count} Zeichen) too_long: ist zu lang (nicht mehr als %{count} Zeichen)
too_short: ist zu kurz (nicht weniger als %{count} Zeichen) too_short: ist zu kurz (nicht weniger als %{count} Zeichen)
wrong_length: hat die falsche Länge (muss genau %{count} Zeichen haben) wrong_length: hat die falsche Länge (muss genau %{count} Zeichen haben)

View file

@ -90,18 +90,6 @@ Foodsoft::Application.routes.draw do
end end
end end
resources :stock_article_selections, :controller => 'stockit_selections',
:only => [ :create, :show, :index, :destroy ], :path => "/stock_articles/selections" do
member do
delete 'articles'
end
collection do
delete 'finished'
end
end
resources :stock_articles, :to => 'stockit' do resources :stock_articles, :to => 'stockit' do
collection do collection do
get :articles_search get :articles_search

View file

@ -1,18 +0,0 @@
class CreateStockArticleSelections < ActiveRecord::Migration
def up
create_table :stock_article_selections do |t|
t.integer :created_by_user_id
t.timestamps
end
create_table :stock_article_selections_stock_articles, :id => false do |t|
t.integer :stock_article_id
t.integer :stock_article_selection_id
end
end
def down
drop_table :stock_article_selections
drop_table :stock_article_selections_stock_articles
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130112121840) do ActiveRecord::Schema.define(:version => 20121230142516) do
create_table "article_categories", :force => true do |t| create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false t.string "name", :default => "", :null => false
@ -268,17 +268,6 @@ ActiveRecord::Schema.define(:version => 20130112121840) do
add_index "pages", ["permalink"], :name => "index_pages_on_permalink" add_index "pages", ["permalink"], :name => "index_pages_on_permalink"
add_index "pages", ["title"], :name => "index_pages_on_title" add_index "pages", ["title"], :name => "index_pages_on_title"
create_table "stock_article_selections", :force => true do |t|
t.integer "created_by_user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "stock_article_selections_stock_articles", :id => false, :force => true do |t|
t.integer "stock_article_id"
t.integer "stock_article_selection_id"
end
create_table "stock_changes", :force => true do |t| create_table "stock_changes", :force => true do |t|
t.integer "delivery_id" t.integer "delivery_id"
t.integer "order_id" t.integer "order_id"