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.2.5 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.2.4 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.2.3 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.2.2 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.2.1 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.2.0 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.1.4 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.1.3 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.1.2 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.1.1 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.1.0 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.52 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.51 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.50 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.42 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.41 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.40 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.39 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.38 spec/active_tools/active_model/delegate_attributes_spec.rb
active_tools-0.0.37 spec/active_tools/active_model/delegate_attributes_spec.rb