Permalink
Browse files

Safe attributes= method_missing

(doesn't override class methods)
  • Loading branch information...
1 parent 6a00a59 commit 9d11199d0b0dc8c2724d1479783443fc3d260c32 @bbenezech committed Feb 23, 2010
Showing with 23 additions and 10 deletions.
  1. +1 −1 VERSION
  2. +20 −4 lib/serialist/serialist_module.rb
  3. +2 −5 test/serialist_test.rb
View
@@ -1 +1 @@
-1.3.0
+1.3.2
@@ -15,7 +15,21 @@ def serialist(serialist_field, serialist_options = [])
serialize(@serialist_field, Hash)
if serialist_options.empty?
+ # catch-all mode
include Serialist::InstanceMethods
+ class_eval do
+ # alias method chaining
+ unless method_defined? :method_missing
+ def method_missing(meth, *args, &block); super; end
+ end
+ alias_method :old_method_missing, :method_missing
+ alias_method :method_missing, :serialist_method_missing
+ unless method_defined?(:attributes=)
+ def attributes=(meth, *args, &block); super; end
+ end
+ alias_method :old_attributes=, :attributes=
+ alias_method :attributes=, :serialist_attributes=
+ end
else
@serialist_options.each do |field|
cols = self.columns.map{|c|c.name.to_s}
@@ -60,7 +74,9 @@ def inherited(subclass)
end
module InstanceMethods
- def attributes=(new_attributes, guard_protected_attributes = true)
+
+ # needed because AR checks with respond_to when doing mass assignment.
+ def serialist_attributes=(new_attributes, guard_protected_attributes = true)
return if new_attributes.nil?
attributes = new_attributes.dup
attributes.stringify_keys!
@@ -70,12 +86,12 @@ def attributes=(new_attributes, guard_protected_attributes = true)
self.class.define_access_method(k) unless respond_to?(k) # for validation
end
end
- super
+ self.send(:old_attributes=, new_attributes, guard_protected_attributes = true)
end
- def method_missing(method, *args, &block)
+ def serialist_method_missing(method, *args, &block)
begin
- super
+ old_method_missing(method, *args, &block)
rescue NoMethodError
self.class.define_access_method(method.to_s)
self.send(method, *args, &block)
View
@@ -1,8 +1,8 @@
require 'test/unit'
require 'rubygems'
-gem 'activerecord'
require 'active_record'
+require 'active_support'
RAILS_ROOT = File.dirname(__FILE__)
$: << File.join(File.dirname(__FILE__), '../lib')
require File.join(File.dirname(__FILE__), '../init')
@@ -174,10 +174,7 @@ def test_mass_assignement_to_declarative_serialisted_should_not_create_record_if
end
def test_lazily_serialist_should_not_try_to_override_existing_methods_and_columns
- assert_raise ActiveRecord::UnknownAttributeError do
- LazyRegressionTestClass.create!({:foo => "foo", :bar => "bar", :title => "hoho", :title_fun => "haha"})
- end
- @serialisted = LazyRegressionTestClass.create!({:foo => "foo", :bar => "bar", :title => "hoho"})
+ @serialisted = LazyRegressionTestClass.create!({ :foo => "foo", :bar => "bar", :title => "hoho" })
assert_nil @serialisted.slug[:title]
assert_equal @serialisted.title, "hoho"
assert_equal @serialisted.title_fun, "ohoh"

0 comments on commit 9d11199

Please sign in to comment.