diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..87cabff9 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,131 @@ +# 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. + +* embed and include were removed from AM::Serializer and there's a global config for them in AM::Serializers + + So code like ... + + class PostSerializer < ActiveModel::Serializer + embed :ids, :include => true + has_many :comments + end + + should be changed to ... + + class PostSerializer < ActiveModel::Serializer + has_many :comments, :embed => :ids, :include => true + end + + or you could change the global defaults by adding ... + + config.active\_model\_serializers.embed = :ids + config.active\_model\_serializers.include = true + + to the config/application.rb file + +# VERSION 0.8.1 + +* Fix bug whereby a serializer using 'options' would blow up. + +# VERSION 0.8.0 + +* Attributes can now have optional types. + +* A new DefaultSerializer ensures that POROs behave the same way as ActiveModels. + +* If you wish to override ActiveRecord::Base#to\_Json, you can now require + 'active\_record/serializer\_override'. We don't recommend you do this, but + many users do, so we've left it optional. + +* Fixed a bug where ActionController wouldn't always have MimeResponds. + +* An optinal caching feature allows you to cache JSON & hashes that AMS uses. + Adding 'cached true' to your Serializers will turn on this cache. + +* URL helpers used inside of Engines now work properly. + +* Serializers now can filter attributes with `only` and `except`: + + ``` + UserSerializer.new(user, only: [:first_name, :last_name]) + UserSerializer.new(user, except: :first_name) + ``` + +* Basic Mongoid support. We now include our mixins in the right place. + +* On Ruby 1.8, we now generate an `id` method that properly serializes `id` + columns. See issue #127 for more. + +* Add an alias for `scope` method to be the name of the context. By default + this is `current_user`. The name is automatically set when using + `serialization_scope` in the controller. + +* Pass through serialization options (such as `:include`) when a model + has no serializer defined. + +# VERSION 0.7.0 + +* ```embed_key``` option to allow embedding by attributes other than IDs +* Fix rendering nil with custom serializer +* Fix global ```self.root = false``` +* Add support for specifying the serializer for an association as a String +* Able to specify keys on the attributes method +* Serializer Reloading via ActiveSupport::DescendantsTracker +* Reduce double map to once; Fixes datamapper eager loading. + +# VERSION 0.6.0 + +* Serialize sets properly +* Add root option to ArraySerializer +* Support polymorphic associations +* Support :each_serializer in ArraySerializer +* Add `scope` method to easily access the scope in the serializer +* Fix regression with Rails 3.2.6; add Rails 4 support +* 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) + +* First tagged version +* Changes generators to always generate an ApplicationSerializer diff --git a/MIT-LICENSE.txt b/MIT-LICENSE.txt new file mode 100644 index 00000000..75e6db1a --- /dev/null +++ b/MIT-LICENSE.txt @@ -0,0 +1,21 @@ +Copyright (c) 2011-2012 José Valim & Yehuda Katz + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..0a1d1f00 --- /dev/null +++ b/Rakefile @@ -0,0 +1,13 @@ +#!/usr/bin/env rake +require "bundler/gem_tasks" +require "rake/testtask" + +desc 'Run tests' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +task :default => :test diff --git a/active_model_serializers.gemspec b/active_model_serializers.gemspec new file mode 100644 index 00000000..98105777 --- /dev/null +++ b/active_model_serializers.gemspec @@ -0,0 +1,25 @@ +# -*- encoding: utf-8 -*- + +$:.unshift File.expand_path("../lib", __FILE__) +require "active_model/serializer/version" + +Gem::Specification.new do |gem| + gem.authors = ["José Valim", "Yehuda Katz", "Santiago Pastorino"] + gem.email = ["jose.valim@gmail.com", "wycats@gmail.com", "santiago@wyeworks.com"] + 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.name = "active_model_serializers" + gem.require_paths = ["lib"] + gem.version = ActiveModel::Serializer::VERSION + + gem.add_dependency 'activemodel', '>= 3.0' + gem.add_development_dependency "rails", ">= 3.0" + gem.add_development_dependency "pry" + gem.add_development_dependency "simplecov" + gem.add_development_dependency "coveralls" +end diff --git a/lib/active_model/serializer/version.rb b/lib/active_model/serializer/version.rb new file mode 100644 index 00000000..10e62668 --- /dev/null +++ b/lib/active_model/serializer/version.rb @@ -0,0 +1,5 @@ +module ActiveModel + class Serializer + VERSION = "0.8.1" + end +end diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index 0e715c3c..1ffdce05 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -1,9 +1,10 @@ -require 'newbase/active_model/serializer' -require 'newbase/active_model/serializer_support' +require 'active_model' +require 'active_model/serializer' +require 'active_model/serializer_support' begin require 'action_controller' - require 'newbase/action_controller/serialization' + require 'action_controller/serialization' ActiveSupport.on_load(:action_controller) do include ::ActionController::Serialization diff --git a/test/integration/action_controller/serialization_test.rb b/test/integration/action_controller/serialization_test.rb index 037fd19d..553e4bb9 100644 --- a/test/integration/action_controller/serialization_test.rb +++ b/test/integration/action_controller/serialization_test.rb @@ -1,5 +1,5 @@ -require 'newbase/test_helper' -require 'newbase/active_model_serializers' +require 'test_helper' +require 'active_model_serializers' module ActionController module Serialization diff --git a/test/test_helper.rb b/test/test_helper.rb index e9ee164a..bb2eb5dc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,7 +1,7 @@ require 'bundler/setup' -require 'newbase/active_model_serializers' +require 'active_model_serializers' require 'test/unit' -require 'newbase/fixtures/poro' +require 'fixtures/poro' module TestHelper Routes = ActionDispatch::Routing::RouteSet.new diff --git a/test/unit/active_model/serializer/attributes_test.rb b/test/unit/active_model/serializer/attributes_test.rb index eba7182f..7db8364d 100644 --- a/test/unit/active_model/serializer/attributes_test.rb +++ b/test/unit/active_model/serializer/attributes_test.rb @@ -1,11 +1,11 @@ -require 'newbase/test_helper' -require 'newbase/active_model/serializer' +require 'test_helper' +require 'active_model/serializer' module ActiveModel class Serializer class AttributesTest < ActiveModel::TestCase def setup - model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) + model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) @model_serializer = ModelSerializer.new(model) end diff --git a/test/unit/active_model/serializer/meta_test.rb b/test/unit/active_model/serializer/meta_test.rb index fdfbf584..85d592a6 100644 --- a/test/unit/active_model/serializer/meta_test.rb +++ b/test/unit/active_model/serializer/meta_test.rb @@ -1,12 +1,12 @@ -require 'newbase/test_helper' -require 'newbase/active_model/serializer' +require 'test_helper' +require 'active_model/serializer' module ActiveModel class Serializer class MetaTest < ActiveModel::TestCase def setup - @model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) + @model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) end def test_meta diff --git a/test/unit/active_model/serializer/root_test.rb b/test/unit/active_model/serializer/root_test.rb index bf513553..e9e700d7 100644 --- a/test/unit/active_model/serializer/root_test.rb +++ b/test/unit/active_model/serializer/root_test.rb @@ -1,5 +1,5 @@ -require 'newbase/test_helper' -require 'newbase/active_model/serializer' +require 'test_helper' +require 'active_model/serializer' module ActiveModel class Serializer @@ -7,7 +7,7 @@ module ActiveModel def setup @old_root = ModelSerializer._root - @model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) + @model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) @serializer = ModelSerializer.new(@model, root: 'initialize') ModelSerializer._root = true end @@ -40,7 +40,7 @@ module ActiveModel }, @serializer.as_json) end - def test_root_as_argument_takes_presedence + def test_root_as_argument_takes_precedence assert_equal({ 'argument' => { 'attr1' => 'value1', 'attr2' => 'value2' diff --git a/test/unit/active_model/serializer/scope_test.rb b/test/unit/active_model/serializer/scope_test.rb index 9ec106b6..30073cb1 100644 --- a/test/unit/active_model/serializer/scope_test.rb +++ b/test/unit/active_model/serializer/scope_test.rb @@ -1,5 +1,5 @@ -require 'newbase/test_helper' -require 'newbase/active_model/serializer' +require 'test_helper' +require 'active_model/serializer' module ActiveModel class Serializer diff --git a/test/unit/active_model/serializer_support/active_record_test.rb b/test/unit/active_model/serializer_support/active_record_test.rb index f58be6e1..af9d5ed4 100644 --- a/test/unit/active_model/serializer_support/active_record_test.rb +++ b/test/unit/active_model/serializer_support/active_record_test.rb @@ -1,6 +1,6 @@ -require 'newbase/test_helper' -require 'newbase/fixtures/active_record' -require 'newbase/active_model/serializer_support' +require 'test_helper' +require 'fixtures/active_record' +require 'active_model/serializer_support' module ActiveModel module SerializerSupport diff --git a/test/unit/active_model/serializer_support/poro_test.rb b/test/unit/active_model/serializer_support/poro_test.rb index fb212007..e16a1e78 100644 --- a/test/unit/active_model/serializer_support/poro_test.rb +++ b/test/unit/active_model/serializer_support/poro_test.rb @@ -1,11 +1,11 @@ -require 'newbase/test_helper' -require 'newbase/active_model/serializer_support' +require 'test_helper' +require 'active_model/serializer_support' module ActiveModel module SerializerSupport class Test < ActiveModel::TestCase def test_active_model_on_poro_returns_its_serializer - assert_equal ModelSerializer, Model.new.active_model_serializer + assert_equal ModelSerializer, ::Model.new.active_model_serializer end end end