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) 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) { { } }