Archive for the 'ruby' Category

30
May
12

rails translating attribute names

I found the answer to my question from here.
Here’s the way:


  activerecord:
    attributes:
      user:
        first_name: "Name first"

22
May
12

making sure your integration tests view the actual data in the db


Monkeypatch ActiveRecord with this:


class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

and your spec_helper with this:


RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before :each do
    if Capybara.current_driver == :rack_test
      DatabaseCleaner.strategy = :transaction
    else
      DatabaseCleaner.strategy = :truncation
    end
    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end
end

Full info available on Nicholas Rutherford’s blog. Thanks Nicholas!

22
May
12

rails select only some columns


If you want to only select a few columns on a query, here’s how you can do it:

 YourModel.find(:all,:select => "column1,column2") 

23
Apr
12

access the database of a heroku application from another heroku application


First thing you have to do is find out the DATABASE_URL of the app you’re interested in. Run heroku config –app your_app to get it. Then, in the other app, run this:

heroku config:add DATABASE_URL=whatever_the_result_of_the_first_command_was

Establish an ActiveRecord connection:


  db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')

  ActiveRecord::Base.establish_connection(
              :adapter  => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
              :host     => db.host,
              :username => db.user,
              :password => db.password,
              :database => db.path[1..-1],
              :encoding => 'utf8'
   )

02
Mar
12

cool ruby gem

state_machine is a nifty gem. Try it!

24
Feb
12

using coffeescript on windows


If you want to develop with CoffeeScript outside of Rails, here’s one way you could do it:

  • download the coffee.exe executable from CoffeeScript compiler for Windows
  • copy coffee.exe in a folder that is on the PATH
  • create a watchr file, for the ruby gem watchr. This way, everytime you modify the .coffee file, the js file gets recompiled. Here are the contents of my watchr file:
    
    watch("js/application.coffee") do |s|
    	puts "#{Time.now} => Compiling #{s}"
    	system("coffee -c #{s[0]}")
    end
    
  • start watching the file for modifications:
    
    watcher your_watch_file.rb
    
    
    
  • add the include for the resulting JS in your html code and you’re done

29
Jan
12

running a single test with rspec


Assuming 31 is the line at which your test starts, do this:

rspec some.spec:31

27
Jan
12

some treetop debug tips


Treetop seems pretty nice, but information on it’s usage is kind of scattered all over internet :) . Here are a few tips that I used to debug grammars:

  1. You can try a specific rule by writing this:

    tree = parser.parse(content,:root => "rule_name")
  2. I usually have my code structured like this, to get some info about where the parser chokes:
    
    if tree
        puts "succesful"
    else
        puts "unsuccesful at #{parser.index}"
        puts parser.failure_reason
    end
    

Hope this helps you as much as it helped me.




Blog Stats

  • 116,541 hits

Follow

Get every new post delivered to your Inbox.