Skip to content

Commit

Permalink
Merge branch 'rewrite'
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Oct 18, 2013
2 parents 919bb38 + 5ae47f7 commit c65d387
Show file tree
Hide file tree
Showing 61 changed files with 1,658 additions and 4,193 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.bundle
.config
.yardoc
Gemfile.lock
*.lock
InstalledFiles
_yardoc
coverage
Expand Down
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ rvm:
- rbx-19mode
gemfile:
- Gemfile
- Gemfile.rails3
- Gemfile.edge
matrix:
allow_failures:
- gemfile: Gemfile.edge
notifications:
email: false
campfire:
Expand Down
62 changes: 18 additions & 44 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,20 @@
# UNRELEASED

* ActiveModel::Serializable was created it has the shared code between
AM::Serializer and AM::ArraySerializer. Basically enable objects to be
serializable by implementing an options method to handle the options
of the serialization and a serialize method that returns an object to
be converted to json by the module. This also removes duplicate code.
https://github.com/rails-api/active_model_serializers/commit/6c6bc8872d3b0f040a200854fa5530a775824dbf

* ActiveModel::Serializer::Caching module was created it enables
Serializers to be able to cache to\_json and serialize calls. This
also helps removing duplicate code.
https://github.com/rails-api/active_model_serializers/commit/3e27110df78696ac48cafd1568f72216f348a188

* We got rid of the Association.refine method which generated
subclasses.
https://github.com/rails-api/active_model_serializers/commit/24923722d4f215c7cfcdf553fd16582e28e3801b

* Associations doesn't know anymore about the source serializer.
That didn't make any sense.
https://github.com/rails-api/active_model_serializers/commit/2252e8fe6dbf45660c6a35f35e2423792f2c3abf
https://github.com/rails-api/active_model_serializers/commit/87eadd09b9a988bc1d9b30d9a501ef7e3fc6bb87
https://github.com/rails-api/active_model_serializers/commit/79a6e13e8f7fae2eb4f48e83a9633e74beb6739e

* Passing options[:hash] is not public API of include!. That was
removed.
https://github.com/rails-api/active_model_serializers/commit/5cbf9317051002a32c90c3f995b8b2f126f70d0c

* ActiveModel::Serializer::Associations::Config is now
ActiveModel::Serializer::Association but it's an internal
thing so shouldn't bother.
ActiveModel::Serializer::Associations::Has\* are now
ActiveModel::Serializer::Association::Has\* and inherit from
ActiveModel::Serializer::Association
https://github.com/rails-api/active_model_serializers/commit/f5de334ddf1f3b9764d914a717311532021785d2
https://github.com/rails-api/active_model_serializers/commit/3dd422d99e8c57f113880da34f6abe583c4dadf9

* serialize\_ids call methods on the corresponding serializer if they
are defined, instead of talking directly with the serialized object.
Serializers are decorators so we shouldn't talk directly with
serialized objects.

* Array items are not wrapped anymore in root element.
# VERSION 0.9.0.pre

* The following methods were removed
- Model#active\_model\_serializer
- Serializer#include!
- Serializer#include?
- Serializer#attr\_disabled=
- Serializer#cache
- Serializer#perform\_caching
- Serializer#schema (needs more discussion)
- Serializer#attribute
- Serializer#include\_#{name}? (filter method added)
- Serializer#attributes (took a hash)

* The following things were added
- Serializer#filter method
- SETTINGS object

* Remove support for ruby 1.8 versions.

Expand Down Expand Up @@ -107,7 +81,7 @@
* Allow serialization_scope to be disabled with serialization_scope nil
* Array serializer should support pure ruby objects besides serializers

# VERSION 0.5.0 (May 16, 2012)
# VERSION 0.5.0

* First tagged version
* Changes generators to always generate an ApplicationSerializer
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ source 'https://rubygems.org'
# Specify gem dependencies in active_model_serializers.gemspec
gemspec

gem "coveralls", require: false
gem 'sqlite3'
gem 'coveralls', :require => false
gem 'simplecov', :require => false

