public
Description: Rails / ActiveRecord - Hide attributes in a model (particularly useful for Single Table Inheritance)
Homepage:
Clone URL: git://github.com/ggonnella/attr_hidden.git
Giorgio Gonnella (author)
Sat Sep 06 03:17:39 -0700 2008
commit  34f3e4fae791d9ce6900d699472cca5afa662989
tree    363ba26e42b45fdf48c1b871da02d9ab5aad8284
parent  5c39f53841b9f441e9c0ffe42d5869ed9a66a275
name age message
file MIT-LICENSE Fri Aug 22 12:31:43 -0700 2008 plugin attr_hidden, version 0.9 [Giorgio Gonnella]
file README Loading commit data...
file init.rb Fri Aug 22 12:31:43 -0700 2008 plugin attr_hidden, version 0.9 [Giorgio Gonnella]
directory lib/ Sat Sep 06 03:17:39 -0700 2008 relaxed assumption that class have instance var... [Giorgio Gonnella]
README
This is a Rails plugin, which was created for Rails 2.1
but might work also for other Rails versions.

It does not contain unit tests, so if you would like to 
collaborate, this would be a nice addition.

Although this plugin does not require STI, it is probably
only really useful with single table inheritance.

Example usage with STI
======================

Let's use the example of STI given by Martin Fowler at:
http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html

table players:
name, club, batting_average, bowling_average, type

type may be: “Footballer”, “Cricketer”, “Bowler”

The attribute bowling_average will always be NULL in any record with type 
Cricketer or Footballer. The same is true for “club” in Cricketer or Bowler. 

so using ActiveRecord you have: 

class Player < ActiveRecord::Base
end

class Footballer < Player
end

class Cricketer < Player
end

class Bowler < Cricketer
end

however when you use an object of the class Bowler you can still access "club",
which make sense only for Footballer; this plugin comes in hand in this situation.

The point of this plugin is to hide from the object instance in the 
object-relational mapping the columns which are anyway always NULL in some 
of these classes.

using attr_hidden: 

class Player < ActiveRecord::Base
end

class Footballer < Player
  attr_hidden :batting_average, :bowling_average
end

class Cricketer < Player
  attr_hidden :club, :bowling_average
end

class Bowler < Cricketer
  attr_not_hidden :bowling_average
end

now each class sees only the columns which are meaningful for them.