Permalink
Browse files

add products

  • Loading branch information...
1 parent f426b60 commit 38bf08484245e2dfba9a792a4f395bda969ec38d @lucapette committed Nov 17, 2011
View
3 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://jashkenas.github.com/coffee-script/
View
3 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/
View
56 app/assets/stylesheets/scaffolds.css.scss
@@ -0,0 +1,56 @@
+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; } }
View
83 app/controllers/products_controller.rb
@@ -0,0 +1,83 @@
+class ProductsController < ApplicationController
+ # GET /products
+ # GET /products.json
+ def index
+ @products = Product.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @products }
+ end
+ end
+
+ # GET /products/1
+ # GET /products/1.json
+ def show
+ @product = Product.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @product }
+ end
+ end
+
+ # GET /products/new
+ # GET /products/new.json
+ def new
+ @product = Product.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @product }
+ end
+ end
+
+ # GET /products/1/edit
+ def edit
+ @product = Product.find(params[:id])
+ end
+
+ # POST /products
+ # POST /products.json
+ def create
+ @product = Product.new(params[:product])
+
+ respond_to do |format|
+ if @product.save
+ format.html { redirect_to @product, notice: 'Product was successfully created.' }
+ format.json { render json: @product, status: :created, location: @product }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @product.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /products/1
+ # PUT /products/1.json
+ def update
+ @product = Product.find(params[:id])
+
+ respond_to do |format|
+ if @product.update_attributes(params[:product])
+ format.html { redirect_to @product, notice: 'Product was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @product.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /products/1
+ # DELETE /products/1.json
+ def destroy
+ @product = Product.find(params[:id])
+ @product.destroy
+
+ respond_to do |format|
+ format.html { redirect_to products_url }
+ format.json { head :ok }
+ end
+ end
+end
View
2 app/helpers/products_helper.rb
@@ -0,0 +1,2 @@
+module ProductsHelper
+end
View
2 app/models/product.rb
@@ -0,0 +1,2 @@
+class Product < ActiveRecord::Base
+end
View
13 app/views/products/_form.html.erb
@@ -0,0 +1,13 @@
+<%= simple_form_for(@product, :wrapper => :inline) do |f| %>
+ <%= f.error_notification %>
+
+ <div class="inputs">
+ <%= f.input :name %>
+ <%= f.input :description %>
+ <%= f.input :price %>
+ </div>
+
+ <div class="actions">
+ <%= f.button :submit %>
+ </div>
+<% end %>
View
6 app/views/products/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing product</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @product %> |
+<%= link_to 'Back', products_path %>
View
31 app/views/products/index.html.erb
@@ -0,0 +1,31 @@
+<h1>Listing products</h1>
+
+<table>
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Description</th>
+ <th>Price</th>
+ <th>Actions</th>
+ </tr>
+ </thead>
+
+ <tbody>
+ <% @products.each do |product| %>
+ <tr>
+ <td><%= product.name %></td>
+ <td><%= product.description %></td>
+ <td><%= product.price %></td>
+ <td>
+ <%= link_to 'Show', product, :class=>"btn" %>
+ <%= link_to 'Edit', edit_product_path(product), :class=>"btn" %>
+ <%= link_to 'Destroy', product, confirm: 'Are you sure?', method: :delete, :class=>"btn" %>
+ </td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+
+<div class="actions">
+ <%= link_to 'New Product', new_product_path, :class=>"btn" %>
+</div>
View
5 app/views/products/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New product</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', products_path %>
View
10 app/views/products/show.html.erb
@@ -0,0 +1,10 @@
+<%= show_for @product do |s| %>
+ <%= s.attribute :name %>
+ <%= s.attribute :description %>
+ <%= s.attribute :price %>
+<% end %>
+
+<div class="actions">
+ <%= link_to 'Edit', edit_product_path(@product), :class=>"btn" %>
+ <%= link_to 'Back', products_path, :class=>"btn" %>
+</div>
View
57 config/routes.rb
@@ -1,58 +1,5 @@
TwitterBootstrapOnRailsDemo::Application.routes.draw do
- # The priority is based upon order of creation:
- # first created -> highest priority.
+ resources :products
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', :on => :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- # root :to => 'welcome#index'
-
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id(.:format)))'
+ root :to => 'products#index'
end
View
11 db/migrate/20111117180928_create_products.rb
@@ -0,0 +1,11 @@
+class CreateProducts < ActiveRecord::Migration
+ def change
+ create_table :products do |t|
+ t.string :name
+ t.text :description
+ t.integer :price
+
+ t.timestamps
+ end
+ end
+end
View
24 db/schema.rb
@@ -0,0 +1,24 @@
+# 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 to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20111117180928) do
+
+ create_table "products", :force => true do |t|
+ t.string "name"
+ t.text "description"
+ t.integer "price"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
241 public/index.html
@@ -1,241 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- <title>Ruby on Rails: Welcome aboard</title>
- <style type="text/css" media="screen">
- body {
- margin: 0;
- margin-bottom: 25px;
- padding: 0;
- background-color: #f0f0f0;
- font-family: "Lucida Grande", "Bitstream Vera Sans", "Verdana";
- font-size: 13px;
- color: #333;
- }
-
- h1 {
- font-size: 28px;
- color: #000;
- }
-
- a {color: #03c}
- a:hover {
- background-color: #03c;
- color: white;
- text-decoration: none;
- }
-
-
- #page {
- background-color: #f0f0f0;
- width: 750px;
- margin: 0;
- margin-left: auto;
- margin-right: auto;
- }
-
- #content {
- float: left;
- background-color: white;
- border: 3px solid #aaa;
- border-top: none;
- padding: 25px;
- width: 500px;
- }
-
- #sidebar {
- float: right;
- width: 175px;
- }
-
- #footer {
- clear: both;
- }
-
- #header, #about, #getting-started {
- padding-left: 75px;
- padding-right: 30px;
- }
-
-
- #header {
- background-image: url("/assets/rails.png");
- background-repeat: no-repeat;
- background-position: top left;
- height: 64px;
- }
- #header h1, #header h2 {margin: 0}
- #header h2 {
- color: #888;
- font-weight: normal;
- font-size: 16px;
- }
-
-
- #about h3 {
- margin: 0;
- margin-bottom: 10px;
- font-size: 14px;
- }
-
- #about-content {
- background-color: #ffd;
- border: 1px solid #fc0;
- margin-left: -55px;
- margin-right: -10px;
- }
- #about-content table {
- margin-top: 10px;
- margin-bottom: 10px;
- font-size: 11px;
- border-collapse: collapse;
- }
- #about-content td {
- padding: 10px;
- padding-top: 3px;
- padding-bottom: 3px;
- }
- #about-content td.name {color: #555}
- #about-content td.value {color: #000}
-
- #about-content ul {
- padding: 0;
- list-style-type: none;
- }
-
- #about-content.failure {
- background-color: #fcc;
- border: 1px solid #f00;
- }
- #about-content.failure p {
- margin: 0;
- padding: 10px;
- }
-
-
- #getting-started {
- border-top: 1px solid #ccc;
- margin-top: 25px;
- padding-top: 15px;
- }
- #getting-started h1 {
- margin: 0;
- font-size: 20px;
- }
- #getting-started h2 {
- margin: 0;
- font-size: 14px;
- font-weight: normal;
- color: #333;
- margin-bottom: 25px;
- }
- #getting-started ol {
- margin-left: 0;
- padding-left: 0;
- }
- #getting-started li {
- font-size: 18px;
- color: #888;
- margin-bottom: 25px;
- }
- #getting-started li h2 {
- margin: 0;
- font-weight: normal;
- font-size: 18px;
- color: #333;
- }
- #getting-started li p {
- color: #555;
- font-size: 13px;
- }
-
-
- #sidebar ul {
- margin-left: 0;
- padding-left: 0;
- }
- #sidebar ul h3 {
- margin-top: 25px;
- font-size: 16px;
- padding-bottom: 10px;
- border-bottom: 1px solid #ccc;
- }
- #sidebar li {
- list-style-type: none;
- }
- #sidebar ul.links li {
- margin-bottom: 5px;
- }
-
- .filename {
- font-style: italic;
- }
- </style>
- <script type="text/javascript">
- function about() {
- info = document.getElementById('about-content');
- if (window.XMLHttpRequest)
- { xhr = new XMLHttpRequest(); }
- else
- { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
- xhr.open("GET","rails/info/properties",false);
- xhr.send("");
- info.innerHTML = xhr.responseText;
- info.style.display = 'block'
- }
- </script>
- </head>
- <body>
- <div id="page">
- <div id="sidebar">
- <ul id="sidebar-items">
- <li>
- <h3>Browse the documentation</h3>
- <ul class="links">
- <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>
- <li><a href="http://api.rubyonrails.org/">Rails API</a></li>
- <li><a href="http://www.ruby-doc.org/core/">Ruby core</a></li>
- <li><a href="http://www.ruby-doc.org/stdlib/">Ruby standard library</a></li>
- </ul>
- </li>
- </ul>
- </div>
-
- <div id="content">
- <div id="header">
- <h1>Welcome aboard</h1>
- <h2>You&rsquo;re riding Ruby on Rails!</h2>
- </div>
-
- <div id="about">
- <h3><a href="rails/info/properties" onclick="about(); return false">About your application&rsquo;s environment</a></h3>
- <div id="about-content" style="display: none"></div>
- </div>
-
- <div id="getting-started">
- <h1>Getting started</h1>
- <h2>Here&rsquo;s how to get rolling:</h2>
-
- <ol>
- <li>
- <h2>Use <code>rails generate</code> to create your models and controllers</h2>
- <p>To see all available options, run it without parameters.</p>
- </li>
-
- <li>
- <h2>Set up a default route and remove <span class="filename">public/index.html</span></h2>
- <p>Routes are set up in <span class="filename">config/routes.rb</span>.</p>
- </li>
-
- <li>
- <h2>Create your database</h2>
- <p>Run <code>rake db:create</code> to create your database. If you're not using SQLite (the default), edit <span class="filename">config/database.yml</span> with your username and password.</p>
- </li>
- </ol>
- </div>
- </div>
-
- <div id="footer">&nbsp;</div>
- </div>
- </body>
-</html>
View
11 test/fixtures/products.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ name: MyString
+ description: MyText
+ price: 1
+
+two:
+ name: MyString
+ description: MyText
+ price: 1
View
49 test/functional/products_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class ProductsControllerTest < ActionController::TestCase
+ setup do
+ @product = products(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:products)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create product" do
+ assert_difference('Product.count') do
+ post :create, product: @product.attributes
+ end
+
+ assert_redirected_to product_path(assigns(:product))
+ end
+
+ test "should show product" do
+ get :show, id: @product.to_param
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @product.to_param
+ assert_response :success
+ end
+
+ test "should update product" do
+ put :update, id: @product.to_param, product: @product.attributes
+ assert_redirected_to product_path(assigns(:product))
+ end
+
+ test "should destroy product" do
+ assert_difference('Product.count', -1) do
+ delete :destroy, id: @product.to_param
+ end
+
+ assert_redirected_to products_path
+ end
+end
View
4 test/unit/helpers/products_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class ProductsHelperTest < ActionView::TestCase
+end
View
7 test/unit/product_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ProductTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 38bf084

Please sign in to comment.