gem 'rails', "~> 4.0.0"
10 changes: 7 additions & 3 deletions Gemfile.edge
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
source 'http://rubygems.org'
source 'https://rubygems.org'

gemspec

gem 'sqlite3'
gem 'coveralls', :require => false
gem 'simplecov', :require => false

# Use Rails master
gem 'rails', github: 'rails/rails'

# Current dependencies of edge rails
gem 'journey', github: 'rails/journey'
gem 'activerecord-deprecated_finders' , github: 'rails/activerecord-deprecated_finders'
gem 'arel', github: 'rails/arel'
11 changes: 11 additions & 0 deletions Gemfile.rails3
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'https://rubygems.org'

# Specify gem dependencies in active_model_serializers.gemspec
gemspec

gem 'sqlite3'
gem 'coveralls', :require => false
gem 'simplecov', :require => false

gem 'minitest', '~> 4.0'
gem 'rails', '~> 3.2'
File renamed without changes.
109 changes: 48 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Purpose

The purpose of `ActiveModel::Serializers` is to provide an object to
encapsulate serialization of `ActiveModel` objects, including `ActiveRecord`
objects.
encapsulate serialization of objects which respond to
read\_attribute\_for\_serialization like ActiveModel ones and including
`ActiveRecord` objects.

Serializers know about both a model and the `current_user`, so you can
customize serialization based upon whether a user is authorized to see the
Expand Down Expand Up @@ -55,17 +56,11 @@ the serializer generator:
$ rails g serializer post
```

### Support for POROs and other ORMs.
### Support for POROs

Currently `ActiveModel::Serializers` adds serialization support to all models
that descend from `ActiveRecord` or include `Mongoid::Document`. If you are
using another ORM, or if you are using objects that are `ActiveModel`
compliant but do not descend from `ActiveRecord` or include
`Mongoid::Document`, you must add an include statement for
`ActiveModel::SerializerSupport` to make models serializable. If you
also want to make collections serializable, you should include
`ActiveModel::ArraySerializerSupport` into your ORM's
relation/criteria class.
Currently `ActiveModel::Serializers` expects objects to implement
read\_attribute\_for\_serialization. That's all you need to do to have
your POROs supported.

# ActiveModel::Serializer

Expand All @@ -92,19 +87,8 @@ This also works with `respond_with`, which uses `to_json` under the hood. Also
note that any options passed to `render :json` will be passed to your
serializer and available as `@options` inside.

To specify a custom serializer for an object, there are 2 options:

#### 1. Specify the serializer in your model:

```ruby
class Post < ActiveRecord::Base
def active_model_serializer
FancyPostSerializer
end
end
```

#### 2. Specify the serializer when you render the object:
To specify a custom serializer for an object, you can specify the
serializer when you render the object:

```ruby
render json: @post, serializer: FancyPostSerializer
Expand Down Expand Up @@ -267,49 +251,44 @@ class VersionSerializer < ActiveModel::Serializer
end
```

You can also access the `current_user` method, which provides an
You can also access the `scope` method, which provides an
authorization context to your serializer. By default, the context
is the current user of your application, but this
[can be customized](#customizing-scope).

Serializers will check for the presence of a method named
`include_[ATTRIBUTE]?` to determine whether a particular attribute should be
included in the output. This is typically used to customize output
based on `current_user`. For example:
Serializers provides a method named `filter` used to determine what
attributes and associations should be included in the output. This is
typically used to customize output based on `current_user`. For example:

```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body, :author

def include_author?
current_user.admin?
def filter(keys)
if scope.admin?
keys
else
keys - [:author]
end
end
end
```

The type of a computed attribute (like :full_name above) is not easily
calculated without some sophisticated static code analysis. To specify the
type of a computed attribute:

```ruby
class PersonSerializer < ActiveModel::Serializer
attributes :first_name, :last_name, {full_name: :string}

def full_name
"#{object.first_name} #{object.last_name}"
end
end
```
And it's also safe to mutate keys argument by doing keys.delete(:author)
in case you want to avoid creating two extra arrays.

If you would like the key in the outputted JSON to be different from its name
in ActiveRecord, you can use the `:key` option to customize it:
in ActiveRecord, you can declare the attribute with the different name
and redefine that method:

```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :body
# look up subject on the model, but use title in the JSON
def title
object.subject
end

