Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a gem for shared stylesheets and using it in a Rails application

I'm trying to create a gem to put all my common stylesheets in. I can't to figure out how to import the stylesheets in a Rails application. Here's what I've done so far (following this guide):

  1. I ran bundle gem apple_core.

  2. I modified apple_core.rb to contain a rails engine class like so:

    require "apple_core/version"
    
    module AppleCore
      class Engine < Rails::Engine
      end
    end
    
  3. I created the apple_core stylesheets directory using mkdir -p lib/apple_core/app/assets/stylesheets/apple_core.

  4. I created an index file inside the apple_core stylesheets directory and added the following to it:

    /*
     *= require_self
     *= require_tree .
     */
    
  5. I made a test.css.scss file in the same directory and added:

    a
    {
      color: red;
    }
    
  6. I created a GitHub repository for the gem and pushed it.

  7. In my Rails application, I added the gem to my Gemfile in the assets group.

  8. I ran bundle install.

I tried to include my gem in one of my files using @import "apple_core";, but when I attempted to load the page I received a couldn't find file 'apple_core' error. What am I missing here?

like image 811
LandonSchropp Avatar asked Nov 30 '12 09:11

LandonSchropp


1 Answers

You just have to mirror the directories in your gem as they are in a rails project. So you can put assets to app/assets, lib/assets and vendor/assets. You then have to make sure that these files are included in your gemspec, for example:

  s.files = Dir["{app,config,db,lib,vendor}/**/*"] + ["MIT-LICENSE", "Rakefile", "Readme.md"]

So the Path should then be vendor/assets/stylesheets/apple_core.

like image 56
ohcibi Avatar answered Nov 14 '22 11:11

ohcibi