Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KG - OS Setting Fixes #1014

Merged
merged 6 commits into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/lib/data_type_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def is_email?(value)
end

def is_url?(value)
value.match?(/^((ftp|http|https):\/\/)?[a-zA-Z0-9]+(.[a-zA-Z0-9])?(:[0-9]+)?/)
value.match?(/^((ftp|http|https):\/\/)?[a-zA-Z0-9]+(((\.[a-zA-Z0-9]+)+(:\d+)?)|(:\d+))(\/\w+)*(\/)?(\?(\w+=\w+(&\w+=\w+)*)+)?$/)
end

def is_path?(value)
value.match?(/^(\/\w+)+(\/)?(\?(\w+=[\w\d]+(&\w+=[\w\d]+)+)+)*$/)
value.match?(/^(\/|(\/\w+)+\/?)(\?(\w+=\w+(&\w+=\w+)*)+)?$/)
end
end
6 changes: 4 additions & 2 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def value
read_attribute(:value) == 'true'
when 'json'
JSON.parse(read_attribute(:value))
else
read_attribute(:value)
end
end

Expand All @@ -53,8 +55,8 @@ def value_matches_type
is_url?(value)
when 'path'
is_path?(value)
else
false
else # Default type = string, no validation needed
true
end
end
end
28 changes: 18 additions & 10 deletions lib/tasks/populate_settings_table.rake
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
desc "Temporary task to populate the settings table"
task :populate_settings_table => :environment do

include DataTypeValidator

environment = Rails.env
hash = YAML.load_file('config/application.yml')
ActiveRecord::Base.transaction do
hash[environment].each do |key, value|
type = ""
if (value.class == TrueClass) || (value.class == FalseClass)
type = 'boolean'
elsif (value.class == Array) || (value.class == Hash)
type = 'json'
else
type = 'string'
end
if [TrueClass, FalseClass].include?(value.class)
type = 'boolean'
value = value.to_s
elsif [Array, Hash].include?(value.class)
type = 'json'
elsif is_email?(value)
type = 'email'
elsif is_url?(value)
type = 'url'
elsif is_path?(value)
type = 'path'
else
type = 'string'
end

setting = Setting.new
setting.assign_attributes(key: key, value: value, data_type: type)
setting.assign_attributes(key: key, value: value, data_type: type, friendly_name: key.titleize, description: key.humanize)
setting.save(validate: false)
end
end
end
end