How can I generate a link/route/path in Roda? - ruby

How can I generate a path or URL in a view in Roda?
Will I need to use a plugin? If not, how else? Or will I have to hard-code urls/paths?
In Rails I'd do this way:
<%= home_about_path %>

To just generate urls based on set semantics, you want the path plugin.
Usage looks something like this:
App < Roda
plugin :path
path :post do |post|
"/blog/#{post.id}"
end
end
And then, in your templates similarly to how you would use something_path in Rails:
<%= #post.title %>

Related

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

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')

Check if a file exists with puppet template

I try to check if a file exists on client who run puppet agent.
On my puppet master, I have a template.erb like this :
<% if File.exists?('/usr/bin/lwp-request') %>SCRIPTWHITELIST="/usr/bin/lwp-request"<% end %>
This little code in my template is needed to my rkhunter module.
The result is always false, however the file exists.
If I add the file on the puppet master, the result is true. So the ruby code seems to be executed on the master.
How can I check on my template if a file exists on client ?
Tested on puppet 2.7.5 and 2.8.1.
Thanks
The only information you have about the node when compiling manifests and templates are Facts that are sent by the node when requesting a catalog.
If you need additional information from the node, then you need to add a Custom Fact that retrieves the information you need (like whether or not a file exists). You can then use the custom fact inside of templates.
Within a Puppet module create a custom fact lib/facter/lwp.rb:
Facter.add(:lwp_request_exists) do
setcode do
File.exists?('/usr/bin/lwp-request')
end
end
then within erb template use something like:
<% if $::lwp_request_exists -%>
some code...
<% end -%>

How to know in what environment the code runs?

In the layout file of haml I would like to determine whether we are in our development and build environments. We're using Middleman.
I would like to do something like this:
- if environment == 'development'
/ Development Code
= javascript_include_tag "Dev.js"
I tried to access Ruby's environment variable, as well as define a custom variable in the config.rb file with no success.
You’ve almost got it right – you need to check against a symbol rather than a string:
- if environment == :development
/ Development Code
= javascript_include_tag "Dev.js"
Middleman also adds the development? and build? methods which may be easier to use:
- if development?
/ Development Code
= javascript_include_tag "Dev.js"
This works with ERB too:
<% if development? %>
<!-- Development Code -->
<%= javascript_include_tag "Dev.js" %>
<% end %>
First, if possible, you should separate the logic from the data. Determine your environment in your controller, and toggle the data being sent to the view (HAML layout file).
Typically you'd set a flag in your environment variables and access it in your code from ENV. For instance, Sinatra makes the development/test/production setting available inside the code using their RACK_ENV symbol:
:environment - configuration/deployment environment A symbol
specifying the deployment environment; typically set to one of
:development, :test, or :production. The :environment defaults to the
value of the RACK_ENV environment variable (ENV['RACK_ENV']), or
:development when no RACK_ENV environment variable is set.
The environment can be set explicitly:
set :environment, :production
If you have to roll your own, that's a good way to go about it.
Use the :environment symbol that middleman creates by default:
http://rubydoc.info/github/middleman/middleman/Middleman/Application#environment-instance_method
combined with haml - you can do something like:
= javascript_include_tag "Dev.js" unless :environment == "developement"
note that middlemans build process changes the :environment value to "build"
you can also use developement? to test whether you're on dev or not: http://rubydoc.info/github/middleman/middleman/Middleman/Application#development%3F-instance_method
All the above applies to middleman 3.0.6 and might not work on lesser versions (won't work on 2.x for sure)

Get Current Path of Page in Middleman Layout File

Is it possible to retrieve the current path of a page in a middleman file? For instance, if I have a layout file layout.erb with something like the following:
<%= page.path %>
<%= yield %>
and a test file index.html:
Testing
then when Middleman rendered the page I would get something like:
/index.html
Testing
Middleman also provides the current_page variable. current_page.path is the source path of this resource (relative to the source directory, without template extensions) and current_page.url is the path without the directory index (so foo/index.html becomes just foo).
<%= current_page.path %>
# -> index.html
<%= current_page.url %>
# -> /
Details from Middleman's Middleman::Sitemap::Resource rubydoc.
http://rubydoc.info/github/middleman/middleman/Middleman/Sitemap/Resource
The solution is:
<%= request.path %>

Can't get asset pipeline working

I can't get the asset pipeline working for some reason. I get a 404 on request to both application.css and application.js. I'm using rails 3.1.0.rc6. Nothing special, just created a new project.
In my layout file:
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
It's linking to /stylesheets/application.css and same with the js file. I'm using pow but also tried starting the server with webrick.
Any ideas?
Note: using sprockets 2.0.0.beta.15
Figured it out. I had to make an explicit require call to "sprockets/railtie"
You might find that adding
config.serve_static_assets = true
to your config/environments/xxx.rb will help.
To be more exact, add this to your Gemfile
gem 'sprockets/railtie'

Resources