Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/active_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ def schema(&block)
@schema[k] = v
@known_attributes << k
end
define_attribute_methods @known_attributes

@schema
else
Expand Down Expand Up @@ -492,6 +493,7 @@ def schema=(the_schema)
# purposefully nulling out the schema
@schema = nil
@known_attributes = []
undefine_attribute_methods
return
end

Expand Down Expand Up @@ -1735,13 +1737,16 @@ def read_attribute(attr_name)
name = self.class.primary_key if name == "id" && self.class.primary_key
@attributes[name]
end
alias_method :attribute, :read_attribute

def write_attribute(attr_name, value)
name = attr_name.to_s

name = self.class.primary_key if name == "id" && self.class.primary_key
singleton_class.define_attribute_methods(name) unless known_attributes.include?(name)
@attributes[name] = value
end
alias_method :attribute=, :write_attribute

protected
def connection(refresh = false)
Expand Down Expand Up @@ -1904,7 +1909,7 @@ class Base
extend ActiveResource::Associations

include Callbacks, CustomMethods, Validations, Serialization
include ActiveModel::Conversion
include ActiveModel::Conversion, ActiveModel::AttributeMethods
include ActiveModel::ForbiddenAttributesProtection
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml
Expand Down
44 changes: 43 additions & 1 deletion test/cases/attribute_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,49 @@
require "fixtures/person"

class AttributeMethodsTest < ActiveSupport::TestCase
setup :setup_response
setup do
setup_response
@previous_schema = Person.schema
end

teardown do
Person.schema = nil
Person.schema = @previous_schema
end

test "setting the schema defines attribute methods" do
assert_changes -> { Person.public_instance_methods.include?(:name) }, from: false, to: true do
Person.schema { attribute :name, :string }
end
end

test "setting the schema to nil undefines attribute methods" do
Person.schema { attribute :name, :string }

assert_changes -> { Person.public_instance_methods.include?(:name) }, from: true, to: false do
Person.schema = nil
end
end

test "assigning an attribute during #load defines attribute methods for the instance" do
resource = Person.new

assert_changes -> { resource.public_methods.include?(:name) }, from: false, to: true do
resource.load name: "changed"
end

assert_not_includes Person.public_instance_methods, :name
end

test "reads and writes attribute methods declared by the schema without method missing" do
Person.schema { attribute :name, :string }

resource = Person.new

assert_changes -> { resource.name }, from: nil, to: "changed" do
resource.name = "changed"
end
end

test "write_attribute string" do
matz = Person.find(1)
Expand Down