4

I built a yml.erb file that will be used for configuring some parts of my application. I would like to preload it with an initializer (I don't require it to change during application running), the biggest problem is that this yml file contains link to images that are inside app/assets/images directory. I would like to use the image_path helper inside my yml.erb file but I'm having troubles (I don't know what I should include and where should I include it: if in the yml.erb file or in the file that parses the yml.erb file).

What I have at the moment

desktop_icons.rb (inside config/initializers)

require 'yaml'
require 'rails'
include ActionView::Helpers::AssetTagHelper

module ManageFedertrekOrg
  class Application < Rails::Application
    def desktop_icons
      @icons ||= YAML.load(ERB.new(File.read("#{Rails.root}/config/icons.yml.erb")).result)
    end
  end
end

icons.yml.erb (inside config)

 - 
  image: <%= image_path "rails" %>
  title: Test this title

home_controller.rb (inside controllers)

class HomeController < ApplicationController
    skip_filter :authenticate_user!

  def index
    @user_is_signed_in = user_signed_in?
    respond_to do |format|
      format.html { render :layout => false } # index.html.erb
    end
  end

  def icons
    result =
    {
      icons: MyApp::Application.desktop_icons,
      success: true,
      total: MyApp::Application.desktop_icons.count
    }

    respond_to do |format|
      format.json { render json: result }
    end
  end

end

Any suggestion?

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
  • 1
    I'm not sure if Rails is initialized "enough" at the point where you want to use the helpers. Have a look at the answers to this for how to do it. Good luck. http://stackoverflow.com/questions/341143/can-rails-routing-helpers-i-e-mymodel-pathmodel-be-used-in-models – ffoeg Dec 11 '11 at 02:23
  • I'm sure I'm near the solution but I definitely can't catch it :\ – Francesco Belladonna Dec 11 '11 at 04:18

3 Answers3

2

If the ERB only needs to be parsed from inside views, you can do something like this:

Controller

@questions = YAML.load_file("#{Rails.root}/config/faq.yml.erb")

View

<%= ERB.new(@questions[2]["answer"]).result(binding).html_safe %>

That way you can control which attributes actually get parsed. Also, all helpers available in the view are available in the yaml, due to (binding).

Jordan
  • 2,281
  • 1
  • 17
  • 24
1

Rails.application.routes.url_helpers is a module with your url_helpers that you can include where you want to use them. You can pass this to ERB via binding

class Application < Rails::Application
  def desktop_icons
    @icons ||= YAML.load(
      ERB.new(File.read("#{Rails.root}/config/icons.yml.erb")).result(binding)
    )
  end
end

and then in yml

<% extend routes.url_helpers %>
- 
 image: <%= image_path "rails" %>
 title: Test this title

since at erb evaluation time the self is Rails.application

clyfe
  • 23,695
  • 8
  • 85
  • 109
  • Your suggestion is really valuable, however it's not working (at least I get other errors), I really don't know how to move in this field. – Francesco Belladonna Dec 11 '11 at 04:19
  • undefined method `image_path' for # – Francesco Belladonna Dec 11 '11 at 14:17
  • I get the same error, I think the biggest problem is connected to the fact that rails is not loaded "enough" in the initializer (infact it requests for all "paths"). As ffoeg stated, maybe this is the problem. – Francesco Belladonna Dec 11 '11 at 15:39
  • I'd say it has to do with **when** you **call** the `desktop_icons` method, I was under the impression that it's called **after** initialization. – clyfe Dec 11 '11 at 17:44
  • Well, I don't know if an initializer is called before or after initialization, if it's called before that's the problem (and if you add it to your answer I can mark yours) – Francesco Belladonna Dec 11 '11 at 18:06
0

Looks like rails is "not initialized enough" as ffoeg and clyfe stated. I moved the script to another part of my code where rails is more initialized and now it's working well.

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147