Module: DynamicActiveModel::Setup::ClassMethods

Defined in:
lib/dynamic-active-model/setup.rb

Overview

ClassMethods provides various class methods for configuring a module

Instance Method Summary collapse

Instance Method Details

#connection_options(options = nil) ⇒ Hash, ...

Sets or gets the database connection options

Parameters:

  • options (Hash, Symbol, String, nil) (defaults to: nil) —

    Anything establish_connection accepts: a config hash, a URL, or a Symbol naming a database.yml entry for the current environment

Returns:

  • (Hash, Symbol, String) —

    The current connection options

Raises:

  • (ArgumentError) —

    For a database.yml name given as a String



73
74
75
76
77
78
79
# File 'lib/dynamic-active-model/setup.rb', line 73

def connection_options(options = nil)
  reject_database_yml_name!(options) if options.is_a?(String)

  update_config(:connection_options, options) if options

  dynamic_active_model_config[:connection_options]
end

#create_models! ⇒ Database

Creates all models and applies extensions This method:

  1. Creates models using Explorer
  2. Applies any model extensions if configured

Returns:

  • (Database) —

    The configured database instance



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/dynamic-active-model/setup.rb', line 182

def create_models!
  redefine_class_method(
    :database,
    DynamicActiveModel::Explorer.explore(
      self,
      connection_options,
      skip_tables,
      relationships,
      table_class_names,
      parent_class: parent_class,
      foreign_key_constraints: foreign_key_constraints,
      has_many_through: has_many_through,
      optional_belongs_to: optional_belongs_to
    )
  )
  database.update_all_models(extensions_path, extensions_suffix) if extensions_path
  database
end

#database ⇒ Database?

Gets the database instance

Returns:

  • (Database, nil) —

    The configured database instance



46
47
48
# File 'lib/dynamic-active-model/setup.rb', line 46

def database
  nil
end

#dynamic_active_model_config ⇒ Hash

Gets the current configuration

Returns:

  • (Hash) —

    The configuration hash with default values



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dynamic-active-model/setup.rb', line 52

def dynamic_active_model_config
  {
    connection_options: nil,
    parent_class: nil,
    foreign_key_constraints: false,
    has_many_through: false,
    optional_belongs_to: false,
    skip_tables: [],
    relationships: {},
    table_class_names: {},
    extensions_path: nil,
    extensions_suffix: '.ext.rb'
  }
end

#extensions_path(path = nil) ⇒ String?

Sets or gets the path for model extensions

Parameters:

  • path (String, nil) (defaults to: nil) —

    Path to extension files

Returns:

  • (String, nil) —

    The current extensions path



164
165
166
167
# File 'lib/dynamic-active-model/setup.rb', line 164

def extensions_path(path = nil)
  update_config(:extensions_path, path) if path
  dynamic_active_model_config[:extensions_path]
end

#extensions_suffix(suffix = nil) ⇒ String

Sets or gets the suffix for extension files

Parameters:

  • suffix (String, nil) (defaults to: nil) —

    File extension suffix

Returns:

  • (String) —

    The current extensions suffix



172
173
174
175
# File 'lib/dynamic-active-model/setup.rb', line 172

def extensions_suffix(suffix = nil)
  update_config(:extensions_suffix, suffix) if suffix
  dynamic_active_model_config[:extensions_suffix]
end

#foreign_key(table_name, foreign_key, relationship_name) ⇒ Object

Adds a custom foreign key relationship

Parameters:

  • table_name (String) —

    Name of the table

  • foreign_key (String) —

    Name of the foreign key column

  • relationship_name (String) —

    Name for the relationship



143
144
145
146
# File 'lib/dynamic-active-model/setup.rb', line 143

def foreign_key(table_name, foreign_key, relationship_name)
  table_relationships = relationships.fetch(table_name, {}).merge(foreign_key => relationship_name)
  update_config(:relationships, relationships.merge(table_name => table_relationships))
end

#foreign_key_constraints(enabled = nil) ⇒ Boolean

Sets or gets whether columns are also related through the database's foreign key constraints (see ForeignKeyConstraints)

