2015-04-08 14:12:19 +00:00
|
|
|
module AutoWizard
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
check if auto wizard is enabled
|
|
|
|
|
|
|
|
AutoWizard.enabled?
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true | false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.enabled?
|
|
|
|
auto_wizard_file_location = file_location
|
|
|
|
return false if !File.file?(auto_wizard_file_location)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get auto wizard data
|
|
|
|
|
|
|
|
AutoWizard.data
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
content of auto wizard file as object
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
auto_wizard_file_location = file_location
|
|
|
|
fail "So such file #{auto_wizard_file_location}" if !File.file?(auto_wizard_file_location)
|
|
|
|
JSON.parse( File.read(auto_wizard_file_location) )
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-04-08 14:12:19 +00:00
|
|
|
creates or updates Users, EmailAddresses and sets Settings based on the 'auto_wizard.json' file placed in the root directory.
|
|
|
|
|
|
|
|
there is an example file 'contrib/auto_wizard_example.json'
|
|
|
|
|
|
|
|
AutoWizard.setup
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
the first created User if a 'auto_wizard.json' file was found and processed, containing at least one entry in Users
|
|
|
|
|
|
|
|
the User with id 1 (NULL) if a 'auto_wizard.json' file was found and processed, containing no Users
|
|
|
|
|
|
|
|
nil if no 'auto_wizard.json' file was found
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.setup
|
2015-05-24 21:59:11 +00:00
|
|
|
auto_wizard_file_location = file_location
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
auto_wizard_hash = data
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2015-05-24 21:59:11 +00:00
|
|
|
admin_user = User.find(1)
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2015-05-22 23:30:21 +00:00
|
|
|
# set Settings
|
|
|
|
if auto_wizard_hash['Settings']
|
|
|
|
auto_wizard_hash['Settings'].each { |setting_data|
|
|
|
|
Setting.set( setting_data['name'], setting_data['value'] )
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# create Organizations
|
|
|
|
if auto_wizard_hash['Organizations']
|
|
|
|
auto_wizard_hash['Organizations'].each { |organization_data|
|
|
|
|
|
2015-05-22 23:54:33 +00:00
|
|
|
organization_data_symbolized = organization_data.symbolize_keys.merge(
|
2015-05-22 23:30:21 +00:00
|
|
|
{
|
|
|
|
updated_by_id: admin_user.id,
|
|
|
|
created_by_id: admin_user.id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-23 00:41:29 +00:00
|
|
|
Organization.create_or_update(
|
2015-05-22 23:30:21 +00:00
|
|
|
organization_data_symbolized
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-04-08 14:12:19 +00:00
|
|
|
# create Users
|
|
|
|
if auto_wizard_hash['Users']
|
|
|
|
|
2015-05-07 07:23:16 +00:00
|
|
|
roles = Role.where( name: %w(Agent Admin) )
|
2015-04-08 14:12:19 +00:00
|
|
|
groups = Group.all
|
|
|
|
|
|
|
|
auto_wizard_hash['Users'].each { |user_data|
|
|
|
|
|
2015-05-22 23:54:33 +00:00
|
|
|
# lookup organization
|
2015-05-22 23:57:27 +00:00
|
|
|
if user_data['organization'] && !user_data['organization'].empty?
|
2015-05-22 23:54:33 +00:00
|
|
|
organization = Organization.find_by(name: user_data['organization'])
|
|
|
|
user_data.delete('organization')
|
|
|
|
if organization
|
|
|
|
user_data['organization_id'] = organization.id
|
|
|
|
end
|
|
|
|
end
|
2015-04-08 14:12:19 +00:00
|
|
|
|
2015-05-22 23:54:33 +00:00
|
|
|
user_data_symbolized = user_data.symbolize_keys.merge(
|
2015-04-08 14:12:19 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
|
|
|
updated_by_id: admin_user.id,
|
|
|
|
created_by_id: admin_user.id
|
2015-04-08 14:12:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
created_user = User.create_or_update(
|
|
|
|
user_data_symbolized
|
|
|
|
)
|
|
|
|
|
|
|
|
# use first created user as admin
|
|
|
|
next if admin_user.id != 1
|
|
|
|
|
|
|
|
admin_user = created_user
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-05-23 01:53:43 +00:00
|
|
|
# create EmailAddresses/Channels/Signatures
|
|
|
|
model_map = {
|
|
|
|
'EmailAddresses' => 'EmailAddress',
|
|
|
|
'Channels' => 'Channel',
|
|
|
|
'Signatures' => 'Signature',
|
|
|
|
}
|
|
|
|
model_map.each {|map_name, model|
|
|
|
|
next if !auto_wizard_hash[map_name]
|
|
|
|
auto_wizard_hash[map_name].each {|data|
|
|
|
|
data_symbolized = data.symbolize_keys.merge(
|
2015-04-08 14:12:19 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
updated_by_id: admin_user.id,
|
|
|
|
created_by_id: admin_user.id
|
2015-04-08 14:12:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-23 01:53:43 +00:00
|
|
|
if data_symbolized[:id] || data_symbolized[:name]
|
|
|
|
Kernel.const_get(model).create_or_update(
|
|
|
|
data_symbolized
|
|
|
|
)
|
|
|
|
else
|
|
|
|
Kernel.const_get(model).create(
|
|
|
|
data_symbolized
|
|
|
|
)
|
|
|
|
end
|
2015-05-23 00:41:29 +00:00
|
|
|
}
|
2015-05-23 01:53:43 +00:00
|
|
|
}
|
2015-05-23 00:41:29 +00:00
|
|
|
|
|
|
|
# remove auto wizard file
|
|
|
|
FileUtils.rm auto_wizard_file_location
|
|
|
|
|
2015-04-08 14:12:19 +00:00
|
|
|
admin_user
|
|
|
|
end
|
2015-05-24 21:59:11 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.file_location
|
|
|
|
auto_wizard_file_name = 'auto_wizard.json'
|
|
|
|
auto_wizard_file_location = "#{Rails.root}/#{auto_wizard_file_name}"
|
|
|
|
auto_wizard_file_location
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|