From bbfa41b081253d8dcb0a8db7855258c0f0d40685 Mon Sep 17 00:00:00 2001 From: Naomi Jacobs Date: Wed, 8 Nov 2017 22:30:13 -0800 Subject: [PATCH 1/2] Add support for plural key in brainstem associations --- lib/brainstem/dsl/association.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/brainstem/dsl/association.rb b/lib/brainstem/dsl/association.rb index 92259155..ca240f32 100644 --- a/lib/brainstem/dsl/association.rb +++ b/lib/brainstem/dsl/association.rb @@ -11,6 +11,7 @@ def initialize(name, target_class, options) @name = name.to_s @target_class = target_class @options = options + @plural = plural end def description @@ -25,6 +26,14 @@ def method_name end end + def plural + if options[:plural].present? + options[:plural] + else + name.singularize != name && name.pluralize == name + end + end + def run_on(model, context, helper_instance = Object.new) if options[:lookup] run_on_with_lookup(model, context, helper_instance) From 32c6fbe59f88ab139ed78126cceedd6fe496dcf4 Mon Sep 17 00:00:00 2001 From: Naomi Jacobs Date: Thu, 9 Nov 2017 13:17:22 -0800 Subject: [PATCH 2/2] Add spec for plural code --- spec/brainstem/dsl/association_spec.rb | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/brainstem/dsl/association_spec.rb b/spec/brainstem/dsl/association_spec.rb index f5203de9..edad532d 100644 --- a/spec/brainstem/dsl/association_spec.rb +++ b/spec/brainstem/dsl/association_spec.rb @@ -24,6 +24,33 @@ end end + describe 'plural' do + context 'when `plural` is specified in the options' do + let(:options) { { plural: true } } + + it 'returns the value specified in the plural key' do + expect(association.plural).to eq true + end + end + + context 'when `plural` is not specified in the options' do + it 'delegates to ruby singularize and pluralize to guess if the association is plural or singular' do + class Dress + + end + + singular_association = Brainstem::DSL::Association.new('user', User, {}) + weird_singular_association = Brainstem::DSL::Association.new('dress', Dress, {}) + plural_association = Brainstem::DSL::Association.new('users', User, {}) + weird_plural_association = Brainstem::DSL::Association.new('dresses', Dress, {}) + expect(singular_association.plural).to eq false + expect(plural_association.plural).to eq true + expect(weird_singular_association.plural).to eq false + expect(weird_plural_association.plural).to eq true + end + end + end + describe "#run_on" do let(:context) { { } }