Sha256: 6d1bdcefe75479b6cb3550b9e2bc566cc8dad20b9d1e73c99a5ea86184d4045a

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

describe "assignment by custom key" do
  context "with mixin syntax" do
    before do
      class Test::Order
        extend Dry::Initializer::Mixin
        extend Dry::Initializer::Rails

        param  :user,    model: "User", find_by: "name"
        option :product, model: Item,   find_by: :name
      end
    end

    let!(:user) { User.create name: "Dude" }
    let!(:item) { Item.create name: "The thing" }

    it "works when records are present" do
      subject = Test::Order.new("Dude", product: "The thing")

      expect(subject.user).to eql user
      expect(subject.product).to eql item
    end

    it "works when records are absent" do
      subject = Test::Order.new("Man", product: "Something strange")

      expect(subject.user).to be_nil
      expect(subject.product).to be_nil
    end
  end

  context "with container syntax" do
    before do
      class Test::Order
        include Dry::Initializer.define -> do
          extend Dry::Initializer::Rails

          param  :user,    model: "User", find_by: "name"
          option :product, model: Item,   find_by: :name
        end
      end
    end

    let!(:user) { User.create name: "Dude" }
    let!(:item) { Item.create name: "The thing" }

    it "works when records are present" do
      subject = Test::Order.new("Dude", product: "The thing")

      expect(subject.user).to eql user
      expect(subject.product).to eql item
    end

    it "works when records are absent" do
      subject = Test::Order.new("Man", product: "Something strange")

      expect(subject.user).to be_nil
      expect(subject.product).to be_nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-initializer-rails-0.0.3 spec/dry/initializer/assignment_by_custom_key_spec.rb
dry-initializer-rails-0.0.2 spec/dry/initializer/assignment_by_custom_key_spec.rb
dry-initializer-rails-0.0.1 spec/dry/initializer/assignment_by_custom_key_spec.rb