How do I add more rows to a class in Ruby?

2

I started to venture into the rubygems world. I am developing a gem to generate the assets of my twitter bootstrap-style application, since I intend to reuse my front-end code. All I need to do is put the assets in the correct places, add some helpers and make some settings. Several gems that work that way make additions to the application.js / .css file at the time of installation. For example, the bootstrap, when the rails g bootstrap:install command is executed, the assets are transferred to the proper path and a require is automatically added to the application.css and the application.js.

I want to do something similar, except at a specific point in the application.rb file. To be 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 it to look 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 making additions to the file is simple, but in that case I can not add outside the scope of the class.

    
asked by anonymous 23.03.2015 / 13:41

1 answer

1

You have some pretty good examples of how to do this, of which I would highlight one of gem responders: link

In this case, it uses the inject_into_class function, which comes from Rails :: Generators :: Base, and your 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 I'd just put this in a generator ...

    
24.03.2015 / 23:22