📜 ⬆️ ⬇️

Using Twitter Bootstrap in RefineryCMS

RefineryCMS is another RoR CMS. Supports Rails3.2, convenient. I will not be engaged in the retelling of press releases, if you're interested - on the site you can play in the demo . Since she was not born yesterday, Twitter Bootstrap cannot support Twitter out of the box. There is a crutch refinerycms-bootstrap , but I was not impressed. Therefore, I will tell you how to fasten the bootstrap yourself quickly. NB! This note does not assume that you have already seen RefineryCMS, but it doesn’t tell exactly about it: the story goes strictly about screwing TB.

Installation is simple as a dietary egg:
$ gem install refinerycms $ cd ~/Projects && refinerycms MyRefineryApp $ cd MyRefineryApp && rails server 

Now at localhost:3000 localhost:3000 admin is available. When you first start creating a user and - go ahead.

Bootstrap it!


The directory structure is no different from a regular application. We will need a sprite with pictures for the main actions of the admin panel: logout and the view site (we put it in /app/assets/images/admin_sprite.png ). We'll start by downloading several different bootstrap themes and decomposing them into /vendor/assets/… under the names (the first underscore and the .scss extension are important!) __.css.scss . Refinery uses Sass , so later we will just import our bootstrap file:
 @import 'cosmo'; 

I trained on the themes with Bootswatch (and the original one, of course):
 $ ls vendor/assets/stylesheets _amelia.css.scss _bootstrap.css.scss _cerulean.css.scss _cosmo.css.scss _spacelab.css.scss 


Training


Refinery by default uses its built-in templates, which can be overwritten and edited at home (search in the application directories is more priority). The "cloning" command of the template to yourself:
 rake refinery:override view=_ 

We will need the following templates for editing:
 rake refinery:override view=layouts/application rake refinery:override view=refinery/_header rake refinery:override view=refinery/_menu rake refinery:override view=refinery/_site_bar 

Here they are:
 $ ls app/views/{refinery,layouts} app/views/layouts: application.html.erb app/views/refinery: _header.html.erb _menu.html.erb _site_bar.html.erb 

It remains only to make friends with the code. In the file app/assets/stylesheets/application.css.scss we write something like this:
 //@import 'cerulean'; //@import 'spacelab'; @import 'amelia'; //@import 'cosmo'; // This is default size of refinery logo. Arghhh $sb_ref_logo: 29px; $sb_lineheight: 18px; $sb_height: $sb_lineheight * 4; #header { height: $sb_height; } // take care of menu item hover-selection .navbar .nav > li > a { padding: $sb_lineheight; } /////////////////////////////////////////////////////////////////////////////// // Refinery admin menu: images taken from app/assets/images/admin_sprite.png /////////////////////////////////////////////////////////////////////////////// ul#main-menu-right > li > a { &:hover { background-color: transparent; } &#site_bar_refinery_cms_logo { line-height: $sb_ref_logo; height: $sb_ref_logo; margin-top: $sb_ref_logo / 3; padding: 0; overflow: hidden; img { line-height: $sb_ref_logo; } &:hover > img { margin-top: -$sb_ref_logo - 2px; } } } // control buttons @mixin admin_sprite($num,$size:24px) { line-height: $size; height: $size; overflow: hidden; padding: 0; margin: $sb_ref_logo / 2; margin-left: $sb_ref_logo / 1.5; margin-right: 0; img { margin-top: -2*$size*$num - 24px; } &:hover > img { margin-top: -2*$size*$num; } } a#adminsite { @include admin_sprite (0); } a#logout { @include admin_sprite (1); } 

')

Change toolbar


