Skip to content

Instantly share code, notes, and snippets.

@fajrif
Created November 12, 2015 12:43
Show Gist options
  • Save fajrif/83a91a0eafb6ba8f01e0 to your computer and use it in GitHub Desktop.
Save fajrif/83a91a0eafb6ba8f01e0 to your computer and use it in GitHub Desktop.
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
Spree::Core::Engine.load_seed if defined?(Spree::Core)
Spree::Auth::Engine.load_seed if defined?(Spree::Auth)
# initiate taxonomy and taxons
Spree::Taxonomy.delete_all
Spree::Taxon.delete_all
taxonomies = [
{ name: "Categories" }
]
taxonomies.each do |taxonomy_attrs|
Spree::Taxonomy.create!(taxonomy_attrs)
end
categories = Spree::Taxonomy.find_by_name!("Categories")
taxons = [
{
:name => "Categories",
:taxonomy => categories,
:position => 0
},
{
:name => "batik",
:taxonomy => categories,
:parent => "Categories",
:position => 1
},
{
:name => "puppets_and_paintings",
:taxonomy => categories,
:parent => "Categories",
:position => 2
},
{
:name => "woodcrafts",
:taxonomy => categories,
:parent => "Categories",
:position => 3
},
{
:name => "leathers",
:taxonomy => categories,
:parent => "Categories",
:position => 4
},
{
:name => "pottery",
:taxonomy => categories,
:parent => "Categories",
:position => 5
},
{
:name => "stones_and_metals",
:taxonomy => categories,
:parent => "Categories",
:position => 6
},
{
:name => "weaving_crafts",
:taxonomy => categories,
:parent => "Categories",
:position => 7
},
]
taxons.each do |taxon_attrs|
if taxon_attrs[:parent]
taxon_attrs[:parent] = Spree::Taxon.where(name: taxon_attrs[:parent]).first
Spree::Taxon.create!(taxon_attrs)
end
end
Spree::Role.create(name: "operator")
Spree::Role.create(name: "marketing")
Spree::Role.create(name: "merchant")
def create_spree_user(options = {
:email => 'user@example.com',
:password => 'spree123',
:message => "Create user sample (press enter for defaults)."
})
user = nil
puts options[:message]
email = options[:email]
password = options[:password]
email = ask("Email [#{ email }]: ") do |q|
q.echo = true
q.whitespace = :strip
end
email = options[:email] if email.blank?
password = ask("Password [#{ password }]: ") do |q|
q.echo = false
q.validate = /^(|.{6,40})$/
q.responses[:not_valid] = 'Invalid password. Must be at least 6 characters long.'
q.whitespace = :strip
end
password = options[:password] if password.blank?
attributes = {
:password => password,
:password_confirmation => password,
:email => email,
:login => email
}
merchant_attributes = {
:name => options[:merchant_name],
:address_attributes => {
:firstname => 'Brandon',
:lastname => 'Rodgers',
:address1 => 'Anfield Rd',
:city => 'Liverpool',
:zipcode => 'L4 0TH',
:country_id => 77,
:state_id => 1067,
:phone => '+44 151 260 6677'
}
}
load 'spree/user.rb'
if Spree::User.find_by_email(email)
say "\nWARNING: There is already a user with the email: #{email}, so no account changes were made. If you wish to create an additional user, please run rake db:seed again with a different email.\n\n"
else
user = Spree::User.new(attributes)
if user.save
role = Spree::Role.find_or_create_by(name: 'user')
user.spree_roles << role
user.save
user.generate_spree_api_key!
if options[:merchant_name]
merchant_name = ask("Brand name [#{ options[:merchant_name] }]: ") do |q|
q.echo = false
q.validate = /^(|.{6,40})$/
q.responses[:not_valid] = 'Invalid brand name. it must be already taken.'
q.whitespace = :strip
end
merchant_name = options[:merchant_name] if merchant_name.blank?
merchant_attributes[:name] = merchant_name
user.build_merchant(merchant_attributes)
user.merchant.email = email
if user.merchant.save
user.update(merchant_id: user.merchant.id)
end
end
else
say "There was some problems with persisting new admin user:"
user.errors.full_messages.each do |error|
say error
end
end
end
user
end
# generate sample user
create_spree_user()
if defined?(Spree::Merchant)
u = create_spree_user(:email => "merchant@example.com", :password => 'spree123', :merchant_name => 'Brandon Jersey', :message => 'Create sample user for a merchant (press enter for defaults).')
u.merchant.toggle_activation!
end
say "Done! create user."
# load spree_sample here...
Rake::Task["spree_sample:load"].invoke
say "Generate default Payment method"
# generate payment method
if defined?(Spree::PaymentMethod::BankTransfer)
bank_transfer = Spree::PaymentMethod::BankTransfer.new(name: "Bank Transfer", description: "Payment using Bank Transfer")
bank_transfer.save
Spree::Bank.create(name: "BANK MANDIRI", account_no: "123456789010", active: true, additional_details: "ATAS NAMA: ROBERT TAMPUBOLON")
say "Done! create payment method bank transfer"
end
if defined?(Spree::Gateway::VeritransGateway)
# delete Bogus default credit card
# replace with Veritrans
cc = Spree::PaymentMethod.find_by_type(Spree::Gateway::Bogus.to_s)
cc.try(:destroy)
veritrans = Spree::Gateway::VeritransGateway.new(name: "Credit Card", description: "Payment using Veritrans VT-Direct Credit Card")
veritrans.preferences[:client_key] = 'VT-client-BO3Lm5WmKlzxg9FI'
veritrans.preferences[:server_key] = 'VT-server-HvU2kqGsVjV08_t79cN8yPiK'
veritrans.preferences[:url_api] = 'https://api.sandbox.veritrans.co.id'
veritrans.save
say "Done! create payment method using veritrans"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment