Module: CommandServiceObject::ModelHelper

Included in:
Service::Generators::CommandGenerator
Defined in:
lib/command_service_object/helpers/model_helper.rb

Instance Method Summary collapse

Instance Method Details

#allowed_column_typesObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/command_service_object/helpers/model_helper.rb', line 41

def allowed_column_types
  {
    string: 'String',
    bigint: 'Integer',
    integer: 'Integer',
    decimal: 'Float',
    boolean: 'Boolean',
    datetime: 'DateTime'
  }
end

#ignored_column_namesObject



33
34
35
36
37
38
39
# File 'lib/command_service_object/helpers/model_helper.rb', line 33

def ignored_column_names
  %w[
    created_at
    updated_at
    encrypted_password
  ]
end

#model_attributesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/command_service_object/helpers/model_helper.rb', line 15

def model_attributes
  default_attr = { REPLACE_ME: String }
  return default_attr if model_class.nil? || model_class.try(:columns_hash).nil?

  attrs = {}

  model_class.columns_hash.each do |k, v|
    next if ignored_column_names.include?(k)

    type = allowed_column_types[v.type]
    next if type.nil?

    attrs[k] = type
  end

  attrs
end

#model_classObject



9
10
11
12
13
# File 'lib/command_service_object/helpers/model_helper.rb', line 9

def model_class
  Object.const_get(model_name)
rescue StandardError
  nil
end

#model_nameObject



5
6
7
# File 'lib/command_service_object/helpers/model_helper.rb', line 5

def model_name
  name.camelize
end