Martí Bosch Martí Bosch - 4 years ago 138
Ruby Question

devise custom user attribute acessors

I'm new to rails and I'm trying to customize the authetication.

I've done a custom confirmation mail view

app/views/devise/mailer/confirmation_instructions.html.erb
as follows:

<p>Welcome <%= @email %>!</p>
<p>
Thanks for registering. You have registered through <%= @resource.provider %>.
The account data has been created automatically as follows:
</p>
<ul>
<li><strong>User Name:</strong> <%= @resource.name %></li>
<li><strong>Password:</strong> <%= @resource.password %></li>
</ul>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>


The
@resource.provider
returns a string such as
'facebook'
,
'twitter'
,
'github'
.

If I wanted to customize these strings, let's say print instead
'Facebook'
,
'Twitter'
,
'GitHub'
…, how should I do it? My first idea is a method

def print_cool_provider(provider)
if provider == 'facebook'
return 'Facebook'
else if ...
...
end
end


I consider a finite short list of possible providers that I can handle one by one. My real questions go more with MVC, Rails and Devise:

Where should I define this method to respect the MVC pattern and at the same time be able to call it from the
confirmation_instructions.html.erb
template?

Answer Source

You could add a simple helper, like so:

# app/helpers/provider_helper.rb
module ProviderHelper

  # only special cases, where #titlecase won't suffice
  PROVIDER_NAMES = {
    'github'        => 'GitHub',
    'google_oauth2' => 'Google'
  }

  def provider_name(user)
    PROVIDER_NAMES[user.provider] || user.provider.titlecase
  end
end

A better approach would be a presenter (or decorator), though.


N.B.: I would not submit the password via email, unless it is encrypted.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download