ProxyColumns adds a proxy column over the top of an existing column associated with an active-record model.
This proxy column overrides the original column's accessor methods. The basic proxy functionality doesn't do much beside set and get the value of original columns.
The Proxy object can be subclassed/overridden to provide any sort of functionality required. This enables additional functionality to be added on top of the underlying column. A ProxyColumn can delegate based on the column and the column's value, as seen in MetaDataProxyColumn
.
One of the problems I faced was that the accessor methods for a column on a model weren't generated until the first model was initialised. That is why the methods are added dynamically using the after_initialize callback.
Include the concern:
ActiveRecord::Base.include(ProxyColumns) # concern
Different ways of instantiating:
class MyModel
# Alternativly, add the concern on a per model basis
include DataCollectionElements # concern
# create proxy column and set various options
proxy_column :my_col_1
proxy_column :my_col_2, :alias => [:col1, col2, col3]
proxy_column :my_col_3, :column => :actual_col_name
# define the proxy object using class/string/symbol
proxy_column :my_col_4, :proxy => MyProxy # or 'MyProxy' or :my_proxy
# define the proxy object using a proc/lambda
proxy_column :my_col_5, :proxy => -> { MyProxy.new(model, code, options) }
proxy_column :my_col_6, :proxy => proc { MyProxy.new(model, code, options) }
# define the proxy object using a block
proxy_column :my_col_7 do |code, options|
def some_custom_method
'some return value'
end
end
# redefine the proxy object using a block and a specified base class
proxy_column :my_col_7, :proxy => MyProxy do |code, options|
def some_custom_method
'some return value'
end
end
end
- Define a method on the ProxyColumn that is called when the ProcyColumn methods are being defined to allow custom functionality