Skip to content
Browse files

Add basic Transactions scaffold

  • Loading branch information...
1 parent 0d8625a commit db3bbf1d5b8e0632ad4a2595d5f9c9dd489adae5 @rizwanreza committed Feb 21, 2014
View
74 app/controllers/transactions_controller.rb
@@ -0,0 +1,74 @@
+class TransactionsController < ApplicationController
+ before_action :set_transaction, only: [:show, :edit, :update, :destroy]
+
+ # GET /transactions
+ # GET /transactions.json
+ def index
+ @transactions = Transaction.all
+ end
+
+ # GET /transactions/1
+ # GET /transactions/1.json
+ def show
+ end
+
+ # GET /transactions/new
+ def new
+ @transaction = Transaction.new
+ end
+
+ # GET /transactions/1/edit
+ def edit
+ end
+
+ # POST /transactions
+ # POST /transactions.json
+ def create
+ @transaction = Transaction.new(transaction_params)
+
+ respond_to do |format|
+ if @transaction.save
+ format.html { redirect_to @transaction, notice: 'Transaction was successfully created.' }
+ format.json { render action: 'show', status: :created, location: @transaction }
+ else
+ format.html { render action: 'new' }
+ format.json { render json: @transaction.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PATCH/PUT /transactions/1
+ # PATCH/PUT /transactions/1.json
+ def update
+ respond_to do |format|
+ if @transaction.update(transaction_params)
+ format.html { redirect_to @transaction, notice: 'Transaction was successfully updated.' }
+ format.json { render action: 'show', status: :ok, location: @transaction }
+ else
+ format.html { render action: 'edit' }
+ format.json { render json: @transaction.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /transactions/1
+ # DELETE /transactions/1.json
+ def destroy
+ @transaction.destroy
+ respond_to do |format|
+ format.html { redirect_to transactions_url }
+ format.json { head :no_content }
+ end
+ end
+
+ private
+ # Use callbacks to share common setup or constraints between actions.
+ def set_transaction
+ @transaction = Transaction.find(params[:id])
+ end
+
+ # Never trust parameters from the scary internet, only allow the white list through.
+ def transaction_params
+ params.require(:transaction).permit(:purchaser_name, :description, :address, :merchant_name, :quantity, :price)
+ end
+end
View
41 app/views/transactions/_form.html.erb
@@ -0,0 +1,41 @@
+<%= form_for(@transaction) do |f| %>
+ <% if @transaction.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@transaction.errors.count, "error") %> prohibited this transaction from being saved:</h2>
+
+ <ul>
+ <% @transaction.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :purchaser_name %><br>
+ <%= f.text_field :purchaser_name %>
+ </div>
+ <div class="field">
+ <%= f.label :description %><br>
+ <%= f.text_field :description %>
+ </div>
+ <div class="field">
+ <%= f.label :address %><br>
+ <%= f.text_field :address %>
+ </div>
+ <div class="field">
+ <%= f.label :merchant_name %><br>
+ <%= f.text_field :merchant_name %>
+ </div>
+ <div class="field">
+ <%= f.label :quantity %><br>
+ <%= f.number_field :quantity %>
+ </div>
+ <div class="field">
+ <%= f.label :price %><br>
+ <%= f.text_field :price %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
View
6 app/views/transactions/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing transaction</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @transaction %> |
+<%= link_to 'Back', transactions_path %>
View
35 app/views/transactions/index.html.erb
@@ -0,0 +1,35 @@
+<h1>Listing transactions</h1>
+
+<table>
+ <thead>
+ <tr>
+ <th>Purchaser name</th>
+ <th>Description</th>
+ <th>Address</th>
+ <th>Merchant name</th>
+ <th>Quantity</th>
+ <th>Price</th>
+ <th colspan="3"></th>
+ </tr>
+ </thead>
+
+ <tbody>
+ <% @transactions.each do |transaction| %>
+ <tr>
+ <td><%= transaction.purchaser_name %></td>
+ <td><%= transaction.description %></td>
+ <td><%= transaction.address %></td>
+ <td><%= transaction.merchant_name %></td>
+ <td><%= transaction.quantity %></td>
+ <td><%= transaction.price %></td>
+ <td><%= link_to 'Show', transaction %></td>
+ <td><%= link_to 'Edit', edit_transaction_path(transaction) %></td>
+ <td><%= link_to 'Destroy', transaction, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+
+<br>
+
+<%= link_to 'New Transaction', new_transaction_path %>
View
4 app/views/transactions/index.json.jbuilder
@@ -0,0 +1,4 @@
+json.array!(@transactions) do |transaction|
+ json.extract! transaction, :id, :purchaser_name, :description, :address, :merchant_name, :quantity, :price
+ json.url transaction_url(transaction, format: :json)
+end
View
5 app/views/transactions/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New transaction</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', transactions_path %>
View
34 app/views/transactions/show.html.erb
@@ -0,0 +1,34 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <strong>Purchaser name:</strong>
+ <%= @transaction.purchaser_name %>
+</p>
+
+<p>
+ <strong>Description:</strong>
+ <%= @transaction.description %>
+</p>
+
+<p>
+ <strong>Address:</strong>
+ <%= @transaction.address %>
+</p>
+
+<p>
+ <strong>Merchant name:</strong>
+ <%= @transaction.merchant_name %>
+</p>
+
+<p>
+ <strong>Quantity:</strong>
+ <%= @transaction.quantity %>
+</p>
+
+<p>
+ <strong>Price:</strong>
+ <%= @transaction.price %>
+</p>
+
+<%= link_to 'Edit', edit_transaction_path(@transaction) %> |
+<%= link_to 'Back', transactions_path %>
View
1 app/views/transactions/show.json.jbuilder
@@ -0,0 +1 @@
+json.extract! @transaction, :id, :purchaser_name, :description, :address, :merchant_name, :quantity, :price, :created_at, :updated_at
View
1 config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
+ resources :transactions
resource :upload, only: [:new, :create]
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
View
27 db/schema.rb
@@ -0,0 +1,27 @@
+# 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: 20140220014430) do
+
+ create_table "transactions", force: true do |t|
+ t.string "purchaser_name"
+ t.string "description"
+ t.string "address"
+ t.string "merchant_name"
+ t.integer "quantity"
+ t.decimal "price"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
49 test/controllers/transactions_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class TransactionsControllerTest < ActionController::TestCase
+ setup do
+ @transaction = transactions(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:transactions)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create transaction" do
+ assert_difference('Transaction.count') do
+ post :create, transaction: { address: @transaction.address, description: @transaction.description, merchant_name: @transaction.merchant_name, price: @transaction.price, purchaser_name: @transaction.purchaser_name, quantity: @transaction.quantity }
+ end
+
+ assert_redirected_to transaction_path(assigns(:transaction))
+ end
+
+ test "should show transaction" do
+ get :show, id: @transaction
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @transaction
+ assert_response :success
+ end
+
+ test "should update transaction" do
+ patch :update, id: @transaction, transaction: { address: @transaction.address, description: @transaction.description, merchant_name: @transaction.merchant_name, price: @transaction.price, purchaser_name: @transaction.purchaser_name, quantity: @transaction.quantity }
+ assert_redirected_to transaction_path(assigns(:transaction))
+ end
+
+ test "should destroy transaction" do
+ assert_difference('Transaction.count', -1) do
+ delete :destroy, id: @transaction
+ end
+
+ assert_redirected_to transactions_path
+ end
+end
View
11 test/fixtures/transactions.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
16 test/integration/transactions_list_test.rb
@@ -0,0 +1,16 @@
+require 'test_helper'
+
+class TransactionsListTest < ActionDispatch::IntegrationTest
+ setup do
+ visit new_upload_path
+ attach_file "CSV File", Rails.root.join('test', 'fixtures', 'example_input.tab')
+ click_button "Upload"
+ end
+
+ test "uploading the transaction file persists them in the DB" do
+ visit transactions_path
+ assert page.has_text?("Snake Plissken")
+ assert page.has_text?("Amy Pond")
+ assert page.has_text?("5.0")
+ end
+end

0 comments on commit db3bbf1

Please sign in to comment.
Something went wrong with that request. Please try again.