Class: Sequel::Plugins::PgArrayAssociations::PgArrayToManyAssociationReflection

Inherits:
Model::Associations::AssociationReflection show all
Defined in:
lib/sequel/plugins/pg_array_associations.rb

Overview

The AssociationReflection subclass for pg_array_to_many associations.

Constant Summary collapse

FINALIZE_SETTINGS =
superclass::FINALIZE_SETTINGS.merge(
  :array_type=>:array_type,
  :primary_key=>:primary_key,
  :primary_key_method=>:primary_key_method
).freeze

Constants inherited from Model::Associations::AssociationReflection

Model::Associations::AssociationReflection::ASSOCIATION_DATASET_PROC

Instance Method Summary collapse

Methods inherited from Model::Associations::AssociationReflection

#_add_method, #_remove_all_method, #_remove_method, #_setter_method, #add_method, #apply_dataset_changes, #apply_distinct_on_eager_limit_strategy, #apply_eager_dataset_changes, #apply_eager_graph_limit_strategy, #apply_eager_limit_strategy, #apply_ruby_eager_limit_strategy, #apply_window_function_eager_limit_strategy, #assign_singular?, #associated_class, #associated_dataset, #association_dataset_for, #association_dataset_proc, #association_method, #cloneable?, #dataset_method, #delete_row_number_column, #eager_graph_lazy_dataset?, #eager_load_results, #eager_loader_key, #eager_loading_use_associated_key?, #filter_by_associations_add_conditions?, #finalize, #initialize_association_cache, #inspect, #limit_and_offset, #need_associated_primary_key?, #placeholder_loader, #predicate_key_values, #predicate_keys, #qualify, #qualify_assoc, #qualify_cur, #reciprocal, #reciprocal_array?, #remove_all_method, #remove_before_destroy?, #remove_method, #remove_should_check_existing?, #returns_array?, #select, #set_reciprocal_to_self?, #setter_method, #slice_range

Methods included from Inflections

clear, irregular, plural, singular, uncountable

Methods inherited from Hash

#&, #case, #hstore, #pg_json, #pg_jsonb, #sql_expr, #sql_negate, #sql_or, #|, #~

Instance Method Details

#array_typeObject



189
190
191
192
193
194
195
196
197
# File 'lib/sequel/plugins/pg_array_associations.rb', line 189

def array_type
  cached_fetch(:array_type) do
    if (sch = self[:model].db_schema) && (s = sch[self[:key]]) && (t = s[:db_type])
      t.sub(/\[\]\z/, '').freeze
    else
      :integer
    end
  end
end

#associated_object_keysObject

An array containing the primary key for the associated model.



200
201
202
# File 'lib/sequel/plugins/pg_array_associations.rb', line 200

def associated_object_keys
  Array(primary_key)
end

#can_have_associated_objects?(obj) ⇒ Boolean

pg_array_to_many associations can only have associated objects if the array field is not nil or empty.

Returns:

  • (Boolean)


206
207
208
209
# File 'lib/sequel/plugins/pg_array_associations.rb', line 206

def can_have_associated_objects?(obj)
  v = obj.get_column_value(self[:key])
  v && !v.empty?
end

#dataset_need_primary_key?Boolean

pg_array_to_many associations do not need a primary key.

Returns:

  • (Boolean)


212
213
214
# File 'lib/sequel/plugins/pg_array_associations.rb', line 212

def dataset_need_primary_key?
  false
end

#default_keyObject

Use a default key name of *_ids, for similarity to other association types that use *_id for single keys.



218
219
220
# File 'lib/sequel/plugins/pg_array_associations.rb', line 218

def default_key
  :"#{singularize(self[:name])}_ids"
end

#eager_graph_limit_strategy(_) ⇒ Object

Always use the ruby eager_graph limit strategy if association is limited.



223
224
225
# File 'lib/sequel/plugins/pg_array_associations.rb', line 223

def eager_graph_limit_strategy(_)
  :ruby if self[:limit]
end

#eager_limit_strategyObject

Always use the ruby eager limit strategy



228
229
230
231
232
# File 'lib/sequel/plugins/pg_array_associations.rb', line 228

def eager_limit_strategy
  cached_fetch(:_eager_limit_strategy) do
    :ruby if self[:limit]
  end
end

#filter_by_associations_conditions_expression(obj) ⇒ Object



269
270
271
272
# File 'lib/sequel/plugins/pg_array_associations.rb', line 269

def filter_by_associations_conditions_expression(obj)
  ds = filter_by_associations_conditions_dataset.where(filter_by_associations_conditions_subquery_conditions(obj))
  Sequel.function(:coalesce, Sequel.pg_array(filter_by_associations_conditions_key).overlaps(ds), Sequel::SQL::Constants::FALSE)
end

#filter_by_associations_limit_strategyObject

Don’t use a filter by associations limit strategy



235
236
237
# File 'lib/sequel/plugins/pg_array_associations.rb', line 235

def filter_by_associations_limit_strategy
  nil
end

#finalize_settingsObject



244
245
246
# File 'lib/sequel/plugins/pg_array_associations.rb', line 244

def finalize_settings
  FINALIZE_SETTINGS
end

#handle_silent_modification_failure?Boolean

Handle silent failure of add/remove methods if raise_on_save_failure is false and save_after_modify is true.

Returns:

  • (Boolean)


250
251
252
# File 'lib/sequel/plugins/pg_array_associations.rb', line 250

def handle_silent_modification_failure?
  self[:raise_on_save_failure] == false && self[:save_after_modify]
end

#predicate_keyObject

A qualified version of the associated primary key.



255
256
257
# File 'lib/sequel/plugins/pg_array_associations.rb', line 255

def predicate_key
  cached_fetch(:predicate_key){qualify_assoc(primary_key)}
end

#primary_keyObject

The primary key of the associated model.



260
261
262
# File 'lib/sequel/plugins/pg_array_associations.rb', line 260

def primary_key
  cached_fetch(:primary_key){associated_class.primary_key || raise(Error, "no primary key specified for #{associated_class.inspect}")}
end

#primary_key_methodObject

The method to call to get value of the primary key of the associated model.



265
266
267
# File 'lib/sequel/plugins/pg_array_associations.rb', line 265

def primary_key_method
  cached_fetch(:primary_key_method){primary_key}
end