undo-serializer-active_model 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d12c416b6ad607a212cc6d5b3d66f7aa5164f72
4
- data.tar.gz: 5ec5d4c5db2e21f6944839c7e378ff2ba128f90e
3
+ metadata.gz: 6188d00d90207d48156387473c490acea184f15f
4
+ data.tar.gz: bdbd1c4a303f7bd9369b14b44e80f7efd4670d6d
5
5
  SHA512:
6
- metadata.gz: 4562cb102abbe4dd871a6fa7578061b4cc5dea5cf0c38933da76086f7544305d8bd4be2ace75b3afcd6b371cb4dc17fd44f7ceb0bb302fd3a03c1287ed7b6098
7
- data.tar.gz: f95498485f8b6659fe465c00bfaa74a9bdf759b8f6430abb649774f835ce3d49477faf21cfcd24186517ccb53d97a80755aa9d878ae2173feda2ce6ed354200a
6
+ metadata.gz: 784789d8a4f0312a731133d06fa067fb960c22344202f645c58a7a3722720928e709878d4b31b4f220dad34777cc437726001bd3c7f3140f1c2f7d7076d22ec6
7
+ data.tar.gz: 2559ff72d0eff7ecff7bb9bdcd9c298444afe5fb39287faac00cbbfb151dff88184f0921929693fa497ffb074579f39fed5e2487715c7adff4b5ed9b974100ce
data/.travis.yml CHANGED
@@ -1,17 +1,19 @@
1
1
  language: ruby
2
+ bundler_args: --without development
3
+ cache: bundler
2
4
  rvm:
3
5
  - 2.1
4
6
  - 2.0
5
7
  - 1.9.3
6
8
  - ruby-head
7
- - rbx
9
+ - rbx-2
8
10
  - jruby-19mode
9
11
  - jruby-head
10
12
 
11
13
  matrix:
12
14
  allow_failures:
13
15
  - rvm: ruby-head
14
- - rvm: rbx
16
+ - rvm: rbx-2
15
17
  - rvm: jruby-19mode
16
18
  - rvm: jruby-head
17
19
 
data/Gemfile CHANGED
@@ -1,22 +1,26 @@
1
1
  source 'https://rubygems.org'
2
- gemspec
2
+
3
+ gem "rake"
4
+
5
+ group :development do
6
+ gem 'pry'
7
+ platforms :ruby_19, :ruby_20 do
8
+ gem 'pry-plus'
9
+ end
10
+ end
3
11
 
4
12
  group :test do
13
+ gem "coveralls", require: false
14
+ end
15
+
16
+ group :test, :development do
17
+ gem "rspec", ">= 3.0.0.beta1"
5
18
  gem 'rails', '4.0.2'
6
19
  gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
7
20
  gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
8
21
  gem 'factory_girl'
9
22
  gem 'faker'
10
- if !!ENV['CI']
11
- gem "coveralls"
12
- else
13
- gem "pry"
14
- gem "pry-plus" if "ruby" == RUBY_ENGINE
15
- end
23
+ gem "active_model_serializers", "~> 0.8"
16
24
  end
17
25
 
18
- platforms :rbx do
19
- gem 'racc'
20
- gem 'rubysl', '~> 2.0'
21
- gem 'psych'
22
- end
26
+ gemspec
data/README.md CHANGED
@@ -7,6 +7,8 @@ Undo
7
7
 
8
8
  ActiveModel serializer for Undo gem.
9
9
 
10
+ Designed to be used with `gem "active_model_serializers"`, but does not depends on it.
11
+
10
12
  Contents
11
13
  ---------
12
14
  1. Installation
@@ -34,44 +36,62 @@ Or install it yourself as:
34
36
  Requirements
35
37
  ------------
36
38
  1. Ruby >= 1.9
37
- 1. `active_model_serializers` "~> 0.8"
38
- 1. `activesupport` (`active_model_serializers` depends on it too)
39
+ 1. `activesupport` (`active_model_serializers` depends on it)
39
40
 
40
41
  Usage
41
42
  ------------
42
43
 
