From 0bcbc2070296f1ba35b7193930622867c937e571 Mon Sep 17 00:00:00 2001 From: benji Date: Sat, 28 Mar 2015 14:36:07 +0100 Subject: [PATCH 1/2] change index of products in table view --- app/assets/stylesheets/products.css.scss | 3 +++ app/controllers/products_controller.rb | 13 +++++++------ app/models/product.rb | 2 +- app/views/products/update.js.erb | 1 + app/views/products_list/_product_row.html.erb | 9 +++++++++ app/views/products_list/listview.html.erb | 17 +++++++++++++++++ 6 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 app/views/products/update.js.erb create mode 100644 app/views/products_list/_product_row.html.erb create mode 100644 app/views/products_list/listview.html.erb diff --git a/app/assets/stylesheets/products.css.scss b/app/assets/stylesheets/products.css.scss index 9ec81e9..55739c9 100644 --- a/app/assets/stylesheets/products.css.scss +++ b/app/assets/stylesheets/products.css.scss @@ -22,3 +22,6 @@ margin-bottom: 20px; } +#products-table .form-group { + margin-bottom: 0; +} diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 9c30afe..2af86f8 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,6 +1,8 @@ class ProductsController < ApplicationController load_and_authorize_resource + respond_to :html, :js + def new @product = Product.new end @@ -17,6 +19,9 @@ class ProductsController < ApplicationController def index @products = Product.all @categories = Product.categories + if current_user.admin? + render 'products_list/listview' + end end def edit @@ -25,12 +30,8 @@ class ProductsController < ApplicationController def update @product = Product.find(params[:id]) - if @product.update_attributes(product_params) - flash[:success] = "Succesfully updated product" - redirect_to products_path - else - render 'edit' - end + @product.update_attributes product_params + respond_with @product end def destroy diff --git a/app/models/product.rb b/app/models/product.rb index 2344d1c..b0ba8a1 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -17,7 +17,7 @@ class Product < ActiveRecord::Base has_many :order_items - has_attached_file :avatar, styles: { dagschotel: "80x80>", medium: "100x100>" }, default_style: :medium + has_attached_file :avatar, styles: { dagschotel: "80x80>", medium: "100x100>", small: "40x40>" }, default_style: :medium enum category: %w(food beverages other) diff --git a/app/views/products/update.js.erb b/app/views/products/update.js.erb new file mode 100644 index 0000000..b0694e6 --- /dev/null +++ b/app/views/products/update.js.erb @@ -0,0 +1 @@ +$("#products_row_<%= dom_id(@product) %>").replaceWith("<%= j render 'products_list/product_row', product: @product %>"); diff --git a/app/views/products_list/_product_row.html.erb b/app/views/products_list/_product_row.html.erb new file mode 100644 index 0000000..e0da8c3 --- /dev/null +++ b/app/views/products_list/_product_row.html.erb @@ -0,0 +1,9 @@ + + <%= f_form_for product, remote: true do |f| %> + <%= image_tag product.avatar(:small) %> + <%= f.text_field :name, skip_label: true %> + <%= f.price_field :price, skip_label: true %> + <%= f.number_field :stock, skip_label: true %> + <%= f.submit "Save" %> + <% end %> + diff --git a/app/views/products_list/listview.html.erb b/app/views/products_list/listview.html.erb new file mode 100644 index 0000000..47cf371 --- /dev/null +++ b/app/views/products_list/listview.html.erb @@ -0,0 +1,17 @@ +
+
+

Products

