diff --git a/app/assets/javascripts/products.js.coffee b/app/assets/javascripts/products.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/products.js.coffee @@ -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/ diff --git a/app/assets/stylesheets/products.css.scss b/app/assets/stylesheets/products.css.scss new file mode 100644 index 0000000..bff386e --- /dev/null +++ b/app/assets/stylesheets/products.css.scss @@ -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/ diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index fe37e3f..edfdb3d 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -2,6 +2,7 @@ class OrdersController < ApplicationController before_action :logged_in_user, only: [:create, :destroy] def new + end def destroy diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb new file mode 100644 index 0000000..34c8744 --- /dev/null +++ b/app/controllers/products_controller.rb @@ -0,0 +1,30 @@ +class ProductsController < ApplicationController + def new + @product = Product.new + end + + def show + @product = Product.find(params[:id]) + end + + def create + @product = Product.new(product_params) # Not the final implementation! + if @product.save + redirect_to @product + else + render 'new' + end + end + + def edit + end + + + + private + + def product_params + params.require(:product).permit(:name, :sale_price, :purchase_price, + :image_path) + end +end diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb new file mode 100644 index 0000000..ab5c42b --- /dev/null +++ b/app/helpers/products_helper.rb @@ -0,0 +1,2 @@ +module ProductsHelper +end diff --git a/app/models/product.rb b/app/models/product.rb new file mode 100644 index 0000000..a1ebbb6 --- /dev/null +++ b/app/models/product.rb @@ -0,0 +1,11 @@ +class Product < ActiveRecord::Base + after_initialize :init + + + def init + self.name ||= "FOOBAR" + self.sale_price ||= 0 + self.purchase_price ||= 0 + self.image_path = "/" #paperclip gem + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 925aadd..84fb93f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,7 @@ class User < ActiveRecord::Base def init self.marks ||= 0 self.role ||= "user" - end + end def feed # This is preliminary. See "Following users" for the full implementation. diff --git a/app/views/products/create.html.erb b/app/views/products/create.html.erb new file mode 100644 index 0000000..d546021 --- /dev/null +++ b/app/views/products/create.html.erb @@ -0,0 +1,2 @@ +

Products#create

+

Find me in app/views/products/create.html.erb

diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb new file mode 100644 index 0000000..279b066 --- /dev/null +++ b/app/views/products/edit.html.erb @@ -0,0 +1,2 @@ +

Products#edit

+

Find me in app/views/products/edit.html.erb

diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb new file mode 100644 index 0000000..26f9b7b --- /dev/null +++ b/app/views/products/new.html.erb @@ -0,0 +1,25 @@ +

Product creation

+ +
+
+ <%= form_for(@product) do |f| %> + <%= render 'shared/error_messages', object: f.object %> + <%= 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 :image_path %> + <%= f.text_field :image_path %> + + + +
+ <%= f.submit "Create product", class: "btn btn-primary" %> + <% end %> +
+
\ No newline at end of file diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 0000000..d71e663 --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,5 @@ +

Products#show

+

Find me in app/views/products/show.html.erb

+name: <%= @product.name %>
+verkoop prijs: <%= @product.sale_price %> EUR
+aankoop prijs: <%= @product.purchase_price %> EUR
\ No newline at end of file diff --git a/app/views/static_pages/_user.html.erb b/app/views/static_pages/_user.html.erb index 0b3bf13..301050e 100644 --- a/app/views/static_pages/_user.html.erb +++ b/app/views/static_pages/_user.html.erb @@ -1,6 +1,7 @@ <% @users.each do |user| %> - > +<% link = 'orders/' + user.id.to_s %> + > <%= user.id %> <%= user.name %> <% user.marks.times do %> diff --git a/config/routes.rb b/config/routes.rb index 7190737..6ab1890 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,13 @@ Rails.application.routes.draw do + get 'products/new' + + get 'products/show' + + get 'products/create' + + get 'products/edit' + get 'orders/show' root 'static_pages#home' @@ -30,6 +38,8 @@ Rails.application.routes.draw do delete 'logout' => 'sessions#destroy' resources :users + resources :orders + resources :products # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20141110141100_create_products.rb b/db/migrate/20141110141100_create_products.rb new file mode 100644 index 0000000..7a4a09a --- /dev/null +++ b/db/migrate/20141110141100_create_products.rb @@ -0,0 +1,13 @@ +class CreateProducts < ActiveRecord::Migration + def change + create_table :products do |t| + t.string :name + t.integer :sale_price + t.integer :purchase_price + t.string :image_path + t.string :type + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 19cee5a..317ed73 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141109174952) do +ActiveRecord::Schema.define(version: 20141110141100) do create_table "orders", force: true do |t| t.text "products" @@ -23,6 +23,16 @@ ActiveRecord::Schema.define(version: 20141109174952) 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 "sale_price" + t.integer "purchase_price" + t.string "image_path" + t.string "type" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "users", force: true do |t| t.string "name" t.integer "marks" diff --git a/test/controllers/products_controller_test.rb b/test/controllers/products_controller_test.rb new file mode 100644 index 0000000..fdd03f5 --- /dev/null +++ b/test/controllers/products_controller_test.rb @@ -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 create" do + get :create + assert_response :success + end + + test "should get edit" do + get :edit + assert_response :success + end + +end diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml new file mode 100644 index 0000000..e46cb4c --- /dev/null +++ b/test/fixtures/products.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + sale-price: 1 + purchase-price: 1 + +two: + name: MyString + sale-price: 1 + purchase-price: 1 diff --git a/test/helpers/products_helper_test.rb b/test/helpers/products_helper_test.rb new file mode 100644 index 0000000..0f5beab --- /dev/null +++ b/test/helpers/products_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class ProductsHelperTest < ActionView::TestCase +end diff --git a/test/models/product_test.rb b/test/models/product_test.rb new file mode 100644 index 0000000..211cdd0 --- /dev/null +++ b/test/models/product_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ProductTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end