Reputation: 22333
I'm modifying a seed file for a site that is already in existence.
One of the fields in the table I'm changing is a text area.
I would like to format the information I am inputting in the seed file so I don't have to go back and change it manually on the site.
Currently an entry in the seed file looks like this
if !Store.exists?(:title => 'store Cavan')
Store.create(
opening_hours: 'Mon. to Fri. 8:30 - 9:00PM
Saturday 8:30 - 7:00 PM
Sunday 11:00 - 7:00 PM',
latitude: 53,
longitude: -7,
title: 'test store Cavan',
street_line_one: 'test Retail Park',
street_line_two: 'test Road',
town: 'Cavan',
county: 'Cavan',
country: 'Ireland')
When I run rake db:seed this comes out in an info box like this
Mon. to Fri. 8:30 - 9:00PMSaturday 8:30 - 7:00 PMSunday 11:00 - 7:00 PM
Instead of this
Mon. to Fri. 8:30 - 9:00PM
Saturday 8:30 - 7:00 PM
Sunday 11:00 - 7:00 PM
I have tried add in '\n' in there and "\n" but that breaks my site. I know this is only a small issue but I would like to know how to keep some sort of formatting when I run my seeds, in this case a new line after each time is displayed.
any help is very much appreciated, thanks.
Upvotes: 1
Views: 962
Reputation: 3454
I'd do this with heredoc:
opening_hours = <<-END.gsub(/^\s*/, '')
Mon. to Fri. 8:30 - 9:00 PM
Saturday 8:30 - 7:00 PM
Sunday 11:00 - 7:00 PM
END
=> "Mon. to Fri. 8:30 - 9:00 PM\nSaturday 8:30 - 7:00 PM\nSunday 11:00 - 7:00 PM\n"
You can use any method you like after the opening END
, e.g. ruby-dedent to remove the indent (in case you don't like regular expressions). The closing END
doesn't have to be at the beginning of the line, the -
in front of the opening END
means, all whitespace in front of the closing END
shall be ignored.
In your case:
Store.create(
latitude: 53,
longitude: -7,
title: 'test store Cavan',
street_line_one: 'test Retail Park',
street_line_two: 'test Road',
town: 'Cavan',
county: 'Cavan',
country: 'Ireland',
opening_hours: <<-END.strip_heredoc
Mon. to Fri. 8:30 - 9:00 PM
Saturday 8:30 - 7:00 PM
Sunday 11:00 - 7:00 PM
END
)
The strip_heredoc
comes with ActiveSupport and yields the same result here as the gsub
.
The heredoc has to be the last attribute assigned because to my knowledge, there's no way to have the closing END
and a comma on the same line.
Heredocs are just a very nice way to embed formatted data in source code - and Ruby has particularly great support for it. Google it!
Upvotes: 0
Reputation: 53018
\n
would definitely work. I am not sure how you used \n
and it didn't work out for you.
Change your create
method call by including \n
as below:
Store.create(
opening_hours: 'Mon. to Fri. 8:30 - 9:00PM\nSaturday 8:30 - 7:00 PM\nSunday 11:00 - 7:00 PM',
latitude: 53,
longitude: -7,
title: 'test store Cavan',
street_line_one: 'test Retail Park',
street_line_two: 'test Road',
town: 'Cavan',
county: 'Cavan',
country: 'Ireland')
UPDATE
As per the chat session. OP opted for adding the code in its entirety in html to the seed file to get the correct format.
Upvotes: 4