Module: Rails::Code::Generator::ModelAttributes

Included in:
RailsCodeGenerator::ResourcesGenerator
Defined in:
lib/rails/code/generator/model_attributes.rb

Constant Summary collapse

EXCLUDED_COLUMNS =
%w[id created_at updated_at].freeze
AUTH_USER_ASSOCIATION =
"user"
DRY_TYPE_MAP =
{
  "string" => :string,
  "text" => :string,
  "citext" => :string,
  "integer" => :integer,
  "bigint" => :integer,
  "float" => :float,
  "decimal" => :decimal,
  "boolean" => :bool,
  "date" => :date,
  "datetime" => :time,
  "timestamp" => :time,
  "timestamptz" => :time,
  "json" => :hash,
  "jsonb" => :hash,
  "uuid" => :string,
}.freeze
FAKER_EXPRESSION_MAP =
{
  "string" => "Faker::Lorem.word",
  "text" => "Faker::Lorem.paragraph",
  "citext" => "Faker::Lorem.word",
  "integer" => "Faker::Number.number(digits: 5)",
  "bigint" => "Faker::Number.number(digits: 5)",
  "float" => "Faker::Number.decimal(l_digits: 2).to_f",
  "decimal" => "Faker::Number.decimal(l_digits: 2)",
  "boolean" => "Faker::Boolean.boolean",
  "date" => "Faker::Date.forward(days: 30)",
  "datetime" => "Faker::Time.forward(days: 30)",
  "timestamp" => "Faker::Time.forward(days: 30)",
  "timestamptz" => "Faker::Time.forward(days: 30)",
  "json" => "{}",
  "jsonb" => "{}",
  "uuid" => "Faker::Internet.uuid",
}.freeze

Instance Method Summary collapse

Instance Method Details

#factory_definition_bodyObject



83
84
85
86
87
88
# File 'lib/rails/code/generator/model_attributes.rb', line 83

def factory_definition_body
  lines = factory_association_lines + factory_attribute_lines
  return "    # TODO: add attributes" if lines.empty?

  lines.join("\n")
end

#model_attributesObject



46
47
48
# File 'lib/rails/code/generator/model_attributes.rb', line 46

def model_attributes
  @model_attributes ||= fetch_model_attributes
end

#normalizer_attributes_list(include_id: false) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/rails/code/generator/model_attributes.rb', line 56

def normalizer_attributes_list(include_id: false)
  names = model_attribute_names
  names = [:id, *names] if include_id
  return "    # TODO: add attributes" if names.empty?

  format_symbol_list(names, indent: 4)
end

#permitted_attributes_list(include_id: false) ⇒ Object



50
51
52
53
54
# File 'lib/rails/code/generator/model_attributes.rb', line 50

def permitted_attributes_list(include_id: false)
  names = model_attribute_names
  names = [:id, *names] if include_id
  format_symbol_list(names)
end

#request_params_attributes_list(indent: 10) ⇒ Object



70
71
72
73
74
75
# File 'lib/rails/code/generator/model_attributes.rb', line 70

def request_params_attributes_list(indent: 10)
  padding = " " * indent
  return "#{padding}# TODO: add attributes" if model_attributes.empty?

  model_attributes.map { |attribute| "#{padding}#{attribute[:name]}: #{param_expression_for(attribute)}," }.join("\n")
end

#request_spec_association_givensObject



77
78
79
80
81
# File 'lib/rails/code/generator/model_attributes.rb', line 77

def request_spec_association_givens
  associations_for_givens.map { |association|
    "  Given(:#{association[:name]}) { create :#{association[:factory]} }"
  }.join("\n")
end

#validator_attributes_listObject



64
65
66
67
68
# File 'lib/rails/code/generator/model_attributes.rb', line 64

def validator_attributes_list
  return "        # TODO: add attributes" if model_attributes.empty?

  model_attributes.map { |attribute| validator_line_for(attribute) }.join("\n")
end