ruby

Implementing sessions in rails

I'm doing a first pass at rolling my own authentication and sessions in rails and am not sure that I understand the session support that is present. (By first pass, I mean I'm initially authenticating via http, not https. Production code will use https.) My understanding of secure sessions is that you pass a token to the browser via a c...

Is it possible to load a Rails helper at run time?

I have a View that can vary significantly, depending on the 'mode' a particular user has chosen. I thought I would extract the different behavior into two different Helpers, and then have code like this in the Controller: class MyController < ApplicationController case mode when 'mode1' helper "mode1" when 'mode2' helper "mode2" e...

Best Rails HTML Parser

Hi everyone, I know that Hpricot is still a standard but I remember hearing about a faster more expressive HTML parser for Ruby. Does anybody know what it's called and if it is worth switching to from Hpricot?? Thanks in advance ...

Truncate Markdown?

I have a Rails site, where the content is written in markdown. I wish to display a snippet of each, with a "Read more.." link. How do I go about this? Simple truncating the raw text will not work, for example.. >> "This is an [example](http://example.com)"[0..25] => "This is an [example](http:" Ideally I want to allow the author to (...

How to go about sending email x hours after a user signs up in Ruby on Rails?

How would I go about sending an email to a user, say, 48 hours after they sign up, in Ruby on Rails? Thanks! ...

Circular Dependencies in Ruby

Let's say we have two classes, Foo and Foo Sub, each in a different file, foo.rb and foo_sub.rb respectively. foo.rb: require "foo_sub" class Foo def foo FooSub.SOME_CONSTANT end end foo_sub.rb: require "foo" class FooSub < Foo SOME_CONSTANT = 1 end This isn't going to work due to the circular dependency - we c...

[] method of Ruby String

When I reading source code of Beast, I found a lot of code like this: <%= 'Password'[:password_title] %> It seems like a call to [] method with Symbol as input parameter to a String to me, but I didn't find such type of parameter of String [] method in the ruby API. What is this means? thanks in advance. ...

ERB like library for C#

I am looking to find a librray that emulates part of the capabilities of Ruby's ERB library. ie substitute text for variables between <% and %>. I dont need the code execution part that ERB provides but if you know of something that has this I would be super appreciative. ...

In rails, how do you stub the render method in functional tests?

I'm writing some functional tests for a controller in rails, using mocha to do mocking/stubbing. Is there a way to prevent the template from being rendered during the test, so that I can test only the code in the controller? It looks like rspec provides something like this, but I'm not using rspec. ...

Railroad diagram generator fails with "NoMethodError"

After making a few modifications to a rails app I am tinkering on, railroad stopped working. The verbose output gives some clues. I wonder if other folks have encountered this and if there are some pointers for fixing this problem. Is it a data modeling error? Is it a problem with railroad? Error log follows... railroad -vM Loading appl...

How to programmatically rename movie and TV show files in an iTunes library, for XBMC compatibility?

I have a collection of movies and TV shows in iTunes, and I'd like to rename them to an XBMC compatible naming convention without breaking the links in iTunes. All the necessary metadata (season number, show name, episode number, etc) seems to be in an XML file that iTunes manages, and the episode name is the current file name. So progr...

How to get a DateTime duration?

Hello, I'm baffled how to do this. I need to take a datetime object and get the duration in hours, days, whatever, to the current time. Thank you. ...

What's the best way to generate environment-specific files for a Rails project?

I have some settings I need in a Javascript file -- servers to connect to -- that changes based on my environment. For development, test, and staging, I want to use the staging servers; for production, the production servers. I already have the settings in Ruby (configured in my environment/xyz.rb files). So far, I've been dynamically...

Rails: How to observe join records that don't actually have a Model?

Dear Stack, Is it possible, using an Observer, to observe the creation of JOIN records? For example, you have a User Model that has_and_belongs_to_many Book Models. Is it possible to monitor books_users records as they are created or deleted or must I have a BookUser model to do this? Example of what I want to observe: User.books <<...

How to get a positive mental attitude towards testing?

I want to write tests for my app, though each time I look at rspec.info, I really don't see a definite path to take towards "doing things right" and testing first. I watched the peepcode videos on rspec more than once, yet it doesn't take. I want to take more pride in my work, and I think that testing will help. How can I break throug...

Is it possible to get the raw param string in rails?

Given the following url: http://foo.com?bar=1&amp;wee=2 What is the quickest way to get the raw param part of the url from an action? i.e. ?bar=1&wee=2 ...

Can you get DB username, pw, database name in Rails?

I'm writing a rake task that does some DB work outside of Rails/ActiveRecord. Is there a way to get the DB connection info (host, username, password, DB name) for the current environment as defined in database.yml? I'd like to get it so I can use it to connect like this... con = Mysql.real_connect("host", "user", "pw", "current_db") ...

rails model attributes without corresponding column in db

I have some rails models which don't need any persistence, however I'd like rails to think the model actually has attributes x, y, z so when calling methods like to_json in the controller I get them included for free. For example, class ModelWithoutTableColumns << ActiveRecord::Base def x return "Custom stuff here" end There is n...

Populating Rails Form Fields after an auto_complete field is populate

I am trying to create a form in Rails 2.2.2 that populates a field based on a choice a user has made in an 'auto_complete' field. It looks as though the observe_field functionality is what I need to use but I'm stuck on how to update the value of an existing text field. The sequence of events I am trying to model is follows: 1) Use typ...

What are the major differences between Rails 1.X and 2.X

Most dead-tree books and web tutorials address Rails 1.X. I'm wondering if they are worth using to learn Rails 2.X. If so, what sections and concepts should should I avoid and what have pretty much stayed the same? ...