Skip to content

Commit

Permalink
Merge pull request #5 from appirits/implement-comable-sample
Browse files Browse the repository at this point in the history
Implement comable_sample
  • Loading branch information
YOSHIDA Cake committed May 23, 2015
2 parents a814a0f + b63c534 commit a71b43c
Show file tree
Hide file tree
Showing 43 changed files with 470 additions and 22 deletions.
17 changes: 9 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ end
APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
load 'rails/tasks/engine.rake'

def command(string)
puts string
system string
end

if File.exist?('comable.gemspec')
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
require 'tasks/release'
Expand Down Expand Up @@ -45,9 +50,7 @@ if File.exist?('comable.gemspec')
namespace :migrate do
task :all do
FRAMEWORKS.each do |framework|
command = "cd #{framework} && test -d db && bundle exec rake db:migrate RAILS_ENV=#{Rails.env}"
puts command
system command
command "cd #{framework} && test -d db/migrate && bundle exec rake db:migrate RAILS_ENV=#{Rails.env}"
end
end
end
Expand All @@ -59,11 +62,8 @@ if File.exist?('comable.gemspec')

namespace :reset do
task :all do
FRAMEWORKS.each do |framework|
command = "cd #{framework} && test -d db && bundle exec rake db:migrate:reset RAILS_ENV=#{Rails.env}"
puts command
system command
end
command "bundle exec rake db:drop db:create RAILS_ENV=#{Rails.env}"
Rake::Task['db:migrate'].invoke
end
end
end
Expand All @@ -90,6 +90,7 @@ end
namespace :brakeman do
task :all do
FRAMEWORKS.each do |framework|
next unless File.directory?("#{framework}/app")
sh "brakeman --exit-on-warn --ignore-config .brakeman.ignore #{framework}"
end
end
Expand Down
1 change: 1 addition & 0 deletions comable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Gem::Specification.new do |s|
s.add_dependency 'comable_core', version
s.add_dependency 'comable_frontend', version
s.add_dependency 'comable_backend', version
s.add_dependency 'comable_sample', version
end
4 changes: 2 additions & 2 deletions core/app/models/comable/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class Address < ActiveRecord::Base

validates :family_name, presence: true, length: { maximum: 255 }
validates :first_name, presence: true, length: { maximum: 255 }
validates :zip_code, presence: true, length: { maximum: 8 }
validates :zip_code, presence: true, length: { maximum: 255 }
validates :state_name, presence: true, length: { maximum: 255 }
validates :city, presence: true, length: { maximum: 255 }
validates :detail, length: { maximum: 255 }
validates :phone_number, length: { maximum: 18 }
validates :phone_number, length: { maximum: 255 }

class << self
def find_or_clone(address)
Expand Down
13 changes: 13 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ en:
categories:
new_node: 'New category'

sample:
suede_dress: 'Suede Dress'
girly_coat: 'Double Button Girly Coat'
fur_gloves: 'Fur Gloves with Side Stitching'
leather_boots: 'Leather Boots'
clothing: 'Clothing'
color: 'Color'
size: 'Size'
navy: 'Navy'
beige: 'Beige'
black_cat: 'Balck Cat'
credit_card: 'Credit Card'

activerecord:
state_machines:
comable/order:
Expand Down
13 changes: 13 additions & 0 deletions core/config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ ja:
categories:
new_node: 新しいカテゴリ

sample:
suede_dress: 'スウェードワンピース'
girly_coat: 'ダブルボタンガーリーコート'
fur_gloves: 'サイドステッチファー付き手袋'
leather_boots: '本革ロングブーツ'
clothing: '衣類'
color: 'カラー'
size: 'サイズ'
navy: 'ネイビー'
beige: 'ベージュ'
black_cat: 'クロネコ'
credit_card: 'クレジットカード'

activerecord:
state_machines:
comable/order:
Expand Down
4 changes: 2 additions & 2 deletions core/db/migrate/20141024025526_create_comable_addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ def change
t.references :user
t.string :family_name, null: false
t.string :first_name, null: false
t.string :zip_code, null: false, limit: 8
t.string :zip_code, null: false
t.references :state
t.string :state_name, null: false
t.string :city, null: false
t.string :detail
t.string :phone_number, null: false, limit: 18
t.string :phone_number, null: false
t.datetime :last_used_at
t.timestamps null: false
end
Expand Down
19 changes: 12 additions & 7 deletions core/lib/generators/comable/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Comable
class InstallGenerator < Rails::Generators::Base
class_option :migrate, type: :boolean, default: true, banner: 'Run Comable migrations'
class_option :seed, type: :boolean, default: true, banner: 'Load seed data (migrations must be run)'
class_option :sample, type: :boolean, default: true, banner: 'Load sample data (migrations must be run)'
class_option :admin_email, type: :string
class_option :admin_password, type: :string

