Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iarie committed Apr 5, 2017
1 parent d0a1010 commit 7e2d03f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
3 changes: 1 addition & 2 deletions spec/example_app/app/models/country.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class Country < ActiveRecord::Base
validates :code, presence: true
validates :name, presence: true
validates :code, :name, presence: true
end
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class CreateCountries < ActiveRecord::Migration
def change
create_table :countries do |t|
t.string :code
t.string :code, null: false, index: { unique: true }
t.string :name

t.timestamps null: false
end
add_index :countries, :code, unique: true
add_column :customers, :country_code, :string, index: true
end
end
30 changes: 17 additions & 13 deletions spec/example_app/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,34 @@
Product.destroy_all
Country.destroy_all

Country.create(code: "US", name: "USA")
Country.create(code: "CA", name: "Canada")
Country.create(code: "CN", name: "China")
Country.create(code: "RU", name: "Russia")
Country.create(code: "AU", name: "Australia")
countries = Country.create! [
{ code: "US", name: "USA" },
{ code: "CA", name: "Canada" },
{ code: "CN", name: "China" },
{ code: "RU", name: "Russia" },
{ code: "AU", name: "Australia" },
]

100.times do
customer_attributes = Array.new(100) do
name = "#{Faker::Name.first_name} #{Faker::Name.last_name}"
Customer.create(
{
name: name,
email: Faker::Internet.safe_email(name),
country: Country.first,
)
country: countries.sample,
}
end

customers = Customer.create!(customer_attributes)

product_attributes = YAML.load_file(Rails.root.join('db/seeds/products.yml'))

product_attributes.each do |attributes|
Product.create attributes.merge(price: 20 + rand(50))
Product.create! attributes.merge(price: 20 + rand(50))
end

Customer.all.each do |customer|
customers.each do |customer|
(1..3).to_a.sample.times do
order = Order.create(
order = Order.create!(
customer: customer,
address_line_one: Faker::Address.street_address,
address_line_two: Faker::Address.secondary_address,
Expand All @@ -46,7 +50,7 @@

item_count = (1..3).to_a.sample
Product.all.sample(item_count).each do |product|
LineItem.create(
LineItem.create!(
order: order,
product: product,
unit_price: product.price,
Expand Down

0 comments on commit 7e2d03f

Please sign in to comment.