lib/super_sti/hook.rb in super_sti-0.2.1 vs lib/super_sti/hook.rb in super_sti-0.3.0
- old
+ new
@@ -1,6 +1,9 @@
module SuperSTI
+
+ class DataMissingError < ::StandardError;end
+
module Hook
######
# has_extra_data(options = {})
#
@@ -15,35 +18,41 @@
#####
def has_extra_data(options = {}, &block)
table_name = options[:table_name] || "#{self.name.underscore.gsub("/", "_")}_data"
klass = Class.new(ActiveRecord::Base) do
- set_table_name(table_name)
+ self.table_name = table_name
end
klass.class_eval &block if block_given?
self.const_set "Data", klass
# Add a reference to a data object that gets created when this is created
- has_one :data, :class_name => "#{self.name}::Data", :foreign_key => options[:foreign_key]
+ has_one :data, :class_name => "#{self.name}::Data", :foreign_key => options[:foreign_key], :readonly => false, :autosave => true, :dependent => :destroy
before_create :get_data
# A helper method which gets the existing data or builds a new object
define_method :get_data do
- data || build_data
+ return data if data
+ return build_data if new_record?
+ raise SuperSTI::DataMissingError
end
# Override respond_to? to check both this object and its data object.
define_method "respond_to?" do |sym, include_private = false|
- super(sym, include_private) || get_data.respond_to?(sym, include_private)
+ begin
+ super(sym, include_private) || get_data.respond_to?(sym, include_private)
+ rescue SuperSTI::DataMissingError
+ false
+ end
end
# Override method_missing to check both this object and it's data object for any methods or magic functionality.
# Firstly, try the original method_missing because there may well be
# some magic piping that will return a result and then try the data object.
define_method :method_missing do |sym, *args|
begin
- super(sym, args)
- rescue
+ super(sym, *args)
+ rescue NoMethodError
get_data.send(sym, *args)
end
end
end
end