Expand All @@ -20,6 +21,7 @@ def source_paths
def prepare_options
@migrate_flag = options[:migrate]
@seed_flag = @migrate_flag ? options[:seed] : false
@sample_flag = @migrate_flag ? options[:sample] : false
end

def add_files
Expand Down Expand Up @@ -73,6 +75,15 @@ def load_seed_data
end
end

def load_sample_data
if @sample_flag
say_status :loading, 'sample data'
quietly { rake 'comable:sample' }
else
say_status :skipping, 'sample data (you can always run rake comable:sample)'
end
end

def insert_routes
insert_into_file File.join('config', 'routes.rb'), after: "Rails.application.routes.draw do\n" do
<<-ROUTES
Expand All @@ -95,13 +106,7 @@ def complete
private

def rake_seed
cmd = -> { rake("db:seed #{rake_seed_arguments.join(' ')}") }

if options[:admin_email] && options[:admin_password]
quietly { cmd.call }
else
cmd.call
end
rake("db:seed #{rake_seed_arguments.join(' ')}")
end

def rake_seed_arguments
Expand Down
11 changes: 11 additions & 0 deletions core/spec/lib/generators/comable/install/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
end

after(:all) do
truncate_tables
remove_link_to_dummy_directory
end

Expand Down Expand Up @@ -50,6 +51,10 @@
expect(Comable::User.first.role).to eq('admin')
end

it 'load samples' do
expect(Comable::Product.count).to be >= 1
end

it 'creates routes' do
assert_file 'config/routes.rb', /mount Comable::Core::Engine/
end
Expand All @@ -62,6 +67,12 @@ def drop_tables
end
end

def truncate_tables
ActiveRecord::Base.connection.tables.each do |table_name|
ActiveRecord::Base.connection.execute("TRUNCATE #{table_name}")
end
end

def prepare_directories
FileUtils.mkdir_p("#{destination_root}/config")
FileUtils.mkdir_p("#{destination_root}/db")
Expand Down
4 changes: 2 additions & 2 deletions core/spec/models/comable/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
it { is_expected.to validate_presence_of(:city) }
it { is_expected.to validate_length_of(:family_name).is_at_most(255) }
it { is_expected.to validate_length_of(:first_name).is_at_most(255) }
it { is_expected.to validate_length_of(:zip_code).is_at_most(8) }
it { is_expected.to validate_length_of(:zip_code).is_at_most(255) }
it { is_expected.to validate_length_of(:state_name).is_at_most(255) }
it { is_expected.to validate_length_of(:city).is_at_most(255) }
it { is_expected.to validate_length_of(:detail).is_at_most(255) }
it { is_expected.to validate_length_of(:phone_number).is_at_most(18) }
it { is_expected.to validate_length_of(:phone_number).is_at_most(255) }
end
1 change: 1 addition & 0 deletions lib/comable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
require 'comable_core'
require 'comable_frontend'
require 'comable_backend'
require 'comable_sample'
2 changes: 2 additions & 0 deletions sample/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require ../spec/rails_helper
3 changes: 3 additions & 0 deletions sample/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Comable 0.4.2 (unreleased) ##

* Initialize this gem.
3 changes: 3 additions & 0 deletions sample/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eval File.read(File.expand_path('../../Gemfile.common', __FILE__))

gemspec
1 change: 1 addition & 0 deletions sample/MIT-LICENSE
1 change: 1 addition & 0 deletions sample/Rakefile
19 changes: 19 additions & 0 deletions sample/comable_sample.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version = File.read(File.expand_path('../../COMABLE_VERSION', __FILE__)).strip

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = 'comable_sample'
s.version = version
s.authors = ['YOSHIDA Hiroki']
s.email = ['hyoshida@appirits.com']
s.homepage = 'https://github.com/appirits/comable#comable'
s.summary = 'Provide sample data for Comable.'
s.description = 'Provide sample data for Comable.'
s.license = 'MIT'

s.files = Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile']

s.add_dependency 'comable_core', version

