Class: Associationist::Builder::SingularAssociation

Inherits:
ActiveRecord::Associations::Builder::SingularAssociation
  • Object
show all
Defined in:
lib/associationist/builder/singular_association.rb

Class Method Summary collapse

Class Method Details

.create_reflection(model, name, scope, options, extension = nil) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/associationist/builder/singular_association.rb', line 24

def self.create_reflection(model, name, scope, options, extension = nil)
  raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)

  validate_options(options)
  case
  when ActiveRecord.version >= Gem::Version.new('6.0.0')
    scope = build_scope(scope)
  else
    scope = build_scope(scope, extension)
  end
  Reflection::SingularReflection.new(name, scope, options, model)
end

.define_accessors(model, reflection) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/associationist/builder/singular_association.rb', line 8

def self.define_accessors(model, reflection)
  super
  mixin = model.generated_association_methods
  name = reflection.name

  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def reload_#{name}
      association(:#{name}).force_reload_reader
    end
  CODE
end

.define_callbacks(model, reflection) ⇒ Object



20
21
22
# File 'lib/associationist/builder/singular_association.rb', line 20

def self.define_callbacks(model, reflection)
  # bypass dependent callback and autosave callback
end

.define_writers(mixin, name) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/associationist/builder/singular_association.rb', line 37

def self.define_writers(mixin, name)
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}=(value)
      raise "Virtual associations are read-only."
    end
  CODE
end

.valid_options(options) ⇒ Object



4
5
6
# File 'lib/associationist/builder/singular_association.rb', line 4

def self.valid_options(options)
  super + [:associationist]
end