From d3cdb9fcd7c191ac52ae1473b6ad8ed1dfba16e5 Mon Sep 17 00:00:00 2001 From: Basho Date: Sun, 9 Nov 2014 22:53:39 +0100 Subject: [PATCH] order model --- Gemfile | 10 +++++++++- Gemfile.lock | 8 ++++++++ app/controllers/users_controller.rb | 3 ++- app/models/order.rb | 3 +++ app/models/user.rb | 1 + app/views/orders/_order.html.erb | 8 ++++++++ app/views/users/index.html.erb | 6 +++++- app/views/users/show.html.erb | 21 ++++++++++++++++++++- config/routes.rb | 2 +- db/migrate/20141109174952_create_orders.rb | 11 +++++++++++ db/schema.rb | 12 +++++++++++- db/seeds.rb | 16 ++++++++++++++++ test/fixtures/orders.yml | 9 +++++++++ test/models/order_test.rb | 7 +++++++ 14 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 app/models/order.rb create mode 100644 app/views/orders/_order.html.erb create mode 100644 db/migrate/20141109174952_create_orders.rb create mode 100644 test/fixtures/orders.yml create mode 100644 test/models/order_test.rb diff --git a/Gemfile b/Gemfile index 947f4c9..c6b7b16 100644 --- a/Gemfile +++ b/Gemfile @@ -41,4 +41,12 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin , :x64_mingw] #bootstrap -gem 'bootstrap-sass', '3.2.0.0' \ No newline at end of file +gem 'bootstrap-sass', '3.2.0.0' + + +#generate data +gem 'faker', '1.4.2' + +#paginate stuff +gem 'will_paginate', '3.0.7' +gem 'bootstrap-will_paginate', '0.0.10' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index b61e801..89a0498 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -30,6 +30,8 @@ GEM arel (5.0.1.20140414130214) bootstrap-sass (3.2.0.0) sass (~> 3.2) + bootstrap-will_paginate (0.0.10) + will_paginate builder (3.2.2) coffee-rails (4.0.1) coffee-script (>= 2.2.0) @@ -40,6 +42,8 @@ GEM coffee-script-source (1.8.0) erubis (2.7.0) execjs (2.2.2) + faker (1.4.2) + i18n (~> 0.5) hike (1.2.3) i18n (0.6.11) jbuilder (2.2.4) @@ -106,13 +110,16 @@ GEM uglifier (2.5.3) execjs (>= 0.3.0) json (>= 1.8.0) + will_paginate (3.0.7) PLATFORMS x64-mingw32 DEPENDENCIES bootstrap-sass (= 3.2.0.0) + bootstrap-will_paginate (= 0.0.10) coffee-rails (~> 4.0.0) + faker (= 1.4.2) jbuilder (~> 2.0) jquery-rails rails (= 4.1.7) @@ -122,3 +129,4 @@ DEPENDENCIES turbolinks tzinfo-data uglifier (>= 1.3.0) + will_paginate (= 3.0.7) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 024648a..3b8a4f3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,7 +3,7 @@ class UsersController < ApplicationController before_action :correct_user, only: [:edit, :update] def index - @users = User.all + @users = User.paginate(page: params[:page]) end @@ -13,6 +13,7 @@ class UsersController < ApplicationController def show @user = User.find(params[:id]) + @orders = @user.orders.paginate(page: params[:page]) end def create diff --git a/app/models/order.rb b/app/models/order.rb new file mode 100644 index 0000000..bd64f52 --- /dev/null +++ b/app/models/order.rb @@ -0,0 +1,3 @@ +class Order < ActiveRecord::Base + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 5d09561..f756d5d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + has_many :orders, dependent: :destroy after_initialize :init validates :name, presence: true, length: { maximum: 50 }, diff --git a/app/views/orders/_order.html.erb b/app/views/orders/_order.html.erb new file mode 100644 index 0000000..dc60779 --- /dev/null +++ b/app/views/orders/_order.html.erb @@ -0,0 +1,8 @@ +
  • + + <%= link_to order.user.name, order.user %> + <%= order.products %> + + Posted <%= time_ago_in_words(order.created_at) %> ago. + +
  • \ No newline at end of file diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 45eec02..0da4fea 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -1,9 +1,13 @@

    All users

    +<%= will_paginate %> + \ No newline at end of file + + +<%= will_paginate %> \ No newline at end of file diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index aa2e444..b9cb844 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1 +1,20 @@ -<%= @user.name %>, <%= @user.marks %> \ No newline at end of file +
    + +
    + <% if @user.orders.any? %> +

    Orders (<%= @user.orders.count %>)

    +
      + <%= render @orders %> +
    + <%= will_paginate @orders %> + <% end %> +
    +
    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 96fb17b..9b5764f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do - get 'orders/new' + get 'orders/show' root 'static_pages#home' diff --git a/db/migrate/20141109174952_create_orders.rb b/db/migrate/20141109174952_create_orders.rb new file mode 100644 index 0000000..6d36487 --- /dev/null +++ b/db/migrate/20141109174952_create_orders.rb @@ -0,0 +1,11 @@ +class CreateOrders < ActiveRecord::Migration + def change + create_table :orders do |t| + t.text :products + t.references :user, index: true + + t.timestamps + end + add_index :orders, [:user_id, :created_at] + end +end diff --git a/db/schema.rb b/db/schema.rb index c368142..19cee5a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141106125617) do +ActiveRecord::Schema.define(version: 20141109174952) do + + create_table "orders", force: true do |t| + t.text "products" + t.integer "user_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + 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 "users", force: true do |t| t.string "name" diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..069e2f6 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,19 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) + + +User.create!(name: "Example User", marks: 5) + +99.times do |n| + name = Faker::Name.name + marks = n+1 + User.create!(name: name, + marks: marks) +end + +users = User.order(:created_at).take(6) +50.times do + products = Faker::Lorem.sentence(5) + users.each { |user| user.orders.create!(products: products) } +end \ No newline at end of file diff --git a/test/fixtures/orders.yml b/test/fixtures/orders.yml new file mode 100644 index 0000000..1b4cdb8 --- /dev/null +++ b/test/fixtures/orders.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + products: + user_id: + +two: + products: + user_id: diff --git a/test/models/order_test.rb b/test/models/order_test.rb new file mode 100644 index 0000000..15b8ed1 --- /dev/null +++ b/test/models/order_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class OrderTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end