简体   繁体   中英

Ruby Gem: Get files in asset pipeline

I've developed a plugin ( jQuery ) which I really want to make into a gem. For the gem to work I need three files to be in the asset pipeline of Rails: an image, a javascript file and a css file.

On this moment I've got the gem working, but! I can't get the files in the asset pipeline. If I include them like this:

//= require gem.js

Or

//= require gem.css

It works, but the files doesn't show up in the asset pipeline and while I can make this work with the css and js file, I can't make this work for the images.

Thereby I really want the files to show up in the asset pipeline ( or at least the image, which is needed to give the plugin extra glance ). On this moment I've done everything I could think off, but it doesn't work..

This is my gemspec:

# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "yellow-text-rails/version"

Gem::Specification.new do |gem|
    # Gem information
    gem.name          = "yellow-text-rails"
    gem.version       = YellowText::Rails::VERSION
    gem.authors       = ["Stefan Vermaas"]

    # Project name
    gem.rubyforge_project = "yellow-text-rails" 

    gem.files         = `git ls-files`.split("\n")
    gem.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
    gem.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
    gem.require_paths = ["lib"]
end

And I've included this in `lib/yellow-text-rails/yellow-text-rails.rb':

require "yellow-text-rails/version"

module YellowText
    module Rails
        if defined?(::Rails) and ::Rails.version >= "3.1"
                class Rails::Engine < ::Rails::Engine
            end
        end
    end
end

Hopefully you guys can help me out! Thanks!

Try this:

module YellowText
  module Rails
    class Engine < ::Rails::Engine
      initializer :append_dependent_assets_path, group: :all do |app|
        app.config.assets.precompile += %w( gem.js gem.css )
      end
    end
  end
end

Make sure to include your assets in these folders in your gem vendor/assets/javascripts and vendor/assets/stylesheets .

Have a look at one of my simple Gems for reference: https://github.com/wazery/vider

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM