Class: Model::HasOne::Association

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/model/has_one.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, assoc, options = {}) ⇒ Association

Returns a new instance of Association.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/concerns/model/has_one.rb', line 8

def initialize(target, assoc, options = {})
  @target = target
  @assoc = assoc.to_sym

  class_name = options[:class_name] || assoc.to_s.camelize
  klass = class_name.constantize

  builder_method_name = "build_#{assoc}".to_sym
  validate_method_name = "validate_associated_#{assoc}".to_sym

  target.send :attr_accessor, assoc
  define_builder builder_method_name, assoc, klass
  define_attributes_writer builder_method_name
  define_present_method assoc
  define_nil_method assoc
  define_validate_method validate_method_name, assoc

  if validate_options = options[:validate]
    target.validate validate_method_name, validate_options.is_a?(Hash) ? validate_options : {}
  end
end

Instance Attribute Details

#assocObject (readonly)

Returns the value of attribute assoc.



6
7
8
# File 'app/models/concerns/model/has_one.rb', line 6

def assoc
  @assoc
end

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'app/models/concerns/model/has_one.rb', line 6

def target
  @target
end