Please explain me this ruby code


#1

Hi all,
This is small code from typo 2.6.0’s “articles_helper.rb” file

def author_link(article)
if config[‘link_to_author’] and article.user and
article.user.email.to_s.size>0
“<a
href=“mailto:#{article.user.email}”>#{article.user.name}”
elsif article.user and article.user.name.to_s.size>0
article.user.name
else
article.author
end
end

In above code What is the meaning of “config[‘link_to_author’]”???
What is “config” here??
Please tell me.

Thanx in advance.
Prash


#2

In above code What is the meaning of “config[‘link_to_author’]”???
What is “config” here??
Please tell me.

Well, it looks like a hash name config. I’ve never used Typo, but I
would venture to guess that it allows you to set some preferences
which get stored in a hash named config.

-Derrick S.


#3

Yeah, basically it’s looking up a configuration setting in the admin
section that says, “does this user want to allow a link to their
email.” If the setting is set, then the anchor html is written out
otherwise the just the author’s name is written out.

Michael


#4

Hello Prashant,

This is small code from typo 2.6.0’s “articles_helper.rb” file

def author_link(article)
if config[‘link_to_author’] and article.user and
article.user.email.to_s.size>0
[…]

In above code What is the meaning of “config[‘link_to_author’]”???
What is “config” here??

config is a method that returns the value of the global variable
$config.
(it hides implementation, just like params() gives access to @params)
$config holds an Configuration instance. The Configuration class derives
from ConfigManager class that defines a [] method. So $config inherits
a [] method and can be seen as a hash-like object (not really, because
[]= method is not implemented). config[key] is a way to access
to default settings (by default config[‘link_to_author’] is false).

All details are in app/models/configuration.rb and
app/models/config_manager.rb

-- Jean-François.