https://github.com/ggonnella/attr_hidden
Rails / ActiveRecord - Hide attributes in a model (particularly useful for Single Table Inheritance)
https://github.com/ggonnella/attr_hidden
Last synced: 3 months ago
JSON representation
Rails / ActiveRecord - Hide attributes in a model (particularly useful for Single Table Inheritance)
- Host: GitHub
- URL: https://github.com/ggonnella/attr_hidden
- Owner: ggonnella
- License: mit
- Created: 2008-08-22T18:48:25.000Z (almost 17 years ago)
- Default Branch: master
- Last Pushed: 2008-09-06T15:06:39.000Z (over 16 years ago)
- Last Synced: 2025-01-16T16:23:22.549Z (4 months ago)
- Language: Ruby
- Homepage:
- Size: 82 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
- License: MIT-LICENSE
Awesome Lists containing this project
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.htmltable players:
name, club, batting_average, bowling_average, typetype 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
endclass Footballer < Player
endclass Cricketer < Player
endclass Bowler < Cricketer
endhowever 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
endclass Footballer < Player
attr_hidden :batting_average, :bowling_average
endclass Cricketer < Player
attr_hidden :club, :bowling_average
endclass Bowler < Cricketer
attr_not_hidden :bowling_average
endnow each class sees only the columns which are meaningful for them.