I have a CSS file which contains several hundred 16x16 icons. They are referenced using a CSS class/subclass arrangement, like so:
<span class="icons icon_name"></span>
So, if I need an "arrow up" icon, I simply code:
<span class="icons arrow_up"></span>
And so on. Because there are so many icons in the library, I created a reference page (in Haml), so I could quickly find which icon I need. An example of the Haml code:
%div.icons.arrow_turn_right
arrow_turn_right
%br
%div.icons.arrow_undo
arrow_undo
%br
%div.icons.arrow_up
ss_arrow_up
%br
It occurs to me that this is a rather brute force method to writing the page. 300 icons require nearly 1000 lines of code.
My question: Is there a way to iterate through a CSS class' subclasses using an each loop (or something similar)?
Something like this:
subs = mainCSSclass.allSubclasses <--Replace this line with functional code
subs.each do |subclass|
... display the |subclass| ...
end
Is such a thing possible?
Edit: Here is a partial of the actual CSS file being referenced:
.icons {
display: inline;
overflow: hidden;
height: 16px;
padding-left: 18px;
background: url(icons.png) no-repeat;
}
.accept {
background-position: 0px 0px;
}
.add {
background-position: 0px -19px;
}
.anchor {
background-position: 0px -37px;
}
.application {
background-position: 0px -55px;
}
.application_add {
background-position: 0px -73px;
}
.application_cascade {
background-position: 0px -91px;
}
Solution (thanks to #Corroded's answer):
In my pages_helper.rb:
def read_css_cfg
css_classes = Array.new
file = File.new("public/stylesheets/blueprint/plugins/icons/icons.css", "r")
while line = file.gets
parsed = line.scan(/[.]\w+ {/).to_s.scan(/\w/).join
css_classes << parsed if !parsed.blank?
end
file.close
css_classes
end
In my actual Haml file:
- parse = read_css_cfg
- parse.delete("icons") #unrelated class, but in the same file'
- column = 1
.container
- parse.each do |class_name|
- case column
- when 1
<div class = "css_row">
%div{:class => "icons #{class_name} three_column"}
#{class_name}
- column += 1
- when 2
%div{:class => "icons #{class_name} three_column"}
#{class_name}
- column += 1
- when 3
%div{:class => "icons #{class_name} three_column"}
#{class_name}
</div>
- column = 1
Finally, though this has nothing to do with the original question, my final answer contained code which auto-generated a 3-column "div-table." If you're interested in the CSS to use DIV's instead of a table, here it is:
.container {
display: table;
width: 675px;
}
.three_column {
display: table-cell;
width: 225px;
}
.css_row {
display: table-row;
Okay, so basically, the plan is to loop through all those classes and generate them in a Haml page, right?
What I would do is open that CSS file (using Ruby of course) and then use a regular expression to parse and get all the subclasses. Since you would want to call that in a view, then I suggest using a helper:
def loadCSSClasses
cssClasses = []
cssFile = File.open("yourcssfile.css") do
#Forgive me, my regular expression is weak, but basically you would want to get all the classnames after the word ".icon."
cssClasses << className
end
cssClasses
end
Then in your Haml file do this:
%ul
- loadCSSClasses.each do |class_name|
%li{:class => "icon #{class_name}"
= "ss_#{class_name}"
Related
I'd like to strip out style tags and the embedded css rules using sanitize. Like the following:
ActionController::Base.helpers.sanitize("<style> .emphasized { font-weight: bold } </style> hey <div class='emphasized'>watch out</div>", tags: %w(strong b br p ul li em u))
=> " .emphasized { font-weight: bold } hey watch out"
but giving me the css rule still. Would I have to use Nokogiri or something else or can sanitize handle this?
Maybe you can try to define a custom scrubber. For example
class MyScrubber < Rails::Html::PermitScrubber
def initialize
super
self.tags = %w(strong b br p ul li em u)
end
def scrub(node)
node.name == 'style' ? node.remove : super(node)
end
end
ActionController::Base.helpers.sanitize("<style> .emphasized { font-weight: bold } </style> hey <div class='emphasized'>watch out</div>", scrubber: MyScrubber.new)
I just want to apply custom style for the next/prev page
But it works in a stange way.
The previous link will be disappeared after clicking the next link, vice versa.
link => http://great-compassion.com:1234/news/index
The snippet of will_pagination
def page_number(page)
unless page == current_page
tag(:li, link(page, page, :rel => rel_value(page)))
else
tag(:li, page, :class => "current")
end
end
def previous_or_next_page(page, text, classname)
if page
tag(:li, link(text, page), :class => classname)
else
tag(:li, text, :class => classname + ' disabled')
end
end
def html_container(html)
tag(:ul, html, container_attributes)
end
The previous link is actually present in the html. You can see it by inspecting the page content. Also, you can hover next to 1 and click on the Previous page even though you can't see it.
The styling applied to it - particularly the text-indent: -9999px; is making it not visible. Changing the value of text-indent to 0 or 10 makes it show up.
The culprit styling is from custom.css
.pagenum .previous_page a {
padding-left: 0;
width: 20px;
height: 20px;
background-image: url(/assets/images/listbtn.png) -40px 0px;
display: block;
text-indent: -9999px;
}
On my dev box when I get a backend error it gives me the error message and a stack trace. However on the staging server it just says
"We're sorry, but something went wrong. If you are the application owner check the logs for more information."
So yeah I know I can check the logs. But what if I want to see the stack trace in the browser. Can I temporarily enable that? This is for Rails 4.04 and Ruby 2.1
Yes, you can enable displaying stack trace in your staging environment by settings
config.consider_all_requests_local = true
in your confing/environments/staging.rb file of your Rails app.
When I want to see it only part of the time, I simply set consider_all_requests_local depending on an environment variable, so I can switch it on an off as I need it without changing the application's source code:
config/environments/staging.rb:
config.consider_all_requests_local = !ENV['LOCAL_REQUESTS'].nil?
You can use edariedl's answer, but a better one is to handle the exceptions on the server:
Great tutorial here
--
Setup
#config/environments/staging.rb
config.exceptions_app = ->(env) { ExceptionController.action(:show).call(env) }
Controller
#app/controllers/exception_controller.rb
class ExceptionController < ApplicationController
#Response
respond_to :html, :xml, :json
#Dependencies
before_action :status
#Layout
layout :layout_status
####################
# Action #
####################
#Show
def show
respond_with status: #status
end
####################
# Dependencies #
####################
protected
#Info
def status
#exception = env['action_dispatch.exception']
#status = ActionDispatch::ExceptionWrapper.new(env, #exception).status_code
#response = ActionDispatch::ExceptionWrapper.rescue_responses[#exception.class.name]
end
#Format
def details
#details ||= {}.tap do |h|
I18n.with_options scope: [:exception, :show, #response], exception_name: #exception.class.name, exception_message: #exception.message do |i18n|
h[:name] = i18n.t "#{#exception.class.name.underscore}.title", default: i18n.t(:title, default: #exception.class.name)
h[:message] = i18n.t "#{#exception.class.name.underscore}.description", default: i18n.t(:description, default: #exception.message)
end
end
end
helper_method :details
####################
# Layout #
####################
private
#Layout
def layout_status
#status.to_s == "404" ? "application" : "error"
end
end
Views
#app/views/exception/show.html.haml
.box
%h1
= details[:name]
%p
= details[:message]
#app/views/layouts/error.html.haml
!!!
%html
%head
/Info
= meta_tags
/CSS
:css
html {
height: 100%;
background: #fff;
}
body {
font-family: Helvetica, Arial, Sans-Serif;
font-size: 14px;
}
.error_container {
display: block;
margin: auto;
margin: 10% auto 0 auto;
width: 40%;
}
.error_container .error {
display: block;
text-align: center;
}
.error_container .error img {
display: block;
margin: 0 auto 15px auto;
}
.error_container .message > * {
display: block;
}
.error_container .message strong {
font-weight: bold;
color: #f00;
}
.error_container .contact_info {
display: block;
text-align: center;
margin: 25px 0 0 0;
}
.error_container .contact_info a {
display: inline-block;
margin: 0;
opacity: 0.4;
transition: opacity 0.15s ease;
}
.error_container .contact_info a:hover {
opacity: 0.8;
}
/Body
%body
.error_container
= yield
This, combined with a gem called ExceptionNotification will give you a much more robust exception handling strategy in staging and production
hi just trying to create a qr code in my rails website using sam vincents qr code generator https://github.com/samvincent/rqrcode-rails3....... first i added this code to a controller
class QrcodeController < ApplicationController
def qrcode
respond_to do |format|
format.html
format.svg { render:qrcode => #qrurl, :level => :l, :unit => 10, :color => black }
format.png { render :qrcode => #qrurl }
format.gif { render :qrcode => #qrurl }
format.jpeg { render :qrcode => #qrurl }
end
end
def options
{:qrcode => "http://helloworld.com", size => 4}
end
end
then i am not sure what to add in the view i tried this
<div class="Qrcode qr">
<h2>Qr code</h2>
<p><%= link_to "SVG", Qrcode_path("svg") %></p>
<p><%= link_to "PNG", Qrcode_path("png") %></p>
<p><%= link_to "JPEG", Qrcode_path("jpeg") %></p>
<p><%= link_to "GIF", Qrcode_path("gif") %></p>
would appreciate any help on how it works as their are not that many instructions online
im using ruby 1.9.3 and rails 4.0.1
I'm using rqrcode gem. It's pretty simple and you don't need to generate images for your qrcodes. The code is generated using tables and some css styles...
You can use this helper: /helpers/qrcode_helper.rb
module QrcodeHelper
require 'rqrcode'
def render_qr_code text, size = 3
return if text.to_s.empty?
qr = RQRCode::QRCode.new(text)
sizeStyle = "width: #{size}px; height: #{size}px;"
content_tag :table, class: "qrcode pull-right" do
qr.modules.each_index do |x|
concat(content_tag(:tr) do
qr.modules.each_index do |y|
color = qr.dark?(x, y) ? 'black' : 'white'
concat content_tag(:td, nil, class: color, style: sizeStyle)
end
end)
end
end
end
end
Into your view some_view.html.erb
<%= render_qr_code("MYCODE") %>
And you need to add style for your code qrcode.css.less
table.qrcode {
border-width: 0;
border-style: none;
border-color: #0000ff;
border-collapse: collapse;
margin-top: 100px;
margin-bottom: 12px;
td {
border-width: 0;
border-style: none;
border-color: #0000ff;
border-collapse: collapse;
padding: 0;
margin: 0;
width: 3px;
height: 3px;
&.black {
background-color: #000 !important
}
&.white {
background-color: #fff !important
}
}
}
My example it's working with Rails 3.
Here's a minimal example that avoids the need to write code to render each (x,y) "pixel" yourself.
In your controller:
def show
#qr = RQRCode::QRCode.new('https://stackoverflow.com/')
end
And then in your corresponding .html.erb view:
<p>
<%== #qr.as_svg %>
</p>
(Note that this uses <%==, not <%=; if you use the latter, you'll get the raw SVG XML source instead of a rendered SVG image.)
See the RQRCode docs for options that can be passed to .as_svg, and for other output options such as PNG.
I am still new to all HAML and CSS and currentyly working on this part of a code:
- my_provider_list.each do |provider|
%li{ class: 'group_classes group-list-colspace', id: provider.name }
= provider.name
%span{class: 'group-list'}= provider.value
%span{class: 'group-list'}= provider.specialty
So I borrowed from other parts of the code we had and got it to that point seen in the picture below
But I can't figure out how to make some space between the values it shows for specialty and the cost value
What do you suggest to add or modify to get that part fixed?
Here is the generate code with html space added:
<li class='group_classes group-list-colspace' id='Physician 2679'>
Physician 2679
<span class='group-list'>218395</span>
<span class='group-list'>Pediatrics</span>
</li>
And the CSS for list items is just this:
.group-list {
float:right;
}
I would add some classes to the spans and increase the padding of one of them for example
- my_provider_list.each do |provider|
%li{ class: 'group_classes group-list-colspace', id: provider.name }
= provider.name
%span{class: 'group-list-value group-list'}= provider.value
%span{class: 'group-list-specialty group-list'}= provider.specialty
Then add some style padding to specialty
.group-list-specialty {
padding-left: 20px;
}
spans by there very nature are inline and are meant to be used inside of block elements and not to default to having any space around them. You can easily get around this with padding.
Likewise, if you are using rails consider putting the provider list into a separate partial
just add a non breakable space between them:
- my_provider_list.each do |provider|
%li{ class: 'group_classes group-list-colspace', id: provider.name }
= provider.name
%span{class: 'group-list'}= provider.value
%span{class: 'group-list'}= provider.specialty