# look up :subject on the model, but use +title+ in the JSON
attribute :subject, key: :title
attributes :id, :body, :title
has_many :comments
end
```
Expand Down Expand Up @@ -360,7 +339,7 @@ class PersonSerializer < ActiveModel::Serializer

def attributes
hash = super
if current_user.admin?
if scope.admin?
hash["ssn"] = object.ssn
hash["secret"] = object.mothers_maiden_name
end
Expand Down Expand Up @@ -405,33 +384,34 @@ class PostSerializer < ActiveModel::Serializer
end
```

Also, as with attributes, serializers will check for the presence
of a method named `include_[ASSOCIATION]?` to determine whether a particular association
should be included in the output. For example:
Also, as with attributes, serializers will execute a filter method to
determine which associations should be included in the output. For
example:

```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments

def include_comments?
!object.comments_disabled?
def filter(keys)
keys.delete :comments if object.comments_disabled?
keys
end
end
```

If you would like lower-level control of association serialization, you can
override `include_associations!` to specify which associations should be included:
Or ...

```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_one :author
has_many :comments

def include_associations!
include! :author if current_user.admin?
include! :comments unless object.comments_disabled?
def filter(keys)
keys.delete :author unless current_user.admin?
keys.delete :comments if object.comments_disabled?
keys
end
end
```
Expand All @@ -445,6 +425,9 @@ You may also use the `:serializer` option to specify a custom serializer class a

Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.

NOTE: polymorphic was removed because was only supported for has\_one
associations and is in the TODO list of the project.

## Embedding Associations

By default, associations will be embedded inside the serialized object. So if
Expand Down Expand Up @@ -635,7 +618,7 @@ class ApplicationController < ActionController::Base
end
```

The above example will also change the scope name from `current_user` to
The above example will also change the scope from `current_user` to
`current_admin`.

Please note that, until now, `serialization_scope` doesn't accept a second
Expand Down Expand Up @@ -678,6 +661,10 @@ that query, only the `show` action will.

## Caching

NOTE: This functionality was removed from AMS and it's in the TODO list.
We need to re-think and re-design the caching strategy for the next
version of AMS.

To cache a serializer, call `cached` and define a `cache_key` method:

```ruby
Expand Down
7 changes: 1 addition & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ Rake::TestTask.new(:test) do |t|
t.verbose = true
end

desc 'Benchmark'
task :bench do
load 'bench/perf.rb'
end

task default: :test
task :default => :test
14 changes: 5 additions & 9 deletions active_model_serializers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ $:.unshift File.expand_path("../lib", __FILE__)
require "active_model/serializer/version"

Gem::Specification.new do |gem|
gem.authors = ["José Valim", "Yehuda Katz"]
gem.email = ["[email protected]", "[email protected]"]
gem.authors = ["José Valim", "Yehuda Katz", "Santiago Pastorino"]
gem.email = ["[email protected]", "[email protected]", "[email protected]"]
gem.description = %q{Making it easy to serialize models for client-side use}
gem.summary = %q{Bringing consistency and object orientation to model serialization. Works great for client-side MVC frameworks!}
gem.homepage = "https://github.com/rails-api/active_model_serializers"

gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.files = Dir['README.md', 'CHANGELOG.md', 'CONTRIBUTING.md', 'DESIGN.textile', 'MIT-LICENSE', 'lib/**/*', 'test/**/*']
gem.test_files = Dir['test/**/*']

gem.name = "active_model_serializers"
gem.require_paths = ["lib"]
gem.version = ActiveModel::Serializer::VERSION

gem.required_ruby_version = ">= 1.9.3"

gem.add_dependency "activemodel", ">= 3.2"

gem.add_development_dependency "rails", ">= 3.2"
gem.add_development_dependency "pry"
gem.add_development_dependency "simplecov"
gem.add_development_dependency "coveralls"
end
Loading

0 comments on commit c65d387

Please sign in to comment.