rubyjekylloctopress

Change deep_merge to Utils.deep_merge_hashes


I am using Octopress to generate static html pages. I tried to change the language of the dates using this instruction (it is in German but we need only the code). When I copy date.rb from this German website to my octopress/plugins, I have the following error: Liquid Exception: undefined method `deep_merge' for # in blog/path/to/post/index.html.

I can generate site if I comment out this part in date.rb:

def to_liquid
  date_format = self.site.config['date_format']
  self.data.deep_merge({
    "title"             => self.data['title'] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
    "url"               => self.url,
    "date"              => self.date,
    # Monkey patch
    "date_formatted"    => format_date(self.date, date_format),
    "updated_formatted" => self.data.has_key?('updated') ? format_date(self.data['updated'], date_format) : nil,
    "id"                => self.id,
    "categories"        => self.categories,
    "next"              => self.next,
    "previous"          => self.previous,
    "tags"              => self.tags,
    "content"           => self.content })
end

Then the language is changed for the dates in blog/archives, but not for the dates in posts. I found a similar problem which has been solved by changing deep_merge → Utils.deep_merge_hashes. So I understand that I need to do exactly the same in the piece of the code I presented above. I think it should be quite easy, but since I don't know Ruby, I didn't succeed yet. Could you please tell me how should I use Utils.deep_merge_hashes instead of deep_merge in this case?


Solution

  • This works (ruby 2.1.1 - Jekyll 2.5.3)

    def to_liquid(attrs = nil)
      date_format = self.site.config['date_format']
      new_datas = {
        "title"             => self.data['title'] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
        "url"               => self.url,
        "date"              => self.date,
        # Monkey patch
        "date_formatted"    => format_date(self.date, date_format),
        "updated_formatted" => self.data.has_key?('updated') ? format_date(self.data['updated'], date_format) : nil,
        "id"                => self.id,
        "categories"        => self.categories,
        "next"              => self.next,
        "previous"          => self.previous,
        "tags"              => self.tags,
        "content"           => self.content }
    
      Utils.deep_merge_hashes(self.data, new_datas)
    end