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
-
#connection_options(options = nil) ⇒ Hash, ...
Sets or gets the database connection options.
-
#create_models! ⇒ Database
Creates all models and applies extensions This method: 1.
-
#database ⇒ Database?
Gets the database instance.
-
#dynamic_active_model_config ⇒ Hash
Gets the current configuration.
-
#extensions_path(path = nil) ⇒ String?
Sets or gets the path for model extensions.
-
#extensions_suffix(suffix = nil) ⇒ String
Sets or gets the suffix for extension files.
-
#foreign_key(table_name, foreign_key, relationship_name) ⇒ Object
Adds a custom foreign key relationship.
-
#foreign_key_constraints(enabled = nil) ⇒ Boolean
Sets or gets whether columns are also related through the database's foreign key constraints (see ForeignKeyConstraints).
-
#has_many_through(enabled = nil) ⇒ Boolean
Sets or gets whether has_many :through associations are added across join models (see HasManyThrough).
-
#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.
-
#parent_class(klass = nil) ⇒ Class?
Sets or gets the superclass for the generated base class.
-
#relationships(all_relationships = nil) ⇒ Hash
Sets or gets the custom relationships.
-
#skip_table(table) ⇒ Object
Adds a single table to the skip list.
-
#skip_tables(tables = nil) ⇒ Array<String>
Sets or gets the list of tables to skip.
-
#table_class_name(table_name, class_name) ⇒ Object
Sets a custom class name for a table.
-
#table_class_names ⇒ Hash
Gets the custom class names by table name.
Instance Method Details
#connection_options(options = nil) ⇒ Hash, ...
Sets or gets the database connection options
73 74 75 76 77 78 79 |
# File 'lib/dynamic-active-model/setup.rb', line 73 def ( = nil) reject_database_yml_name!() if .is_a?(String) update_config(:connection_options, ) if dynamic_active_model_config[:connection_options] end |
#create_models! ⇒ Database
Creates all models and applies extensions This method:
- Creates models using Explorer
- Applies any model extensions if configured
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, , 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
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
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
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
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
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)
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)
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
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.
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
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
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
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
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
157 158 159 |
# File 'lib/dynamic-active-model/setup.rb', line 157 def table_class_names dynamic_active_model_config[:table_class_names] end |