Module: ROM::SQL::Plugin::Associates::ClassMethods
- Defined in:
- lib/rom/sql/plugin/associates.rb
Instance Method Summary collapse
-
#associates(name, options = EMPTY_HASH) ⇒ Object
Set command to associate tuples with a parent tuple using provided keys.
- #build(relation, **options) ⇒ Object
- #create_class(relation:, rel_meta: {}, parent_relation: nil, &block) ⇒ Object private
-
#setup_associates(klass, relation, parent_relation) ⇒ Object
private
Sets up
associates
plugin for a given command class and relation.
Instance Method Details
#associates(name, options = EMPTY_HASH) ⇒ Object
Set command to associate tuples with a parent tuple using provided keys
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rom/sql/plugin/associates.rb', line 126 def associates(name, = EMPTY_HASH) if associations.key?(name) raise ArgumentError, "#{name} association is already defined for #{self.class}" end associations[name] = self end |
#build(relation, **options) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rom/sql/plugin/associates.rb', line 85 def build(relation, **) command = super configured_assocs = command.configured_associations = command.associations.map { |(name, opts)| next if configured_assocs.include?(name) AssociateOptions.new(name, relation, opts) }.compact before_hooks = .reject(&:after?).map(&:to_hash) after_hooks = .select(&:after?).map(&:to_hash) command .with(configured_associations: configured_assocs + .map(&:name)) .before(*before_hooks) .after(*after_hooks) end |
#create_class(relation:, rel_meta: {}, parent_relation: nil, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
50 51 52 53 54 55 56 57 58 |
# File 'lib/rom/sql/plugin/associates.rb', line 50 def create_class(relation:, rel_meta: {}, parent_relation: nil, **, &block) klass = super if relation && [:combine_type] setup_associates(klass, relation, parent_relation) end klass end |
#setup_associates(klass, relation, parent_relation) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets up associates
plugin for a given command class and relation
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rom/sql/plugin/associates.rb', line 66 def setup_associates(klass, relation, parent_relation) assoc_name = if relation.associations.key?(parent_relation) parent_relation else singular_name = relation.inflector.singularize(parent_relation).to_sym singular_name if relation.associations.key?(singular_name) end if assoc_name klass.associates(assoc_name) else klass.associates(parent_relation) end end |