43
- It is required to set `somethig___association_class_name` as `key` in `active_model_serializer`:
44
+ Gem is designed to be used with Undo gem.
45
+ Add it in global config:
46
+
44
47
  ``` ruby
45
- class UserSerializer < ActiveModel::Serializer
46
- attributes *User.attribute_names.map(&:to_sym)
47
- has_many :roles, key: :has_many___roles
48
+ Undo.configure do |config|
49
+ config.serializer = Undo::Serializer::ActiveModel.new
48
50
  end
49
51
  ```
50
52
 
51
- Gem is designed to be used with Undo gem.
52
- Add it in global config:
53
+ Custom serializer could be provided to the adapter:
54
+ ``` ruby
55
+ Undo.configure do |config|
56
+ config.serializer =
57
+ Undo::Serializer::ActiveModel.new serializer: ->(object) { "#{object.class.name}UndoSerializer".constantize.new(object) }
58
+ end
59
+ ```
53
60
 
61
+ Or it may be initialized by serializer instance:
54
62
  ``` ruby
55
63
  Undo.configure do |config|
56
- config.serializer = Undo::Serializer::ActiveModel.new
64
+ config.serializer =
65
+ Undo::Serializer::ActiveModel.new CustomSerializer.new
57
66
  end
58
67
  ```
59
68
 
60
- or use in place:
69
+ As usual any Undo configuration may be set in place on wrap and restore:
61
70
  ``` ruby
62
71
  Undo.wrap user, serializer: Undo::Serializer::ActiveModel.new
63
72
  Undo.restore uuid, serializer: Undo::Serializer::ActiveModel.new
64
73
  ```
65
74
 
66
- In place using the specific serializer from `gem active_model_serializers`:
75
+ In place using the specific serializer from `gem "active_model_serializers"`:
67
76
  ``` ruby
68
77
  Undo.wrap user, serializer: Undo::Serializer::ActiveModel.new(UserSerializer.new(user))
69
78
  ```
70
79
 
80
+ ### Associations
81
+
82
+ It is required to set `somethig___association_class_name` as `key` in `active_model_serializer`:
83
+ ``` ruby
84
+ class UserSerializer < ActiveModel::Serializer
85
+ attributes *User.attribute_names.map(&:to_sym)
86
+ has_many :roles, key: :has_many___roles
87
+ end
88
+ ```
89
+
71
90
 
72
91
  Contacts
73
92
  -------------
74
93
  Have questions or recommendations? Contact me via `alexander.n.paramonov@gmail.com`
