Sunday, 24 February 2013

validates exclusion of controller names when catching all routes

Of course catching all routes is bad, but if you have to:
class Account < ActiveRecord::Base
  INVALID_PAGE_NAMES = Dir[Rails.root.join('app/controllers/*_controller.rb')].map { |path| path.match(/(\w+)_controller.rb/); $1 }
  validates :page_name, :exclusion => { :in => INVALID_PAGE_NAMES,
    :message => "Page %{value} is reserved." }
end

Saturday, 23 February 2013

rials and ruby postcasts and screencasts



Also worth checking

rspec, capybara and factorygirl hints

# spec_helper.rb

# this line will make rpsec end at the first failing test
RSpec.configure do |c|
  c.fail_fast = true
end
# in integration specs

# xpath can be copied from chrome, rx-click on the element (in the dev-tool)
page.should have_xpath("//a[contains(@href,'users')]"), count: num)

rails sandbox tasks

desc 'do not permanently write on the db (good for testing rake tasks)'
task :sandbox => :environment do
  puts "** << USING SANDBOX!! >> **"

  # beginning
  ActiveRecord::Base.connection.increment_open_transactions
  ActiveRecord::Base.connection.begin_db_transaction

  # end
  at_exit do
     ActiveRecord::Base.connection.rollback_db_transaction
     ActiveRecord::Base.connection.decrement_open_transactions
  end    
end

yaml defaults example (postgres on ruby on rails)

postgres:   &postgres
  adapter:  postgresql
  encoding: unicode
  host:     localhost
  username: netengine
  password:

development:
  <<: *postgres
  database: propertyconnect_development

test:
  <<: *postgres
  database: propertyconnect_test

Saturday, 12 January 2013

first post at netengine!

Not the best I've ever done, but I had to write something... A ruby implementation of the OAuth2 protocol for third party roles, enjoy!

http://goo.gl/UbHxI

Saturday, 5 January 2013

extend vs include

I always make confusion, although I'm getting better, this should help:


module MainMod
  module SubMod
    def self.included(cls)
      puts "#{self} included in #{cls}"
      cls.extend ClassMethods
    end

    module ClassMethods
      
      def ext
        puts 'extended'
      end
  
    end
  
    def inc
      puts 'included'
    end

  end
end

class A
  include MainMod::SubMod # => MainMod::SubMod included in A
end

A.new.inc
A.ext

module Mod1
  inc MainMod::SubMod
end

class B
  inc Mod1 # => MainMod::SubMod included in Mod1 !!!(Mod1)
end

B.new.inc
# B.ext # does not exists, `ext` was extended on Mod1