Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Aug 24, 2012
@cgriego Extract Serialization 41dc315
View
1  CHANGELOG.md
@@ -1,5 +1,6 @@
# ActiveAttr 0.7.0 (Unreleased) #
+* Added Serialization
* Changed Typecasting::DateTypecaster to not raise on invalid Strings
# ActiveAttr 0.6.0 (June 27, 2012) #
View
9 README.md
@@ -193,6 +193,15 @@ including the MassAssignmentSecurity model.
include ActiveModel::ForbiddenAttributesProtection
end
+### Serialization ###
+
+The Serialization module is a shortcut for incorporating ActiveModel's
+serialization functionality into your model with one include.
+
+ class Person
+ include ActiveAttr::Model
+ end
+
### Model ###
The Model module is a shortcut for incorporating the most common model
View
1  lib/active_attr.rb
@@ -21,6 +21,7 @@ module ActiveAttr
autoload :MassAssignmentSecurity
autoload :Model
autoload :QueryAttributes
+ autoload :Serialization
autoload :TypecastedAttributes
autoload :Typecasting
autoload :UnknownAttributeError
View
11 lib/active_attr/model.rb
@@ -4,8 +4,8 @@
require "active_attr/logger"
require "active_attr/mass_assignment_security"
require "active_attr/query_attributes"
+require "active_attr/serialization"
require "active_attr/typecasted_attributes"
-require "active_model"
require "active_support/concern"
module ActiveAttr
@@ -29,13 +29,6 @@ module Model
include AttributeDefaults
include QueryAttributes
include TypecastedAttributes
-
- if defined? ActiveModel::Serializable
- include ActiveModel::Serializable::JSON
- include ActiveModel::Serializable::XML
- else
- include ActiveModel::Serializers::JSON
- include ActiveModel::Serializers::Xml
- end
+ include Serialization
end
end
View
31 lib/active_attr/serialization.rb
@@ -0,0 +1,31 @@
+require "active_attr/attributes"
+require "active_attr/mass_assignment"
+require "active_model"
+require "active_support/concern"
+
+module ActiveAttr
+ # Serialization is a shortcut for incorporating ActiveModel's
+ # serialization functionality into your model with one include
+ #
+ # See the included modules to learn more.
+ #
+ # @example Usage
+ # class Person
+ # include ActiveAttr::Serialization
+ # end
+ #
+ # @since 0.7.0
+ module Serialization
+ extend ActiveSupport::Concern
+ include Attributes
+ include MassAssignment
+
+ if defined? ActiveModel::Serializable
+ include ActiveModel::Serializable::JSON
+ include ActiveModel::Serializable::XML
+ else
+ include ActiveModel::Serializers::JSON
+ include ActiveModel::Serializers::Xml
+ end
+ end
+end
View
31 spec/functional/active_attr/serialization_spec.rb
@@ -0,0 +1,31 @@
+require "spec_helper"
+require "active_attr/serialization"
+require "active_support/core_ext/hash/conversions"
+require "active_support/json/decoding"
+
+module ActiveAttr
+ describe Serialization do
+ let :model_class do
+ Class.new do
+ include Serialization
+ attribute :first_name
+
+ def self.name
+ "Person"
+ end
+ end
+ end
+
+ subject { model_class.new }
+
+ it "serializes to/from JSON" do
+ subject.first_name = "Chris"
+ model_class.new.from_json(subject.to_json).first_name.should == "Chris"
+ end
+
+ it "serializes to/from XML" do
+ subject.first_name = "Chris"
+ model_class.new.from_xml(subject.to_xml).first_name.should == "Chris"
+ end
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.