94
+
75
95
  Found a bug or have enhancement request? You are welcome at [Github bugtracker](https://github.com/AlexParamonov/undo-serializer-active_model/issues)
76
96
 
77
97
 
@@ -83,7 +103,7 @@ tested with Ruby
83
103
  * 2.0
84
104
  * 1.9.3
85
105
  * ruby-head
86
- * rbx
106
+ * rbx-2
87
107
  * jruby-19mode
88
108
  * jruby-head
89
109
 
@@ -100,5 +120,5 @@ See [build history](http://travis-ci.org/#!/AlexParamonov/undo-serializer-active
100
120
 
101
121
  Copyright
102
122
  ---------
103
- Copyright © 2014 Alexander Paramonov.
123
+ Copyright © 2014 Alexander Paramonov.
104
124
  Released under the MIT License. See the LICENSE file for further details.
data/Rakefile CHANGED
@@ -4,7 +4,8 @@ require 'rspec/core/rake_task'
4
4
  namespace :ci do
5
5
  desc "Run tests on CI"
6
6
  RSpec::Core::RakeTask.new('all') do |t|
7
- t.rspec_opts = '-fprogress'
8
7
  t.verbose = true
9
8
  end
10
9
  end
10
+
11
+ task :default => "ci:all"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.3
@@ -1,14 +1,17 @@
1
+ require "active_support"
2
+
1
3
  module Undo
2
4
  module Serializer
3
5
  class ActiveModel
4
- VERSION = "0.0.2"
5
-
6
- def initialize(model_serializer = nil)
7
- @model_serializer = model_serializer
6
+ def initialize(*args)
7
+ options = args.extract_options!
8
+ @serializer = args.first
9
+ @serializer_source = options.fetch :serializer,
10
+ ->(object) { object.active_model_serializer.new object }
8
11
  end
9
12
 
10
13
  def serialize(object)
11
- model_serializer(object).as_json
14
+ serializer(object).as_json
12
15
  end
13
16
 
14
17
  def deserialize(hash)
@@ -35,9 +38,10 @@ module Undo
35
38
  end
36
39
 
37
40
  private
38
- def model_serializer(object)
39
- @model_serializer ||= object.active_model_serializer
40
- @model_serializer.new object
41
+ attr_reader :serializer_source
42
+
43
+ def serializer(object)
44
+ @serializer ||= serializer_source.call object
41
45
  end
42
46
 
43
47
  def deserialize_association(association, values)
@@ -0,0 +1,14 @@
1
+ if !!ENV["CI"] || "test" == ENV["RAILS_ENV"]
2
+ require 'simplecov'
3
+ require 'coveralls'
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+
9
+ SimpleCov.start do
10
+ add_filter '/spec/'
11
+ add_filter '/vendor/'
12
+ minimum_coverage(90)
13
+ end
14
+ end
@@ -1,4 +1,7 @@
1
- test:
1
+ test: &test
2
2
  adapter: <%= "jruby" == RUBY_ENGINE ? "jdbcsqlite3" : "sqlite3" %>
3
3
  database: ":memory:"
4
4
  timeout: 5000
5
+
6
+ development:
7
+ <<: *test
@@ -0,0 +1,34 @@
1
+ RailsApp::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+ config.eager_load = false
10
+
11
+ # Show full error reports and disable caching
12
+ config.consider_all_requests_local = true
13
+ config.action_controller.perform_caching = false
14
+
15
+ # Raise exceptions instead of rendering exception templates
16
+ config.action_dispatch.show_exceptions = false
17
+
18
+ # Disable request forgery protection in test environment
19
+ config.action_controller.allow_forgery_protection = false
20
+
21
+ # Tell Action Mailer not to deliver emails to the real world.
22
+ # The :test delivery method accumulates sent emails in the
23
+ # ActionMailer::Base.deliveries array.
24
+ config.action_mailer.delivery_method = :test
25
+ config.action_mailer.default_url_options = { :host => 'www.example.com' }
26
+
27
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
28
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
29
+ # like if you have constraints or database-specific column types
30
+ # config.active_record.schema_format = :sql
31
+
32
+ # Print deprecation notices to the stderr
33
+ config.active_support.deprecation = :stderr
34
+ end
@@ -0,0 +1,10 @@
1
+ require_relative "coverage_helper"
2
+ require "undo/serializer/active_model"
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec do |c|
6
+ c.syntax = :expect
7
+ end
8
+ end
9
+
10
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
@@ -1,22 +1,10 @@
1
- if !!ENV['CI']
2
- require 'coveralls'
3
- Coveralls.wear!
4
- else
5
- require 'pry'
6
- end
7
-
8
- ENV['RAILS_ENV'] ||= 'test'
9
-
10
- require File.expand_path('../rails_app/config/environment', __FILE__)
11
- require 'rspec'
1
+ require_relative 'spec_helper_lite'
2
+ require_relative 'rails_app/config/environment'
12
3
  require_relative 'factories'
13
4
 
14
5
  ActiveRecord::Migration.verbose = false
15
6
  ActiveRecord::Migrator.migrate(Rails.root.join('db', 'migrate').to_s)
16
7
 
17
8
  RSpec.configure do |config|
18
- config.mock_with :rspec do |config|
19
- config.syntax = :expect
20
- end
21
9
  config.include FactoryGirl::Syntax::Methods
22
10
  end
@@ -0,0 +1,44 @@
1
+ require "spec_helper_rails"
2
+
3
+ describe Undo::Serializer::ActiveModel do
4
+ subject { described_class }
5
+ let(:serializer) { subject.new }
6
+ let(:user) { create :user }
7
+
8
+ it "restores object" do
9
+ hash = serializer.serialize user
10
+ user.destroy
11
+ restored_user = serializer.deserialize hash
12
+
13
+ expect(restored_user).to eq user
14
+ expect(user).not_to be_persisted
15
+ expect(restored_user).to be_persisted
16
+ end
17
+
18
+ it "restores object and associations" do
19
+ roles = create_list :role, 3, user: user
20
+ hash = serializer.serialize user
21
+ user.destroy
22
+ restored_user = serializer.deserialize hash
23
+
24
+ expect(restored_user).to eq user
25
+ expect(restored_user.roles).to eq roles
26
+ end
27
+
28
+ it "reverts changes to object" do
29
+ hash = serializer.serialize user
30
+ user.name = "Changed"
31
+ user.save!
32
+
33
+ restored_user = serializer.deserialize hash
34
+
35
+ expect(restored_user.name).not_to eq "Changed"
36
+ expect(restored_user).to eq user.reload
37
+ end
38
+
39
+ it "detects default serializer for a model" do
40
+ serializer = subject.new
41
+ expect(UserSerializer).to receive(:new)
42
+ serializer.serialize(user)
43
+ end
44
+ end
@@ -1,54 +1,41 @@
1
- require "spec_helper_rails"
2
- require "undo/serializer/active_model"
1
+ require "spec_helper_lite"
3
2
 
4
3
  describe Undo::Serializer::ActiveModel do
5
4
  subject { described_class }
6
- let(:serializer) { described_class.new UserSerializer }
7
- let(:user) { create :user }
5
+ let(:serializer) { subject.new }
6
+ let(:object) { double :object }
7
+ let(:am_serializer_method) { :as_json }
8
8
 
9
- it "restores object" do
10
- hash = serializer.serialize user
11
- user.destroy
12
- restored_user = serializer.deserialize hash
13
-
14
- expect(restored_user).to eq user
15
- expect(user).not_to be_persisted
16
- expect(restored_user).to be_persisted
17
- end
9
+ describe "custom serializer" do
10
+ it "uses provided serializer" do
11
+ custom_serializer = double :custom_serializer
12
+ serializer = subject.new custom_serializer
18
13
 
19
- it "restores object and associations" do
20
- roles = create_list :role, 3, user: user
21
- hash = serializer.serialize user
22
- user.destroy
23
- restored_user = serializer.deserialize hash
14
+ expect(custom_serializer).to receive am_serializer_method
15
+ serializer.serialize object
16
+ end
24
17
 
25
- expect(restored_user).to eq user
26
- expect(restored_user.roles).to eq roles
27
- end
18
+ it "uses custom serializer source" do
19
+ custom_serializer_source = double :custom_serializer_source
20
+ custom_serializer = double :custom_serializer
21
+ serializer = subject.new serializer: custom_serializer_source
28
22
 
29
- it "reverts changes to object" do
30
- hash = serializer.serialize user
31
- user.name = "Changed"
32
- user.save!
23
+ expect(custom_serializer_source).to receive(:call).with(object) { custom_serializer }
24
+ expect(custom_serializer).to receive am_serializer_method
25
+ serializer.serialize object
26
+ end
33
27
 
34
- restored_user = serializer.deserialize hash
28
+ it "has lower priority than providing the serializer directly" do
29
+ custom_serializer_source = double :custom_serializer_source
30
+ custom_serializer = double :custom_serializer
35
31
 
36
- expect(restored_user.name).not_to eq "Changed"
37
- expect(restored_user).to eq user.reload
38
- end
32
+ serializer = subject.new custom_serializer, serializer: custom_serializer_source
39
33
 
40
- describe "find model serializer" do
41
- it "detects serializer for a model" do
42
- serializer = subject.new
43
- expect(UserSerializer).to receive(:new)
44
- serializer.serialize(user)
45
- end
34
+ expect(custom_serializer_source).not_to receive(:call)
35
+ expect(custom_serializer).to receive am_serializer_method
46
36
 
47
- it "uses provided serializer" do
48
- model_serializer = double :model_serializer
49
- serializer = subject.new model_serializer
50
- expect(model_serializer).to receive(:new)
51
- serializer.serialize(user)
37
+ serializer.serialize object
52
38
  end
53
39
  end
40
+
54
41
  end
@@ -1,11 +1,10 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'undo/serializer/active_model'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = "undo-serializer-active_model"
8
- spec.version = Undo::Serializer::ActiveModel::VERSION
7
+ spec.version = IO.read("VERSION")
9
8
  spec.authors = ["Alexander Paramonov"]
10
9
  spec.email = ["alexander.n.paramonov@gmail.com"]
11
10
  spec.summary = %q{ActiveModel serializer for Undo gem.}
@@ -19,8 +18,5 @@ Gem::Specification.new do |spec|
19
18
  spec.require_paths = ["lib"]
20
19
 
21
20
  spec.add_dependency "activesupport"
22
- spec.add_dependency "active_model_serializers", "~> 0.8"
23
- spec.add_development_dependency "bundler", "~> 1.5"
24
- spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rspec", ">= 3.0.0.beta1"
21
+ spec.add_development_dependency 'bundler', '~> 1.0'
26
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: undo-serializer-active_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Paramonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-18 00:00:00.000000000 Z
11
+ date: 2014-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,62 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: active_model_serializers
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '0.8'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0.8'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: bundler
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: '1.5'
33
+ version: '1.0'
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: '1.5'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: 3.0.0.beta1
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: 3.0.0.beta1
40
+ version: '1.0'
83
41
  description: ActiveModel serializer for Undo gem.
84
42
  email:
85
43
  - alexander.n.paramonov@gmail.com
@@ -97,7 +55,9 @@ files:
97
55
  - LICENSE.txt
98
56
  - README.md
99
57
  - Rakefile
58
+ - VERSION
100
59
  - lib/undo/serializer/active_model.rb
60
+ - spec/coverage_helper.rb
101
61
  - spec/factories.rb
102
62
  - spec/rails_app/.gitignore
103
63
  - spec/rails_app/Rakefile
@@ -110,6 +70,7 @@ files:
110
70
  - spec/rails_app/config/boot.rb
111
71
  - spec/rails_app/config/database.yml
112
72
  - spec/rails_app/config/environment.rb
73
+ - spec/rails_app/config/environments/development.rb
113
74
  - spec/rails_app/config/environments/test.rb
114
75
  - spec/rails_app/config/routes.rb
115
76
  - spec/rails_app/db/migrate/20140214213610_create_users.rb
@@ -117,7 +78,9 @@ files:
117
78
  - spec/rails_app/db/schema.rb
118
79
  - spec/rails_app/db/seeds.rb
119
80
  - spec/rails_app/script/rails
81
+ - spec/spec_helper_lite.rb
120
82
  - spec/spec_helper_rails.rb
83
+ - spec/undo/serializer/active_model_integration_spec.rb
121
84
  - spec/undo/serializer/active_model_spec.rb
122
85
  - undo-serializer-active_model.gemspec
123
86
  homepage: http://github.com/AlexParamonov/undo-serializer-active_model
@@ -145,6 +108,7 @@ signing_key:
145
108
  specification_version: 4
146
109
  summary: ActiveModel serializer for Undo gem.
147
110
  test_files:
111
+ - spec/coverage_helper.rb
148
112
  - spec/factories.rb
149
113
  - spec/rails_app/.gitignore
150
114
  - spec/rails_app/Rakefile
@@ -157,6 +121,7 @@ test_files:
157
121
  - spec/rails_app/config/boot.rb
158
122
  - spec/rails_app/config/database.yml
159
123
  - spec/rails_app/config/environment.rb
124
+ - spec/rails_app/config/environments/development.rb
160
125
  - spec/rails_app/config/environments/test.rb
161
126
  - spec/rails_app/config/routes.rb
162
127
  - spec/rails_app/db/migrate/20140214213610_create_users.rb
@@ -164,6 +129,8 @@ test_files:
164
129
  - spec/rails_app/db/schema.rb
165
130
  - spec/rails_app/db/seeds.rb
166
131
  - spec/rails_app/script/rails
132
+ - spec/spec_helper_lite.rb
167
133
  - spec/spec_helper_rails.rb
134
+ - spec/undo/serializer/active_model_integration_spec.rb
168
135
  - spec/undo/serializer/active_model_spec.rb
169
136
  has_rdoc: