Class: HerokuSchemas::SchemaUtilities

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku-schemas/schema_utilities.rb

Class Method Summary collapse

Class Method Details

.add_schema_search_path_to_url(url, schema) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/heroku-schemas/schema_utilities.rb', line 4

def add_schema_search_path_to_url(url, schema)
  uri = URI(url)
  params = uri_to_params(uri)
  
  # Handle the case of poorly-formed query strings where CGI::parse turns 'pool=20?pool=20'
  # into {'pool' => ["20?pool=20"]}
  params.each do |key, value|
    if value.is_a?(Array) && value.length == 1
      params[key] = value.first.split('?').first
    end
  end
  
  params['schema_search_path'] = schema
  uri.query = params.to_query
  uri.to_s
end

.app_to_schema(app) ⇒ Object



21
22
23
# File 'lib/heroku-schemas/schema_utilities.rb', line 21

def app_to_schema(app)
  app.downcase.gsub(/[^\w_]/, '_')
end

.uri_to_params(uri) ⇒ Object



33
34
35
# File 'lib/heroku-schemas/schema_utilities.rb', line 33

def uri_to_params(uri)
  uri.query ? CGI::parse(uri.query) : {}
end

.url_to_schema(url) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/heroku-schemas/schema_utilities.rb', line 25

def url_to_schema(url)
  uri = URI(url)
  params = uri_to_params(uri)
  schema = params['schema_search_path'] || 'public'
  schema = schema.first if schema.is_a?(Array)
  schema
end

.validate_schema(schema) ⇒ Object



37
38
39
40
41
42
# File 'lib/heroku-schemas/schema_utilities.rb', line 37

def validate_schema(schema)
  if schema.blank? || schema =~ /[^a-z0-9_]/
    return "Schema must only contain lowercase letters, numbers and underscores. Provided value: #{schema}"
  end
  true
end