Skip to content

Commit

Permalink
Hobofields -- support :null => true/false on belongs_to
Browse files Browse the repository at this point in the history
  • Loading branch information
tslocke committed Jun 11, 2008
1 parent a73b4bd commit 124d2d2
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions hobofields/lib/hobo_fields/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ def self.attr_accessor_with_rich_types(*attrs)

# Extend belongs_to so that it creates a FieldSpec for the foreign key
def self.belongs_to_with_field_declarations(name, options={}, &block)
res = belongs_to_without_field_declarations(name, options, &block)
refl = reflections[name.to_sym]
fkey = refl.primary_key_name
column_options = {}
column_options[:null] = options[:null] if options.has_key?(:null)
declare_field(fkey, :integer, column_options)
declare_polymorphic_type_field(name, column_options) if refl.options[:polymorphic]
res
column_options[:null] = options.delete(:null) if options.has_key?(:null)

returning belongs_to_without_field_declarations(name, options, &block) do
refl = reflections[name.to_sym]
fkey = refl.primary_key_name
declare_field(fkey, :integer, column_options)
declare_polymorphic_type_field(name, column_options) if refl.options[:polymorphic]
end
end
class << self
alias_method_chain :belongs_to, :field_declarations
Expand Down

0 comments on commit 124d2d2

Please sign in to comment.