Ruby rails - list active sessions - ruby

Is there any way to get the list of all active sessions in rails? I have memory cached session list, and I want to view the user ids, source ip, time stamp, duration etc. for basic administration purposes.

To expand on my comment, I am doing something similar to what you want to do. I am using authlogic but I imagine that you can do something equivalent with other authentication packages.
I am using authlogic's auto logout capabilities and have it set to log people out after 120 minutes. Knowing that, generating a list is simple. I just do the following:
<% User.find(:all, :conditions => ["last_request_at > ?", 120.minutes.ago]).each do |user| %>
<li>
<%= link_to user.name, admin_person_path(user.bio) %>
(<%= mail_to user.email %>) <%= time_ago_in_words(user.last_request_at) %> ago
</li>
<% end %>
You can see that I just list their names, e-mails, and when they last made a request. It would be trivial to add last_login_ip, current_login_at, etc.
Hope this helps.

Related

Unable to use findAll() to search sub-directories

I'm using an eco renderer within Docpad (building a site based on https://github.com/zenorocha/browser-diet)
In it, I need to find all the documents in a particular sub-directory.
The directory structure is:
/src/documents/test1/test2
I need to find the documents stored in test2
I can find all the documents in test1 using:
<% for item in #getCollection("documents").findAll({ url: $startsWith: '/test1'}, [order:-1]).toJSON(): %>
No documents are returned when I add an extra sub-directory, using:
<% for item in #getCollection("documents").findAll({ url: $startsWith: '/test1/test2'}, [order:-1]).toJSON(): %>
There are documents at that URL, so what am I doing wrong?
I think you are doing it right. I just tested it in my project with the following code (tools is my generic collection defined in docpad.coffee):
<% for docu in #getCollection("tools").findAll(url:$startsWith:"/tools/adminui").toJSON(): %>
<% console.log(docu.url) %>
<% end %>
and in the console I get only the list of urls that start with /tools/adminui.
Just try to investigate it further with console output or share more details

heroku re-orders ruby on rails loop output of partials

I have a page which renders several partials; on my local mac everything is fine but after I push to heroku and visit the page the order of the partials is different!
I render partials with the code below and thought I controlled the order with a file naming convention.
html.erb:
<% Dir["app/views/partials/ws/*.html.erb"].each do |ws| %>
<%= render 'partials/ws/' + File.basename(ws,'.html.erb').slice(1..-1) %>
<% end %>
The partials use a naming convention:
_ws_01-why.html.erb
_ws_02-what.html.erb
_ws_03-who.html.erb
_ws_04-where.html.erb
_ws_05-when1.html.erb
_ws_06-how.html.erb
Heroku renders in this order:
_ws_01-why.html.erb
_ws_02-what.html.erb
_ws_06-how.html.erb
_ws_04-where.html.erb
_ws_05-when1.html.erb
_ws_03-who.html.erb
I'm not sure how heroku is interpreting the naming convention / ruby loop order... Wondering if there is a better naming convection or logic to add in my loop to control the order?
Thanks!
Change this:
<% Dir["app/views/partials/ws/*.html.erb"].each do |ws| %>
to this:
<% Dir["app/views/partials/ws/*.html.erb"].sort.each do |ws| %>
You cannot be sure than an enumerator will choose the same order in all cases unless you force it somehow.

Ruby on Rails 3, Changing Views

Im working with Rails 3.0.9 and Ruby 1.9.2.
I have a search page that will display a list of results in grid format. However, I want to be able to give the user an option to display them in list format.
Could anyone please advise me about how I can offer a change view option,and still keep the search results?
As you have not specified where query is stored,
I will hope that in url, like example.com/search?query=mylookupstring
So in this case all we need is to place somewhere on a page, link_to search path and pass your query + some parameter back to controller to prepare new view
<%= link_to "List", search_some_thing_path(:query=>params[:query],:list=>true) %>
nice & easy
Post your details if i show bad skills with telepathy today.
UPDATE:
Pay attention to params & url. Yours & my example. You shouldn't just copy
as your controller expects params[:search] not params[:query]
So fix error in you link_to & move_on!
PS Great tip to find out how rails really work is to use debugger.
You may attach it in view
<% debugger %> or in model/controller simply debugger
Don't forget to include debug gems in your Gemfile, smth like
group :development, :test do
gem 'ruby-debug19'
end

1 for 0 arguments errors formtastic, mongomapper and rails 3.1

While experimenting with mongomapper in a rails 3.1 app I decided to also have a look at using formtastic as well. This is more than likely down to my own incompetence but I am getting a strange error that I can't get to the bottom of
wrong number of arguments (1 for 0)
It is being caused by the form.inputs line in the form below
<%= semantic_form_for #product do |form| %>
<%= form.inputs :name %>
<%= form.buttons %>
<% end %>
The model
class Product
include MongoMapper::Document
key :name, String
end
Originally I has tried using <%= form.inputs %> on a more complex model but no form fields were generated so started adding them explicitly and ran into the same problem I have recreated with this simple model. I read in the documentation that formtastic is quite ActiveRecord-centric. Is this a compatibility issue or is it just something silly that I'm missing?
I'm using
rails 3.1,
ruby 1.9.2,
mongo_mapper 0.9.2,
formtastic 2.0.0.rc4 ( I originally tried rc5)
This issue has now been resolved by the author of the gem, thanks Justin! The 2.0.2 release should work without any problems
More details can be found here
https://github.com/justinfrench/formtastic/issues/676#issuecomment-2304425

Rails3 and automatic nofollow links

i have some model, let it be Post with field :content. Any user can submit post with html (with links of course:) ) and i'd like to set nofollow on those links. Is there any rails plugin to automate this task? Does this plugin have ability to manage "nofollowing" in conditional way - e.g. admin can add links without nofollow, but other - with only nofollow?
This should do what you're looking for: https://github.com/rgrove/sanitize/
Install the plugin, then for a block of text you can run:
<%=raw Sanitize.clean(#your_html, Sanitize::Config::BASIC) %>
There are other options that you can use to customise it, but the Config::BASIC version will detect all links in that block of text and add the nofollow tag to them.
You can define a helper for this, overriding (or rather wrapping) the default link_to
In app/helpers/posts_helper.rb do something along the lines of:
def nf_link_to(link_text, post)
opts = {}
opts[:rel] = "nofollow" unless post.author == "admin"
return link_to text, post, opts
end
So that in your view you can do:
<%= nf_link_to post.title, post %>
Which should result in:
My First[tm] Post
You should have a good look at the actual implementation of link_to and make your ''nf_link_to'' as complex as (as in; passing arguments and perhaps a block) as you desire.

Resources