Class: RubyUiScaffold::ValueGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ui_scaffold/value_generator.rb

Overview

Produces a single attribute value for a given ActiveRecord column, using an inference chain: belongs_to FK → enum → inclusion validator →name-based heuristic → type-based fallback.

Faker is a runtime dependency of this gem, so realistic fake data (names, emails, addresses, etc.) is always available.

Examples:

Build a full attribute hash for a model

attrs = RubyUiScaffold::ValueGenerator.attributes_for(User)
User.new(attrs)

Constant Summary collapse

SKIPPED_COLUMN_SUFFIXES =
%w[_count].freeze
SKIPPED_COLUMN_NAMES =
%w[id created_at updated_at].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, model_class) ⇒ ValueGenerator

Returns a new instance of ValueGenerator.



52
53
54
55
# File 'lib/ruby_ui_scaffold/value_generator.rb', line 52

def initialize(column, model_class)
  @column = column
  @model_class = model_class
end

Class Method Details

.attributes_for(model_class) ⇒ Hash{String => Object}

Returns attributes safe to assign via Model.new.

Parameters:

  • model_class (Class)

    an ActiveRecord::Base subclass

Returns:

  • (Hash{String => Object})

    attributes safe to assign via Model.new



25
26
27
28
29
# File 'lib/ruby_ui_scaffold/value_generator.rb', line 25

def attributes_for(model_class)
  seedable_columns(model_class).each_with_object({}) do |column, hash|
    hash[column.name] = new(column, model_class).call
  end
end

.seedable_columns(model_class) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_ui_scaffold/value_generator.rb', line 31

def seedable_columns(model_class)
  inheritance_column = model_class.inheritance_column
  polymorphic_types = polymorphic_type_columns(model_class)

  model_class.columns.reject do |c|
    SKIPPED_COLUMN_NAMES.include?(c.name) ||
      SKIPPED_COLUMN_SUFFIXES.any? { |s| c.name.end_with?(s) } ||
      c.name == inheritance_column ||
      polymorphic_types.include?(c.name)
  end
end

Instance Method Details

#callObject



57
58
59
60
61
62
63
64
# File 'lib/ruby_ui_scaffold/value_generator.rb', line 57

def call
  belongs_to_value ||
    enum_value ||
    inclusion_value ||
    numericality_value ||
    name_based_value ||
    type_based_value
end