Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/brainstem/dsl/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions spec/brainstem/dsl/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👗


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

Expand Down