s.add_dependency 'ffaker', '~> 2.0.0'
end
21 changes: 21 additions & 0 deletions sample/db/samples/addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Billing address
Comable::Address.create!(
family_name: Comable::Sample::Name.family_name,
first_name: Comable::Sample::Name.first_name,
zip_code: Comable::Sample::Address.zip_code,
state_name: Comable::Sample::Address.state_name,
city: Comable::Sample::Address.city,
detail: Comable::Sample::Address.detail,
phone_number: Comable::Sample::PhoneNumber.phone_number
)

# Shipping address
Comable::Address.create!(
family_name: Comable::Sample::Name.family_name,
first_name: Comable::Sample::Name.first_name,
zip_code: Comable::Sample::Address.zip_code,
state_name: Comable::Sample::Address.state_name,
city: Comable::Sample::Address.city,
detail: Comable::Sample::Address.detail,
phone_number: Comable::Sample::PhoneNumber.phone_number
)
1 change: 1 addition & 0 deletions sample/db/samples/categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Comable::Category.create!(name: Comable::Sample.t(:clothing))
46 changes: 46 additions & 0 deletions sample/db/samples/images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Comable::Sample.import('products')

suede_dress = Comable::Product.where(name: Comable::Sample.t(:suede_dress)).first!
girly_coat = Comable::Product.where(name: Comable::Sample.t(:girly_coat)).first!
fur_gloves = Comable::Product.where(name: Comable::Sample.t(:fur_gloves)).first!
leather_boots = Comable::Product.where(name: Comable::Sample.t(:leather_boots)).first!

def image(name, suffix: '.jpg')
image_path = File.join(File.dirname(__FILE__), 'images', name + suffix)
File.open(image_path)
end

images_attributes = [
{
product: suede_dress,
file: image('suede_dress')
},
{
product: suede_dress,
file: image('suede_dress_full')
},
{
product: girly_coat,
file: image('girly_coat')
},
{
product: fur_gloves,
file: image('fur_gloves')
},
{
product: leather_boots,
file: image('leather_boots')
},
{
product: leather_boots,
file: image('leather_boots_front')
},
{
product: leather_boots,
file: image('leather_boots_back')
}
]

images_attributes.each do |attributes|
Comable::Image.create!(attributes)
end
Binary file added sample/db/samples/images/fur_gloves.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/db/samples/images/girly_coat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/db/samples/images/leather_boots.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/db/samples/images/leather_boots_back.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/db/samples/images/leather_boots_front.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/db/samples/images/suede_dress.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/db/samples/images/suede_dress_full.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions sample/db/samples/orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Comable::Sample.import('products')
Comable::Sample.import('stocks')
Comable::Sample.import('addresses')

bill_address = Comable::Address.first
ship_address = Comable::Address.last
suede_dress = Comable::Product.where(name: Comable::Sample.t(:suede_dress)).first!
girly_coat = Comable::Product.where(name: Comable::Sample.t(:girly_coat)).first!

orders_attributes = [
{
email: 'comable@example.com',
bill_address: bill_address,
ship_address: ship_address,
shipment_fee: 300,
payment_fee: 200,
total_price: 500 + suede_dress.price,
order_items_attributes: [
{
stock: suede_dress.stocks.first,
quantity: 1
}
]
},
{
email: 'comable@example.com',
bill_address: bill_address,
ship_address: ship_address,
shipment_fee: 300,
payment_fee: 200,
total_price: 500 + (suede_dress.price * 2) + (girly_coat.price * 3),
order_items_attributes: [
{
stock: suede_dress.stocks.first,
quantity: 2
},
{
stock: girly_coat.stocks.first,
quantity: 3
}
]
}
]

orders_attributes.each do |attributes|
order = Comable::Order.create!(attributes)
order.send(:generate_code)
order.state = 'completed'
order.completed_at = Time.now
order.save!
end
6 changes: 6 additions & 0 deletions sample/db/samples/payment_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Comable::PaymentMethod.create!(
name: Comable::Sample.t(:credit_card),
fee: 200,
payment_provider_type: Comable::PaymentProvider::General.name,
payment_provider_kind: 1
)
11 changes: 11 additions & 0 deletions sample/db/samples/payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Comable::Sample.import('orders')
Comable::Sample.import('payment_methods')

payment_method = Comable::PaymentMethod.first

Comable::Order.complete.each do |order|
payment = order.create_payment!(payment_method: payment_method, fee: payment_method.fee)
payment.state = 'completed'
payment.completed_at = Time.now
payment.save!
end
Loading

0 comments on commit a71b43c

Please sign in to comment.