|
require 'bundler/setup' |
|
Bundler.require |
|
|
|
require 'nested-attributes' |
|
require 'pry' |
|
require 'minitest/autorun' |
|
|
|
describe "attributes" do |
|
it "simple attribute" do |
|
example = Class.new(NestedAttributes::Base) do |
|
attribute :key1, String |
|
end |
|
|
|
example.new.key1.must_be_nil |
|
example.new(key1: "value1").key1.must_equal "value1" |
|
|
|
lambda { example.new.unknown }.must_raise NoMethodError |
|
end |
|
|
|
it "invalid definition" do |
|
exception = lambda do |
|
Class.new(NestedAttributes::Base) do |
|
attribute :key1, String do |
|
attribute :key10, String |
|
end |
|
end |
|
end.must_raise NestedAttributes::AttributeDefinitionError |
|
|
|
exception.message.must_equal "Pass options hash as second argument" |
|
|
|
exception = lambda do |
|
Class.new(NestedAttributes::Base) do |
|
attribute :key1, String, {some: :option} do |
|
attribute :key10, String |
|
end |
|
end |
|
end.must_raise NestedAttributes::AttributeDefinitionError |
|
|
|
exception.message.must_equal "Unable to specify type and options" |
|
|
|
exception = lambda do |
|
Class.new(NestedAttributes::Base) do |
|
attribute :key1, {default: "1"} do |
|
attribute :key10, String |
|
end |
|
end |
|
end.must_raise NestedAttributes::AttributeDefinitionError |
|
|
|
exception.message.must_equal "Default must be a Hash" |
|
end |
|
|
|
it "simple nested attribute" do |
|
example = Class.new(NestedAttributes::Base) do |
|
attribute :key1 do |
|
attribute :key10, String |
|
attribute :key11, String, default: "value11" |
|
attribute :key12 |
|
attribute :key13, Array[Symbol] |
|
end |
|
end |
|
|
|
example.new(key1: {key10: "value10"}).key1.key10.must_equal "value10" |
|
example.new.key1.key10.must_be_nil |
|
example.new.key1.key11.must_equal "value11" |
|
example.new.key1.key12.must_be_nil |
|
example.new(key1: {key13: ["value13"]}).key1.key13.must_equal [:value13] |
|
end |
|
|
|
it "nested attribute from subclass" do |
|
key1 = Class.new(NestedAttributes::Base) do |
|
attribute :key10, String |
|
attribute :key11, String, default: "value11" |
|
end |
|
|
|
example = Class.new(NestedAttributes::Base) do |
|
attribute :key1, key1 |
|
end |
|
|
|
example.new(key1: {key10: "value10"}).key1.key10.must_equal "value10" |
|
example.new.key1.key10.must_be_nil |
|
example.new.key1.key11.must_equal "value11" |
|
|
|
exception = lambda do |
|
Class.new(NestedAttributes::Base) do |
|
attribute :key1, key1, default: "1" |
|
end |
|
end.must_raise NestedAttributes::AttributeDefinitionError |
|
|
|
exception.message.must_equal "Default must be a Hash" |
|
end |
|
end |
|
|
|
describe "validations" do |
|
it "validates nested attributes" do |
|
Partner = Class.new(NestedAttributes::Base) do |
|
attribute :person_data do |
|
attribute :pesel, String |
|
attribute :id_type, String |
|
|
|
validates :pesel, presence: true |
|
validates :id_type, presence: true |
|
end |
|
|
|
attribute :company_data do |
|
attribute :name, String |
|
attribute :nip, String |
|
|
|
validates :name, presence: true |
|
validates :nip, presence: true |
|
end |
|
end |
|
|
|
partner = Partner.new |
|
partner.valid?.must_equal false |
|
|
|
partner.errors.to_hash.must_equal({ |
|
person_data: ["is invalid"], |
|
company_data: ["is invalid"] |
|
}) |
|
|
|
partner.person_data.valid?.must_equal false |
|
|
|
partner.person_data.errors.to_hash.must_equal({ |
|
pesel: ["can't be blank"], |
|
id_type: ["can't be blank"] |
|
}) |
|
|
|
partner.company_data.valid?.must_equal false |
|
|
|
partner.company_data.errors.to_hash.must_equal({ |
|
name: ["can't be blank"], |
|
nip: ["can't be blank"] |
|
}) |
|
|
|
partner.nested_errors_hash.must_equal({ |
|
person_data: { |
|
pesel: ["can't be blank"], |
|
id_type: ["can't be blank"] |
|
}, |
|
company_data: { |
|
name: ["can't be blank"], |
|
nip: ["can't be blank"] |
|
} |
|
}) |
|
end |
|
end |
|
|