While working on one of rails applications, as I usually tend to check whether user is logged in, specially in views as follows
<% if @current_user.nil? %> <a href='url to login page'> <% else %> <a href='some url where logged in user have access'> <% end %>
And over some period I found this pattern repeating everywhere.
I thought of DRYing it, I wanted to have a kind of before_filter for every link that appear on the page, every link should go for login if that page or feature require user entity. and this should be handled by a common thing, similar to before_filter: login_require, on server side. I intend to achieve similar in views. A while back when I read up about html5 custom data attribute, and unobtrusive javascript etc, referring to them, I got one idea as described below, though it is not something that I am making use of custom data attribute exactly, but I found this idea useful and working. May be someone would have good suggestions here for me.
I will put any link which require user login as follows
<a href="/feature/page/" login-require >Feature</a>
And then I will have my after_load.js which will have the following event binding
jQuery('a[login-require]').bind('click', function() { <% if @current_user.nil? %> openOverlay('overlay-login'); return false; <% else %> return true; <% end %> });
You should use a HTML 5 compatible attribute, like data-login-required=”true”
Agree, I will do it. But given me these two choices, I would prefer just to have “login-require” for reasons like it is more clear and simple, straight forward… and no need to use words like data, true. And I thought ‘something=”true”‘ is same as ‘something’.
If you don’t use the ‘data’ this HTML is invalid…
Why not use a simple css class and make it also compatible with HTML4/XHTML?
…
jQuery(‘a.login-required’).bind
what happen if the user disable javascript in his browser?
User will not be able to use 99% of sites currently in internet :)
link will work as usual, and will hit the server, server side check will take care of this
before_filter: login_require
If the server will handle it anyway, you’re not really DRYing anything. In fact, you’re doing the opposite. You’re CRYing (Code & Repeat Yourself :D).
Another suggestion: Unconditionally link to the restricted page, and simply redirect anonymous users to the sign-in page when they try to access a restricted page. This of course only works if you have the same link text for all users.
I wanted to avoid server trip ….
Hm, I see. I’m not sure if it’s worth the extra hassle, to be honest. Redirects are very very fast, since no HTML is loaded. Google for instance has lots of redirects every time you log in. If it’s good enough for them, it’s good enough for me. So I guess I’d just avoid doing the `if current_user` thing in the first place.
A nice semantic approach could be to use the rel attribute like this:
Feature
The rel attribute is intended to contain information about the kind of relationship to the link’s destination (http://www.w3.org/TR/html4/struct/links.html#h-12.1.2). So in my opinion this is a good use case for it.
Just modify your jQuery selector to
jQuery(‘a[rel~=required]’)
and your solution wil work with this approach!
Sorry, my code was interpreted as html. Once again:
<a href=”/feature/page/” rel=”restricted”>Feature</a>