order model
This commit is contained in:
parent
c54e13978a
commit
d3cdb9fcd7
14 changed files with 111 additions and 6 deletions
10
Gemfile
10
Gemfile
|
@ -41,4 +41,12 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin , :x64_mingw]
|
|||
|
||||
|
||||
#bootstrap
|
||||
gem 'bootstrap-sass', '3.2.0.0'
|
||||
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'
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
3
app/models/order.rb
Normal file
3
app/models/order.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Order < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
end
|
|
@ -1,4 +1,5 @@
|
|||
class User < ActiveRecord::Base
|
||||
has_many :orders, dependent: :destroy
|
||||
after_initialize :init
|
||||
|
||||
validates :name, presence: true, length: { maximum: 50 },
|
||||
|
|
8
app/views/orders/_order.html.erb
Normal file
8
app/views/orders/_order.html.erb
Normal file
|
@ -0,0 +1,8 @@
|
|||
<li id="order-<%= order.id %>">
|
||||
|
||||
<span class="user"><%= link_to order.user.name, order.user %></span>
|
||||
<span class="products"><%= order.products %></span>
|
||||
<span class="timestamp">
|
||||
Posted <%= time_ago_in_words(order.created_at) %> ago.
|
||||
</span>
|
||||
</li>
|
|
@ -1,9 +1,13 @@
|
|||
<h1>All users</h1>
|
||||
|
||||
<%= will_paginate %>
|
||||
|
||||
<ul class="users">
|
||||
<% @users.each do |user| %>
|
||||
<li>
|
||||
<%= link_to user.name, user %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</ul>
|
||||
|
||||
<%= will_paginate %>
|
|
@ -1 +1,20 @@
|
|||
<%= @user.name %>, <%= @user.marks %>
|
||||
<div class="row">
|
||||
<aside class="col-md-4">
|
||||
<section class="user_info">
|
||||
<h1>
|
||||
|
||||
<%= @user.name %>
|
||||
<%= @user.marks %>
|
||||
</h1>
|
||||
</section>
|
||||
</aside>
|
||||
<div class="col-md-8">
|
||||
<% if @user.orders.any? %>
|
||||
<h3>Orders (<%= @user.orders.count %>)</h3>
|
||||
<ol class="orders">
|
||||
<%= render @orders %>
|
||||
</ol>
|
||||
<%= will_paginate @orders %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
|
@ -1,6 +1,6 @@
|
|||
Rails.application.routes.draw do
|
||||
|
||||
get 'orders/new'
|
||||
get 'orders/show'
|
||||
|
||||
root 'static_pages#home'
|
||||
|
||||
|
|
11
db/migrate/20141109174952_create_orders.rb
Normal file
11
db/migrate/20141109174952_create_orders.rb
Normal file
|
@ -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
|
12
db/schema.rb
12
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"
|
||||
|
|
16
db/seeds.rb
16
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
|
9
test/fixtures/orders.yml
vendored
Normal file
9
test/fixtures/orders.yml
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
products:
|
||||
user_id:
|
||||
|
||||
two:
|
||||
products:
|
||||
user_id:
|
7
test/models/order_test.rb
Normal file
7
test/models/order_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require 'test_helper'
|
||||
|
||||
class OrderTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in a new issue