order form almost done

This commit is contained in:
ohsab 2014-11-24 21:45:32 +01:00
parent 4318f5b56a
commit cd78e5719d
21 changed files with 250 additions and 39 deletions

View file

@ -1,28 +1,14 @@
== README
== Tab
This README would normally document whatever steps are necessary to get the
application up and running.
Yes. We have to drink. But we also have to pay. This combines both.
Things you may want to cover:
* Ruby version
* System dependencies
* Configuration
* Database creation
* Database initialization
* How to run the test suite
* Services (job queues, cache servers, search engines, etc.)
* Deployment instructions
* ...
* Ruby v2.0.5
* rails v4.1.7
Please feel free to use a different markup language if you do not plan to run
<tt>rake doc:app</tt>.
= To do:
- layout fixes
- user acces
- products JSON -> rails params !!
- product images

View file

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View file

@ -0,0 +1,3 @@
// Place all the styles related to the Products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -15,13 +15,14 @@ class OrdersController < ApplicationController
def order
@order = current_ordering_user.orders.build
@products = Product.all
end
def create
@order = current_ordering_user.orders.build(order_params)
if @order.save
flash[:success] = "order created!"
#flash[:success] = "order created!"
end_order
redirect_to overview_path
else

View file

@ -0,0 +1,32 @@
class ProductsController < ApplicationController
def new
@product = Product.new
end
def create
@product = Product.new(params_products)
if @product.save
redirect_to @product
else
render "new"
end
end
def show
@product = Product.find(params[:id])
end
def edit
end
def delete
end
private
def params_products
params.require(:product).permit(:name, :purchase_price, :sale_price,
:img_path)
end
end

View file

@ -23,8 +23,8 @@ class UsersController < ApplicationController
private
def user_params
params.require(:user).permit(:name, :last_name, :password,
:password_confirmation, :nickname)
end
def user_params
params.require(:user).permit(:name, :last_name, :password,
:password_confirmation, :nickname)
end
end

View file

@ -0,0 +1,2 @@
module ProductsHelper
end

2
app/models/product.rb Normal file
View file

@ -0,0 +1,2 @@
class Product < ActiveRecord::Base
end

View file

@ -0,0 +1,5 @@
<button class="btn btn-default order-btn"
data-name= '<%= p.name %>'
date-sale-price= '<%= p.sale_price %>'>
<%= p.name %>
</button>

View file

@ -1,17 +1,11 @@
<ul class="users">
<% @users.each do |user| %>
<li>
<%= form_for overview_path do |f| %>
<%= hidden_field_tag 'user_id', user.id %>
<%= f.submit user.name , class: "btn btn-primary" %>
<% end %>
</li>
<% end %>
</ul>

View file

@ -1,7 +1,89 @@
<%= current_ordering_user.name %>
<% @products.each do |p| %>
<%#= render "orders/order_button" %>
<!-- dit moet in een andere file -->
<button class="btn btn-default product-btn"
data-name= '<%= p.name %>'
date-price= '<%= p.sale_price %>'>
<%= p.name %>
</button>
<!-- tot hier -->
<% end %>
<%= form_for @order do |f| %>
<div class="field">
<%= f.text_area :products, placeholder: "Compose new order..." %>
<%= f.text_area :products , placeholder: "dit zou een hidden field moeten zijn met product JSON..." %>
</div>
<%= f.submit "Order", class: "btn btn-primary" %>
<%#= hidden_field_tag "products", "return_products_string" %>
<div class="hidden"></div>
<%= f.submit "Order", class: "btn btn-primary " %>
<% end %>
<div style="padding-top: 50px;" class="order_list ">
<p>
Nothing ordered yet!
</p>
</div>
<script charset="utf-8">
var products = {};
//var product_number = 0;
$(document).ready(function() {
//Making/adding orderlist/JSON
$('.product-btn').click(function() {
//product_number++;
var name = $(this).data('name');
var price = $(this).data('price');
if(products.hasOwnProperty(name)){
products[name].number_of++;
} else{
products[name] = {
name : name,
price : price,
number_of: 1
}
}
update();
});
//updates the orderlist view
var update = function(){
$('.order_list').empty().append("<p>Ordered: </p>" );
jQuery.each(products, function(i, product) {
$(".order_list").append("<p> - "+ i +": "+ product.number_of +"</p>");
});
//$('.hidden').empty().append("<input id='products' name='products' type='hidden' value='test' />")
}
});
</script>

View file

@ -0,0 +1,2 @@
<h1>Products#edit</h1>
<p>Find me in app/views/products/edit.html.erb</p>

View file

@ -0,0 +1,15 @@
<%= form_for @product do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :purchase_price %>
<%= f.number_field :purchase_price %>
<%= f.label :sale_price %>
<%= f.number_field :sale_price %>
<%= f.label :img_path %>
<%= f.text_field :img_path %>
<%= f.submit "Create product", class: "btn btn-primary" %>
<% end %>

View file

@ -0,0 +1,9 @@
<aside>
<h1>Products#show</h1>
<h1>Name: <%= @product.name %></h1>
<h1>Purchase price: <%= @product.purchase_price %> </h1>
<h1>Sale price: <%= @product.sale_price %> </h1>
<h1>Img path: <%= @product.img_path %> </h1>
</aside>

View file

@ -1,14 +1,20 @@
Rails.application.routes.draw do
root "users#index"
resources :users
resources :products
get 'overview' => 'orders#new'
post 'overview' => 'orders#create_session'
delete 'end_order' => 'orders#destroy'
#products
get 'products' => 'products#new'
post 'products' => 'products#create'
#orders
get 'order' => 'orders#order'
post 'orders' => 'orders#create'

View file

@ -0,0 +1,12 @@
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.integer :purchase_price
t.integer :sale_price
t.string :img_path
t.timestamps
end
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141123151756) do
ActiveRecord::Schema.define(version: 20141124091707) do
create_table "orders", force: true do |t|
t.integer "user_id"
@ -24,6 +24,15 @@ ActiveRecord::Schema.define(version: 20141123151756) do
add_index "orders", ["user_id", "created_at"], name: "index_orders_on_user_id_and_created_at"
add_index "orders", ["user_id"], name: "index_orders_on_user_id"
create_table "products", force: true do |t|
t.string "name"
t.integer "purchase_price"
t.integer "sale_price"
t.string "img_path"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "name"
t.string "last_name"

View file

@ -0,0 +1,24 @@
require 'test_helper'
class ProductsControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
test "should get show" do
get :show
assert_response :success
end
test "should get edit" do
get :edit
assert_response :success
end
test "should get delete" do
get :delete
assert_response :success
end
end

13
test/fixtures/products.yml vendored Normal file
View file

@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
purchase_price: 1
sale_price: 1
img_path: MyString
two:
name: MyString
purchase_price: 1
sale_price: 1
img_path: MyString

View file

@ -0,0 +1,4 @@
require 'test_helper'
class ProductsHelperTest < ActionView::TestCase
end

View file

@ -0,0 +1,7 @@
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end