Module: FlexAttributes::ClassMethods
- Defined in:
- lib/flex-attributes.rb
Instance Method Summary collapse
-
#has_flex_attributes(options = {}) ⇒ Object
Will make the current class have flex attributes.
Instance Method Details
#has_flex_attributes(options = {}) ⇒ Object
Will make the current class have flex attributes.
class User < ActiveRecord::Base
has_flex_attributes
end
eric = User.find_by_login 'eric'
puts "My AOL instant message name is: #{eric.aim}"
eric.phone = '555-123-4567'
eric.save
The above example should work even though “aim” and “phone” are not attributes on the User model.
The following options are available on for has_flex_attributes to modify the behavior. Reasonable defaults are provided:
- class_name
-
The class for the related model. This defaults to the model name prepended to “Attribute”. So for a “User” model the class name would be “UserAttribute”. The class can actually exist (in that case the model file will be loaded through Rails dependency system) or if it does not exist a basic model will be dynamically defined for you. This allows you to implement custom methods on the related class by simply defining the class manually.
- table_name
-
The table for the related model. This defaults to the attribute model’s table name.
- relationship_name
-
This is the name of the actual has_many relationship. Most of the type this relationship will only be used indirectly but it is there if the user wants more raw access. This defaults to the class name underscored then pluralized finally turned into a symbol.
- foreign_key
-
The key in the attribute table to relate back to the model. This defaults to the model name underscored prepended to “_id”
- name_field
-
The field which stores the name of the attribute in the related object
- value_field
-
The field that stores the value in the related object
- fields
-
A list of fields that are valid flex attributes. By default this is “nil” which means that all field are valid. Use this option if you want some fields to go to one flex attribute model while other fields will go to another. As an alternative you can override the #flex_attributes method which will return a list of all valid flex attributes. This is useful if you want to read the list of attributes from another source to keep your code DRY. This method is given a single argument which is the class for the related model. The following provide an example:
class User < ActiveRecord::Base
has_flex_attributes :class_name => 'UserContactInfo'
has_flex_attributes :class_name => 'Preferences'
def flex_attributes(model)
case model
when UserContactInfo
%w(email phone aim yahoo msn)
when Preference
%w(project_search project_order user_search user_order)
else Array.new
end
end
end
eric = User.find_by_login 'eric'
eric.email = '[email protected]' # Will save to UserContactInfo model
eric.project_order = 'name' # Will save to Preference
eric.save # Carries out save so now values are in database
Note the else clause in our case statement. Since an empty array is returned for all other models (perhaps added later) then we can be certain that only the above flex attributes are allowed.
If both a :fields option and #flex_attributes method is defined the :fields option take precidence. This allows you to easily define the field list inline for one model while implementing #flex_attributes for another model and not having #flex_attributes need to determine what model it is answering for. In both cases the list of flex attributes can be a list of string or symbols
A final alternative to :fields and #flex_attributes is the #is_flex_attribute? method. This method is given two arguments. The first is the attribute being retrieved/saved the second is the Model we are testing for. If you override this method then the #flex_attributes method or the :fields option will have no affect. Use of this method is ideal when you want to retrict the attributes but do so in a algorithmic way. The following is an example:
class User < ActiveRecord::Base
has_flex_attributes :class_name => 'UserContactInfo'
has_flex_attributes :class_name => 'Preferences'
def is_flex_attribute?(attr, model)
case attr.to_s
when /^contact_/ then true
when /^preference_/ then true
else
false
end
end
end
eric = User.find_by_login 'eric'
eric.contact_phone = '555-123-4567'
eric.contact_email = '[email protected]'
eric.preference_project_order = 'name'
eric.some_attribute = 'blah' # If some_attribute is not defined on
# the model then method not found is thrown
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/flex-attributes.rb', line 194 def has_flex_attributes(={}) # Provide default options [:class_name] ||= self.class_name + 'Attribute' [:table_name] ||= [:class_name].tableize [:relationship_name] ||= [:class_name].tableize.to_sym [:foreign_key] ||= self.class_name.foreign_key [:base_foreign_key] ||= self.name.underscore.foreign_key [:name_field] ||= 'name' [:value_field] ||= 'value' [:fields].collect! {|f| f.to_s} unless [:fields].nil? [:versioned] = .has_key?(:versioned) ? [:versioned] : false # Init option storage if necessary cattr_accessor :flex_options self. ||= Hash.new # Return if already processed. return if self..keys.include? [:class_name] # Attempt to load related class. If not create it begin [:class_name].constantize rescue Object.const_set([:class_name], Class.new(ActiveRecord::Base)).class_eval do def self.reloadable? #:nodoc: false end end end # Store options self.[[:class_name]] = # Mix in instance methods send :include, FlexAttributes::InstanceMethods # Modify attribute class attribute_class = [:class_name].constantize base_class = self.name.underscore.to_sym attribute_class.class_eval do belongs_to base_class, :foreign_key => [:base_foreign_key] alias_method :base, base_class # For generic access if [:versioned] begin version_column = column_names.include?('lock_version') ? 'lock_version' : 'version' def version # :nodoc: lock_version end if version_column == 'lock_version' rescue version_column = 'version' end acts_as_versioned :version_column => version_column acts_as_versioned_association base_class, :both_sides => true def version_condition_met? # :nodoc: base.version_condition_met? end end end # Modify main class class_eval do has_many [:relationship_name], :class_name => [:class_name], :table_name => [:table_name], :foreign_key => [:foreign_key], :dependent => :destroy if [:versioned] begin version_column = column_names.include?('lock_version') ? 'lock_version' : 'version' def version # :nodoc: lock_version end if version_column == 'lock_version' rescue version_column = 'version' end acts_as_versioned :version_column => version_column acts_as_versioned_association [:relationship_name], :both_sides => true version_class.send :include, FlexAttributes::InstanceMethods end # The following is only setup once unless private_method_defined? :method_missing_without_flex_attributes # Carry out delayed actions before save after_validation_on_update :save_modified_flex_attributes # Make attributes seem real alias_method :method_missing_without_flex_attributes, :method_missing alias_method :method_missing, :method_missing_with_flex_attributes if [:versioned] version_class_alias_method :method_missing_without_flex_attributes, :method_missing version_class_alias_method :method_missing, :method_missing_with_flex_attributes end private alias_method :read_attribute_without_flex_attributes, :read_attribute alias_method :read_attribute, :read_attribute_with_flex_attributes alias_method :write_attribute_without_flex_attributes, :write_attribute alias_method :write_attribute, :write_attribute_with_flex_attributes if [:versioned] version_class_alias_method :read_attribute_without_flex_attributes, :read_attribute version_class_alias_method :read_attribute, :read_attribute_with_flex_attributes version_class_alias_method :write_attribute_without_flex_attributes, :write_attribute version_class_alias_method :write_attribute, :write_attribute_with_flex_attributes end end end end |