Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
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.
base fork: cadwallion/active_model_serializers
base: 0027882cfc
...
head fork: cadwallion/active_model_serializers
compare: 92b1642251
  • 13 commits
  • 11 files changed
  • 0 commit comments
  • 6 contributors
View
22 README.markdown
@@ -45,11 +45,9 @@ the `serializer generator`:
$ rails g serializer post
```
-# ApplicationSerializer the global serializer
+# ActiveModel::Serializer
-All new serializers descend from either ActiveModel::Serializer or from
-ApplicationSerializer if you create this file in `app/serializers`. This file
-is no longer required.
+All new serializers descend from ActiveModel::Serializer
# render :json
@@ -57,7 +55,7 @@ In your controllers, when you use `render :json`, Rails will now first search
for a serializer for the object and use it if available.
```ruby
-class PostController < ApplicationController
+class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
render :json => @post
@@ -83,7 +81,7 @@ Once you have a serializer, you can specify which attributes and associations
you would like to include in the serialized form.
```ruby
-class PostSerializer < ApplicationSerializer
+class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
end
@@ -100,7 +98,7 @@ 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:
```ruby
-class PostSerializer < ApplicationSerializer
+class PostSerializer < ActiveModel::Serializer
attributes :id, :body
# look up :subject on the model, but use +title+ in the JSON
@@ -122,7 +120,7 @@ association and returning a different Array. Often, you will do this to
customize the objects returned based on the current user.
```ruby
-class PostSerializer < ApplicationSerializer
+class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
@@ -141,7 +139,7 @@ As with attributes, you can also change the JSON key that the serializer should
use for a particular association.
```ruby
-class PostSerializer < ApplicationSerializer
+class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
# look up comments, but use +my_comments+ as the key in JSON
@@ -174,7 +172,7 @@ flexible from a performance standpoint and avoids wasteful duplication.
To embed IDs instead of associations, simply use the `embed` class method:
```ruby
-class PostSerializer < ApplicationSerializer
+class PostSerializer < ActiveModel::Serializer
embed :ids
attributes :id, :title, :body
@@ -204,7 +202,7 @@ objects (like tags), are only delivered once for the entire payload.
You can specify that the data be included like this:
```ruby
-class PostSerializer < ApplicationSerializer
+class PostSerializer < ActiveModel::Serializer
embed :ids, :include => true
attributes :id, :title, :body
@@ -239,7 +237,7 @@ You can also specify a different root for the embedded objects than the key
used to reference them:
```ruby
-class PostSerializer < ApplicationSerializer
+class PostSerializer < ActiveModel::Serializer
embed :ids, :include => true
attributes :id, :title, :body
View
4 RELEASE_NOTES.md
@@ -0,0 +1,4 @@
+# VERSION 0.5 (May 16, 2012)
+
+* First tagged version
+* Changes generators to always generate an ApplicationSerializer
View
6 active_model_serializers.gemspec
@@ -1,4 +1,8 @@
# -*- encoding: utf-8 -*-
+
+$:.unshift File.expand_path("../lib", __FILE__)
+require "active_model/serializers/version"
+
Gem::Specification.new do |gem|
gem.authors = ["José Valim", "Yehuda Katz"]
gem.email = ["jose.valim@gmail.com", "wycats@gmail.com"]
@@ -11,7 +15,7 @@ Gem::Specification.new do |gem|
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "active_model_serializers"
gem.require_paths = ["lib"]
- gem.version = "0.1.0"
+ gem.version = ActiveModel::Serializer::VERSION
gem.add_dependency 'activemodel', '~> 3.0'
gem.add_development_dependency "rails", "~> 3.0"
View
1  lib/action_controller/serialization.rb
@@ -49,6 +49,7 @@ def _render_option_json(json, options)
if serializer
options[:scope] = serialization_scope
+ options[:url_options] = url_options
json = serializer.new(json, options.merge(default_serializer_options || {}))
end
super
View
15 lib/active_model/serializer.rb
@@ -246,12 +246,16 @@ def attributes(*attrs)
self._attributes = _attributes.dup
attrs.each do |attr|
- self._attributes[attr] = attr
+ attribute attr
end
end
def attribute(attr, options={})
self._attributes = _attributes.merge(attr => options[:key] || attr)
+
+ unless method_defined?(attr)
+ class_eval "def #{attr}() object.read_attribute_for_serialization(:#{attr}) end", __FILE__, __LINE__
+ end
end
def associate(klass, attrs) #:nodoc:
@@ -376,6 +380,10 @@ def initialize(object, options={})
@object, @options = object, options
end
+ def url_options
+ @options[:url_options]
+ end
+
# Returns a json representation of the serializable
# object including the root.
def as_json(options=nil)
@@ -490,11 +498,14 @@ def attributes
hash = {}
_attributes.each do |name,key|
- hash[key] = self.respond_to?(name) ? self.send(name) : @object.read_attribute_for_serialization(name)
+ #hash[key] = self.respond_to?(name) ? self.send(name) : @object.read_attribute_for_serialization(name)
+ hash[key] = read_attribute_for_serialization(name)
end
hash
end
+
+ alias :read_attribute_for_serialization :send
end
end
View
5 lib/active_model/serializers/version.rb
@@ -0,0 +1,5 @@
+module ActiveModel
+ class Serializer
+ VERSION = "0.5.0"
+ end
+end
View
8 lib/active_model_serializers.rb
@@ -10,6 +10,12 @@ class Railtie < Rails::Railtie
Rails::Generators.configure!(app.config.generators)
require "generators/resource_override"
end
+
+ initializer "include_routes.active_model_serializer" do |app|
+ ActiveSupport.on_load(:active_model_serializers) do
+ include app.routes.url_helpers
+ end
+ end
end
end
end
@@ -57,3 +63,5 @@ def active_model_serializer
rescue LoadError => ex
# rails on installed, continuing
end
+
+ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)
View
2  lib/generators/serializer/serializer_generator.rb
@@ -30,7 +30,7 @@ def parent_class_name
# Only works on 3.2
# elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
# "ApplicationSerializer"
- elsif defined?(:ApplicationSerializer)
+ elsif defined?(::ApplicationSerializer)
"ApplicationSerializer"
else
"ActiveModel::Serializer"
View
21 test/serialization_test.rb
@@ -54,6 +54,18 @@ def as_json(*)
end
end
+ class HypermediaSerializable
+ def active_model_serializer
+ HypermediaSerializer
+ end
+ end
+
+ class HypermediaSerializer < ActiveModel::Serializer
+ def as_json(*)
+ { :link => hypermedia_url }
+ end
+ end
+
class TestController < ActionController::Base
protect_from_forgery
@@ -124,6 +136,10 @@ def render_json_with_custom_serializer
render :json => [], :serializer => CustomSerializer
end
+ def render_json_with_links
+ render :json => HypermediaSerializable.new
+ end
+
private
def default_serializer_options
if params[:check_defaults]
@@ -229,4 +245,9 @@ def test_render_json_with_custom_serializer
get :render_json_with_custom_serializer
assert_match '{"hello":true}', @response.body
end
+
+ def test_render_json_with_links
+ get :render_json_with_links
+ assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body
+ end
end
View
77 test/serializer_test.rb
@@ -48,14 +48,6 @@ class Comment < Model
def active_model_serializer; CommentSerializer; end
end
- class Match
- include ActiveModel::SerializerSupport
-
- def player1 ; 'testPlayer1' ; end
- def player2 ; 'testPlayer2' ; end
- def winner ; 'testPlayer1' ; end
- end
-
class UserSerializer < ActiveModel::Serializer
attributes :first_name, :last_name
@@ -152,6 +144,12 @@ def test_serializer_receives_scope
}, hash)
end
+ def test_serializer_receives_url_options
+ user = User.new
+ user_serializer = UserSerializer.new(user, :url_options => { :host => "test.local" })
+ assert_equal({ :host => "test.local" }, user_serializer.url_options)
+ end
+
def test_pretty_accessors
user = User.new
user.superuser = true
@@ -835,18 +833,65 @@ def test_embed_with_include_inserts_at_root
}, actual)
end
- def test_attribute_calls_serializer_method_if_existing
- match = Match.new
- actual = MatchSerializer.new(match, view_results: false).as_json
-
+ def test_can_customize_attributes
+ serializer = Class.new(ActiveModel::Serializer) do
+ attributes :title, :body
+
+ def title
+ object.title.upcase
+ end
+ end
+
+ klass = Class.new do
+ def read_attribute_for_serialization(name)
+ { :title => "New post!", :body => "First post body" }[name]
+ end
+
+ def title
+ read_attribute_for_serialization(:title)
+ end
+
+ def body
+ read_attribute_for_serialization(:body)
+ end
+ end
+
+ object = klass.new
+
+ actual = serializer.new(object, :root => :post).as_json
+
assert_equal({
- match: { player1: 'testPlayer1', player2: 'testPlayer2', winner: nil }
+ :post => {
+ :title => "NEW POST!",
+ :body => "First post body"
+ }
}, actual)
+ end
+
+ def test_can_customize_attributes_with_read_attributes
+ serializer = Class.new(ActiveModel::Serializer) do
+ attributes :title, :body
+
+ def read_attribute_for_serialization(name)
+ { :title => "New post!", :body => "First post body" }[name]
+ end
+ end
+
+ actual = serializer.new(Object.new, :root => :post).as_json
- actual = MatchSerializer.new(match, view_results: true).as_json
-
assert_equal({
- match: { player1: 'testPlayer1', player2: 'testPlayer2', winner: 'testPlayer1' }
+ :post => {
+ :title => "New post!",
+ :body => "First post body"
+ }
}, actual)
end
+
+ def test_active_support_on_load_hooks_fired
+ loaded = nil
+ ActiveSupport.on_load(:active_model_serializers) do
+ loaded = self
+ end
+ assert_equal ActiveModel::Serializer, loaded
+ end
end
View
6 test/test_helper.rb
@@ -20,11 +20,13 @@
module TestHelper
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
+ resource :hypermedia
match ':controller(/:action(/:id))'
match ':controller(/:action)'
end
ActionController::Base.send :include, Routes.url_helpers
+ ActiveModel::Serializer.send :include, Routes.url_helpers
end
ActiveSupport::TestCase.class_eval do
@@ -32,3 +34,7 @@ module TestHelper
@routes = ::TestHelper::Routes
end
end
+
+class Object
+ undef_method :id if respond_to?(:id)
+end

No commit comments for this range

Something went wrong with that request. Please try again.