Parameters:

  • enabled (Boolean, nil) (defaults to: nil)

Returns:

  • (Boolean) —

    The current setting



94
95
96
97
# File 'lib/dynamic-active-model/setup.rb', line 94

def foreign_key_constraints(enabled = nil)
  update_config(:foreign_key_constraints, enabled) unless enabled.nil?
  dynamic_active_model_config[:foreign_key_constraints]
end

#has_many_through(enabled = nil) ⇒ Boolean

Sets or gets whether has_many :through associations are added across join models (see HasManyThrough)

Parameters:

  • enabled (Boolean, nil) (defaults to: nil)

Returns:

  • (Boolean) —

    The current setting



103
104
105
106
# File 'lib/dynamic-active-model/setup.rb', line 103

def has_many_through(enabled = nil) # rubocop:disable Naming/PredicatePrefix -- DSL setter/getter
  update_config(:has_many_through, enabled) unless enabled.nil?
  dynamic_active_model_config[:has_many_through]
end

#optional_belongs_to(enabled = nil) ⇒ Boolean

Sets or gets whether every belongs_to is optional, including those on NOT NULL foreign keys, so saving a record doesn't validate that its parent exists

Parameters:

  • enabled (Boolean, nil) (defaults to: nil)

Returns:

  • (Boolean) —

    The current setting



112
113
114
115
# File 'lib/dynamic-active-model/setup.rb', line 112

def optional_belongs_to(enabled = nil)
  update_config(:optional_belongs_to, enabled) unless enabled.nil?
  dynamic_active_model_config[:optional_belongs_to]
end

#parent_class(klass = nil) ⇒ Class?

Sets or gets the superclass for the generated base class. Without connection options, models inherit the parent class's connection.

Parameters:

  • klass (Class, nil) (defaults to: nil) —

    Superclass, e.g. ApplicationRecord

Returns:

  • (Class, nil) —

    The current parent class



85
86
87
88
# File 'lib/dynamic-active-model/setup.rb', line 85

def parent_class(klass = nil)
  update_config(:parent_class, klass) if klass
  dynamic_active_model_config[:parent_class]
end

#relationships(all_relationships = nil) ⇒ Hash

Sets or gets the custom relationships

Parameters:

  • all_relationships (Hash, nil) (defaults to: nil) —

    All custom relationships

Returns:

  • (Hash) —

    The current relationships



134
135
136
137
# File 'lib/dynamic-active-model/setup.rb', line 134

def relationships(all_relationships = nil)
  update_config(:relationships, all_relationships) if all_relationships
  dynamic_active_model_config[:relationships]
end

#skip_table(table) ⇒ Object

Adds a single table to the skip list

Parameters:

  • table (String) —

    Table to skip



127
128
129
# File 'lib/dynamic-active-model/setup.rb', line 127

def skip_table(table)
  update_config(:skip_tables, skip_tables + [table])
end

#skip_tables(tables = nil) ⇒ Array<String>

Sets or gets the list of tables to skip

Parameters:

  • tables (Array<String>, nil) (defaults to: nil) —

    Tables to skip

Returns:

  • (Array<String>) —

    The current list of skipped tables



120
121
122
123
# File 'lib/dynamic-active-model/setup.rb', line 120

def skip_tables(tables = nil)
  update_config(:skip_tables, tables) if tables
  dynamic_active_model_config[:skip_tables]
end

#table_class_name(table_name, class_name) ⇒ Object

Sets a custom class name for a table

Parameters:

  • table_name (String) —

    Name of the table

  • class_name (String) —

    Class name to use for the table's model



151
152
153
# File 'lib/dynamic-active-model/setup.rb', line 151

def table_class_name(table_name, class_name)
  update_config(:table_class_names, table_class_names.merge(table_name.to_s => class_name))
end

#table_class_names ⇒ Hash

Gets the custom class names by table name

Returns:

  • (Hash) —

    The current table class names



157
158
159
# File 'lib/dynamic-active-model/setup.rb', line 157

def table_class_names
  dynamic_active_model_config[:table_class_names]
end