From 0decbb36e196b712f708c4e46a2460a11670daaa Mon Sep 17 00:00:00 2001 From: benni Date: Wed, 18 May 2011 16:10:30 +0200 Subject: [PATCH] Refactored article_categories. --- Gemfile | 1 + Gemfile.lock | 6 ++ .../article_categories_controller.rb | 64 ++----------------- app/controllers/articles_controller.rb | 4 +- app/models/article_category.rb | 5 +- app/views/article_categories/_form.html.haml | 4 ++ app/views/article_categories/_form.rhtml | 24 ------- app/views/article_categories/_list.rhtml | 27 -------- app/views/article_categories/edit.html.haml | 3 + app/views/article_categories/index.html.haml | 15 ++++- app/views/article_categories/new.html.haml | 3 + config/locales/de.yml | 1 + 12 files changed, 39 insertions(+), 118 deletions(-) create mode 100644 app/views/article_categories/_form.html.haml delete mode 100644 app/views/article_categories/_form.rhtml delete mode 100644 app/views/article_categories/_list.rhtml create mode 100644 app/views/article_categories/edit.html.haml create mode 100644 app/views/article_categories/new.html.haml diff --git a/Gemfile b/Gemfile index 1cbdb910..0906428c 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,7 @@ gem 'jquery-rails' gem 'simple_form' gem 'rails3_acts_as_paranoid' gem 'meta_where' +gem 'inherited_resources' group :development do gem 'annotate' diff --git a/Gemfile.lock b/Gemfile.lock index 0c0bbb96..b70016c1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,8 +36,12 @@ GEM exception_notification (2.4.0) fastercsv (1.5.4) haml (3.1.1) + has_scope (0.5.0) hirb (0.3.4) i18n (0.5.0) + inherited_resources (1.2.2) + has_scope (~> 0.5.0) + responders (~> 0.6.0) jquery-rails (1.0.1) railties (~> 3.0) thor (~> 0.14) @@ -84,6 +88,7 @@ GEM rake (>= 0.8.7) thor (~> 0.14.4) rake (0.8.7) + responders (0.6.4) simple_form (1.3.1) thor (0.14.6) treetop (1.4.9) @@ -100,6 +105,7 @@ DEPENDENCIES fastercsv haml hirb + inherited_resources jquery-rails meta_where mysql diff --git a/app/controllers/article_categories_controller.rb b/app/controllers/article_categories_controller.rb index 944c1717..21ee691f 100644 --- a/app/controllers/article_categories_controller.rb +++ b/app/controllers/article_categories_controller.rb @@ -1,71 +1,15 @@ class ArticleCategoriesController < ApplicationController + inherit_resources # Build default REST Actions via plugin + before_filter :authenticate_article_meta - def index - @article_categories = ArticleCategory.all :order => 'name' - end - - def new - @article_category = ArticleCategory.new - - render :update do |page| - page['category_form'].replace_html :partial => 'article_categories/form' - page['category_form'].show - end - end - - def edit - @article_category = ArticleCategory.find(params[:id]) - - render :update do |page| - page['category_form'].replace_html :partial => 'article_categories/form' - page['category_form'].show - end - end - def create - @article_category = ArticleCategory.new(params[:article_category]) - - if @article_category.save - render :update do |page| - page['category_form'].hide - page['category_list'].replace_html :partial => 'article_categories/list' - page['category_'+@article_category.id.to_s].visual_effect(:highlight, - :duration => 2) - end - else - render :update do |page| - page['category_form'].replace_html :partial => 'article_categories/form' - end - end + create!(:notice => "Die Kategorie wurde gespeichert") { article_categories_path } end def update - @article_category = ArticleCategory.find(params[:id]) - - if @article_category.update_attributes(params[:article_category]) - render :update do |page| - page['category_form'].hide - page['category_list'].replace_html :partial => 'article_categories/list' - page['category_'+@article_category.id.to_s].visual_effect(:highlight, - :duration => 2) - end - else - render :update do |page| - page['category_form'].replace_html :partial => 'article_categories/form' - end - end + update!(:notice => "Die Kategorie wurde aktualisiert") { article_categories_path } end - def destroy - @article_category = ArticleCategory.find(params[:id]) - @article_category.destroy - - if @article_category.destroy - render :update do |page| - page['category_'+@article_category.id.to_s].visual_effect :drop_out - end - end - end end diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 69da3b76..f1425737 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -28,8 +28,8 @@ class ArticlesController < ApplicationController # if somebody uses the search field: conditions = ["articles.name LIKE ?", "%#{params[:query]}%"] unless params[:query].nil? - @total = @supplier.articles.without_deleted.count(:conditions => conditions) - @articles = @supplier.articles.without_deleted.paginate( + @total = @supplier.articles.count(:conditions => conditions) + @articles = @supplier.articles.paginate( :order => sort, :conditions => conditions, :page => params[:page], diff --git a/app/models/article_category.rb b/app/models/article_category.rb index 2fc39aaf..aa82e719 100644 --- a/app/models/article_category.rb +++ b/app/models/article_category.rb @@ -1,8 +1,7 @@ class ArticleCategory < ActiveRecord::Base has_many :articles - - validates_length_of :name, :in => 2..20 - validates_uniqueness_of :name + + validates :name, :presence => true, :uniqueness => true, :length => { :in => 2..20 } end diff --git a/app/views/article_categories/_form.html.haml b/app/views/article_categories/_form.html.haml new file mode 100644 index 00000000..b0244b2f --- /dev/null +++ b/app/views/article_categories/_form.html.haml @@ -0,0 +1,4 @@ += simple_form_for @article_category do |f| + = f.input :name + = f.input :description + = f.submit \ No newline at end of file diff --git a/app/views/article_categories/_form.rhtml b/app/views/article_categories/_form.rhtml deleted file mode 100644 index 357b7252..00000000 --- a/app/views/article_categories/_form.rhtml +++ /dev/null @@ -1,24 +0,0 @@ -<% remote_form_for @article_category, - :before => "Element.show('loader')", - :success => "Element.hide('loader')" do |@f| %> - - <%= @f.error_messages %> - - - - - - - - - -
NameBeschreibung
- <%= @f.text_field :name, :size => 20 %> - - <%= @f.text_field :description, :size => 30 %> -
- -
- <%= submit_tag "Speichern" %> | <%= link_to_function("Abbrechen", "Element.hide('category_form')") %> -<% end %> - diff --git a/app/views/article_categories/_list.rhtml b/app/views/article_categories/_list.rhtml deleted file mode 100644 index baa2c7ae..00000000 --- a/app/views/article_categories/_list.rhtml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - -<% for article_category in ArticleCategory.find(:all) -%> - 'category') -%>" id="category_<%= article_category.id -%>"> - - - - - -<% end -%> -
NameBeschreibung
<%=h article_category.name -%><%=h article_category.description -%><%= link_to_remote icon(:edit), - :url => edit_article_category_path(article_category), - :method => :get, - :before => "Element.show('loader')", - :success => "Element.hide('loader')" -%><%= link_to_remote icon(:delete), - :url => article_category, - :method => :delete, - :confirm => 'Are you sure?' -%>
-
-<%= link_to_remote 'Neue Kategorie', :url => new_article_category_path, - :method => :get, - :before => "Element.show('loader')", - :success => "Element.hide('loader')" %> diff --git a/app/views/article_categories/edit.html.haml b/app/views/article_categories/edit.html.haml new file mode 100644 index 00000000..3e6ebc25 --- /dev/null +++ b/app/views/article_categories/edit.html.haml @@ -0,0 +1,3 @@ +- title "Kategorie ändern" + += render 'form' \ No newline at end of file diff --git a/app/views/article_categories/index.html.haml b/app/views/article_categories/index.html.haml index 137dfdc5..b907b87b 100644 --- a/app/views/article_categories/index.html.haml +++ b/app/views/article_categories/index.html.haml @@ -4,5 +4,16 @@ .box_title %h2 Artikelkategorien .column_content#categories - #category_form.box.edit_form{:style => "display:none;margin-bottom:1em;"} - #category_list= render :partial => 'article_categories/list' \ No newline at end of file + %table + %tr + %th Name + %th Beschreibung + %th + - for article_category in ArticleCategory.all + %tr{:class => cycle("even","odd", :name => 'category')}[article_category] + %td= article_category.name + %td= article_category.description + %td + = link_to icon(:edit), edit_article_category_path(article_category) + = link_to icon(:delete), article_category, :method => :delete, :confirm => 'Are you sure?' + %p= link_to 'Neue Kategorie anlegen', new_article_category_path diff --git a/app/views/article_categories/new.html.haml b/app/views/article_categories/new.html.haml new file mode 100644 index 00000000..055d656c --- /dev/null +++ b/app/views/article_categories/new.html.haml @@ -0,0 +1,3 @@ +- title "Neue Kategorie anlegen" + += render 'form' \ No newline at end of file diff --git a/config/locales/de.yml b/config/locales/de.yml index b13f81be..65586699 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -159,6 +159,7 @@ de: ordergroup: Bestellgruppe task: Aufgabe message: Nachricht + article_category: Artikelkategorie attributes: article: price: Nettopreis