class NestedRecord::Methods::One

Public Class Methods

new(setup) click to toggle source
Calls superclass method
# File lib/nested_record/methods/one.rb, line 3
def initialize(setup)
  super
  define :writer
  define :build
  alias_method rewrite_attributes_method_name, build_method_name
  define :bang
  define :upsert_attributes
  define :validation
  define_attributes_writer_method
end

Public Instance Methods

bang_method_body() click to toggle source
# File lib/nested_record/methods/one.rb, line 41
    def bang_method_body
      <<~RUBY
        #{@setup.name} || #{build_method_name}
      RUBY
    end
bang_method_name() click to toggle source
# File lib/nested_record/methods/one.rb, line 37
def bang_method_name
  :"#{@setup.name}!"
end
build_method_body() click to toggle source
# File lib/nested_record/methods/one.rb, line 28
def build_method_body
  setup = @setup
  writer_method_name = self.writer_method_name
  proc do |attributes = {}|
    record = setup.record_class.new(attributes)
    public_send(writer_method_name, record)
  end
end
build_method_name() click to toggle source
# File lib/nested_record/methods/one.rb, line 24
def build_method_name
  :"build_#{@setup.name}"
end
upsert_attributes_method_body() click to toggle source
# File lib/nested_record/methods/one.rb, line 47
def upsert_attributes_method_body
  build_method_name = self.build_method_name
  name = @setup.name
  proc do |attributes|
    if (record = public_send(name))
      record.assign_attributes(attributes)
    else
      public_send(build_method_name, attributes)
    end
  end
end
validation_method_body() click to toggle source
# File lib/nested_record/methods/one.rb, line 63
def validation_method_body
  name = @setup.name
  if ActiveModel::VERSION::MAJOR < 6 || (ActiveModel::VERSION::MAJOR == 6 && ActiveModel::VERSION::MINOR < 1)
    proc do
      record = public_send(name)
      return true unless record
      return true if record.valid?

      record.errors.each do |attribute, message|
        error_attribute = "#{name}.#{attribute}"
        errors[error_attribute] << message
        errors[error_attribute].uniq!
      end
      record.errors.details.each_key do |attribute|
        error_attribute = "#{name}.#{attribute}"
        record.errors.details[attribute].each do |error|
          errors.details[error_attribute] << error
          errors.details[error_attribute].uniq!
        end
      end
      false
    end
  else
    proc do
      record = public_send(name)
      return true unless record
      return true if record.valid?

      record.errors.group_by_attribute.each do |attribute, errors|
        error_attribute = "#{name}.#{attribute}"
        errors.each do |error|
          self.errors.import(error, attribute: error_attribute)
        end
      end
      false
    end
  end
end
validation_method_name() click to toggle source
# File lib/nested_record/methods/one.rb, line 59
def validation_method_name
  :"validate_associated_record_for_#{@setup.name}"
end
writer_method_body() click to toggle source
Calls superclass method
# File lib/nested_record/methods/one.rb, line 14
def writer_method_body
  setup = @setup
  proc do |record|
    unless record.nil? || record.kind_of?(setup.record_class)
      raise NestedRecord::TypeMismatchError, "#{record.inspect} should be a #{setup.record_class}"
    end
    super(record)
  end
end