Skip to content

Commit

Permalink
Merge pull request #326 from novikserg/325_mass_assignment_bug_fix
Browse files Browse the repository at this point in the history
mass assignment bug fix

fixes #325
  • Loading branch information
solnic committed Jul 15, 2015
2 parents d494b95 + 00b9d85 commit 8d8755f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
6 changes: 1 addition & 5 deletions lib/virtus/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ class ValueObjectBuilder < Builder

# @api private
def extensions
super + [
Extensions::AllowedWriterMethods,
ValueObject::AllowedWriterMethods,
ValueObject::InstanceMethods
]
super << ValueObject::AllowedWriterMethods << ValueObject::InstanceMethods
end

# @api private
Expand Down
1 change: 0 additions & 1 deletion lib/virtus/class_inclusions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module ClassInclusions
def self.included(descendant)
super
descendant.extend(ClassMethods)
descendant.extend(Extensions::AllowedWriterMethods)
descendant.class_eval { include Methods }
descendant.class_eval { include InstanceMethods }
descendant.class_eval { include InstanceMethods::Constructor }
Expand Down
32 changes: 12 additions & 20 deletions lib/virtus/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def self.extended(object)
object.instance_eval do
extend Methods
extend InstanceMethods
extend AllowedWriterMethods
extend InstanceMethods::MassAssignment
end
end
Expand Down Expand Up @@ -75,23 +74,6 @@ def values(&block)
include(::Equalizer.new(*attribute_set.map(&:name)))
end

private

# Return an attribute set for that instance
#
# @return [AttributeSet]
#
# @api private
def attribute_set
@attribute_set
end

end # Methods

module AllowedWriterMethods
WRITER_METHOD_REGEXP = /=\z/.freeze
INVALID_WRITER_METHODS = %w[ == != === []= attributes= ].to_set.freeze

# The list of writer methods that can be mass-assigned to in #attributes=
#
# @return [Set]
Expand All @@ -106,8 +88,18 @@ def allowed_writer_methods
end
end

end # AllowedWriterMethods
private

end # module Extensions
# Return an attribute set for that instance
#
# @return [AttributeSet]
#
# @api private
def attribute_set
@attribute_set
end

end # Methods

end # module Extensions
end # module Virtus
2 changes: 1 addition & 1 deletion lib/virtus/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Constructor
#
# @api private
def initialize(attributes = nil)
self.class.attribute_set.set(self, attributes) if attributes
attribute_set.set(self, attributes) if attributes
set_default_attributes
end

Expand Down
2 changes: 0 additions & 2 deletions lib/virtus/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ module MassAssignment
# @api private
def self.included(descendant)
super
descendant.extend(Extensions::AllowedWriterMethods)
descendant.send(:include, InstanceMethods::MassAssignment)
end
private_class_method :included

# @api private
def self.extended(descendant)
super
descendant.extend(Extensions::AllowedWriterMethods)
descendant.extend(InstanceMethods::MassAssignment)
end
private_class_method :extended
Expand Down
28 changes: 28 additions & 0 deletions spec/integration/attributes_attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

describe "Adding attribute called 'attributes'" do

context "when mass assignment is disabled" do
before do
module Examples
class User
include Virtus.model(mass_assignment: false)

attribute :attributes
end
end
end

it "allows model to use `attributes` attribute" do
user = Examples::User.new
expect(user.attributes).to eq(nil)
user.attributes = "attributes string"
expect(user.attributes).to eq("attributes string")
end

it "doesn't accept `attributes` key in initializer" do
user = Examples::User.new(attributes: 'attributes string')
expect(user.attributes).to eq(nil)
end
end
end

1 comment on commit 8d8755f

@backus
Copy link

@backus backus commented on 8d8755f Jul 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this bugfix been released yet?

Please sign in to comment.