ruby on rails qr code implemetation - ruby-on-rails

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.

Related

Active scffold + paperclip, display document from S3

I'm using s3 to host my images and active scaffold gem to deal with the admin part of my app. I'm currently facing the following problem:
Considering the following show action:
def show
#record = find_if_allowed(params[:id], :read)
do_show
send_data(#record.document_file_content)
end
I can't display my image correctly, clicking the show of a s3 image results in weird encrypted characters such as:
�PNG IHDRVO �iCCPICC ProfileH��WXS��[R -)�w�W�#l�$#(��]YTp�"���
��ņ]Y{XPYY6Tޤ���}��ͽ?g�9�s�#ц����*��/��2��S��G�*�(���|���pe��wyw�B�j%�����U�9\!$�4����!puv��B��*��!�U�
D\�3�X]�Ӥ�Rb��7d*�%�#A̛Y�΀q�m���=ٙ,�� ���̓X�...
Since the bucket is configured correctly, I assume it must be a way to display this content as an image tag. I tried overwritting the show view for this controller but was unsuccessful.
Have you run into this problem before? If yes, what would be the best option for me?
Kind regards
I had to make an Helper such as:
module Admin::DocumentHelper
def document_document_column(record, opts={})
str = ""
if record.document.file?
if record.document_content_type.match(/pdf/)
str << content_tag(:iframe, record.document.original_filename, style: 'border:none;', width: 400, height: 250, src: admin_document_path(record, :style => :original, disposition: :inline) )
else
str << content_tag(:div, class: 'img-document', style: "display: table-cell; vertical-align: middle; cursor: pointer; width: 400px; height: 400px; overflow-x: hidden; overflow-y:auto") do
image_tag(admin_document_path(record, :style => :original, disposition: :inline), style: 'border:none;max-height: 400px; width:400px')
end
end
str << link_to("download", admin_document_path(record, :style => :original, disposition: :inline), target: "_blank")
end
str.html_safe
end
end

Will_paginate prev next link unexpected behavior

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;
}

rails 4 foundation 5 slim-lang flash messages helper with devise

I was not able to find many sources that matched my needs out there so I wanted to post this for fellow hackers to chop up and make a bit better. The goal of this helper, was to allow both a list of messages or a single message to be passed to it. This gave me the ability to add all active record failures when saving to the model, or just a generic message any where in my application. I also use rwz/nestive for multiple layouts, and slim-lang for my templates.
Let's start with my flash message partial that allows me to pass a column count to it:
- if flash.present?
div[class="row"]
div[class="large-#{column_count} columns"]
= display_flash_messages(flash)
Which can be called like so:
= render partial: 'partials/flash_message', locals: { column_count: '12'}
The helper code is as follows. Note, I also embed a fontawesome icon before the messages:
module FlashHelper
DEFAULT_KEY_MATCHING = {
alert: {
type: 'alert',
i_class: 'fa fa-exclamation-triangle'
},
notice: {
type: 'success',
i_class: 'fa fa-check'
},
info: {
type: 'standard',
i_class: 'fa fa-check'
},
secondary: {
type: 'secondary',
i_class: 'fa fa-check'
},
success: {
type: 'success',
i_class: 'fa fa-ban'
},
error: {
type: 'alert',
i_class: 'fa fa-exclamation-triangle'
}
}
def display_flash_messages(flash)
html_output = ''
flash.each do |type, message|
key = DEFAULT_KEY_MATCHING[type.to_sym]
alert_wrapper_class = ('alert-box-multiple-wrapper' if message.is_a? Array) || 'alert-box-single-wrapper'
html_output +=
content_tag(:div, class: "alert-box #{key[:type]}", data: { alert: '' } ) do
if message.is_a? Array
html_output_nested =
content_tag(:ul, class: 'alert-box-list') do
message.to_a.map do |list_message|
content_tag(:li, list_message.humanize + '.', class: 'alert-box-li-item')
end.reduce(:<<)
end
else
html_output_nested = content_tag(:span, message.humanize + '.', class: 'alert-box-span-item')
end
html_output_nested = content_tag( :div, html_output_nested, class: "#{alert_wrapper_class}" )
html_output_nested = content_tag(:i, nil, class: "#{key[:i_class]}") + html_output_nested
html_output_nested += link_to('×'.html_safe, '#', class: 'close')
html_output_nested
end
end
raw(html_output)
end
end
The associated SASS:
div.alert-box {
.close {
color: $app-color-code-5;
#include opacity();
}
i {
font-size: 1.3em;
padding: 9px 6px 4px;
line-height: 0;
position: absolute;
top: 50%;
left: 0.5rem;
color: $app-color-code-5;
margin-top: -0.6875rem;
}
div.alert-box-single-wrapper {
margin-left: 25px;
.alert-box-span-item {
font-size: 1em;
}
}
div.alert-box-multiple-wrapper {
ul {
margin-left: 3.7rem;
list-style-type: disc;
margin-bottom: 0;
}
.alert-box-li-item {
font-size: 1em;
}
}
}
In my devise setup, I am now able to use the following. Yes I override the default devise configuration:
if resource_saved
....
else
# merge the ar errors together with devise
if resource.errors.messages.values.flatten.length > 0
flash[:error] = resource.errors.messages.values.flatten
end
# remove passwords from the view layer
clean_up_passwords resource
respond_with resource
end
At the end of the day, this setup builds 2 different sets of html:
<div class="alert-box alert data-alert">
<i class="fa fa-exclamation-triangle"></i>
<div class="alert-box-multiple-wrapper">
<ul class="alert-box-list">
<li class="alert-box-item">The error message.</li>
</ul>
</div>
<a class="close" href="#">×</a>
</div>
<div class="alert-box alert data-alert">
<i class="fa fa-exclamation-triangle"></i>
<div class="alert-box-single-wrapper">
<span class="alert-box-span-item">The error message.</span>
</div>
<a class="close" href="#">×</a>
</div>
Now, all of this works, but I am wondering how others have tackled this problem.

Can you interpolate ruby variables within HAML css?

I need a series of classes, .module1, .module2, ... module(n).
I also want to define those classes using css, ruby and HAML:
:css
.mod1 {
background-image: url("#{extra.image}");
}
Is it possible to interpolate ruby variables to save work?
.module.mod"#{extra.id}"
%h3 #{extra.title}
%p #{extra.description}
%a.btn-default{:href => "#", :target => "_top"} enter now
:css
.mod#{extra.id} {
background-image: url("#{extra.image}");
}
According to the HAML_REFERENCE I used this method:
- flavor = "raspberry"
#content
:textile
I *really* prefer _#{h flavor}_ jam.
to interpolate variables after :css
.module.modone{:class => "#{cycle("mod_#{extra.id}_")}"}
%h3 #{extra.title}
%p #{extra.description}
%a.btn-default{:href => "#", :target => "_top"} enter now
:css
.mod_#{extra.id}_ {
background-image: url("#{extra.image}");
background-color: #4073DF;
}
:css
.mod_#{extra.id}_ {
background-image: url("#{extra.image}");
height: #{ruby_height}#{measure_unit_ruby_variable};
}

How can I iterate through a CSS class' subclasses?

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}"

Resources