Skip to content

Commit

Permalink
Add has_one support to format_association
Browse files Browse the repository at this point in the history
  • Loading branch information
amaierhofer committed Dec 4, 2016
1 parent 7822d44 commit c3b2913
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/helpers/format_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ def format_with_helper(obj, attr)
# Checks whether the given attr is an association of obj and formats it
# accordingly if it is.
def format_association(obj, attr)
belongs_to = association(obj, attr, :belongs_to)
belongs_to = association(obj, attr, :belongs_to, :has_one)
has_many = association(obj, attr, :has_many, :has_and_belongs_to_many)

if belongs_to
format_belongs_to(obj, belongs_to)
elsif has_many
Expand Down Expand Up @@ -129,7 +130,7 @@ def format_type(obj, attr)
end
end

# Formats an ActiveRecord +belongs_to+ association
# Formats an ActiveRecord +belongs_to+ or +has_one+ association.
def format_belongs_to(obj, assoc)
val = obj.send(assoc.name)
if val
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ def format_string_size(obj) #:nodoc:
expect(string).to eq('AAAAA')
end

it 'formats empty has_one' do
expect(format_attr(crud_test_models(:FFFFF), :comrad)).to eq(
t('global.associations.no_entry'))
end

it 'formats existing has_one' do
string = format_attr(crud_test_models(:AAAAA), :comrad)
expect(string).to eq('BBBBB')
end

it 'formats existing has_many' do
string = format_attr(crud_test_models(:CCCCC), :others)
expect(string).to be_html_safe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ def format_string_size(obj)
assert_equal 'AAAAA', format_attr(m, :companion)
end

test 'format has one without content' do
m = crud_test_models(:FFFFF)
assert_equal t('global.associations.no_entry'),
format_attr(m, :comrad)
end

test 'format has one with content' do
m = crud_test_models(:AAAAA)
assert_equal 'BBBBB', format_attr(m, :comrad)
end

test 'format has_many column with content' do
m = crud_test_models(:CCCCC)
assert_equal '<ul><li>AAAAA</li><li>BBBBB</li></ul>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class CrudTestModel < ActiveRecord::Base #:nodoc:
has_many :mores, class_name: 'OtherCrudTestModel',
foreign_key: :more_id

has_one :comrad, class_name: 'CrudTestModel', foreign_key: :companion_id

before_destroy :protect_if_companion

validates :name, presence: true
Expand Down

0 comments on commit c3b2913

Please sign in to comment.