Module: RailsCom::ModelHelper

Defined in:
lib/rails_com/active_record/model_helper.rb

Instance Method Summary collapse

Instance Method Details

#column_attributesObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_com/active_record/model_helper.rb', line 16

def column_attributes
  columns.map do |column|
    {
      name: column.name.to_sym,
      name_i18n: human_attribute_name(column.name),
      type: column.type,
      sql_type: column.sql_type,
      null: column.null,
      default: column.default,
      default_function: column.default_function,
      comment: column.comment
    }
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_com/active_record/model_helper.rb', line 31

def print_table(with_column: false)
  columns.each do |column|
    info = with_column ? '#  t.column' : '# '
    info << " :#{column.name.to_sym}, :#{column.type}"
    info << ", precision: #{column.precision}" if column.precision
    info << ", scale: #{column.scale}" if column.scale
    info << ", limit: #{column.limit}" if column.limit
    if column.default && column.type == :string
      info << ", default: '#{column.default}'"
    elsif column.default
      info << ", default: #{column.default}"
    end
    info << ", null: false" unless column.null
    info << ", comment: '#{column.comment} type: #{column.sql_type}'" if column.comment
    puts info
  end

  nil
end

#sql_table(except: [], only: [], pure: true) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rails_com/active_record/model_helper.rb', line 51

def sql_table(except: [], only: [], pure: true)
  if only.size > 0
    _columns = columns.select { |column| only.include?(column.name) }
  else
    _columns = columns.reject { |column| except.include?(column.name) }
  end

  if pure
    sql = ""
  else
    sql = "CREATE TABLE `#{self.table_name}` (\n"
  end

  _columns.each do |column|
    sql << "  `#{column.name}` #{column.sql_type}"
    sql << " COLLATE #{column.collation}" if column.collation
    sql << " NOT NULL" unless column.null
    if column.default
      sql << " DEFAULT '#{column.default}',\n"
    elsif column.default.nil? && column.null
      sql << " DEFAULT NULL,\n"
    else
      sql << ",\n"
    end
  end

  sql << "  PRIMARY KEY (`#{self.primary_key}`)"

  _indexes = connection.indexes(self.table_name).reject { |index| (index.columns & _columns.map { |col| col.name }).blank? }
  if _indexes.present?
    sql << ",\n"
  else
    sql << "\n"
  end
  _indexes.each_with_index do |obj, index|
    sql << "  KEY `#{obj.name}` ("
    sql << obj.columns.map { |col| "`#{col}`" }.join(',')

    if index + 1 == _indexes.size
      sql << ")\n"
    else
      sql << "),\n"
    end
  end

  if pure
    sql
  else
    sql << ")"
  end
end

#to_factory_botObject



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rails_com/active_record/model_helper.rb', line 3

def to_factory_bot
  require 'rails/generators'
  require 'generators/factory_bot/model/model_generator'

  args = [
    self.name.underscore
  ]
  cols = columns.map { |col| "#{col.name}:#{col.type}" }

  generator = FactoryBot::Generators::ModelGenerator.new(args + cols, destination_root: Rails.root)
  generator.invoke_all
end