Skip to content

Commit

Permalink
Allows HABTM relationships to coexist with aggregated behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
zbelzer committed Oct 1, 2019
1 parent 5c92fbb commit c5c8be5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2
2.4.6
2 changes: 1 addition & 1 deletion HornsAndHooves-moribus.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
s.add_development_dependency "rspec-rails"
s.add_development_dependency "sqlite3"
s.add_development_dependency "sqlite3", "~> 1.3.6"
end
12 changes: 6 additions & 6 deletions lib/moribus/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def extend_has_aggregated_reflection(reflection)
def association(name)
association = super
reflection = self.class.reflect_on_association(name)
case reflection.macro
when :belongs_to
association.extend(HasAggregatedExtension) if reflection.options[:aggregated]
when :has_one
association.extend(HasCurrentExtension) if reflection.options[:is_current]
else # do nothing
case reflection.try(:macro)
when :belongs_to
association.extend(HasAggregatedExtension) if reflection.options[:aggregated]
when :has_one
association.extend(HasCurrentExtension) if reflection.options[:is_current]
else # do nothing
end
association
end
Expand Down
16 changes: 16 additions & 0 deletions spec/moribus/aggregated_behavior_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@ class SpecSuffix < MoribusSpecModel(name: :string, description: :string)
create!(name: "jr" , description: "Junior")
end

class SpecTag < MoribusSpecModel()
has_and_belongs_to_many :person_names
end

class SpecPersonNamesTags < MoribusSpecModel(spec_tag_id: :integer,
spec_person_name_id: :integer
)
end

class SpecPersonName < MoribusSpecModel(first_name: :string,
last_name: :string,
spec_suffix_id: :integer
)
acts_as_aggregated
has_enumerated :spec_suffix, default: ""
has_and_belongs_to_many :spec_tags

validates_presence_of :first_name, :last_name

Expand All @@ -35,6 +45,12 @@ class SpecCustomerFeature < MoribusSpecModel(feature_name: :string)
end

describe "Aggregated" do
it "supports has_and_belongs_to_many association reflections which do not have a macro" do
tags = [SpecTag.new, SpecTag.new]
name = SpecPersonName.create!(first_name: "John", last_name: "Smith", spec_tags: tags)
expect(name.spec_tags.size).to eq(2)
end

context "definition" do
it "raises an error on an unknown option" do
expect{
Expand Down

0 comments on commit c5c8be5

Please sign in to comment.