forked from openware/peatio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task which feeds database with demo members (openware#96)
- Loading branch information
Showing
7 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Generating demo members | ||
|
||
To feed database with demo members (including admin account) execute the following: | ||
|
||
`bundle exec rake peatio:feed` | ||
|
||
Member access level, login and password will be printed on the screen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AbstractFeeder | ||
def feed | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AdminFeeder < MemberFeeder | ||
def feed(email, *args) | ||
super(email, *args) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class MemberFeeder < AbstractFeeder | ||
def feed(email, password) | ||
identity = Identity.find_or_initialize_by(email: email) | ||
identity.update! \ | ||
password: password, | ||
password_confirmation: password, | ||
is_active: true | ||
|
||
member = Member.find_or_initialize_by(email: email) | ||
member.assign_attributes \ | ||
activated: true, | ||
nickname: Faker::Internet.user_name, | ||
phone_number: Faker::PhoneNumber.cell_phone | ||
member.authentications = [Authentication.new(provider: 'identity', uid: identity.id)] | ||
member.save! | ||
|
||
member.id_document.update! \ | ||
name: Faker::Name.name, | ||
address: Faker::Address.street_address, | ||
city: Faker::Address.city, | ||
country: Faker::Address.country, | ||
zipcode: Faker::Address.zip, | ||
id_document_type: :id_card, | ||
id_document_number: Faker::Number.number(15), | ||
aasm_state: :verified | ||
|
||
member | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace :peatio do | ||
task feed: :environment do | ||
members = [] | ||
|
||
def random_email | ||
Faker::Internet.email | ||
end | ||
|
||
def random_password | ||
Faker::Internet.password(8, 64) | ||
end | ||
|
||
MemberFeeder.new.tap do |feeder| | ||
10.times do | ||
email, password = random_email, random_password | ||
feeder.feed(email, password) | ||
members << { email: email, password: password } | ||
end | ||
end | ||
|
||
AdminFeeder.new.tap do |feeder| | ||
ENV.fetch('ADMIN').split(',').each do |email| | ||
password = random_password | ||
feeder.feed(email, password) | ||
members << { email: email, password: password, admin: true } | ||
end | ||
end | ||
|
||
longest_email = members.map { |m| m[:email] }.sort_by(&:length).last | ||
members.each do |member| | ||
Rails.logger << "#{member[:admin] ? 'ADMIN ' : 'MEMBER'} #{member[:email].ljust(longest_email.length)} #{member[:password]}\n" | ||
end | ||
end | ||
end |