active_model_serializers 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,6 +49,7 @@ module ActionController
49
49
 
50
50
  if serializer
51
51
  options[:scope] = serialization_scope
52
+ options[:url_options] = url_options
52
53
  json = serializer.new(json, options.merge(default_serializer_options || {}))
53
54
  end
54
55
  super
@@ -246,12 +246,16 @@ module ActiveModel
246
246
  self._attributes = _attributes.dup
247
247
 
248
248
  attrs.each do |attr|
249
- self._attributes[attr] = attr
249
+ attribute attr
250
250
  end
251
251
  end
252
252
 
253
253
  def attribute(attr, options={})
254
254
  self._attributes = _attributes.merge(attr => options[:key] || attr)
255
+
256
+ unless method_defined?(attr)
257
+ class_eval "def #{attr}() object.read_attribute_for_serialization(:#{attr}) end", __FILE__, __LINE__
258
+ end
255
259
  end
256
260
 
257
261
  def associate(klass, attrs) #:nodoc:
@@ -376,6 +380,10 @@ module ActiveModel
376
380
  @object, @options = object, options
377
381
  end
378
382
 
383
+ def url_options
384
+ @options[:url_options]
385
+ end
386
+
379
387
  # Returns a json representation of the serializable
380
388
  # object including the root.
381
389
  def as_json(options=nil)
@@ -490,11 +498,13 @@ module ActiveModel
490
498
  hash = {}
491
499
 
492
500
  _attributes.each do |name,key|
493
- hash[key] = @object.read_attribute_for_serialization(name)
501
+ hash[key] = read_attribute_for_serialization(name)
494
502
  end
495
503
 
496
504
  hash
497
505
  end
506
+
507
+ alias :read_attribute_for_serialization :send
498
508
  end
499
509
  end
500
510
 
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
- VERSION = "0.5.0"
3
+ VERSION = "0.5.1"
4
4
  end
5
5
  end
@@ -10,6 +10,12 @@ if defined?(Rails)
10
10
  Rails::Generators.configure!(app.config.generators)
11
11
  require "generators/resource_override"
12
12
  end
13
+
14
+ initializer "include_routes.active_model_serializer" do |app|
15
+ ActiveSupport.on_load(:active_model_serializers) do
16
+ include app.routes.url_helpers
17
+ end
18
+ end
13
19
  end
14
20
  end
15
21
  end
@@ -57,3 +63,5 @@ begin
57
63
  rescue LoadError => ex
58
64
  # rails on installed, continuing
59
65
  end
66
+
67
+ ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)
@@ -30,7 +30,7 @@ module Rails
30
30
  # Only works on 3.2
31
31
  # elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
32
32
  # "ApplicationSerializer"
33
- elsif defined?(:ApplicationSerializer)
33
+ elsif defined?(::ApplicationSerializer)
34
34
  "ApplicationSerializer"
35
35
  else
36
36
  "ActiveModel::Serializer"
@@ -16,12 +16,12 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
16
16
 
17
17
  def test_generates_a_serializer
18
18
  run_generator
19
- assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
19
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/
20
20
  end
21
21
 
22
22
  def test_generates_a_namespaced_serializer
23
23
  run_generator ["admin/account"]
24
- assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ApplicationSerializer/
24
+ assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/
25
25
  end
26
26
 
27
27
  def test_uses_application_serializer_if_one_exists
@@ -62,6 +62,6 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
62
62
 
63
63
  def test_with_no_attributes_does_not_add_extra_space
64
64
  run_generator ["account"]
65
- assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer\nend/
65
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer\nend/
66
66
  end
67
67
  end
@@ -54,6 +54,18 @@ class RenderJsonTest < ActionController::TestCase
54
54
  end
55
55
  end
56
56
 
57
+ class HypermediaSerializable
58
+ def active_model_serializer
59
+ HypermediaSerializer
60
+ end
61
+ end
62
+
63
+ class HypermediaSerializer < ActiveModel::Serializer
64
+ def as_json(*)
65
+ { :link => hypermedia_url }
66
+ end
67
+ end
68
+
57
69
  class TestController < ActionController::Base
58
70
  protect_from_forgery
59
71
 
@@ -124,6 +136,10 @@ class RenderJsonTest < ActionController::TestCase
124
136
  render :json => [], :serializer => CustomSerializer
125
137
  end
126
138
 
139
+ def render_json_with_links
140
+ render :json => HypermediaSerializable.new
141
+ end
142
+
127
143
  private
128
144
  def default_serializer_options
129
145
  if params[:check_defaults]
@@ -229,4 +245,9 @@ class RenderJsonTest < ActionController::TestCase
229
245
  get :render_json_with_custom_serializer
230
246
  assert_match '{"hello":true}', @response.body
231
247
  end
248
+
249
+ def test_render_json_with_links
250
+ get :render_json_with_links
251
+ assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body
252
+ end
232
253
  end
