From a0ab50d3b9cfdfd4f479412cd9b5236000d274f0 Mon Sep 17 00:00:00 2001 From: calvin Date: Fri, 18 Jul 2014 14:52:15 +0800 Subject: [PATCH 1/4] first commit --- .DS_Store | Bin 0 -> 6148 bytes app/.DS_Store | Bin 0 -> 6148 bytes app/assets/javascripts/products.js.coffee | 3 + app/assets/stylesheets/products.css.scss | 3 + app/assets/stylesheets/scaffolds.css.scss | 69 +++++++++++++++++ app/controllers/products_controller.rb | 74 +++++++++++++++++++ app/helpers/products_helper.rb | 2 + app/models/product.rb | 2 + app/views/layouts/application.html.erb | 6 ++ app/views/products/_form.html.erb | 25 +++++++ app/views/products/edit.html.erb | 6 ++ app/views/products/index.html.erb | 27 +++++++ app/views/products/index.json.jbuilder | 4 + app/views/products/new.html.erb | 5 ++ app/views/products/show.html.erb | 14 ++++ app/views/products/show.json.jbuilder | 1 + config/routes.rb | 4 + db/migrate/20140718043635_create_products.rb | 10 +++ db/schema.rb | 23 ++++++ 19 files changed, 278 insertions(+) create mode 100644 .DS_Store create mode 100644 app/.DS_Store create mode 100644 app/assets/javascripts/products.js.coffee create mode 100644 app/assets/stylesheets/products.css.scss create mode 100644 app/assets/stylesheets/scaffolds.css.scss create mode 100644 app/controllers/products_controller.rb create mode 100644 app/helpers/products_helper.rb create mode 100644 app/models/product.rb create mode 100644 app/views/products/_form.html.erb create mode 100644 app/views/products/edit.html.erb create mode 100644 app/views/products/index.html.erb create mode 100644 app/views/products/index.json.jbuilder create mode 100644 app/views/products/new.html.erb create mode 100644 app/views/products/show.html.erb create mode 100644 app/views/products/show.json.jbuilder create mode 100644 db/migrate/20140718043635_create_products.rb create mode 100644 db/schema.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e81c242592628868a41ba832ff3693730f757c78 GIT binary patch literal 6148 zcmeH~JqiLr422WdLa^D=avBfd4F=H@cme-PK|$>2=)U~0;A$-*FOa;MY{If%u`>}7 zT|dq%kzPb*a8p@Y7@1<<$w6*%JKs;2q+Xuer(ih*ggixzBPT3r}OfCNSay4d%2{%_%*=KoO(w?mHq-SE8F0$NjBC^9f!1RMhc34E2n6WFp5 AYybcN literal 0 HcmV?d00001 diff --git a/app/.DS_Store b/app/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9846351313a9f8c454456c50e7b0998ab2c9c786 GIT binary patch literal 6148 zcmeHKyH3ME5S)bokzr0a|zbHnR^NCx?~-cCEef?d{yz zQ+T}qWcA!#0ZRaLx+30mn3}GuPwXTjGosiYI}GS~ZLynV9}g&Zfem?2IFR+l-*@{> ze?JVp5-;$GJ3PeJ&C)rS26Q~LN3(E-7c#bZZNtZ(BcrK6Dv%1K0;#}%QGhjDZN6~K zF%?J!Qh`qe^!rfgiZyU_v`+^IYXOM*8*j#Y>m`Uq1H>9QIx<2NrxKki(PD_x884An z14l=vL!$YRI9Z}Y5j&mx7fXj!#~f3ERG?Mh)Tc|W|JU>%`u~=cvs54z_)`kVY<;s{ z@s*;sPF_xHZK2=NKa9DS&cRAC(MmBVT8b~fdJP;Mjn25yiTNX-x}>E7zoEc8 DiZLf3 literal 0 HcmV?d00001 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..6ec6a8f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,69 @@ +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..06935f1 --- /dev/null +++ b/app/controllers/products_controller.rb @@ -0,0 +1,74 @@ +class ProductsController < ApplicationController + before_action :set_product, only: [:show, :edit, :update, :destroy] + + # GET /products + # GET /products.json + def index + @products = Product.all + 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, :price) + 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/application.html.erb b/app/views/layouts/application.html.erb index a286513..9356fdf 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,6 +5,12 @@ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> + + + + + + diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb new file mode 100644 index 0000000..02a3aae --- /dev/null +++ b/app/views/products/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for(@product) do |f| %> + <% if @product.errors.any? %> +
+

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

+ +
    + <% @product.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :price %>
+ <%= f.number_field :price %> +
+
+ <%= 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..39f7f4f --- /dev/null +++ b/app/views/products/edit.html.erb @@ -0,0 +1,6 @@ +

Editing product

+ +<%= render 'form' %> + +<%= link_to 'Show', @product %> | +<%= link_to 'Back', products_path %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb new file mode 100644 index 0000000..05d2647 --- /dev/null +++ b/app/views/products/index.html.erb @@ -0,0 +1,27 @@ +

Products List

+ + + + + + + + + + + + <% @products.each do |product| %> + + + + + + + + <% end %> + +
NamePrice
<%= product.name %><%= product.price %><%= link_to 'Show', product %><%= link_to 'Edit', edit_product_path(product) %><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Product', new_product_path %> diff --git a/app/views/products/index.json.jbuilder b/app/views/products/index.json.jbuilder new file mode 100644 index 0000000..c4887c6 --- /dev/null +++ b/app/views/products/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@products) do |product| + json.extract! product, :id, :name, :price + 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..e5e0037 --- /dev/null +++ b/app/views/products/new.html.erb @@ -0,0 +1,5 @@ +

New product

+ +<%= render 'form' %> + +<%= link_to 'Back', products_path %> diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 0000000..1433a77 --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

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

+ +

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

+ +<%= link_to 'Edit', edit_product_path(@product) %> | +<%= link_to 'Back', products_path %> diff --git a/app/views/products/show.json.jbuilder b/app/views/products/show.json.jbuilder new file mode 100644 index 0000000..e25ce28 --- /dev/null +++ b/app/views/products/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @product, :id, :name, :price, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index 3f66539..b01eae6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ Rails.application.routes.draw do + resources :products + + root 'products#index' + # 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/20140718043635_create_products.rb b/db/migrate/20140718043635_create_products.rb new file mode 100644 index 0000000..08cc16e --- /dev/null +++ b/db/migrate/20140718043635_create_products.rb @@ -0,0 +1,10 @@ +class CreateProducts < ActiveRecord::Migration + def change + create_table :products do |t| + t.string :name + t.integer :price + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..a9a221a --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# 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: 20140718043635) do + + create_table "products", force: true do |t| + t.string "name" + t.integer "price" + t.datetime "created_at" + t.datetime "updated_at" + end + +end From e2de098ee6e2fdfb001586848cc94c647f986004 Mon Sep 17 00:00:00 2001 From: calvin Date: Fri, 18 Jul 2014 15:28:02 +0800 Subject: [PATCH 2/4] second commit --- app/views/products/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index 05d2647..f6dad3d 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -24,4 +24,4 @@
-<%= link_to 'New Product', new_product_path %> +<%= link_to 'Add New Product', new_product_path %> From fbfb9b3ce7edbe127d76a892c7b5ebf49991e3d3 Mon Sep 17 00:00:00 2001 From: calvin Date: Fri, 18 Jul 2014 15:40:02 +0800 Subject: [PATCH 3/4] Updates for heroku deployment --- Gemfile | 9 ++++++++- Gemfile.lock | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 6219184..0387b99 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,14 @@ source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.1.4' # Use sqlite3 as the database for Active Record -gem 'sqlite3' +group :development, :test do + gem 'sqlite3' +end + +group :production do + gem 'pg' + gem 'rails_12factor' +end # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.3' # Use Uglifier as compressor for JavaScript assets diff --git a/Gemfile.lock b/Gemfile.lock index 777f120..38bd5c3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -53,6 +53,7 @@ GEM mime-types (1.25.1) minitest (5.4.0) multi_json (1.10.1) + pg (0.17.1) polyglot (0.3.5) rack (1.5.2) rack-test (0.6.2) @@ -67,6 +68,11 @@ GEM bundler (>= 1.3.0, < 2.0) railties (= 4.1.4) sprockets-rails (~> 2.0) + rails_12factor (0.0.2) + rails_serve_static_assets + rails_stdout_logging + rails_serve_static_assets (0.0.2) + rails_stdout_logging (0.0.3) railties (4.1.4) actionpack (= 4.1.4) activesupport (= 4.1.4) @@ -116,7 +122,9 @@ DEPENDENCIES coffee-rails (~> 4.0.0) jbuilder (~> 2.0) jquery-rails + pg rails (= 4.1.4) + rails_12factor sass-rails (~> 4.0.3) sdoc (~> 0.4.0) spring From 53266e492a26c5d03912ae2dcae47b837e1db4d9 Mon Sep 17 00:00:00 2001 From: calvin Date: Fri, 18 Jul 2014 16:55:52 +0800 Subject: [PATCH 4/4] third commit --- app/assets/stylesheets/application.css | 3 +++ app/views/layouts/application.html.erb | 32 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index a443db3..bfa1180 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,3 +13,6 @@ *= require_tree . *= require_self */ +footer { margin-top: 100px; } +td { text-align: center; padding-left: 10px; } +th { border-bottom: 1px solid #DDD; padding-left: 20px; } \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9356fdf..fd05396 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,7 +14,37 @@ -<%= yield %> + + +
+ + <%= yield %> + +
+ +
+
+ calvin's rails 2014 +
+
+