Skip to content

Commit

Permalink
Core::ResourceBase: handle polymorphic associations
Browse files Browse the repository at this point in the history
  • Loading branch information
miks committed Jan 25, 2015
1 parent 0f2012f commit 345b292
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion releaf-core/app/lib/releaf/core/resource_base.rb
Expand Up @@ -36,7 +36,11 @@ def associations_attributes
end

def association_attributes(association)
self.class.new(association.klass).values - [association.foreign_key]
self.class.new(association.klass).values - association_excluded_attributes(association)
end

def association_excluded_attributes(association)
[association.foreign_key, association.type].compact
end

def associations
Expand Down
15 changes: 13 additions & 2 deletions releaf-core/spec/lib/releaf/core/resource_base_spec.rb
Expand Up @@ -51,15 +51,26 @@
end

describe "#association_attributes" do
it "returns association params without `foreign_key` param" do
it "returns association params without association excluded attributes" do
association = subject.resource_class.reflections[:chapters]
allow(association).to receive(:foreign_key).and_return("b")
allow(described_class).to receive(:new).with(association.klass).and_call_original
allow_any_instance_of(described_class).to receive(:values).and_return(["a", "b", "c"])
allow(subject).to receive(:association_excluded_attributes).and_return(["b"])
expect(subject.association_attributes(association)).to eq(["a", "c"])
end
end

describe "#association_excluded_attributes" do
it "returns `foreign_key` and polymorphic association type key (if exists)" do
association = subject.resource_class.reflections[:chapters]
allow(association).to receive(:foreign_key).and_return("b")
expect(subject.association_excluded_attributes(association)).to eq(["b"])

allow(association).to receive(:type).and_return("x")
expect(subject.association_excluded_attributes(association)).to eq(["b", "x"])
end
end

describe "#values" do
before do
allow(subject).to receive(:associations_attributes).and_return(["x", "y"])
Expand Down

0 comments on commit 345b292

Please sign in to comment.