Is it possible to use gem faker from my yml file? - ruby

My great desire is to use the gem faker inside the yml file. I know this way is wrong, but is it possible to do it?
see my code:
------yml file -----
:usuarios:
:ncpf: Faker::Number.number
:birth Faker::Date.birthday
---my page (site-prism)-----
def new_user
cpf.set(DADOS[:users][:ncpf])
dt_birth.set(DADOS[:users][:birth])
end

Out of the box, this is not possible with plain YAML. But when you run your YAML file through ERB before parsing then you can do that.
Change your YAML to
:users:
:ncpf: <%= Faker::Number.number %>
:birth: <%= Faker::Date.birthday %>
and read the file like this
require 'erb'
require 'json'
file = File.read('path/filename.yml')
yaml = ERB.new(file).result
DADOS = YAML.load(yaml)
Btw this is what Rails does internally with configuration files. So when you are using Rails then you can use this simplified version to load that file
DADOS = ActiveSupport::ConfigurationFile.parse('path/filename.yml')

Related

Rails wicked_pdf, convert page as pdf

I have a normal Rails layout which is my normal Site.
I want a Button on this page to convert/transform this page into
a PDF file.
I want to do it with this gem:
https://github.com/mileszs/wicked_pdf
I added gem 'wicked_pdf' to my Gemfile, but now how can I convert the actual page?
Here's what I did to use wicked_pdf:
# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary' # if you need the binary too
Then run
$ bundle install
$ (bundle exec) rails g wicked_pdf # generates the initializer
Then you can configure global settings in config/initializers/wicked_pdf.rb as stated in the wicked readme on github.
Rails 4 already knows the pdf mime type, but in older versions you'll probably need to register pdf as a mime type:
# config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
In your controller
respond_to do |format|
format.html
format.pdf do
render pdf: "your-filename"
end
end
Then you put a view under app/views/<your-view>/<controller-action>.pdf.erb with the content you wan't.
To reference this by a link you can do something like:
= link_to "Get PDF", your_path(format: :pdf)
Does this solve your question?
Please follow the below link. Its a details tutorial, this will make your life easier:
http://dchua.com/2014/10/30/generate-pdfs-with-html-templates-in-rails/
Note
If you don't want to have your pdf views separately and want to use the same html view, then please use
template: invoices/show.html.erb
instead of
template: invoices/show.pdf.erb

ActiveRecord Schema Dump without rails

In rails you can setup a rails app, assign the right db driver (I need firebird/fb) and then do a rake db:schema:dump pretty much out of the box.
I'm trying to do a version control for my database schema. How can I just make a ruby script that requires activerecord and fb libraries and achieve the same thing. I dont' need an entire rails app. All I want is a consistent script to extract the schema.
Looking at the source of the db:schema:dump task, the following code should get you started:
require 'active_record'
require 'active_record/schema_dumper'
require 'activerecord-fb-adapter'
filename = './schema.rb'
ActiveRecord::Base.establish_connection(
adapter: 'fb',
database: 'db/development.fdb',
username: 'SYSDBA',
password: 'masterkey',
host: 'localhost',
encoding: 'UTF-8',
create: true
)
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
I just happen to have been working on something similar except for the firebird/fb part.
require "yaml"
require "active_record"
include ActiveRecord
include ActiveRecord::Tasks
ActiveRecord::Migrator.migrations_path='./db/migrate'
DatabaseTasks.db_dir = './db'
db_config_file = "./config/database.yml"
db_config = YAML.load_file(db_config_file)
db_type = 'development'
db_object = db_config[db_type]
#sldbtask = SQLiteDatabaseTasks.new(db_object, './')
unless File.exist?(db_object['database'])
#sldbtask.create
#sldbtask.connection
# try different migation versions
migration_version = 0
ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_path, migration_version)
end
unless File.exist?('./db/schema.rb')
#DatabaseTasks.check_schema_file('./db/schema.rb')
File.open('./db/schema.rb', "w:utf-8") do |file|
#sldbtask.establish_connection(db_object)
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
Of course my adapter is different but it could be modified and it's versatile with a Rails app so you could drop the code part into the root directory. But of course you can rake for that but this just shows that it works. Tou might nave problems with the migration version though. I haven't thoroughly tested it.

How can I require another .rb file in my rhtml file?

In my rhtml file, I want to require another ruby file in the same directory. So I tried:
<% require_relative 'another.rb' %>
but I got this error in my log:
(erb):2:in `require_relative': cannot infer basepath (LoadError)
Then I tried to use:
<% require './another.rb' %>
And it gives error saying:
/usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- ./views/another.rb (LoadError)
Then I tried to see what is the base path by doing:
puts File.absolute_path(__FILE__)
And it returns:
(erb)
How can I require another file using relative path? I do not want to use absoulute path.
As you can see from the error, the current path is views (app/views). If the file you want is in your app's lib directory, you could do:
<% require_relative '../../lib/another' %>
You don't need .rb.
This is a fairly bad idea, though; you shouldn't be requiring things from inside views. If you're trying to access some sort of view-related functionality, you can require it in your controller; all controller actions are accessible to views. Going this route, it's best to wrap the functionality in a controller method.
You could also put the functionality into a helper; that's what helpers are for. See app/helpers/application_helper.rb for a good starting point, or make one specific to the view. See the view helper documentation.
A very unhealthy way is to add '.' to the load path
$LOAD_PATH << '.'
then you can use
<% require './another.rb' %>
The most usual way I believe that is
require File.join(File.dirname(__FILE__), 'path', 'to', '..', 'file')
If you consider that you are only using a constant and a relative path, I believe it is correct to say that this path is relative.

Sinatra HAML Heroku in-file templates

I'm following a tutorial from http://ruby.about.com/od/sinatra/a/sinatra7_2.htm however I'm having a few problems running the app within my own environment.
The problem is that the following line:
haml :list, :locals => { :cs => Contact.all }
results in a "No such file or directory - [...]/views/list.haml"
The HAML template is within the file, and terminated by:
__END__
## layout
however ruby seems to be looking in the views/ directory for the Haml files.
Is this tutorial missing a call to force ruby to look inside the file, or this resource suggests that in-file templates are broken for version 1.9.2.
I'm using sinatra version 1.1.2 and ruby 1.8.7.
I can't reproduce with Sinatra 1.1.2 and Ruby 1.9.2.
So something like this (sinatrarb.com) example generates the error?
require 'sinatra'
get '/' do
haml :index
end
__END__
## layout
%html
= yield
## index
%div.title Hello world!!!!!
calling a list template you will also need add it to the end of your file:
require 'sinatra'
get '/' do
haml :list
end
__END__
## layout
%html
= yield
## list
%div.title the LIST

Can I use Ruby in-built RSS module to read atom feed?

I am in an environment where I don't have access to install any gems. I only have standard ruby (version:1.8.7) installation.
I am trying something like this:
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
source = "http://www.example.com/feed.atom" # url or local file
content = "" # raw content of rss feed will be loaded here
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)
When I am parsing the content, I am getting nil. So I am wondering if in-built RSS module supports parsing an atom feed.
If you look under RSS::Maker what it can parse.
As an alternative, consider trying the nokogiri gem.

Resources