We will change the top control only to "view" the site. In the admin he is to us unnecessarily. In the main template ( /app/views/layouts/application.html.erb ) we add identifiers:
 <!DOCTYPE html> <%= render '/refinery/html_tag' %> <% site_bar = render('/refinery/site_bar', :head => true) -%> <%= render '/refinery/head' %> <body> <%= site_bar -%> <%= render '/refinery/ie6check' if request.env['HTTP_USER_AGENT'] =~ /MSIE/ -%> <div id="page_container" class="container"> <header id="header"> <%= render '/refinery/header' -%> </header> <section id="page"> <%= yield %> </section> <footer id="footer"> <%= render '/refinery/footer' -%> </footer> </div> <%= render '/refinery/javascripts' %> </body> </html> 

In the header /app/views/refinery/_header.html.erb - markup:
 <div class="navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> <a data-target=".nav-collapse" data-toggle="collapse" class="btn btn-navbar"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </a> <%= link_to Refinery::Core.site_name, refinery.root_path, :class => "brand" %> <%= render(:partial => "/refinery/menu", :locals => { :dom_id => 'menu', :css => 'menu' }) %> </div> </div> </div> 

In the menu /app/views/refinery/_menu.html.erb - markup and most importantly - rendering the refinery admin menu (the fourth line from the end):
 <% # Collect the root items. # ::Refinery::Menu is smart enough to remember all of the items in the original collection. if (roots = local_assigns[:roots] || (collection ||= refinery_menu_pages).roots).present? dom_id ||= 'menu' css = [(css || 'menu clearfix')].flatten.join(' ') hide_children = Refinery::Core.menu_hide_children if hide_children.nil? -%> <div id="main-menu" class="nav-collapse"> <nav id='<%= dom_id %>' class='<%= css %>'> <ul id="main-menu-left" class="nav"> <%= render :partial => '/refinery/menu_branch', :collection => roots, :locals => { :hide_children => hide_children, :sibling_count => (roots.length - 1), :menu_levels => local_assigns[:menu_levels], :apply_css => true #if you don't care about class='first' class='last' or class='selected' set apply_css to false for speed. } -%> </ul> <%= render :partial => '/refinery/bs_site_bar' %> </nav> </div> <% end -%> 

It's time to create an admin menu template ( vi app/views/refinery/_bs_site_bar.html.erb ):
 <% if refinery_user? && "#{controller_name}##{action_name}" != 'pages#preview' %> <ul id="main-menu-right" class="nav pull-right"> <li><%= link_to image_tag("#{"http://refinerycms.com/images/" unless local_request?}refinery/logo-site-bar.png",:alt => "Refinery (tm) Content Manager"), 'http://refinerycms.com', :target => '_blank', :id => 'site_bar_refinery_cms_logo' %></li> <li><%= #site_bar_switch_link link_to image_tag("admin_sprite.png", :alt => "Administer site"), (if session.keys.map(&:to_sym).include?(:refinery_return_to) and session[:refinery_return_to].present? session[:refinery_return_to] else refinery.admin_root_path end rescue refinery.admin_root_path), :id => 'adminsite' -%></li> <li><%= link_to image_tag("admin_sprite.png", :alt => "Log out"), refinery.destroy_refinery_user_session_path, :id => 'logout' %></li> </ul> <% end %> 

Well, remove the extra from app/views/refinery/_site_bar.html.erb :
 <% if refinery_user? && admin? && "#{controller_name}##{action_name}" != 'pages#preview' %> <div id='site_bar'> <div id='site_bar_content' class='clearfix'> <div id='editor_switch'> <%= site_bar_switch_link -%> </div> <%= link_to image_tag("#{"http://refinerycms.com/images/" unless local_request?}refinery/logo-site-bar.png", :alt => "Refinery (tm) Content Manager"), 'http://refinerycms.com', :target => '_blank', :id => 'site_bar_refinery_cms_logo' %> <div id='site_bar_branding'> <span id='site_bar_company_name'> <%= Refinery::Core.site_name %> </span> <%= link_to t('.log_out', site_bar_translate_locale_args), refinery.destroy_refinery_user_session_path, :id => 'logout' %> </div> </div> </div> <% end %> 


Rough is ready. You can switch themes in application.css.scss and be horrified by the designers' fantasies.

Source: https://habr.com/ru/post/160807/


All Articles