+ <%= render partial: 'flash' %> + + + + + + + + + + <%= render partial: 'products_list/product_row', collection: @products, as: :product %> +
NamePriceStock
+
+
From 99c49db84886883c65db69b2e489ce3fc63f8792 Mon Sep 17 00:00:00 2001 From: benji Date: Fri, 3 Apr 2015 23:56:13 +0200 Subject: [PATCH 2/2] change products to table view --- app/assets/stylesheets/products.css.scss | 7 +++++++ app/controllers/products_controller.rb | 1 + app/views/application/_errors.html.erb | 14 +++++++++++++ app/views/products/edit.js.erb | 1 + app/views/products/update.js.erb | 7 ++++++- .../products_list/_product_edit_row.html.erb | 20 +++++++++++++++++++ app/views/products_list/_product_row.html.erb | 12 +++++------ app/views/products_list/listview.html.erb | 1 + 8 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 app/views/application/_errors.html.erb create mode 100644 app/views/products/edit.js.erb create mode 100644 app/views/products_list/_product_edit_row.html.erb diff --git a/app/assets/stylesheets/products.css.scss b/app/assets/stylesheets/products.css.scss index 55739c9..2a9487c 100644 --- a/app/assets/stylesheets/products.css.scss +++ b/app/assets/stylesheets/products.css.scss @@ -25,3 +25,10 @@ #products-table .form-group { margin-bottom: 0; } + +#products-table tr { + height: 57px; + td { + vertical-align: middle; + } +} diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 2af86f8..d44d99d 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -26,6 +26,7 @@ class ProductsController < ApplicationController def edit @product = Product.find(params[:id]) + respond_with @product end def update diff --git a/app/views/application/_errors.html.erb b/app/views/application/_errors.html.erb new file mode 100644 index 0000000..a0b3ddf --- /dev/null +++ b/app/views/application/_errors.html.erb @@ -0,0 +1,14 @@ +<% if object.errors.any? %> +
+
+ <%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.name.downcase %> from being saved: +
+
+
    + <% object.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+
+
+<% end %> diff --git a/app/views/products/edit.js.erb b/app/views/products/edit.js.erb new file mode 100644 index 0000000..c230148 --- /dev/null +++ b/app/views/products/edit.js.erb @@ -0,0 +1 @@ +$("#products_row_<%= dom_id(@product) %>").replaceWith("<%= j render "products_list/product_edit_row", product: @product %>") diff --git a/app/views/products/update.js.erb b/app/views/products/update.js.erb index b0694e6..384a31c 100644 --- a/app/views/products/update.js.erb +++ b/app/views/products/update.js.erb @@ -1 +1,6 @@ -$("#products_row_<%= dom_id(@product) %>").replaceWith("<%= j render 'products_list/product_row', product: @product %>"); +<% if @product.errors.any? %> + $('#products-errors').html("<%= j render 'errors', object: @product %>") +<% else %> + $('#products-errors').html('') + $("#products_row_<%= dom_id(@product) %>").replaceWith("<%= j render 'products_list/product_row', product: @product %>"); +<% end %> diff --git a/app/views/products_list/_product_edit_row.html.erb b/app/views/products_list/_product_edit_row.html.erb new file mode 100644 index 0000000..9ef9b8f --- /dev/null +++ b/app/views/products_list/_product_edit_row.html.erb @@ -0,0 +1,20 @@ + + <%= f_form_for product, remote: true do |f| %> + <%= image_tag product.avatar(:small) %> + <%= f.text_field :name, skip_label: true %> + <%= f.price_field :price, skip_label: true %> + <%= f.number_field :stock, skip_label: true %> + <%= f.button "Update", class: "btn btn-primary" %> + <%= javascript_tag do %> + var id = "#edit_<%= dom_id(product) %>"; + var inputs = $(id).parent().find('input'); + + $(id).parent().find('button').on('click', function(e) { + e.preventDefault(); + $(id).append(inputs.clone()); + $(id).append(''); + $(id).submit(); + }); + <% end %> + <% end %> + diff --git a/app/views/products_list/_product_row.html.erb b/app/views/products_list/_product_row.html.erb index e0da8c3..f2fc527 100644 --- a/app/views/products_list/_product_row.html.erb +++ b/app/views/products_list/_product_row.html.erb @@ -1,9 +1,7 @@ - <%= f_form_for product, remote: true do |f| %> - <%= image_tag product.avatar(:small) %> - <%= f.text_field :name, skip_label: true %> - <%= f.price_field :price, skip_label: true %> - <%= f.number_field :stock, skip_label: true %> - <%= f.submit "Save" %> - <% end %> + <%= image_tag product.avatar(:small) %> + <%= product.name %> + <%= euro(product.price) %> + <%= product.stock %> + <%= button_to "Edit", edit_product_path(product), method: :get, class: "btn btn-default", remote: true %> diff --git a/app/views/products_list/listview.html.erb b/app/views/products_list/listview.html.erb index 47cf371..85091dd 100644 --- a/app/views/products_list/listview.html.erb +++ b/app/views/products_list/listview.html.erb @@ -1,3 +1,4 @@ +

Products