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..89e2e8d --- /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/assets/stylesheets/scaffolds.css.scss b/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..481b7ce --- /dev/null +++ b/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,71 @@ +/* +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + &:visited { + color: #666; + } + &:hover { + color: #fff; + background-color: #000; + } +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; + } + ul li { + font-size: 12px; + list-style: square; + } +} +*/ diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb new file mode 100644 index 0000000..fbf3414 --- /dev/null +++ b/app/controllers/products_controller.rb @@ -0,0 +1,89 @@ +class ProductsController < ApplicationController + before_action :set_product, only: [:show, :edit, :update, :destroy] + + # GET /products + # GET /products.json + def index + #puts params[:page], '*'*100 + @pages_count = (Product.all.size/10) + (((Product.all.size % 10) > 0) ? 1 : 0) + @current_page = params[:page].to_i + @next_page = if @current_page < (@pages_count - 1) + @current_page + 1 + else + @current_page + end + + @prev_page = if @current_page > 0 + @current_page - 1 + else + @current_page + end + + @products = Product.limit(10).offset(10 * @current_page) # @tonytonyjan++ + end + + # GET /products/1 + # GET /products/1.json + def show + end + + # GET /products/new + def new + @product = Product.new + end + + # GET /products/1/edit + def edit + end + + # POST /products + # POST /products.json + def create + @product = Product.new(product_params) + + respond_to do |format| + if @product.save + format.html { redirect_to @product, notice: 'Product was successfully created.' } + format.json { render :show, status: :created, location: @product } + else + format.html { render :new } + format.json { render json: @product.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /products/1 + # PATCH/PUT /products/1.json + def update + respond_to do |format| + if @product.update(product_params) + format.html { redirect_to @product, notice: 'Product was successfully updated.' } + format.json { render :show, status: :ok, location: @product } + else + format.html { render :edit } + format.json { render json: @product.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /products/1 + # DELETE /products/1.json + def destroy + @product.destroy + respond_to do |format| + format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_product + @product = Product.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def product_params + params.require(:product).permit(:name, :description, :price, :quantity) + 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..077a819 --- /dev/null +++ b/app/models/product.rb @@ -0,0 +1,2 @@ +class Product < ActiveRecord::Base +end diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb new file mode 100644 index 0000000..b308d7b --- /dev/null +++ b/app/views/layouts/_navbar.html.erb @@ -0,0 +1,23 @@ + + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a286513..f10e6ff 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,10 +5,20 @@ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> + + + + + + -<%= yield %> +<%= render 'layouts/navbar' %> + +
+ <%= yield %> +
diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb new file mode 100644 index 0000000..4765271 --- /dev/null +++ b/app/views/products/_form.html.erb @@ -0,0 +1,33 @@ +<%= form_for(@product) do |f| %> + <% if @product.errors.any? %> +
+

<%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :description %>
+ <%= f.text_area :description %> +
+
+ <%= f.label :price %>
+ <%= f.text_field :price %> +
+
+ <%= f.label :quantity %>
+ <%= f.number_field :quantity %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb new file mode 100644 index 0000000..9745de9 --- /dev/null +++ b/app/views/products/edit.html.erb @@ -0,0 +1,11 @@ +

Editing product

+ +<%= render 'form' %> + +
+ +| diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb new file mode 100644 index 0000000..fec87af --- /dev/null +++ b/app/views/products/index.html.erb @@ -0,0 +1,48 @@ +

Listing products

+ +
+ +

+ + + + + + + + + + + + + + + + + <% @products.each do |product| %> + + + + + + + + + + <% end %> + +
NameDescriptionPriceQuantity
<%= product.name %><%= product.description %><%= product.price %><%= product.quantity %><%= link_to 'Show', product %><%= link_to 'Edit', edit_product_path(product) %><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ diff --git a/app/views/products/index.json.jbuilder b/app/views/products/index.json.jbuilder new file mode 100644 index 0000000..7f8c148 --- /dev/null +++ b/app/views/products/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@products) do |product| + json.extract! product, :id, :name, :description, :price, :quantity + json.url product_url(product, format: :json) +end diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb new file mode 100644 index 0000000..38da173 --- /dev/null +++ b/app/views/products/new.html.erb @@ -0,0 +1,6 @@ +

New product

+ +<%= render 'form' %> + +
+ diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 0000000..3860cf3 --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

+ Name: + <%= @product.name %> +

+ +

+ Description: + <%= @product.description %> +

+ +

+ Price: + <%= @product.price %> +

+ +

+ Quantity: + <%= @product.quantity %> +

+ + +| + diff --git a/app/views/products/show.json.jbuilder b/app/views/products/show.json.jbuilder new file mode 100644 index 0000000..0e2a32a --- /dev/null +++ b/app/views/products/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @product, :id, :name, :description, :price, :quantity, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index 3f66539..579a7e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + 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/20140712191944_create_products.rb b/db/migrate/20140712191944_create_products.rb new file mode 100644 index 0000000..cea5c44 --- /dev/null +++ b/db/migrate/20140712191944_create_products.rb @@ -0,0 +1,12 @@ +class CreateProducts < ActiveRecord::Migration + def change + create_table :products do |t| + t.text :name + t.text :description + t.decimal :price + t.integer :quantity + + t.timestamps + end + end +end diff --git a/db/migrate/20140714144837_change_name_type_in_products.rb b/db/migrate/20140714144837_change_name_type_in_products.rb new file mode 100644 index 0000000..55d8c2a --- /dev/null +++ b/db/migrate/20140714144837_change_name_type_in_products.rb @@ -0,0 +1,5 @@ +class ChangeNameTypeInProducts < ActiveRecord::Migration + def change + change_column :products, :name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..82e0267 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,25 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20140714144837) do + + create_table "products", force: true do |t| + t.string "name" + t.text "description" + t.decimal "price" + t.integer "quantity" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/lib/fake_data.rb b/lib/fake_data.rb new file mode 100644 index 0000000..87b34f6 --- /dev/null +++ b/lib/fake_data.rb @@ -0,0 +1,9 @@ +# Write fake data to DB +50.times do |i| + product = Product.new + product.name = "item-#{i}" + product.description = "#{i}: item-#{i}" + product.price = rand(100) + product.quantity = rand(10) + product.save +end