Top Level Namespace

Defined Under Namespace

Classes: CLIConstants, CLIOptions, Commander, DocumentationController, GlobalController, String

Constant Summary collapse

SKIP_MODEL =
true?(ARGV[0])
V2_GLOBAL =
true?(ARGV[1])
MODEL_NAME =
ARGV[0]
MODEL_PARAMS =
ARGV
MODEL_SNAKE_CASE =
MODEL_NAME.underscore
MODEL_CAMEL_CASE =
MODEL_NAME.camelize
GLOBAL_CONTROLLER =
V2_GLOBAL ? 'V2::GlobalController' : 'GlobalController'
TIMESTAMP =
DateTime.now.to_s.split('-').join('').split(':').join('').split('T').join('').slice(0, 14)
API_ROUTE =
"/#{MODEL_SNAKE_CASE}"

Instance Method Summary collapse

Instance Method Details

#common_seedfilesObject

Get all seedfiles in db/seedfiles/common



67
68
69
# File 'lib/autogenerators/rails_templates/seeds.rb', line 67

def common_seedfiles
  seeds_in_subfolder 'common'
end

#concat_all_with_default(array) ⇒ Object



60
61
62
63
64
65
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 60

def concat_all_with_default array
  array.each_with_object(['']) do |e, acc|
    acc = acc.concat(e)
    acc
  end
end

#env_seedfilesObject

Get all seedfiles in the current environment’s seedfile directory



72
73
74
# File 'lib/autogenerators/rails_templates/seeds.rb', line 72

def env_seedfiles
  seeds_in_subfolder Rails.env
end

#get_associations(model) ⇒ Object



83
84
85
86
87
88
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 83

def get_associations model 
  model.reflect_on_all_associations.map(&:name)
    .reject do |e|
      model.reflect_on_association(e).is_a?(ActiveRecord::Reflection::ThroughReflection)
    end.map { |e| e.to_s }
end

#include_querystrings(array) ⇒ Object



67
68
69
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 67

def include_querystrings array
  [''].concat(string_permuations(array).map { |e| string_list_to_querystring('include', e) })
end

#like_querystrings(array) ⇒ Object



75
76
77
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 75

def like_querystrings array
  [''].concat(array.map { |e| objects_list_to_querystring('like', e) })
end

#login_userObject



1
2
3
4
5
6
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 1

def 
  # Update your own login authenication endpoint
  post '/login', params: { email: @user[:email], password: @user[:password] }
  auth_header = response.headers['Authorization']
  { Authorization: auth_header }
end

#mainObject

The main method for rails db:seed (called on the last line of this file)



11
12
13
14
# File 'lib/autogenerators/rails_templates/seeds.rb', line 11

def main
  puts "Running db:seed for the #{Rails.env} environment"
  run_seeds
end

#objects_list_to_querystring(qs, object) ⇒ Object



8
9
10
11
12
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 8

def objects_list_to_querystring qs, object
  querystring = "#{qs}="
  v = object.map { |key, val| "#{key.to_s}:#{val.to_s}" }.join(',')
  querystring + v
end

#order_options(array) ⇒ Object



53
54
55
56
57
58
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 53

def order_options array
  array.each_with_object([]) do |e, acc|
    acc.push("#{e} ASC")
    acc.push("#{e} DESC")
  end
end

#order_querystrings(array) ⇒ Object



79
80
81
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 79

def order_querystrings array
  [''].concat(array.map { |e| string_list_to_querystring('like', e) })
end

#pagination_querystring_checks(count, init_limit = 3) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 22

def pagination_querystring_checks count, init_limit = 3
  if count < init_limit
    ["offset=0&limit=1"]
  else
    limit = (count/init_limit.to_f).ceil
    init_limit.times.map { |i| "offset=#{i}&limit=#{limit}" }
  end  
end

#parse_qs_value(string) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 14

def parse_qs_value string
  string.split('&').each_with_object({}) do |e, acc|
    key, val = e.split('=')
    acc[key.to_sym] = val
    acc
  end
end

#resolve_seeder_path(str) ⇒ Object



50
51
52
53
54
# File 'lib/autogenerators/rails_templates/seeds.rb', line 50

def resolve_seeder_path(str)
  common_seedfiles
    .concat(env_seedfiles)
    .find { |path| path.to_s.include?(str.to_s) }
end

#run_seedsObject



56
57
58
59
60
61
62
63
64
# File 'lib/autogenerators/rails_templates/seeds.rb', line 56

def run_seeds
  seeds_to_run.each do |path|
    # check for the file
    raise StandardError, "Seedfile #{s}.rb not found" unless File.exist? path

    system("rails runner #{path}")
    puts "Running #{path}"
  end
end

#seedfiles_dirObject

Directory where all seedfiles live note: this is a Pathname object



5
6
7
# File 'lib/autogenerators/rails_templates/seeds.rb', line 5

def seedfiles_dir
  Rails.root + 'db/seedfiles/'
end

#seeds_in_subfolder(folder) ⇒ Object

Get all files within a given subfolder of seedfiles_dir



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/autogenerators/rails_templates/seeds.rb', line 77

def seeds_in_subfolder(folder)
  dir = seedfiles_dir + folder
  # Environments aren't required to have their own directory; check first that one exists
  if Dir.exist?(dir)
    Dir.entries(dir).select do |fname|
        fname[0] != '.' && fname.match(/\.rb$/)
      end.to_a.sort.map do |fname|
        seedfiles_dir + folder + fname
      end
  else
    []
  end
end

#seeds_to_runObject

Which seedfiles should get processed when running rails db:seed



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/autogenerators/rails_templates/seeds.rb', line 17

def seeds_to_run
  filepaths = []
  # if a filepath was passed as an argument, only run that seeder
  # if ARGV.length > 1
  #   cmd, *seedfiles = ARGV

  # set this by: rails db:seed seeds=comma_separated,file_names
  if ENV['seeds']

    seedfiles = ENV['seeds'].split(',')

    filepaths = seedfiles.map do |str|
      is_relative_path = str.split('/').length > 1
      path = path = is_relative_path ? str : resolve_seeder_path

      raise StandardError, "Seedfile #{path} not found" unless File.exist? path

      path
    end

  else
    # get the seedfiles which are supposed to have been run
    target_seed_files = common_seedfiles + env_seedfiles

    filepaths = (filepaths).concat(target_seed_files).map do |str|
      resolve_seeder_path str
    end

  end

  filepaths
end

#string_list_to_querystring(qs, array) ⇒ Object



31
32
33
34
35
36
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 31

def string_list_to_querystring qs, array
  array = array.is_a?(Array) ? array : [array]
  querystring = "#{qs}="
  v = array.join(',')
  querystring + v
end

#string_permuations(array) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 38

def string_permuations array
  index = 0
  perms = array.each_with_object([]) do |e, acc|
    index += 1
    acc.push(array.slice(0, index))
  end
  order = perms.each_with_object([]) do |e, acc|
    acc.push(e.permutation.to_a.map { |d| d.join(',') })
  end
  order.each_with_object([]) do |e, acc|
    acc = acc.concat(e)
    acc
  end
end

#true?(obj) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/utils/rails_methods.rb', line 18

def true?(obj)
    obj.to_s == "true"
end

#where_querystrings(array) ⇒ Object



71
72
73
# File 'lib/autogenerators/rails_templates/auth_helper.rb', line 71

def where_querystrings array
  [''].concat(array.map { |e| objects_list_to_querystring('where', e) })
end