Generate RGhost bar code inline (stream) in Ruby - ruby

I'm using RGhost-Rails to generate bar codes on a pdf file (with wicked-pdf).
I've the following code (in Ruby on Rails 3)
<% doc=RGhost::Document.new :paper => [7,2]%>
<% doc.barcode_code39('1234567', :text => {:size => 10, :offset => [0,-10], :enable => [:text, :check, :checkintext] }) %>
<% r= doc.render :png, :resolution => 100, :debug => true, :filename => "./public/pdf/rghost_barcode.png" %>
This code works fine but i want to generate my image inline (without worrying with the filename and filepath). Something like (i guess):
<% doc=RGhost::Document.new :paper => [7,2]%>
<% doc.barcode_code39('1234567', :text => {:size => 10, :offset => [0,-10], :enable => [:text, :check, :checkintext] }) %>
<% r= doc.render_stream :png, :filename => "./public/pdf/test.png" %>
<img src="<%= RGhost inline functionality/ r.stream_Object_String? %>" width="16" height="335" alt="asd">
In this way, i wouldn't have to worry about deleting the (generated) code bar files after showing the pdf.
There is any way to do this? If not, there's other gems with these functionality (please note that i also need to show the code bar text - some gems doesn't have this).

Apparently the only solution (not the one i'm looking for) is this

Related

Rendering a partial with locals

I've rendered a partial that also loads the Users latest post in one of the tab panels.
<div role="tabpanel" class="tab-pane" id="updates_panel">
<%= render :partial => "pages/update_panel", :locals => { :post => #user.latest_post } %>
</div>
At first the method latest_post was to display the users "Last or Latest Post".
def latest_post
posts.order(:created_at => :desc).first
end
But now I realized I need to display all the post in descending order and I cant seem to get this right.
create another method like latest_posts,
def latest_posts
posts.order(:created_at => :desc)
end
def latest_post
latest_posts.first
end
And now use this in your partal,
<%= render :partial => "pages/update_panel", :locals => { :post => #user.latest_post, :posts => #user.latest_posts } %>

Storing rails code in database. Upgraded from Rails 2 to Rails 3

I have the following piece of code illustrated as below. Was working in my Rails 2 application but having upgraded to Rails 3 it not longer works.
When I render this code, I'm getting like,
<%= radio_button <%= text_field "submission_data", :size => 40 %>
"submission_data", "first_application", "team_lead" %>
Here is my code in view file,
<%= form_for #submission, :url => {:action => 'update', :id => #submission},
:html => {:id => "submission_form", :name => 'submission_form_name'} do |f|%>
<%= render :inline => #submission.cycle.seeker_form,
:locals => { :submission_data => #submission_data } %>
We're using pg as a back-end support.

Rails 3, windows, Paperclip can't make thumbnails

So I am using windows 7, Rails 3, latest paperclip gem and ImageMagick-6.7.7-Q16 (tested in cmd), my PATH environment is updated.
Model
class Image < ActiveRecord::Base
attr_accessible :description, :user_id, :file
has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :storage => :filesystem
belongs_to :user
#validations
validates_attachment_presence :file
validates_attachment_size :file, :less_than => 4.megabytes
validates_attachment_content_type :file, :content_type => [ 'image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg' ]
end
Form
<%= form_for(#image, :html => { :multipart => true }) do |image| %>
<div class="control-group">
<%= image.label :description, "Description", :class => 'control-label' %>
<div class="controls">
<div class="input-prepend">
<%= image.text_field :description %>
</div>
</div>
</div>
<div class="control-group">
<%= image.label :file, "Image", :class => 'control-label' %>
<div class="controls">
<div class="input-prepend">
<%= image.file_field :file %>
</div>
</div>
</div>
<%= image.hidden_field :user_id, :value => current_user.id %>
<div class="actions">
<%= image.submit "Upload Image", :class => 'btn btn-primary btn-medium' %>
</div>
<% end %>
My paperclip.rb in initializers
require "paperclip"
Paperclip.options[:command_path] = 'C:\Program Files (x86)\ImageMagick-6.7.7-Q16'
Paperclip.options[:swallow_stderr] = false
Paperclip.options[:whiny_thumbnails] = true
Everything is working fine without cropping (:styles => { :medium => "300x300>", :thumb => "100x100>" }). But when I want to make thumbnails Paperclip throws this error
Command :: identify -format %wx%h "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw5.JPG[0]"
Command :: convert "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw5.JPG[0]" -resize "300x300>" "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw520120519-7696-1p8rcsr"
[paperclip] An error was received while processing: #<Paperclip::Error: There was an error processing the thumbnail for DSCN630520120519-7696-18l3nw5>
Command :: identify -format %wx%h "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw5.JPG[0]"
Command :: convert "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw5.JPG[0]" -resize "100x100>" "C:/Users/Zaraka/AppData/Local/Temp/DSCN630520120519-7696-18l3nw520120519-7696-tx3bmo"
[paperclip] An error was received while processing: #<Paperclip::Error: There was an error processing the thumbnail for DSCN630520120519-7696-18l3nw5>
I figured out that uploaded image isn't even in temp folder, so those created images just have 0 bytes. I'm completely stuck here dunno if something wrong is with paperclip or image upload.
Any help will be appreciated
This probably means you're not pointing to IM properly. Either you don't have it installed yet or just not finding it as expected. Use double backslashes instead: 'C:\\Program Files (x86)\\ImageMagick-6.7.7-Q16'. (In order to avoid white spaces you can also use 8.3 filenames system to identify the path with regular slashes.)
If you're using the last version of Paperclip (3.1.4), on Windows I recommend you to install File package from GnuWin32. Please see this post: https://github.com/thoughtbot/paperclip/issues/926.
Finally, in order to support non-English characters in filenames, you might want to monkey patch Cocaine. I put a cocaine_path.rb between my initializers. Be aware that 'iso-8859-1' might not be the proper encoding for you.
cocaine_pacth.rb:
if RUBY_PLATFORM == "i386-mingw32"
module Cocaine
class CommandLine
def run
output = ''
begin
with_modified_path do
#logger.info("\e[32mCommand\e[0m :: #{command}") if #logger
ec = Encoding::Converter.new("utf-8", "iso-8859-1")
output = self.class.send(:'`', ec.convert(command.encode('UTF-8')))
end
rescue Errno::ENOENT
raise Cocaine::CommandNotFoundError
end
if $?.exitstatus == 127
raise Cocaine::CommandNotFoundError
end
unless #expected_outcodes.include?($?.exitstatus)
raise Cocaine::ExitStatusError, "Command '#{command}' returned #{$?.exitstatus}. Expected #{#expected_outcodes.join(", ")}"
end
output
end
end
end
end
This covers all the issues I have had with Paperclip on Windows in the last days. I hope it helps.

How to write a custom SimpleForm Builder to replace <INPUT> by <P>?

Extract of my Gemfile:
gem 'rails', '3.0.3'
gem 'inherited_resources', '1.2.1'
gem 'simple_form', '1.4.0'
For any resource, I have 1 view for the 3 actions (new, edit & show). Example:
<h1><%= I18n.t('admin.form.'+action_name.downcase, :name => controller_friendly_name) %></h1>
<%= simple_form_for([:admin, resource]) do |f| %>
<%= render "admin/shared/errors" %>
<%= f.input :title,
:label => "Title",
:hint => I18n.t('admin.form.input.title.hint', :name => controller_friendly_name),
:required => true,
:error => false,
:input_html => { :class => :large, :placeholder => I18n.t('admin.form.input.title.placeholder', :name => controller_friendly_name) }
%>
<%= f.input :is_visible,
:as => :radio,
:label => "Visible",
:error => false,
:required => true,
:collection => [['Yes', true], ['No', false]],
:wrapper_class => 'checkboxes-and-radiobuttons',
:checked => true
%>
<%= render "admin/shared/validation", :f => f %>
<% end %>
<% init_javascript "MyApplication.Form.disable();" if [:show].include?(action_name.to_sym) %>
See how the #show action set all the fields to disabled ? This is ugly.
Consider I can't refactor the views to have a show.html.erb file.
What I want to do:
When the action is #show, the simple_form builder use a custom builder wich replace <input>, <textarea>, <select> by <p> html tag, with the value.
Furthermore, I will customise the radiobuttons, checkboxes to.
My first step:
# app/inputs/showvalue_input.rb
class ShowvalueInput < SimpleForm::Inputs::Base
def input
# how to change 'text_field' by <p> container ?
#builder.text_field(attribute_name, input_html_options)
end
end
Can't find the way to do it. Custom Form Builders or Custom Inputs (with monkey patching) ?
Thank for the help !
Here's my solution
in my application_helper.rb:
def set_show_method_to_builder(builder)
builder.instance_eval <<-EVAL
def show?
#{action_name == "show"}
end
EVAL
end
In my forms (in the simple_form block):
<%- set_show_method_to_builder(f) -%>
And finally, in #app/inputs/string_input.rb:
class StringInput < SimpleForm::Inputs::StringInput
def input
if #builder.show?
content_tag(:p, #builder.object[attribute_name], :class => :show)
else
super
end
end
end
There's some problem with data types not mapped, but it's another story:
Can't create Custom inputs for some (Text, Booleans, ...) types, with SimpleForm

Rails 3 - Category Filter Using Select - Load Partial Via Ajax

I am trying to filter Client Comments by using a select and rendering it in a partial. Right now the partial loads #client.comments. I have a Category model with a Categorizations join. This all works, just need to know how to get the select to call the filter action and load the partial with ajax. Thanks for you help.
Categories controller:
def filter_category
#categories = Category.all
respond_to do |format|
format.js # filter.rjs
end
end
filter.js.erb:
page.replace_html 'client-note-inner',
:partial => 'comments',
:locals => { :com => Category.first.comments }
show.html.erb (clients)
<% form_tag(filter_category_path(:id), :method => :put, :class => 'categories', :remote => true, :controller => 'categoires', :action => 'filter') do %>
<label>Categories</label>
<%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, #category_id)) %>
<% end %>
<div class="client-note-inner">
<%= render :partial => 'comments', :locals => { :com => #comments } %>
</div><!--end client-note-inner-->
Hope that makes sense.
Cheers.
It's straightforward with a simple onchange
<%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, #category_id), onchange => 'form.submit()') %>

Resources