Sha256: 1fdb64ea7eb80dc36bbf9083d290cb584b723f44978141aa7a429879c90d60ea

Contents?: true

Size: 1.36 KB

Versions: 47

Compression:

Stored size: 1.36 KB

Contents

require 'spec_helper'

describe ActiveTools::ActiveModel::DelegateAttributes do
  class Parent
    include ActiveModel::Validations
    include ActiveTools::ActiveModel::DelegateAttributes

    attr_accessor :child

    delegate_attributes :name, to: :child, writer: true
    delegate_attributes :name, to: :child, prefix: :prefixed, writer: true
  end

  class Child
    include ActiveModel::Validations
    include ActiveTools::ActiveModel::DelegateAttributes

    attr_accessor :name

    validates_presence_of :name
  end

  let(:teh_object) { Parent.new.tap{|parent| parent.child = teh_child} }
  let(:teh_child) { Child.new }

  it "delegates the given attribute from parent to child" do
    teh_object.name = "Foo"

    expect(teh_child.name).to eq("Foo")
    expect(teh_object.name).to eq("Foo")
  end

  it "delegates the given attribute with a prefix from parent to child" do
    teh_object.prefixed_name = "Bar"

    expect(teh_child.name).to eq("Bar")
    expect(teh_object.prefixed_name).to eq("Bar")
  end

  it "forwards the errors from child to parent" do
    expect(teh_object.valid?).to be_false
    expect(teh_object.errors.messages[:name]).to eq(["can't be blank"])
  end

  it "forwards the errors from child to parent via prefix" do
    expect(teh_object.valid?).to be_false
    expect(teh_object.errors.messages[:prefixed_name]).to eq(["can't be blank"])
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
active_tools-0.0.36 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.35 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.34 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.33 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.32 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.31 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.30 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.29 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.28 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.27 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.26 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.25 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.24 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.22 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.21 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.20 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.19 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.18 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.17 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.16 spec/active_tools/active_model/delegate_attributes_spec.rb