Skip to content

starrhorne/Example-Rails-Asset-Structure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example of how I organize my front end code for new rails >= 3.1 projects

Generally, I break the front end down into two parts. Re-usable components, and page specific code.

This is an extremly flexible model in that it allows us to handle:

  1. Completely unique pages

  2. Pages that consist entirely of re-usable components

  3. Pages that use components, but need their style & behavior tweaked slightly.

Traditionally, that last one has been the hardest.

How does it work?

This is just a naming convention for CSS classes and filenames. Results in a super clean CSS namespace, which we then make use of in JS to implement behaviors.

Page-Specific Styles

  • Each page is namespaced to the controller & action. ie, <body class=“controller-name action-name”>

  • All styles for that page are wrapped in a body.controller-name.action-name { .. } block

  • Any styles specific to that page go in stylesheets/controller_name/_action_name.scss

  • Any images go in images/controller_name/action_name/

Shared Component Styles

  • All components have class names like ‘.component.dialog`

  • Their CSS rules are namespaced to .component.dialog { … }

  • Their Stylesheets go in stylesheets/components/_component_name.scss

  • Any images go in images/components/component_name/

Javascript

  • Javascript behaviors are “attached” to a particular component & only modify its child nodes

  • Page specific JS checks for the presence of the correct body.class_name.action_name before running

The asset directory. What does it look like?

├── images
│   ├── components
│   │   └── help
│   │       └── background.png
│   ├── content
│   │   └── feature
│   │       └── geisha.png
│   └── rails.png
├── javascripts
│   ├── application.js
│   └── components
│       └── help.js.coffee
└── stylesheets
    ├── application.scss
    ├── components
    │   ├── _article_container.scss
    │   ├── _feature_container.scss
    │   ├── _footer.scss
    │   ├── _header.scss
    │   └── _help.scss
    ├── content
    │   ├── _article.scss
    │   └── _feature.scss
    └── styleguide
        ├── _layout.scss
        ├── _reset.scss
        └── _text.scss

Further Reading

About

This is an example of how I lay out my assets for new rails >= 3.1 projects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published