tire-am_serializers 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -5
- data/Rakefile +11 -0
- data/lib/tire/active_model/serializer_support.rb +27 -0
- data/lib/tire/am_serializers.rb +2 -14
- data/lib/tire/version.rb +1 -1
- data/test/test_helper.rb +21 -0
- data/test/tire_serializers_support_test.rb +45 -0
- data/tire-am_serializers.gemspec +4 -3
- metadata +10 -4
- data/lib/tire/active_model/serializer.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aaeb1f24b76db25a951875f4a20c456caba3861b
|
4
|
+
data.tar.gz: 9afd95fe25a4099a97217a2c0948e8fa0b7a5cd0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a39fc38d7b4d7df60aada0b33249b0d34e49f2c5452dde6934a18e623f899b8b41a1269c8dd5a98967e42a45aa8d5a5b91a472513a636360e923645d8118547
|
7
|
+
data.tar.gz: 6d45d3653e1c682f5b71d6d9ab741dfa729ede4b82be73d914b8f83147ee359d5b5315e5c106119d869a0b0ad2635517f189783f9cb85756cb19b6814d6af2ca
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Active Model Serializers + Tire
|
1
|
+
# Active Model Serializers + Tire (Beta)
|
2
2
|
|
3
3
|
The easy way to add [Active Model Serializers](https://github.com/rails-api/active_model_serializers) to your [Tire](https://github.com/karmi/tire/) results.
|
4
4
|
|
@@ -11,14 +11,16 @@ gem 'tire-am_serializers'
|
|
11
11
|
|
12
12
|
## Usage
|
13
13
|
|
14
|
-
|
14
|
+
Same as [Active Model Serializers](https://github.com/rails-api/active_model_serializers):
|
15
15
|
```ruby
|
16
|
-
render json: User.search(...)
|
16
|
+
render json: User.search(...)
|
17
17
|
```
|
18
18
|
|
19
|
+
There is only one thing, Rails will look for `TireUserSeralizer` by default. If class doesn't exist, it will try to find `UserSerailizer`. If no Serilaizers present for this model, it will use default behaviour of `to_json`.
|
20
|
+
|
19
21
|
If you want to get old behaviour from Tire:
|
20
22
|
```ruby
|
21
|
-
render json: Vacancy.search("*").
|
23
|
+
render json: Vacancy.search("*").to_json
|
22
24
|
```
|
23
25
|
|
24
26
|
## Contributing
|
@@ -27,4 +29,4 @@ render json: Vacancy.search("*").map(&:tire_as_json)
|
|
27
29
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
30
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
31
|
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
-
5. Create new Pull Request
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Tire
|
2
|
+
module ActiveModel
|
3
|
+
module SerializerSupport
|
4
|
+
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
if "".respond_to?(:safe_constantize)
|
8
|
+
def tire_active_model_serializer
|
9
|
+
"Tire#{self.class.name}Serializer".safe_constantize
|
10
|
+
end
|
11
|
+
else
|
12
|
+
def tire_active_model_serializer
|
13
|
+
begin
|
14
|
+
"Tire#{self.class.name}Serializer".constantize
|
15
|
+
rescue NameError => e
|
16
|
+
raise unless e.message =~ /uninitialized constant/
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns a model serializer for this object considering its namespace.
|
22
|
+
def active_model_serializer
|
23
|
+
self.tire_active_model_serializer || self.class.active_model_serializer
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/tire/am_serializers.rb
CHANGED
@@ -1,18 +1,6 @@
|
|
1
1
|
require "tire/version"
|
2
|
-
require "tire/active_model/
|
2
|
+
require "tire/active_model/serializer_support"
|
3
3
|
|
4
4
|
Tire::Results::Collection.send(:include, ActiveModel::ArraySerializerSupport)
|
5
|
-
|
6
5
|
Tire::Results::Item.send(:include, ActiveModel::SerializerSupport)
|
7
|
-
Tire::Results::Item.send(:include, Tire::ActiveModel::
|
8
|
-
|
9
|
-
Tire::Results::Item.send(:alias_method, :tire_as_json, :as_json)
|
10
|
-
|
11
|
-
Tire::Results::Item.send :define_method, :to_json do |options|
|
12
|
-
tire_as_json.to_json(options)
|
13
|
-
end
|
14
|
-
|
15
|
-
Tire::Results::Item.send(:alias_method, :tire_to_json, :to_json)
|
16
|
-
|
17
|
-
Tire::Results::Item.send(:alias_method, :to_json, :serializer_to_json)
|
18
|
-
Tire::Results::Item.send(:alias_method, :as_json, :serializer_as_json)
|
6
|
+
Tire::Results::Item.send(:include, Tire::ActiveModel::SerializerSupport)
|
data/lib/tire/version.rb
CHANGED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
require "test/unit"
|
4
|
+
require "tire"
|
5
|
+
require "active_model_serializers"
|
6
|
+
|
7
|
+
class Test::Unit::TestCase
|
8
|
+
|
9
|
+
def mock_response(body, code=200, headers={})
|
10
|
+
Tire::HTTP::Response.new(body, code, headers)
|
11
|
+
end
|
12
|
+
|
13
|
+
def fixtures_path
|
14
|
+
Pathname( File.expand_path( 'fixtures', File.dirname(__FILE__) ) )
|
15
|
+
end
|
16
|
+
|
17
|
+
def fixture_file(path)
|
18
|
+
File.read File.expand_path( path, fixtures_path )
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'tire/am_serializers'
|
4
|
+
|
5
|
+
class SomeClass
|
6
|
+
include ActiveModel::SerializerSupport
|
7
|
+
end
|
8
|
+
|
9
|
+
class SomeClassSerializer
|
10
|
+
end
|
11
|
+
|
12
|
+
class OtherClass
|
13
|
+
include ActiveModel::SerializerSupport
|
14
|
+
end
|
15
|
+
|
16
|
+
class TireOtherClassSerializer
|
17
|
+
end
|
18
|
+
|
19
|
+
class OtherClassSerializer
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
class TireSerializerSupportTest < ActiveModel::TestCase
|
24
|
+
|
25
|
+
test "active model serializer is nil if no serializer exists" do
|
26
|
+
assert_equal nil, Tire::Results::Item.new({"_type" => "RandomClass"}).active_model_serializer
|
27
|
+
end
|
28
|
+
|
29
|
+
test "active model serializer returned for item if serializer exists" do
|
30
|
+
class ::Rails; end
|
31
|
+
assert_equal SomeClassSerializer, Tire::Results::Item.new({"_type" => "SomeClass"}).active_model_serializer
|
32
|
+
Object.class_eval{remove_const :Rails}
|
33
|
+
end
|
34
|
+
|
35
|
+
test "active model serializer with tire prefix returned for item if serializer exists" do
|
36
|
+
class ::Rails; end
|
37
|
+
assert_equal TireOtherClassSerializer, Tire::Results::Item.new({"_type" => "OtherClass"}).active_model_serializer
|
38
|
+
Object.class_eval{remove_const :Rails}
|
39
|
+
end
|
40
|
+
|
41
|
+
test "active model serializer is ArraySerializer for Collection" do
|
42
|
+
assert_equal ActiveModel::ArraySerializer, Tire::Results::Collection.new({}).active_model_serializer
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/tire-am_serializers.gemspec
CHANGED
@@ -6,8 +6,8 @@ require 'tire/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "tire-am_serializers"
|
8
8
|
spec.version = Tire::AmSerializers::VERSION
|
9
|
-
spec.authors = ["Sergey Efremov", "Ilya Zayats"]
|
10
|
-
spec.email = ["efremov.sergey@gmail.com", "somebody32@gmail.com"]
|
9
|
+
spec.authors = ["Sergey Efremov", "Ilya Zayats", "Vyacheslav Shebanov"]
|
10
|
+
spec.email = ["efremov.sergey@gmail.com", "somebody32@gmail.com", "terminal2010@gmail.com"]
|
11
11
|
spec.description = %q{The easy way to add active model serializers to your tire results}
|
12
12
|
spec.summary = %q{The easy way to add active model serializers to your tire results}
|
13
13
|
spec.homepage = "https://github.com/ResumUP/tire_am_serializers/"
|
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
|
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
25
25
|
spec.add_development_dependency "rake"
|
26
|
-
|
26
|
+
|
27
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tire-am_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Efremov
|
8
8
|
- Ilya Zayats
|
9
|
+
- Vyacheslav Shebanov
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
13
|
+
date: 2013-03-15 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: tire
|
@@ -71,6 +72,7 @@ description: The easy way to add active model serializers to your tire results
|
|
71
72
|
email:
|
72
73
|
- efremov.sergey@gmail.com
|
73
74
|
- somebody32@gmail.com
|
75
|
+
- terminal2010@gmail.com
|
74
76
|
executables: []
|
75
77
|
extensions: []
|
76
78
|
extra_rdoc_files: []
|
@@ -80,9 +82,11 @@ files:
|
|
80
82
|
- LICENSE.txt
|
81
83
|
- README.md
|
82
84
|
- Rakefile
|
83
|
-
- lib/tire/active_model/
|
85
|
+
- lib/tire/active_model/serializer_support.rb
|
84
86
|
- lib/tire/am_serializers.rb
|
85
87
|
- lib/tire/version.rb
|
88
|
+
- test/test_helper.rb
|
89
|
+
- test/tire_serializers_support_test.rb
|
86
90
|
- tire-am_serializers.gemspec
|
87
91
|
homepage: https://github.com/ResumUP/tire_am_serializers/
|
88
92
|
licenses:
|
@@ -108,4 +112,6 @@ rubygems_version: 2.0.1
|
|
108
112
|
signing_key:
|
109
113
|
specification_version: 4
|
110
114
|
summary: The easy way to add active model serializers to your tire results
|
111
|
-
test_files:
|
115
|
+
test_files:
|
116
|
+
- test/test_helper.rb
|
117
|
+
- test/tire_serializers_support_test.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module Tire
|
2
|
-
module ActiveModel
|
3
|
-
module Serializer
|
4
|
-
|
5
|
-
def serializer_to_json(options = {})
|
6
|
-
serialization :to_json, options
|
7
|
-
end
|
8
|
-
|
9
|
-
def serializer_as_json(options = {})
|
10
|
-
serialization :as_json, options
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def serializer_defined?
|
16
|
-
!active_model_serializer.nil?
|
17
|
-
end
|
18
|
-
|
19
|
-
def serialization(method, options)
|
20
|
-
if serializer_defined?
|
21
|
-
active_model_serializer.new(self).send(method, options)
|
22
|
-
else
|
23
|
-
send("tire_#{method}", options)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|