Skip to content

Commit

Permalink
Implement auto discovery for has_one resource associations
Browse files Browse the repository at this point in the history
  • Loading branch information
miks committed Mar 30, 2015
1 parent f70a89e commit a6ce5a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion releaf-core/app/lib/releaf/core/resource_base.rb
Expand Up @@ -50,12 +50,16 @@ def associations
end

def includable_association?(association)
association.macro == :has_many &&
includable_association_types.include?(association.macro) &&
excluded_associations.exclude?(association.name) &&
association.class != ActiveRecord::Reflection::ThroughReflection &&
resource_class.nested_attributes_options.has_key?(association.name)
end

def includable_association_types
[:has_many, :has_one]
end

def excluded_associations
[:translations]
end
Expand Down
10 changes: 8 additions & 2 deletions releaf-core/spec/lib/releaf/core/resource_base_spec.rb
Expand Up @@ -125,15 +125,15 @@
describe "#includable_association?" do
let(:association){ subject.resource_class.reflections["chapters"] }

context "when given association is :has_many, not excluded, not `ThroughReflection` and has nested_attributes for it" do
context "when given association type is includable, association is not excluded, not `ThroughReflection` and has nested_attributes for it" do
it "returns true" do
expect(subject.includable_association?(association)).to be true
end
end

context "when given association is not :has_many" do
it "returns false" do
allow(association).to receive(:macro).and_return(:belongs_to)
allow(subject).to receive(:includable_association_types).and_return([:a, :b])
expect(subject.includable_association?(association)).to be false
end
end
Expand All @@ -160,6 +160,12 @@
end
end

describe "#includable_association_types" do
it "returns array with `:has_many` and `:has_one` as includable association types" do
expect(subject.includable_association_types).to eq([:has_many, :has_one])
end
end

describe "#excluded_associations" do
it "returns array with `translations`" do
expect(subject.excluded_associations).to eq([:translations])
Expand Down

0 comments on commit a6ce5a3

Please sign in to comment.