@@ -132,6 +132,12 @@ class SerializerTest < ActiveModel::TestCase
132
132
  }, hash)
133
133
  end
134
134
 
135
+ def test_serializer_receives_url_options
136
+ user = User.new
137
+ user_serializer = UserSerializer.new(user, :url_options => { :host => "test.local" })
138
+ assert_equal({ :host => "test.local" }, user_serializer.url_options)
139
+ end
140
+
135
141
  def test_pretty_accessors
136
142
  user = User.new
137
143
  user.superuser = true
@@ -814,4 +820,66 @@ class SerializerTest < ActiveModel::TestCase
814
820
  ]
815
821
  }, actual)
816
822
  end
823
+
824
+ def test_can_customize_attributes
825
+ serializer = Class.new(ActiveModel::Serializer) do
826
+ attributes :title, :body
827
+
828
+ def title
829
+ object.title.upcase
830
+ end
831
+ end
832
+
833
+ klass = Class.new do
834
+ def read_attribute_for_serialization(name)
835
+ { :title => "New post!", :body => "First post body" }[name]
836
+ end
837
+
838
+ def title
839
+ read_attribute_for_serialization(:title)
840
+ end
841
+
842
+ def body
843
+ read_attribute_for_serialization(:body)
844
+ end
845
+ end
846
+
847
+ object = klass.new
848
+
849
+ actual = serializer.new(object, :root => :post).as_json
850
+
851
+ assert_equal({
852
+ :post => {
853
+ :title => "NEW POST!",
854
+ :body => "First post body"
855
+ }
856
+ }, actual)
857
+ end
858
+
859
+ def test_can_customize_attributes_with_read_attributes
860
+ serializer = Class.new(ActiveModel::Serializer) do
861
+ attributes :title, :body
862
+
863
+ def read_attribute_for_serialization(name)
864
+ { :title => "New post!", :body => "First post body" }[name]
865
+ end
866
+ end
867
+
868
+ actual = serializer.new(Object.new, :root => :post).as_json
869
+
870
+ assert_equal({
871
+ :post => {
872
+ :title => "New post!",
873
+ :body => "First post body"
874
+ }
875
+ }, actual)
876
+ end
877
+
878
+ def test_active_support_on_load_hooks_fired
879
+ loaded = nil
880
+ ActiveSupport.on_load(:active_model_serializers) do
881
+ loaded = self
882
+ end
883
+ assert_equal ActiveModel::Serializer, loaded
884
+ end
817
885
  end
@@ -20,11 +20,13 @@ require 'rails'
20
20
  module TestHelper
21
21
  Routes = ActionDispatch::Routing::RouteSet.new
22
22
  Routes.draw do
23
+ resource :hypermedia
23
24
  match ':controller(/:action(/:id))'
24
25
  match ':controller(/:action)'
25
26
  end
26
27
 
27
28
  ActionController::Base.send :include, Routes.url_helpers
29
+ ActiveModel::Serializer.send :include, Routes.url_helpers
28
30
  end
29
31
 
30
32
  ActiveSupport::TestCase.class_eval do
@@ -32,3 +34,7 @@ ActiveSupport::TestCase.class_eval do
32
34
  @routes = ::TestHelper::Routes
33
35
  end
34
36
  end
37
+
38
+ class Object
39
+ undef_method :id if respond_to?(:id)
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-16 00:00:00.000000000 Z
13
+ date: 2012-05-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
17
- requirement: !ruby/object:Gem::Requirement
17
+ requirement: &2153228860 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,15 +22,10 @@ dependencies:
22
22
  version: '3.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ~>
29
- - !ruby/object:Gem::Version
30
- version: '3.0'
25
+ version_requirements: *2153228860
31
26
  - !ruby/object:Gem::Dependency
32
27
  name: rails
33
- requirement: !ruby/object:Gem::Requirement
28
+ requirement: &2153227780 !ruby/object:Gem::Requirement
34
29
  none: false
35
30
  requirements:
36
31
  - - ~>
@@ -38,12 +33,7 @@ dependencies:
38
33
  version: '3.0'
39
34
  type: :development
40
35
  prerelease: false
41
- version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
- requirements:
44
- - - ~>
45
- - !ruby/object:Gem::Version
46
- version: '3.0'
36
+ version_requirements: *2153227780
47
37
  description: Making it easy to serialize models for client-side use
48
38
  email:
49
39
  - jose.valim@gmail.com
@@ -95,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
85
  version: '0'
96
86
  requirements: []
97
87
  rubyforge_project:
98
- rubygems_version: 1.8.24
88
+ rubygems_version: 1.8.15
99
89
  signing_key:
100
90
  specification_version: 3
101
91
  summary: Bringing consistency and object orientation to model serialization. Works
@@ -107,3 +97,4 @@ test_files:
107
97
  - test/serializer_support_test.rb
108
98
  - test/serializer_test.rb
109
99
  - test/test_helper.rb
100
+ has_rdoc: