How to add more lines to a Ruby class?

Asked

Viewed 58 times

2

I began to venture into the world of rubygems. I’m developing a Gem to generate my twitter bootstrap-style app’s Assets, as I intend to reuse my front-end code. All I need to do is put the Assets in the right places, add some helpers and make some settings. Several Gems that work this way make additions to the application.js/. css file at the time of installation. For example, bootstrap, when the command rails g bootstrap:install is executed, the Assets are transferred to the proper path and a require is automatically added to the application.css and application.js.

I want to do something similar, only at a specific point of the application.br file. Being more exact, what I want is to add more folders to the Assets path. The file looks something like this:

class Application < Rails::Application

end

I need you to be like this:

class Application < Rails::Application
     config.assets.paths << 'path/para/a/pasta'
end

But how would I ensure that this content will always be added within the class definition? I mean, simply adding to the file is simple, but in this case I can’t add outside the scope of the class.

1 answer

1


There are some really cool examples of how to do this, of which I would highlight one of the answers: https://github.com/plataformatec/responders/blob/master/lib/generators/responders/install_generator.rb

In this case, it uses the inject_into_class function, which comes from Rails::Generators::Base, and its code would look like this:

def add_assets
  inject_into_class "config/application.rb", "Application", <<-RUBY
    config.assets.paths << 'path/para/a/pasta'
  RUBY
end

I think you could just put that in a General...

  • Thanks! That’s just what I need!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.